Search in sources :

Example 6 with Handler

use of org.apache.servicecomb.core.Handler in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method doInvoke.

@Test
public void doInvoke(@Mocked Endpoint endpoint, @Mocked OperationMeta operationMeta, @Mocked Object[] swaggerArguments, @Mocked SchemaMeta schemaMeta) throws Throwable {
    Response response = Response.ok("ok");
    Handler handler = (invocation, asyncResp) -> asyncResp.complete(response);
    List<Handler> handlerChain = Arrays.asList(handler);
    Deencapsulation.setField(invocation, "handlerList", handlerChain);
    Holder<Response> result = new Holder<>();
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void sendResponse(Response response) {
            result.value = response;
        }
    };
    restInvocation.invocation = invocation;
    restInvocation.doInvoke();
    Assert.assertSame(response, result.value);
    assertEquals(nanoTime, invocation.getInvocationStageTrace().getStartHandlersRequest());
    assertEquals(nanoTime, invocation.getInvocationStageTrace().getFinishHandlersResponse());
}
Also used : AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Arrays(java.util.Arrays) SCBEngine(org.apache.servicecomb.core.SCBEngine) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) MultiMap(io.vertx.core.MultiMap) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) RestMetaUtils(org.apache.servicecomb.common.rest.definition.RestMetaUtils) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) After(org.junit.After) Mock(mockit.Mock) ProduceProcessorManager(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) MockUp(mockit.MockUp) Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Buffer(io.vertx.core.buffer.Buffer) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Mocked(mockit.Mocked) CONTENT_LENGTH(com.google.common.net.HttpHeaders.CONTENT_LENGTH) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Expectations(mockit.Expectations) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) TRANSFER_ENCODING(com.google.common.net.HttpHeaders.TRANSFER_ENCODING) HashMap(java.util.HashMap) JsonUtils(org.apache.servicecomb.foundation.common.utils.JsonUtils) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AsyncContext(javax.servlet.AsyncContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Subscribe(com.google.common.eventbus.Subscribe) Status(javax.ws.rs.core.Response.Status) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) Executor(java.util.concurrent.Executor) Handler(org.apache.servicecomb.core.Handler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Const(org.apache.servicecomb.core.Const) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) Rule(org.junit.Rule) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Assert(org.junit.Assert) TestPathSchema(org.apache.servicecomb.common.rest.locator.TestPathSchema) Assert.assertEquals(org.junit.Assert.assertEquals) Holder(org.apache.servicecomb.foundation.common.Holder) Handler(org.apache.servicecomb.core.Handler) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 7 with Handler

use of org.apache.servicecomb.core.Handler in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocation_flowControlReject.

@SuppressWarnings("deprecation")
@Test
public void scheduleInvocation_flowControlReject() {
    new Expectations(operationMeta) {

        {
            operationMeta.getProviderQpsFlowControlHandler();
            result = (Handler) (invocation, asyncResp) -> asyncResp.producerFail(new InvocationException(new HttpStatus(429, "Too Many Requests"), new CommonExceptionData("rejected by qps flowcontrol")));
        }
    };
    Holder<Integer> status = new Holder<>();
    Holder<String> reasonPhrase = new Holder<>();
    Holder<Integer> endCount = new Holder<>(0);
    Holder<String> responseBody = new Holder<>();
    responseEx = new AbstractHttpServletResponse() {

        @SuppressWarnings("deprecation")
        @Override
        public void setStatus(int sc, String sm) {
            status.value = sc;
            reasonPhrase.value = sm;
        }

        @Override
        public void flushBuffer() {
            endCount.value = endCount.value + 1;
        }

        @Override
        public void setContentType(String type) {
            assertEquals("application/json; charset=utf-8", type);
        }

        @Override
        public void setBodyBuffer(Buffer bodyBuffer) {
            responseBody.value = bodyBuffer.toString();
        }
    };
    initRestInvocation();
    restInvocation.scheduleInvocation();
    assertEquals(Integer.valueOf(429), status.value);
    assertEquals("Too Many Requests", reasonPhrase.value);
    assertEquals("{\"message\":\"rejected by qps flowcontrol\"}", responseBody.value);
    assertEquals(Integer.valueOf(1), endCount.value);
}
Also used : Expectations(mockit.Expectations) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Arrays(java.util.Arrays) SCBEngine(org.apache.servicecomb.core.SCBEngine) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) MultiMap(io.vertx.core.MultiMap) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) RestMetaUtils(org.apache.servicecomb.common.rest.definition.RestMetaUtils) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) After(org.junit.After) Mock(mockit.Mock) ProduceProcessorManager(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) MockUp(mockit.MockUp) Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Buffer(io.vertx.core.buffer.Buffer) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Mocked(mockit.Mocked) CONTENT_LENGTH(com.google.common.net.HttpHeaders.CONTENT_LENGTH) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Expectations(mockit.Expectations) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) TRANSFER_ENCODING(com.google.common.net.HttpHeaders.TRANSFER_ENCODING) HashMap(java.util.HashMap) JsonUtils(org.apache.servicecomb.foundation.common.utils.JsonUtils) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AsyncContext(javax.servlet.AsyncContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Subscribe(com.google.common.eventbus.Subscribe) Status(javax.ws.rs.core.Response.Status) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) Executor(java.util.concurrent.Executor) Handler(org.apache.servicecomb.core.Handler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Const(org.apache.servicecomb.core.Const) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) Rule(org.junit.Rule) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Assert(org.junit.Assert) TestPathSchema(org.apache.servicecomb.common.rest.locator.TestPathSchema) Assert.assertEquals(org.junit.Assert.assertEquals) Buffer(io.vertx.core.buffer.Buffer) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) Holder(org.apache.servicecomb.foundation.common.Holder) Endpoint(org.apache.servicecomb.core.Endpoint) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Aggregations

Handler (org.apache.servicecomb.core.Handler)7 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Invocation (org.apache.servicecomb.core.Invocation)3 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)3 Subscribe (com.google.common.eventbus.Subscribe)2 CONTENT_LENGTH (com.google.common.net.HttpHeaders.CONTENT_LENGTH)2 TRANSFER_ENCODING (com.google.common.net.HttpHeaders.TRANSFER_ENCODING)2 MultiMap (io.vertx.core.MultiMap)2 Buffer (io.vertx.core.buffer.Buffer)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Executor (java.util.concurrent.Executor)2 AsyncContext (javax.servlet.AsyncContext)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 MediaType (javax.ws.rs.core.MediaType)2 Status (javax.ws.rs.core.Response.Status)2 Deencapsulation (mockit.Deencapsulation)2