Search in sources :

Example 36 with Context

use of io.vertx.core.Context in project java-chassis by ServiceComb.

the class TestVertxHttpMethod method testDoMethod.

@Test
public void testDoMethod(@Mocked HttpClient httpClient) throws Exception {
    Context context = new MockUp<Context>() {

        @Mock
        public void runOnContext(Handler<Void> action) {
            action.handle(null);
        }
    }.getMockInstance();
    HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    RestOperationMeta swaggerRestOperation = Mockito.mock(RestOperationMeta.class);
    Endpoint endpoint = Mockito.mock(Endpoint.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    URLPathBuilder urlPathBuilder = Mockito.mock(URLPathBuilder.class);
    Mockito.when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
    operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
    Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
    Mockito.when(request.exceptionHandler(Mockito.any())).then(answer -> null);
    this.doMethod(httpClientWithContext, invocation, asyncResp);
    Assert.assertTrue(true);
}
Also used : HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Context(io.vertx.core.Context) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) AsyncResponse(io.servicecomb.core.AsyncResponse) Mock(mockit.Mock) Test(org.junit.Test)

Example 37 with Context

use of io.vertx.core.Context in project java-chassis by ServiceComb.

the class TestTcp method testTcpClient.

@Test
public void testTcpClient() throws Exception {
    NetClient oNetClient = new NetClient() {

        @Override
        public boolean isMetricsEnabled() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler) {
            return Mockito.mock(NetClient.class);
        }

        @Override
        public void close() {
        // TODO Auto-generated method stub
        }
    };
    TcpClient oTcpClient = new TcpClient(Mockito.mock(Context.class), oNetClient, "highway://127.2.0.1:8080", new TcpClientConfig());
    oTcpClient.checkTimeout();
    oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
    oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
    Assert.assertNotEquals(null, oTcpClient.getContext());
    new MockUp<TcpClientPool>() {

        @Mock
        protected void startCheckTimeout(TcpClientConfig clientConfig, Context context) {
        }
    };
    TcpClientConfig config = new TcpClientConfig();
    TcpClientPool oClientPool = new TcpClientPool(config, Vertx.vertx().getOrCreateContext(), oNetClient);
    oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
    oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
    Assert.assertNotNull(oClientPool);
    TcpRequest oTcpRequest = new TcpRequest(1234, Mockito.mock(TcpResonseCallback.class));
    oTcpRequest.isTimeout();
    oTcpRequest.onReply(Buffer.buffer(), Buffer.buffer(("test").getBytes()));
    oTcpRequest.onSendError(new Throwable("test Errorsss"));
    Assert.assertNotNull(oTcpRequest);
    TcpClientVerticle oTcpClientVerticle = new TcpClientVerticle();
    oTcpClientVerticle.init(Vertx.vertx(), Vertx.vertx().getOrCreateContext());
    oTcpClientVerticle.createClientPool();
    oTcpClientVerticle.createClientPool();
    Assert.assertNotNull(oTcpClientVerticle.getVertx());
    NetSocket socket = Mockito.mock(NetSocket.class);
    Throwable e = Mockito.mock(Throwable.class);
    Buffer hBuffer = Mockito.mock(Buffer.class);
    Buffer bBuffer = Mockito.mock(Buffer.class);
    Deencapsulation.invoke(oTcpClient, "connect");
    Deencapsulation.invoke(oTcpClient, "onConnectSuccess", socket);
    Mockito.when(socket.localAddress()).thenReturn(new SocketAddressImpl(0, "127.0.0.1"));
    Deencapsulation.setField(oTcpClient, "netSocket", socket);
    Deencapsulation.invoke(oTcpClient, "onDisconnected", e);
    Deencapsulation.invoke(oTcpClient, "tryLogin");
    Deencapsulation.invoke(oTcpClient, "onLoginSuccess");
    Deencapsulation.invoke(oTcpClient, "onConnectFailed", e);
    long l = 10;
    Deencapsulation.invoke(oTcpClient, "onReply", l, hBuffer, bBuffer);
    oTcpClient.checkTimeout();
    Assert.assertNotNull(oTcpClient);
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Context(io.vertx.core.Context) Buffer(io.vertx.core.buffer.Buffer) TcpClientVerticle(io.servicecomb.foundation.vertx.client.tcp.TcpClientVerticle) TcpClientConfig(io.servicecomb.foundation.vertx.client.tcp.TcpClientConfig) Handler(io.vertx.core.Handler) MockUp(mockit.MockUp) TcpClientPool(io.servicecomb.foundation.vertx.client.tcp.TcpClientPool) TcpResonseCallback(io.servicecomb.foundation.vertx.client.tcp.TcpResonseCallback) NetClient(io.vertx.core.net.NetClient) TcpRequest(io.servicecomb.foundation.vertx.client.tcp.TcpRequest) TcpClient(io.servicecomb.foundation.vertx.client.tcp.TcpClient) SocketAddressImpl(io.vertx.core.net.impl.SocketAddressImpl) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 38 with Context

use of io.vertx.core.Context in project java-chassis by ServiceComb.

the class TcpClientTest method setUp.

@Before
public void setUp() throws Exception {
    Context context = Mockito.mock(Context.class);
    NetClient netClient = Mockito.mock(NetClient.class);
    InetSocketAddress socketAddress = Mockito.mock(InetSocketAddress.class);
    mockTestCases();
    Mockito.when(NetUtils.parseIpPort("sss").getSocketAddress()).thenReturn(socketAddress);
    instance = new TcpClient(context, netClient, "highway://127.0.0.1:80", new TcpClientConfig());
}
Also used : Context(io.vertx.core.Context) NetClient(io.vertx.core.net.NetClient) InetSocketAddress(java.net.InetSocketAddress) Before(org.junit.Before)

Example 39 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorHasInitCalled.

@Test(timeout = 20000)
public void testCustomAuthenticatorHasInitCalled(TestContext context) {
    Async initCalledAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new ProtonSaslAuthenticatorFactory() {

        @Override
        public ProtonSaslAuthenticator create() {
            return new ProtonSaslAuthenticator() {

                @Override
                public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                    initCalledAsync.complete();
                }

                @Override
                public void process(Handler<Boolean> completionHandler) {
                    completionHandler.handle(false);
                }

                @Override
                public boolean succeeded() {
                    return false;
                }
            };
        }
    }).connectHandler(protonConnection -> {
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), protonConnectionAsyncResult -> {
    }));
}
Also used : NetSocket(io.vertx.core.net.NetSocket) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Transport(org.apache.qpid.proton.engine.Transport) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Test(org.junit.Test)

Example 40 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorSuceedsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorSuceedsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    Async authenticatedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
        // Verify the expected auth detail was recorded in the connection attachments, just using a String here.
        String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
        context.assertEquals(AUTH_VALUE, authValue);
        authenticatedAsync.complete();
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertTrue(protonConnectionAsyncResult.succeeded());
        protonConnectionAsyncResult.result().disconnect();
        connectedAsync.complete();
    }));
    authenticatedAsync.awaitSuccess();
    connectedAsync.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Context (io.vertx.core.Context)125 Test (org.junit.Test)99 Handler (io.vertx.core.Handler)75 Vertx (io.vertx.core.Vertx)75 Before (org.junit.Before)58 Buffer (io.vertx.core.buffer.Buffer)57 TestContext (io.vertx.ext.unit.TestContext)57 Future (io.vertx.core.Future)53 Async (io.vertx.ext.unit.Async)53 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)53 RunWith (org.junit.runner.RunWith)53 JsonObject (io.vertx.core.json.JsonObject)49 HttpURLConnection (java.net.HttpURLConnection)48 StandardCharsets (java.nio.charset.StandardCharsets)48 Assert.assertThat (org.junit.Assert.assertThat)48 Mockito (org.mockito.Mockito)48 Rule (org.junit.Rule)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)38 ArgumentCaptor (org.mockito.ArgumentCaptor)38 Timeout (org.junit.rules.Timeout)36