Search in sources :

Example 6 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestDefaultLogPublisher method init_enabled_true.

@Test
public void init_enabled_true() {
    Holder<Boolean> registered = new Holder<>();
    new MockUp<EventBus>(eventBus) {

        @Mock
        void register(Object object) {
            registered.value = true;
        }
    };
    ArchaiusUtils.setProperty(DefaultLogPublisher.ENABLED, true);
    publisher.init(globalRegistry, eventBus, new MetricsBootstrapConfig());
    Assert.assertTrue(registered.value);
}
Also used : MetricsBootstrapConfig(org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 7 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestRestServerVerticle method mountGlobalRestFailureHandler.

@Test
public void mountGlobalRestFailureHandler() {
    Router mainRouter = Mockito.mock(Router.class);
    Holder<Handler<RoutingContext>> handlerHolder = new Holder<>();
    Holder<Route> routeHolder = new Holder<>();
    Route route = new MockUp<Route>() {

        @Mock
        Route failureHandler(Handler<RoutingContext> failureHandler) {
            handlerHolder.value = failureHandler;
            return null;
        }

        @Mock
        Route handler(io.vertx.core.Handler<io.vertx.ext.web.RoutingContext> requestHandler) {
            return routeHolder.value;
        }
    }.getMockInstance();
    routeHolder.value = route;
    Mockito.when(mainRouter.route()).thenReturn(route);
    RestServerVerticle restServerVerticle = new RestServerVerticle();
    Deencapsulation.invoke(restServerVerticle, "mountGlobalRestFailureHandler", mainRouter);
    Assert.assertNotNull(handlerHolder.value);
    RoutingContext routingContext = Mockito.mock(RoutingContext.class);
    HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
    Mockito.when(response.setStatusCode(500)).thenReturn(response);
    Mockito.when(response.putHeader("Content-Type", "application/json")).thenReturn(response);
    Mockito.when(routingContext.response()).thenReturn(response);
    handlerHolder.value.handle(routingContext);
    Mockito.verify(response).end("{\"message\":\"unknown error\"}");
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) Context(io.vertx.core.Context) Holder(org.apache.servicecomb.foundation.common.Holder) Router(io.vertx.ext.web.Router) Handler(io.vertx.core.Handler) CorsHandler(io.vertx.ext.web.handler.CorsHandler) Mock(mockit.Mock) RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Route(io.vertx.ext.web.Route) Test(org.junit.Test)

Example 8 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocationException.

@Test
public void scheduleInvocationException(@Mocked OperationMeta operationMeta) {
    Executor executor = new ReactiveExecutor();
    requestEx = new AbstractHttpServletRequestForTest();
    requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
        }
    };
    Holder<Throwable> result = new Holder<>();
    RuntimeException error = new RuntimeExceptionWithoutStackTrace("run on executor");
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            throw error;
        }

        @Override
        public void sendFailResponse(Throwable throwable) {
            result.value = throwable;
            invocation.onFinish(Response.ok(null));
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    Assert.assertSame(error, result.value);
}
Also used : Expectations(mockit.Expectations) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) Holder(org.apache.servicecomb.foundation.common.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 9 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocationNormal.

@Test
public void scheduleInvocationNormal(@Mocked OperationMeta operationMeta) {
    Holder<InvocationStartEvent> eventHolder = new Holder<>();
    Object subscriber = new ScheduleInvocationEventHandler(eventHolder);
    EventManager.register(subscriber);
    Executor executor = new ReactiveExecutor();
    requestEx = new AbstractHttpServletRequestForTest();
    requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
    new Expectations(requestEx) {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
            requestEx.getHeader(Const.TRACE_ID_NAME);
            result = "tid";
        }
    };
    Holder<Boolean> result = new Holder<>();
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            result.value = true;
            invocation.onFinish(Response.ok(null));
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    EventManager.unregister(subscriber);
    Assert.assertTrue(result.value);
    assertEquals(nanoTime, invocation.getInvocationStageTrace().getStart());
    assertEquals(nanoTime, invocation.getInvocationStageTrace().getStartSchedule());
    Assert.assertSame(invocation, eventHolder.value.getInvocation());
    assertEquals("tid", invocation.getTraceId());
}
Also used : Expectations(mockit.Expectations) Holder(org.apache.servicecomb.foundation.common.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 10 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method invokeFilterException.

@Test
public void invokeFilterException(@Mocked HttpServerFilter filter) {
    Exception error = new RuntimeExceptionWithoutStackTrace();
    new Expectations() {

        {
            filter.enabled();
            result = true;
            filter.afterReceiveRequest(invocation, requestEx);
            result = error;
        }
    };
    Holder<Throwable> result = new Holder<>();
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        public void sendFailResponse(Throwable throwable) {
            result.value = throwable;
        }

        @Override
        protected void doInvoke() {
        }
    };
    initRestInvocation();
    restInvocation.httpServerFilters = Arrays.asList(filter);
    restInvocation.invoke();
    Assert.assertSame(error, result.value);
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) Expectations(mockit.Expectations) Holder(org.apache.servicecomb.foundation.common.Holder) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExpectedException(org.junit.rules.ExpectedException) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)108 Test (org.junit.Test)88 MockUp (mockit.MockUp)36 AtomicLong (java.util.concurrent.atomic.AtomicLong)28 Invocation (org.apache.servicecomb.core.Invocation)24 Expectations (mockit.Expectations)22 Vertx (io.vertx.core.Vertx)20 List (java.util.List)18 Response (org.apache.servicecomb.swagger.invocation.Response)18 Mock (mockit.Mock)16 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 SCBEngine (org.apache.servicecomb.core.SCBEngine)14 ExpectedException (org.junit.rules.ExpectedException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 Status (javax.ws.rs.core.Response.Status)12 Deencapsulation (mockit.Deencapsulation)12 Mocked (mockit.Mocked)12 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)12