package hello.controllers;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import hello.configuration.Application;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingControllerIntegrationTest {

	@LocalServerPort
	private int port;

	private URL greeting;
	private URL greetingName;

	@Autowired
	private TestRestTemplate template;

	@Before
	public void setUp() throws MalformedURLException {
		String url = "http://localhost:" + port + "/greeting";
		this.greeting = new URL(url);
		this.greetingName = new URL(url + "?name=Rob");
	}

	@Test
	public void getJsonGreeting() {
		ResponseEntity<String> response = template.getForEntity(greeting.toString(), String.class);
		assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
		assertThat(response.getHeaders().getContentType(), equalTo(MediaType.APPLICATION_JSON_UTF8));
		assertThat(response.getBody(), containsString("Hello, World!"));
	}

	@Test
	public void getJsonGreetingName() {
		ResponseEntity<String> response = template.getForEntity(greetingName.toString(), String.class);
		assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
		assertThat(response.getHeaders().getContentType(), equalTo(MediaType.APPLICATION_JSON_UTF8));
		assertThat(response.getBody(), containsString("Hello, Rob!"));
	}
}

@@ -232,16 +232,46 @@
 net.URL;
+%0Aimport java.util.Collections;
 %0A%0Aimport
@@ -601,16 +601,149 @@
 mplate;%0A
+import org.springframework.http.HttpEntity;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpMethod;%0A
 import o
@@ -1245,16 +1245,53 @@
 gName;%0A%0A
+%09private HttpEntity%3CString%3E entity;%0A%0A
 %09@Autowi
@@ -1535,367 +1535,1342 @@
 %22);%0A
-%09%7D%0A%0A%09@Test%0A%09public void getJsonGreeting() %7B%0A%09%09ResponseEntity%3CString%3E response = template.getForEntity(greeting.toString(), String.class);%0A%09%09assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));%0A%09%09assertThat(response.getHeaders().getContentType(), equalTo(MediaType.APPLICATION_JSON_UTF8));%0A%09%09assertThat(response.getBody(), containsString(%22Hello, World
+%0A%09%09HttpHeaders headers = new HttpHeaders();%0A%09%09headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));%0A%09%09entity = new HttpEntity%3CString%3E(%22parameters%22, headers);%0A%09%7D%0A%0A%09@Test%0A%09public void getHtmlGreeting() %7B%0A%09%09ResponseEntity%3CString%3E response = template.exchange(greeting.toString(), HttpMethod.GET, entity, String.class);%0A%09%09assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));%0A%09%09assertThat(response.getHeaders().getContentType().toString(), containsString(MediaType.TEXT_HTML_VALUE));%0A%09%09assertThat(response.getBody(), containsString(%22Hello, World!%22));%0A%09%7D%0A%0A%09@Test%0A%09public void getJsonGreeting() %7B%0A%09%09ResponseEntity%3CString%3E response = template.getForEntity(greeting.toString(), String.class);%0A%09%09assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));%0A%09%09assertThat(response.getHeaders().getContentType(), equalTo(MediaType.APPLICATION_JSON_UTF8));%0A%09%09assertThat(response.getBody(), containsString(%22Hello, World!%22));%0A%09%7D%0A%0A%09@Test%0A%09public void getHtmlGreetingName() %7B%0A%09%09ResponseEntity%3CString%3E response = template.exchange(greetingName.toString(), HttpMethod.GET, entity,%0A%09%09%09%09String.class);%0A%09%09assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));%0A%09%09assertThat(response.getHeaders().getContentType().toString(), containsString(MediaType.TEXT_HTML_VALUE));%0A%09%09assertThat(response.getBody(), containsString(%22Hello, Rob
 !%22))

Test for HTML response in integration tests.