package at.gv.egiz.eaaf.utils.springboot.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.junit.Assert; import org.junit.Test; import org.springframework.boot.ExitCodeGenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.ConfigurableApplicationContext; import at.gv.egiz.eaaf.utils.springboot.test.dummy.DummySpringBootApp; import at.gv.egiz.eaaf.utils.springboot.utils.VersionHolder; public class SimpleSpringBootStarterTest { @Test public void Test() throws ClientProtocolException, IOException { DummySpringBootApp.main(new String[] { "--spring.config.location=classpath:/config/jUnit_application.properties"}); ConfigurableApplicationContext ctx = DummySpringBootApp.getCtx(); Assert.assertNotNull("SpringBootContext", ctx); // check if AJP Connector config was set TomcatServletWebServerFactory ajp = ctx.getBean(TomcatServletWebServerFactory.class); Assert.assertNotNull("No AJP connector", ajp); // check simple http calls testSimpleHttpCall(); // check version holder checkVersionHolder(ctx); SpringApplication.exit(ctx, new ExitCodeGenerator() { @Override public int getExitCode() { // TODO Auto-generated method stub return 0; } }); } private void testSimpleHttpCall() throws ClientProtocolException, IOException { // check if authentication works on actuator end-point final HttpClientBuilder builder = HttpClients.custom(); final CloseableHttpClient client = builder.build(); assertNotNull("httpClient", client); final HttpUriRequest httpGet1 = new HttpGet("http://localhost:8080/junit"); final CloseableHttpResponse httpResp1 = client.execute(httpGet1); assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); } private void checkVersionHolder(ConfigurableApplicationContext ctx) { VersionHolder versionHolder = ctx.getBean(VersionHolder.class); assertEquals("can not extract version", "unknown", versionHolder.getVersion()); } }