use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestGrpcCodec method testDecodeResponseEx.
@Test
public void testDecodeResponseEx() {
boolean status = false;
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
Invocation invocation = Mockito.mock(Invocation.class);
Response response = Mockito.mock(Response.class);
Mockito.when(response.getResult()).thenReturn("test");
Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
Buffer buffer = Mockito.mock(Buffer.class);
Mockito.when(httpResponse.statusCode()).thenReturn(200);
try {
GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
} catch (Exception e) {
status = true;
}
Assert.assertFalse(status);
}
use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestProducerSchemaFactory method testGetOrCreateProducer.
@Test
public void testGetOrCreateProducer() throws Exception {
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
Assert.assertEquals("add", operationMeta.getOperationId());
ProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
ReflectUtils.setField(addBody, "x", 1);
ReflectUtils.setField(addBody, "y", 2);
Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody });
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { 1, 2 });
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(true, holder.value.isFailed());
InvocationException exception = (InvocationException) holder.value.getResult();
CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestShutdownHookHandler method testShutdownHookHandlerCount.
@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
Assert.assertEquals(0, handler.getActiveCount());
// no reply
Invocation invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(1, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// normal
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
asyncResp.handle(response);
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(2, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// next exception
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
throw new Error();
}
}.getMockInstance();
try {
handler.handle(invocation, asyncResp -> {
});
Assert.assertFalse(true);
} catch (Throwable e) {
Assert.assertEquals(3, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
}
// reply exception
// TODO: should be fixed
// try {
// handler.handle(invocation, asyncResp -> {
// throw new Error();
// });
//
// Assert.assertFalse(true);
// } catch (Throwable e) {
// Assert.assertEquals(3, requestCounter.get());
// Assert.assertEquals(1, handler.getActiveCount());
// }
}
use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestInvokerUtils method testInvokeWithException.
@Test
public void testInvokeWithException() {
new MockUp<SyncResponseExecutor>() {
@Mock
public Response waitResponse() throws InterruptedException {
return Mockito.mock(Response.class);
}
};
Invocation invocation = Mockito.mock(Invocation.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
Mockito.when(operationMeta.isSync()).thenReturn(true);
try {
InvokerUtils.invoke(invocation);
} catch (InvocationException e) {
Assert.assertEquals(490, e.getStatusCode());
}
}
use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class TestInvokerUtils method testReactiveInvoke.
@Test
public void testReactiveInvoke() {
Invocation invocation = Mockito.mock(Invocation.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
boolean validAssert;
try {
InvokerUtils.reactiveInvoke(invocation, asyncResp);
validAssert = true;
} catch (Exception e) {
validAssert = false;
}
Assert.assertTrue(validAssert);
}
Aggregations