package at.gv.egiz.eaaf.core.test.http; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.Security; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateEncodingException; import org.apache.commons.lang3.RandomStringUtils; 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.util.EntityUtils; import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.http.HttpClientConfiguration; import at.gv.egiz.eaaf.core.impl.http.IHttpClientFactory; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/spring/test_eaaf_pvp_not_lazy.beans.xml") @DirtiesContext(classMode = ClassMode.BEFORE_CLASS) public class HttpClientFactoryProdHostTest { @Autowired private IHttpClientFactory httpClientFactory; /** * Initialize full class. */ @BeforeClass public static void classInitializer() { final Logger logger = (Logger) LoggerFactory.getLogger("org.bouncycastle.jsse"); // do not set log-level to trace, because otherwise you get a NullPointerException from BCJSSE Provider logger.setLevel(Level.INFO); Security.removeProvider(BouncyCastleJsseProvider.PROVIDER_NAME); System.setProperty("org.bouncycastle.jsse.client.acceptRenegotiation", "true"); } /** * JUnit test set-up. * */ @Before public void setup() { } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void getCustomClientX509AuthWithHsmFacadeTrustStore() throws EaafException, ClientProtocolException, IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateEncodingException { System.setProperty("javax.net.debug", "ssl:handshake"); final HttpClientConfiguration clientConfig = new HttpClientConfiguration( "jUnit-client-" + RandomStringUtils.randomAlphabetic(5)); clientConfig.setAuthMode("ssl"); clientConfig.buildKeyStoreConfig("hsmfacade", null, null, "authhandler"); clientConfig.setSslKeyAlias("authhandler-mis"); clientConfig.setDisableTlsHostCertificateValidation(true); final CloseableHttpClient client = httpClientFactory.getHttpClient(clientConfig); Assert.assertNotNull("httpClient", client); //perform test request final HttpUriRequest httpGet3 = new HttpGet("https://vollmachten.egiz.gv.at/mms-eid-test/services/GetMandatesService?wsdl"); final CloseableHttpResponse httpResp3 = client.execute(httpGet3); Assert.assertEquals("http statusCode", 200, httpResp3.getStatusLine().getStatusCode()); String body = EntityUtils.toString(httpResp3.getEntity()); assertFalse("no http body", body.isEmpty()); assertTrue("no WSDL", body.contains("name=\"GetMandatesOperation\"")); } }