package com.pollistics;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.pollistics.models.Poll;
import com.pollistics.services.PollService;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class PollisticsApplicationTests {

	@Autowired
	private TestRestTemplate restTemplate;
	@Autowired
	private PollService pollService;

	/**
	 * Test that the poll "Amerikaanse verkiezingen" is available on the homepage
	 */
	@Test
	public void homeTest() {
		String body = this.restTemplate.getForObject("/", String.class);
		assertThat(body, containsString("Amerikaanse verkiezingen"));
	}

	/**
	 * Test that /polls/{id} fetches the right poll
	 */
	@Test
	public void pollByIdTest() {
		Poll poll = pollService.getAllPolls().get(0);

		String body = this.restTemplate.getForObject("/polls/" + poll.getId(), String.class);
		assertThat(body, containsString(poll.getName()));
	}

}

@@ -341,16 +341,64 @@
 mplate;%0A
+import org.springframework.http.ResponseEntity;%0A
 import o
@@ -617,16 +617,61 @@
 ssert.*;
+%0Aimport static org.junit.Assert.assertEquals;
 %0A%0A@RunWi
@@ -1020,35 +1020,50 @@
 t() %7B%0A%09%09
-String body = this.
+ResponseEntity%3CString%3E response = 
 restTemp
@@ -1073,22 +1073,22 @@
 e.getFor
-Object
+Entity
 (%22/%22, St
@@ -1104,33 +1104,99 @@
 s);%0A%09%09assert
-That(b
+Equals(response.getStatusCodeValue(), 200);%0A%09%09assertThat(response.getB
 ody
+()
 , containsSt
@@ -1385,27 +1385,42 @@
 %0A%0A%09%09
-String body = this.
+ResponseEntity%3CString%3E response = 
 rest
@@ -1438,14 +1438,14 @@
 tFor
-Object
+Entity
 (%22/p
@@ -1474,32 +1474,84 @@
  String.class);%0A
+%09%09assertEquals(response.getStatusCodeValue(), 200);%0A
 %09%09assertThat(bod
@@ -1551,12 +1551,26 @@
 hat(
-b
+response.getB
 ody
+()
 , co
@@ -1603,11 +1603,600 @@
 )));%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Test that /polls/%7Bnotavalidid%7D returns an error page%0A%09 */%0A%09@Test%0A%09public void errorPollByIdTest() %7B%0A%09%09String uri = %22/polls/notavalidid123%22;%0A%09%09String uri2 = %22/notavalidid123%22;%0A%09%09%0A%09%09ResponseEntity%3CString%3E response = restTemplate.getForEntity(uri, String.class);%0A%09%09ResponseEntity%3CString%3E responseRoot = restTemplate.getForEntity(uri2, String.class);%0A%09%09%0A%09%09assertEquals(response.getStatusCodeValue(), 404);%0A%09%09assertThat(response.getBody(), containsString(%22404%22));%0A%09%09assertEquals(responseRoot.getStatusCodeValue(), 404);%0A%09%09assertThat(responseRoot.getBody(), containsString(%22404%22));%0A%09%7D
 %0A%7D%0A

Add new integration test and add statuscode checks