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);
}
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\"}");
}
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);
}
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());
}
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);
}
Aggregations