Search in sources :

Example 1 with AbstractHttpServletResponse

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocation_invocationContextDeserializeError.

@Test
public void scheduleInvocation_invocationContextDeserializeError(@Mocked AsyncContext asyncContext) {
    requestEx = new AbstractHttpServletRequest() {

        @Override
        public String getHeader(String name) {
            return "{\"x-cse-src-microservice\":'source\"}";
        }

        @Override
        public AsyncContext getAsyncContext() {
            return asyncContext;
        }
    };
    Holder<Integer> status = new Holder<>();
    Holder<String> reasonPhrase = new Holder<>();
    Holder<Integer> endCount = new Holder<>(0);
    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);
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.responseEx = responseEx;
    restInvocation.scheduleInvocation();
    assertEquals(Integer.valueOf(590), status.value);
    assertEquals("Unexpected producer error, please check logs for details", reasonPhrase.value);
    assertEquals(Integer.valueOf(1), endCount.value);
}
Also used : AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Holder(org.apache.servicecomb.foundation.common.Holder) AsyncContext(javax.servlet.AsyncContext) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 2 with AbstractHttpServletResponse

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse 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)

Example 3 with AbstractHttpServletResponse

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse in project incubator-servicecomb-java-chassis by apache.

the class TestAbstractRestInvocation method scheduleInvocation_invocationContextDeserializeError.

@Test
public void scheduleInvocation_invocationContextDeserializeError(@Mocked AsyncContext asyncContext) {
    requestEx = new AbstractHttpServletRequest() {

        @Override
        public String getHeader(String name) {
            return "{\"x-cse-src-microservice\":'source\"}";
        }

        @Override
        public AsyncContext getAsyncContext() {
            return asyncContext;
        }
    };
    Holder<Integer> status = new Holder<>();
    Holder<String> reasonPhrase = new Holder<>();
    Holder<Integer> endCount = new Holder<>(0);
    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);
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.responseEx = responseEx;
    restInvocation.scheduleInvocation();
    assertEquals(Integer.valueOf(590), status.value);
    assertEquals("Unexpected producer error, please check logs for details", reasonPhrase.value);
    assertEquals(Integer.valueOf(1), endCount.value);
}
Also used : AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Holder(org.apache.servicecomb.foundation.common.Holder) AsyncContext(javax.servlet.AsyncContext) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 4 with AbstractHttpServletResponse

use of org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse in project incubator-servicecomb-java-chassis by apache.

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

AsyncContext (javax.servlet.AsyncContext)4 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)4 Endpoint (org.apache.servicecomb.core.Endpoint)4 Holder (org.apache.servicecomb.foundation.common.Holder)4 AbstractHttpServletRequest (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest)4 AbstractHttpServletResponse (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse)4 Test (org.junit.Test)4 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 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2