package sample.ui; import java.io.IOException; import java.net.URI; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.RestTemplate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * Basic integration tests for demo application. * * @author Dave Syer */ public class SampleWebUiApplicationTests { private static ConfigurableApplicationContext context; @BeforeClass public static void start() throws Exception { Future future = Executors .newSingleThreadExecutor().submit( new Callable() { @Override public ConfigurableApplicationContext call() throws Exception { return SpringApplication .run(SampleWebUiApplication.class); } }); context = future.get(60, TimeUnit.SECONDS); } @AfterClass public static void stop() { if (context != null) { context.close(); } } @Test public void testHome() throws Exception { ResponseEntity entity = getRestTemplate().getForEntity( "http://localhost:8080", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity .getBody().contains("Messages")); assertFalse("Wrong body (found layout:fragment):\n" + entity.getBody(), entity .getBody().contains("layout:fragment")); } @Test public void testCreate() throws Exception { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.set("text", "FOO text"); map.set("summary", "FOO"); URI location = getRestTemplate().postForLocation("http://localhost:8080", map); assertTrue("Wrong location:\n" + location, location.toString().contains("localhost:8080")); } @Test public void testCss() throws Exception { ResponseEntity<String> entity = getRestTemplate().getForEntity( "http://localhost:8080/css/bootstrap.min.css", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); } private RestTemplate getRestTemplate() { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { @Override public void handleError(ClientHttpResponse response) throws IOException { } }); return restTemplate; } }