Search in sources :

Example 1 with Mock

use of mockit.Mock 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 2 with Mock

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

the class TestShutdownHookHandler method testShutdownHookHandlerReject.

@Test
public void testShutdownHookHandlerReject() throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", true);
    Holder<InvocationType> typeHolder = new Holder<>(InvocationType.PRODUCER);
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public InvocationType getInvocationType() {
            return typeHolder.value;
        }
    }.getMockInstance();
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 590);
    });
    typeHolder.value = InvocationType.CONSUMER;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 490);
    });
}
Also used : Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) InvocationType(io.servicecomb.core.invocation.InvocationType) Mock(mockit.Mock) Test(org.junit.Test)

Example 3 with Mock

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

the class MockUtil method mockConsumerProviderManager.

public void mockConsumerProviderManager() {
    ConsumerProviderManager consumerProviderManager = new MockUp<ConsumerProviderManager>() {

        @Mock
        public ReferenceConfig getReferenceConfig(String microserviceName) {
            return new ReferenceConfig("test", "test", "test");
        }
    }.getMockInstance();
    CseContext.getInstance().setConsumerProviderManager(consumerProviderManager);
}
Also used : ConsumerProviderManager(io.servicecomb.core.provider.consumer.ConsumerProviderManager) ReferenceConfig(io.servicecomb.core.provider.consumer.ReferenceConfig) Mock(mockit.Mock)

Example 4 with Mock

use of mockit.Mock in project drill by apache.

the class TestBitRpc method testConnectionBackpressure.

@Test
public void testConnectionBackpressure(@Injectable WorkerBee bee, @Injectable final WorkEventBus workBus) throws Exception {
    DrillConfig config1 = DrillConfig.create();
    final BootStrapContext c = new BootStrapContext(config1, ClassPathScanner.fromPrescan(config1));
    DrillConfig config2 = DrillConfig.create();
    BootStrapContext c2 = new BootStrapContext(config2, ClassPathScanner.fromPrescan(config2));
    final FragmentContext fcon = new MockUp<FragmentContext>() {

        BufferAllocator getAllocator() {
            return c.getAllocator();
        }
    }.getMockInstance();
    final FragmentManager fman = new MockUp<FragmentManager>() {

        int v = 0;

        @Mock
        boolean handle(IncomingDataBatch batch) throws FragmentSetupException, IOException {
            try {
                v++;
                if (v % 10 == 0) {
                    System.out.println("sleeping.");
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
            }
            RawFragmentBatch rfb = batch.newRawFragmentBatch(c.getAllocator());
            rfb.sendOk();
            rfb.release();
            return true;
        }

        public FragmentContext getFragmentContext() {
            return fcon;
        }
    }.getMockInstance();
    new NonStrictExpectations() {

        {
            workBus.getFragmentManagerIfExists((FragmentHandle) any);
            result = fman;
            workBus.getFragmentManager((FragmentHandle) any);
            result = fman;
        }
    };
    int port = 1234;
    DataConnectionConfig config = new DataConnectionConfig(c.getAllocator(), c, new DataServerRequestHandler(workBus, bee));
    DataServer server = new DataServer(config);
    port = server.bind(port, true);
    DrillbitEndpoint ep = DrillbitEndpoint.newBuilder().setAddress("localhost").setDataPort(port).build();
    DataConnectionManager manager = new DataConnectionManager(ep, config);
    DataTunnel tunnel = new DataTunnel(manager);
    AtomicLong max = new AtomicLong(0);
    for (int i = 0; i < 40; i++) {
        long t1 = System.currentTimeMillis();
        tunnel.sendRecordBatch(new TimingOutcome(max), new FragmentWritableBatch(false, QueryId.getDefaultInstance(), 1, 1, 1, 1, getRandomBatch(c.getAllocator(), 5000)));
        System.out.println(System.currentTimeMillis() - t1);
    // System.out.println("sent.");
    }
    System.out.println(String.format("Max time: %d", max.get()));
    assertTrue(max.get() > 2700);
    Thread.sleep(5000);
}
Also used : FragmentContext(org.apache.drill.exec.ops.FragmentContext) FragmentWritableBatch(org.apache.drill.exec.record.FragmentWritableBatch) Mock(mockit.Mock) DrillbitEndpoint(org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint) DrillConfig(org.apache.drill.common.config.DrillConfig) RawFragmentBatch(org.apache.drill.exec.record.RawFragmentBatch) FragmentSetupException(org.apache.drill.exec.exception.FragmentSetupException) IOException(java.io.IOException) DrillbitEndpoint(org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint) BufferAllocator(org.apache.drill.exec.memory.BufferAllocator) FragmentManager(org.apache.drill.exec.work.fragment.FragmentManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) BootStrapContext(org.apache.drill.exec.server.BootStrapContext) NonStrictExpectations(mockit.NonStrictExpectations) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test)

Example 5 with Mock

use of mockit.Mock in project pentaho-platform by pentaho.

the class RuntimeContextTest method before.

@Before
public void before() {
    mockedRuntimeRepository = mock(IRuntimeRepository.class);
    mockedSolutionEngine = mock(ISolutionEngine.class);
    mockedPentahoSystem = new MockUp<PentahoSystem>() {

        @Mock
        public <T> T get(Class<T> type, IPentahoSession session) {
            if (type == IRuntimeRepository.class) {
                return (T) mockedRuntimeRepository;
            } else if (type == ISolutionEngine.class) {
                return (T) mockedSolutionEngine;
            }
            throw new IllegalStateException("Unsupported type: " + type.getSimpleName());
        }

        @Mock
        public void setApplicationContext(final IApplicationContext applicationContext) {
        }

        @Mock
        public IApplicationContext getApplicationContext() {
            final String solutionPath = ".";
            final String applicationPath = "";
            return new StandaloneApplicationContext(solutionPath, applicationPath);
        }
    };
    runtimeCtx = new RuntimeContext("id", mockedSolutionEngine, "solutionName", makeRuntimeData(session), session, null, "processId", urlFactory, makeParameterProviders(session), new ArrayList<String>(), null);
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) IApplicationContext(org.pentaho.platform.api.engine.IApplicationContext) Matchers.anyString(org.mockito.Matchers.anyString) StandaloneApplicationContext(org.pentaho.platform.engine.core.system.StandaloneApplicationContext) PentahoSystem(org.pentaho.platform.engine.core.system.PentahoSystem) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mock(mockit.Mock) IRuntimeRepository(org.pentaho.platform.api.repository.IRuntimeRepository) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) Before(org.junit.Before)

Aggregations

Mock (mockit.Mock)83 Test (org.junit.Test)43 Expectations (mockit.Expectations)22 MockUp (mockit.MockUp)22 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)19 ArrayList (java.util.ArrayList)18 Channel (com.alibaba.otter.shared.common.model.config.channel.Channel)16 Test (org.testng.annotations.Test)16 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Assert (org.junit.Assert)11 ChannelArbitrateEvent (com.alibaba.otter.shared.arbitrate.impl.manage.ChannelArbitrateEvent)10 Event (com.alibaba.otter.shared.communication.core.model.Event)10 Buffer (io.vertx.core.buffer.Buffer)10 BeforeClass (org.testng.annotations.BeforeClass)10 PipelineArbitrateEvent (com.alibaba.otter.shared.arbitrate.impl.manage.PipelineArbitrateEvent)9 Handler (io.vertx.core.Handler)9 List (java.util.List)9 Mocked (mockit.Mocked)9 After (org.junit.After)9