diff options
Diffstat (limited to 'eaaf-springboot-utils/src/test/java/at')
5 files changed, 259 insertions, 0 deletions
diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java new file mode 100644 index 00000000..e0c478af --- /dev/null +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/SimpleSpringBootStarterTest.java @@ -0,0 +1,73 @@ +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()); + + } +} diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckNoKeyStoreFactoryTest.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckNoKeyStoreFactoryTest.java new file mode 100644 index 00000000..9d3c0d02 --- /dev/null +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckNoKeyStoreFactoryTest.java @@ -0,0 +1,41 @@ +package at.gv.egiz.eaaf.utils.springboot.test.actuator; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory; +import at.gv.egiz.eaaf.utils.springboot.actuator.HsmFacadeProviderHealthCheck; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("/spring/test_spring_actuator.xml") +@DirtiesContext(classMode = ClassMode.BEFORE_CLASS) +public class HsmFacadeProviderHealthCheckNoKeyStoreFactoryTest { + + @Mock + private EaafKeyStoreFactory keyStoreFactory; + + @InjectMocks + @Autowired + private HsmFacadeProviderHealthCheck check; + + @Test + public void noEaafKeyStoreFactoryBean() { + //get current status + Health status = check.health(); + + //validate result + Assert.assertEquals("wrong statusCode", Status.UNKNOWN.getCode(), status.getStatus().getCode()); + + } + +} diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckTest.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckTest.java new file mode 100644 index 00000000..d6bdf26a --- /dev/null +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/actuator/HsmFacadeProviderHealthCheckTest.java @@ -0,0 +1,96 @@ +package at.gv.egiz.eaaf.utils.springboot.test.actuator; + +import static org.mockito.Mockito.when; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory; +import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory.HsmFacadeStatus; +import at.gv.egiz.eaaf.utils.springboot.actuator.HsmFacadeProviderHealthCheck; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("/spring/test_spring_actuator.xml") +public class HsmFacadeProviderHealthCheckTest { + + @Mock + private EaafKeyStoreFactory keyStoreFactory = Mockito.mock(EaafKeyStoreFactory.class); + + @InjectMocks + @Autowired + private HsmFacadeProviderHealthCheck check; + + @Before + public void initMocks() { + MockitoAnnotations.initMocks(this); + + } + + @Test + public void hsmFacadeStatusUnknown() { + //set-up test result + when(keyStoreFactory.isHsmFacadeInitialized()).thenReturn(false); + + //get current status + Health status = check.health(); + + //validate result + Assert.assertEquals("wrong statusCode", Status.UNKNOWN.getCode(), status.getStatus().getCode()); + + } + + @Test + public void statusUp() throws Exception { + //set-up test result + when(keyStoreFactory.isHsmFacadeInitialized()).thenReturn(true); + when(keyStoreFactory.checkHsmFacadeStatus()).thenReturn(HsmFacadeStatus.UP); + + //get current status + Health status = check.health(); + + //validate result + Assert.assertEquals("wrong statusCode", Status.UP.getCode(), status.getStatus().getCode()); + + } + + @Test + public void statusDown() throws Exception { + //set-up test result + when(keyStoreFactory.isHsmFacadeInitialized()).thenReturn(true); + when(keyStoreFactory.checkHsmFacadeStatus()).thenReturn(HsmFacadeStatus.DOWN); + + //get current status + Health status = check.health(); + + //validate result + Assert.assertEquals("wrong statusCode", Status.DOWN.getCode(), status.getStatus().getCode()); + + } + + @Test + public void statusUnknown() throws Exception { + //set-up test result + when(keyStoreFactory.isHsmFacadeInitialized()).thenReturn(true); + when(keyStoreFactory.checkHsmFacadeStatus()).thenReturn(HsmFacadeStatus.UNKNOWN); + + //get current status + Health status = check.health(); + + //validate result + Assert.assertEquals("wrong statusCode", Status.OUT_OF_SERVICE.getCode(), status.getStatus().getCode()); + + } + + +} diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummyController.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummyController.java new file mode 100644 index 00000000..65dcf5c1 --- /dev/null +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummyController.java @@ -0,0 +1,23 @@ +package at.gv.egiz.eaaf.utils.springboot.test.dummy; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + + +@Controller +public class DummyController { + + @RequestMapping(value = {"/junit"}, + method = { RequestMethod.POST, RequestMethod.GET }) + public void performGenericAuthenticationProcess(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + resp.setStatus(200); + + } +} diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummySpringBootApp.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummySpringBootApp.java new file mode 100644 index 00000000..bc742371 --- /dev/null +++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/dummy/DummySpringBootApp.java @@ -0,0 +1,26 @@ +package at.gv.egiz.eaaf.utils.springboot.test.dummy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.ComponentScan; + +import lombok.Getter; + +@ComponentScan(basePackages = {"at.gv.egiz.eaaf.utils.springboot"}) +@EnableAutoConfiguration +@SpringBootApplication +public class DummySpringBootApp { + + @Getter + private static ConfigurableApplicationContext ctx; + + public static void main(String[] args) { + + final SpringApplication springApp = new SpringApplication(DummySpringBootApp.class); + ctx = springApp.run(args); + + } + +} |