Search in sources :

Example 11 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class CertificateUtilTest method testFindRootCAException.

@Test
public void testFindRootCAException(@Mocked X500Principal aX500Principal1, @Mocked X500Principal aX500Principal2, @Mocked MyX509Certificate myX509Certificate) {
    new Expectations() {

        {
            aX500Principal1.getName();
            result = "Huawei1";
        }

        {
            aX500Principal2.getName();
            result = "Huawei3";
        }

        {
            myX509Certificate.getSubjectX500Principal();
            result = aX500Principal1;
            myX509Certificate.getIssuerX500Principal();
            result = aX500Principal2;
        }
    };
    MyX509Certificate myX509Certificate1 = new MyX509Certificate();
    MyX509Certificate myX509Certificate2 = new MyX509Certificate();
    MyX509Certificate[] xxmyX509Certificate = new MyX509Certificate[2];
    xxmyX509Certificate[0] = myX509Certificate1;
    xxmyX509Certificate[1] = myX509Certificate2;
    try {
        X509Certificate aX509Certificate = CertificateUtil.findOwner(xxmyX509Certificate);
        Assert.assertNull(aX509Certificate);
    } catch (IllegalArgumentException e) {
        Assert.assertEquals("bad certificate chain: no root CA.", e.getMessage());
    }
}
Also used : Expectations(mockit.Expectations) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 12 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class TrustManagerExtTest method testCheckClientTrusted.

@Test
public void testCheckClientTrusted(@Mocked CertificateUtil certificateUtil) {
    MyX509Certificate myX509Certificate1 = new MyX509Certificate();
    MyX509Certificate myX509Certificate2 = new MyX509Certificate();
    MyX509Certificate[] MyX509CertificateArray = new MyX509Certificate[2];
    MyX509CertificateArray[0] = myX509Certificate1;
    MyX509CertificateArray[1] = myX509Certificate2;
    new Expectations() {

        {
            CertificateUtil.findOwner((X509Certificate[]) any);
            result = any;
            CertificateUtil.getCN((X509Certificate) any);
            result = "10.67.147.115";
        }
    };
    MyX509ExtendedTrustManager myX509ExtendedTrustManager = new MyX509ExtendedTrustManager();
    TrustManagerExt trustManagerExt = new TrustManagerExt(myX509ExtendedTrustManager, option, custom);
    Socket socket = null;
    SSLEngine sslengine = null;
    boolean validAssert = true;
    try {
        trustManagerExt.checkClientTrusted(MyX509CertificateArray, "pks", socket);
        trustManagerExt.checkClientTrusted(MyX509CertificateArray, "pks", sslengine);
        trustManagerExt.checkServerTrusted(MyX509CertificateArray, "pks", socket);
        trustManagerExt.checkServerTrusted(MyX509CertificateArray, "pks", sslengine);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Expectations(mockit.Expectations) SSLEngine(javax.net.ssl.SSLEngine) X509Certificate(java.security.cert.X509Certificate) Socket(java.net.Socket) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) Test(org.junit.Test)

Example 13 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class TestRestServerVerticle method testRestServerVerticleWithRouter.

@Test
public void testRestServerVerticleWithRouter(@Mocked Transport transport, @Mocked Vertx vertx, @Mocked Context context, @Mocked JsonObject jsonObject, @Mocked Future<Void> startFuture) throws Exception {
    URIEndpointObject endpointObject = new URIEndpointObject("http://127.0.0.1:8080");
    new Expectations() {

        {
            transport.parseAddress("http://127.0.0.1:8080");
            result = endpointObject;
        }
    };
    Endpoint endpiont = new Endpoint(transport, "http://127.0.0.1:8080");
    new Expectations() {

        {
            context.config();
            result = jsonObject;
            jsonObject.getValue(AbstractTransport.ENDPOINT_KEY);
            result = endpiont;
        }
    };
    RestServerVerticle server = new RestServerVerticle();
    // process stuff done by Expectations
    server.init(vertx, context);
    server.start(startFuture);
}
Also used : Expectations(mockit.Expectations) Endpoint(io.servicecomb.core.Endpoint) URIEndpointObject(io.servicecomb.foundation.common.net.URIEndpointObject) Test(org.junit.Test)

Example 14 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class TestRegistry method testRegistryUtilsWithStub.

@Test
public void testRegistryUtilsWithStub(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
    HeartbeatResponse response = new HeartbeatResponse();
    response.setOk(true);
    response.setMessage("OK");
    new Expectations() {

        {
            oMockServiceRegistryClient.init();
            oMockServiceRegistryClient.registerMicroservice((Microservice) any);
            result = "sampleServiceID";
            oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
            result = "sampleInstanceID";
            oMockServiceRegistryClient.unregisterMicroserviceInstance(anyString, anyString);
            result = true;
        }
    };
    RegistryUtils.setSrClient(oMockServiceRegistryClient);
    RegistryUtils.init();
    Assert.assertEquals(true, RegistryUtils.unregsiterInstance());
}
Also used : Expectations(mockit.Expectations) HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Test(org.junit.Test)

Example 15 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class TestRegistry method testRegistryUtilsWithStubHeartbeatFailure.

@Test
public void testRegistryUtilsWithStubHeartbeatFailure(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
    final HeartbeatResponse response = new HeartbeatResponse();
    response.setOk(false);
    response.setMessage("FAIL");
    new Expectations() {

        {
            oMockServiceRegistryClient.init();
            oMockServiceRegistryClient.registerMicroservice((Microservice) any);
            result = "sampleServiceID";
            oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
            result = "sampleInstanceID";
            oMockServiceRegistryClient.heartbeat(anyString, anyString);
            result = response;
            oMockServiceRegistryClient.unregisterMicroserviceInstance(anyString, anyString);
            result = false;
        }
    };
    RegistryUtils.setSrClient(oMockServiceRegistryClient);
    RegistryUtils.init();
    Assert.assertEquals(false, RegistryUtils.heartbeat().isOk());
    Assert.assertEquals(false, RegistryUtils.unregsiterInstance());
}
Also used : Expectations(mockit.Expectations) HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Test(org.junit.Test)

Aggregations

Expectations (mockit.Expectations)113 Test (org.junit.Test)98 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)60 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)19 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 Test (org.testng.annotations.Test)14 AmqpSendHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler)12 ByteBuffer (java.nio.ByteBuffer)12 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)10 AmqpFeedbackReceivedHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFeedbackReceivedHandler)6 AmqpReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpReceive)5 URIEndpointObject (io.servicecomb.foundation.common.net.URIEndpointObject)5 FeedbackBatch (com.microsoft.azure.sdk.iot.service.FeedbackBatch)4 Message (com.microsoft.azure.sdk.iot.service.Message)4 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)4 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)4 WebSocketImpl (com.microsoft.azure.sdk.iot.deps.ws.impl.WebSocketImpl)3 Tools (com.microsoft.azure.sdk.iot.service.Tools)3 AmqpFileUploadNotificationReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFileUploadNotificationReceive)3 Endpoint (io.servicecomb.core.Endpoint)3