package at.asitplus.eidas.specific.connector.test; import java.io.IOException; import java.lang.reflect.Field; 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.apache.ignite.Ignition; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import at.asitplus.eidas.specific.connector.SpringBootApplicationInitializer; import at.gv.egiz.eaaf.core.impl.logging.DummyStatusMessager; import at.gv.egiz.eaaf.core.impl.logging.LogMessageProviderFactory; import eu.eidas.auth.cache.IgniteInstanceInitializerSpecificCommunication; @RunWith(BlockJUnit4ClassRunner.class) public class MainClassWebAppModeTest { /** * jUnit class initializer. * */ @BeforeClass public static void classInitializer() { final String current = new java.io.File(".").toURI().toString(); // eIDAS Ref. Impl. properties System.setProperty("SPECIFIC_CONNECTOR_CONFIG_REPOSITORY", current.substring("file:".length()) + "../basicConfig/eIDAS/"); } /** * Initializer. * */ @AfterClass public static void closeIgniteNode() { System.out.println("Closing Ignite Node ... "); Ignition.stopAll(true); } /** * Test reseter. * */ @After public void cleanJvmState() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { final Field field = LogMessageProviderFactory.class.getDeclaredField("internalMessager"); field.setAccessible(true); field.set(null, new DummyStatusMessager()); System.clearProperty("eidas.ms.configuration"); SpringBootApplicationInitializer.exit(); //set Ignite-node holder to 'null' because static holders are shared between different tests final Field field1 = IgniteInstanceInitializerSpecificCommunication.class.getDeclaredField("instance"); field1.setAccessible(true); field1.set(null, null); } @Test public void wrongConfigLocation() throws Throwable { //MS-specific connector property final String current = new java.io.File(".").toURI().toString(); System.setProperty("eidas.ms.configuration", current + "src/test/resources/config/notextist.properties"); try { //starting application SpringBootApplicationInitializer .main(new String[] { "--spring.profiles.active=jUnitTestMode" }); Assert.fail("Missing configuration not detected"); } catch (final Exception e) { Assert.assertNotNull("Exception is null", e); } } @Test public void systemdConfigLocation() throws Throwable { //MS-specific connector property final String current = new java.io.File(".").toURI().toString(); System.setProperty("eidas.ms.configuration", current + "src/test/resources/config/junit_config_1_springboot.properties"); //starting application SpringBootApplicationInitializer .main(new String[] { "--spring.profiles.active=jUnitTestMode,springBoot" }); System.out.println("Is started!"); // test Spring-Actuator http Basic-Auth testSpringActuatorSecurity(); } private void testSpringActuatorSecurity() throws ClientProtocolException, IOException { // check if authentication works on actuator end-point final HttpClientBuilder builder = HttpClients.custom(); final CloseableHttpClient client = builder.build(); Assert.assertNotNull("httpClient", client); final HttpUriRequest httpGetInfo = new HttpGet("http://localhost:8080/ms_connector/actuator/info"); final CloseableHttpResponse httpRespInfo = client.execute(httpGetInfo); Assert.assertEquals("http statusCode", 200, httpRespInfo.getStatusLine().getStatusCode()); final HttpUriRequest httpGetHealth = new HttpGet("http://localhost:8080/ms_connector/actuator/health"); final CloseableHttpResponse httpRespHealth = client.execute(httpGetHealth); Assert.assertEquals("http statusCode", 503, httpRespHealth.getStatusLine().getStatusCode()); } }