Search in sources :

Example 1 with HttpsConfigInfoBean

use of io.servicecomb.foundation.common.entities.HttpsConfigInfoBean in project java-chassis by ServiceComb.

the class TestHttpsClient method testCreateSSLContext.

@Test
public void testCreateSSLContext() {
    final String SSL_VERSION = "TLSv1.2";
    String sslVersion = SSL_VERSION;
    try {
        final SSLContext sslContext = SSLContext.getInstance(sslVersion);
        new MockUp<HttpsClient>() {

            @Mock
            private SSLContext createSSLContext(HttpsConfigInfoBean configBean) throws GeneralSecurityException, IOException {
                return sslContext;
            }
        };
    } catch (NoSuchAlgorithmException e) {
        Assert.assertTrue(false);
    }
    HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
    HttpsClient.getHttpsClient(oBean);
    Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
}
Also used : MockUp(mockit.MockUp) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) Test(org.junit.Test)

Example 2 with HttpsConfigInfoBean

use of io.servicecomb.foundation.common.entities.HttpsConfigInfoBean in project java-chassis by ServiceComb.

the class TestHttpsClient method testHttpClient.

@Test
public void testHttpClient() throws ClientProtocolException, IOException {
    // test valid Invalid Inputs
    Map<String, String> oHeaders = new HashMap<String, String>();
    oHeaders.put("X-Auth", "JHGUGJGH");
    HttpResponse oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", null);
    Assert.assertEquals(null, oResponse);
    oResponse = HttpsClient.execute("", oHeaders, "//check", "body", null);
    Assert.assertEquals(null, oResponse);
    oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "", "body", null);
    Assert.assertEquals(null, oResponse);
    oResponse = HttpsClient.execute("UNKNOWN METHOD", null, "//check", "body", null);
    Assert.assertEquals(null, oResponse);
    // With default Bean
    HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
    oBean.setKeyStorePath("JHGJ");
    oBean.setKeyStorePasswd("HJGJH");
    oBean.setTrustStorePasswd("JHGJHG");
    oBean.setTrustStorePath("JHGJGj");
    Assert.assertEquals("JHGJGj", oBean.getTrustStorePath());
    Assert.assertEquals("JHGJHG", oBean.getTrustStorePasswd());
    oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", oBean);
    Assert.assertEquals(null, oResponse);
    HttpsClient.getHttpsClient(oBean);
    Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
    //Handle Error Scenarios
    try {
        oResponse = HttpsClient.execute("POST", oHeaders, "//check", "body", oBean);
    } catch (Exception e) {
        Assert.assertEquals("Target host is null", e.getMessage());
    }
    try {
        oResponse = HttpsClient.execute("GET", oHeaders, "//check", "body", oBean);
    } catch (Exception e) {
        Assert.assertEquals("Target host is null", e.getMessage());
    }
    try {
        oResponse = HttpsClient.execute("DELETE", oHeaders, "//check", "body", oBean);
    } catch (Exception e) {
        Assert.assertEquals("Target host is null", e.getMessage());
    }
// TODO Check the valid Responses
}
Also used : HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 3 with HttpsConfigInfoBean

use of io.servicecomb.foundation.common.entities.HttpsConfigInfoBean in project java-chassis by ServiceComb.

the class TestHttpsClient method testInitKeyStore.

@Test
public void testInitKeyStore(@Mocked final HttpsConfigInfoBean configInfoBean, @Mocked final KeyManagerFactory factory) {
    HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
    new Expectations() {

        {
            configInfoBean.getKeyStorePath();
            result = "/foundation-common/src/test/resources/config/test.1.properties";
            configInfoBean.getKeyStorePasswd();
            result = "1769";
            configInfoBean.getTrustStorePath();
            result = "/foundation-common/src/test/resources/config/test.1.properties";
            configInfoBean.getTrustStorePasswd();
            result = "1769";
        }
    };
    new MockUp<KeyManagerFactory>() {

        @Mock
        public final void init(KeyStore ks, char[] password) {
        }

        @Mock
        public final KeyManager[] getKeyManagers() {
            return null;
        }
    };
    String keyStoreType = KeyStore.getDefaultType();
    try {
        final KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        new MockUp<HttpsClient>() {

            @Mock
            private KeyStore initKeyStore(String storePath, String storePasswd, String storeType) throws IOException {
                return keyStore;
            }
        };
    } catch (KeyStoreException e) {
        Assert.assertTrue(false);
    }
    HttpsClient.getHttpsClient(oBean);
    Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
}
Also used : Expectations(mockit.Expectations) MockUp(mockit.MockUp) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) Test(org.junit.Test)

Example 4 with HttpsConfigInfoBean

use of io.servicecomb.foundation.common.entities.HttpsConfigInfoBean in project java-chassis by ServiceComb.

the class TestHttpsClient method testGeneralSecurityException.

@Test
public void testGeneralSecurityException() {
    new MockUp<HttpsClient>() {

        @Mock
        private SSLContext createSSLContext(HttpsConfigInfoBean configBean) throws GeneralSecurityException, IOException {
            throw new GeneralSecurityException();
        }
    };
    HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
    Assert.assertNotNull(HttpsClient.getHttpsClient(oBean));
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) MockUp(mockit.MockUp) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) Test(org.junit.Test)

Example 5 with HttpsConfigInfoBean

use of io.servicecomb.foundation.common.entities.HttpsConfigInfoBean in project java-chassis by ServiceComb.

the class TestHttpsClient method testFullOperation.

@Test
public void testFullOperation() {
    new MockUp<KeyStore>() {

        @Mock
        public KeyStore getInstance(String type) throws KeyStoreException {
            throw new KeyStoreException();
        }
    };
    HttpsClient.getHttpsClient();
    new MockUp<KeyStore>() {

        @Mock
        public KeyStore getInstance(String type) throws KeyStoreException {
            throw new RuntimeException();
        }
    };
    HttpsClient.getHttpsClient();
    HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
    HttpsClient.getHttpsClient(oBean);
    Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
}
Also used : MockUp(mockit.MockUp) KeyStoreException(java.security.KeyStoreException) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) Test(org.junit.Test)

Aggregations

HttpsConfigInfoBean (io.servicecomb.foundation.common.entities.HttpsConfigInfoBean)5 Test (org.junit.Test)5 MockUp (mockit.MockUp)4 KeyStoreException (java.security.KeyStoreException)3 GeneralSecurityException (java.security.GeneralSecurityException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1 HashMap (java.util.HashMap)1 KeyManager (javax.net.ssl.KeyManager)1 SSLContext (javax.net.ssl.SSLContext)1 Expectations (mockit.Expectations)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1