Search in sources :

Example 16 with RuntimeExceptionWithoutStackTrace

use of org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace in project java-chassis by ServiceComb.

the class TestResponse method testAr.

@Test
public void testAr() {
    ar.success(Status.ACCEPTED, 1);
    Assert.assertEquals(true, response.isSucceed());
    Assert.assertEquals(false, response.isFailed());
    Assert.assertEquals(1, (int) response.getResult());
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatusCode());
    Assert.assertEquals(Status.ACCEPTED.getReasonPhrase(), response.getReasonPhrase());
    Assert.assertEquals(Status.ACCEPTED, response.getStatus());
    ar.success(2);
    Assert.assertEquals(2, (int) response.getResult());
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatusCode());
    Response r = Response.succResp(3);
    ar.complete(r);
    Assert.assertEquals(r, response);
    ar.consumerFail(new RuntimeExceptionWithoutStackTrace("abc"));
    CommonExceptionData data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Unexpected consumer error, please check logs for details", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, new RuntimeExceptionWithoutStackTrace("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Unexpected consumer error, please check logs for details", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException consumerException = new InvocationException(300, "abc", "def");
    ar.consumerFail(consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.producerFail(new RuntimeExceptionWithoutStackTrace("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Unexpected producer error, please check logs for details", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, new RuntimeExceptionWithoutStackTrace("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Unexpected producer error, please check logs for details", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException producerException = new InvocationException(500, "abc", "def");
    ar.producerFail(producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 17 with RuntimeExceptionWithoutStackTrace

use of org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method sendResponseQuietlyExceptionOnNullInvocation.

@Test
public void sendResponseQuietlyExceptionOnNullInvocation(@Mocked Response response) {
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void doInvoke() {
        }

        @Override
        protected void sendResponse(Response response) {
            throw new RuntimeExceptionWithoutStackTrace("");
        }
    };
    initRestInvocation();
    restInvocation.invocation = null;
    restInvocation.sendResponseQuietly(response);
// just log, check nothing, and should not throw NPE
}
Also used : AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 18 with RuntimeExceptionWithoutStackTrace

use of org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method testDoSendResponseHeaderNormal.

@Test
public void testDoSendResponseHeaderNormal(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.add("h1", "h1v1");
    headers.add("h1", "h1v2");
    headers.add("h2", "h2v");
    new Expectations() {

        {
            response.getResult();
            result = new RuntimeExceptionWithoutStackTrace("stop");
            response.getHeaders();
            result = headers;
        }
    };
    MultiMap resultHeaders = MultiMap.caseInsensitiveMultiMap();
    responseEx = new MockUp<HttpServletResponseEx>() {

        private Map<String, Object> attributes = new HashMap<>();

        @Mock
        public void setAttribute(String key, Object value) {
            this.attributes.put(key, value);
        }

        @Mock
        public Object getAttribute(String key) {
            return this.attributes.get(key);
        }

        @Mock
        void addHeader(String name, String value) {
            resultHeaders.add(name, value);
        }
    }.getMockInstance();
    invocation.onStart(0);
    initRestInvocation();
    try {
        restInvocation.sendResponse(response);
        Assert.fail("must throw exception");
    } catch (Error e) {
        assertEquals(headers.toString(), resultHeaders.toString());
    }
}
Also used : Expectations(mockit.Expectations) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) MultiMap(io.vertx.core.MultiMap) MockUp(mockit.MockUp) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 19 with RuntimeExceptionWithoutStackTrace

use of org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace in project java-chassis by ServiceComb.

the class TestClassPathStaticResourceHandler method readContentFailed.

@Test
public void readContentFailed() throws IOException {
    new Expectations(handler) {

        {
            handler.findResource(anyString);
            result = new RuntimeExceptionWithoutStackTrace("read content failed.");
        }
    };
    try (LogCollector logCollector = new LogCollector()) {
        Response response = handler.handle("index.html");
        Assert.assertEquals("failed to process static resource, path=web-root/index.html", logCollector.getLastEvents().getMessage());
        InvocationException invocationException = response.getResult();
        Assert.assertEquals(Status.INTERNAL_SERVER_ERROR, invocationException.getStatus());
        Assert.assertEquals("failed to process static resource.", ((CommonExceptionData) invocationException.getErrorData()).getMessage());
        Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode());
        Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), response.getReasonPhrase());
    }
}
Also used : Expectations(mockit.Expectations) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) LogCollector(org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector) Response(org.apache.servicecomb.swagger.invocation.Response) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Example 20 with RuntimeExceptionWithoutStackTrace

use of org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace in project java-chassis by ServiceComb.

the class TestSwaggerUtils method parseSwaggerUrlException.

@Test
public void parseSwaggerUrlException(@Mocked URL url) throws IOException {
    new Expectations(IOUtils.class) {

        {
            IOUtils.toString(url, StandardCharsets.UTF_8);
            result = new RuntimeExceptionWithoutStackTrace("failed");
        }
    };
    expectedException.expect(ServiceCombException.class);
    expectedException.expectMessage("Parse swagger from url failed, ");
    SwaggerUtils.parseSwagger(url);
}
Also used : Expectations(mockit.Expectations) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) Test(org.junit.Test)

Aggregations

RuntimeExceptionWithoutStackTrace (org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace)24 Test (org.junit.Test)20 Expectations (mockit.Expectations)15 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)8 Response (org.apache.servicecomb.swagger.invocation.Response)6 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)6 MockUp (mockit.MockUp)4 LogCollector (org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector)4 HashMap (java.util.HashMap)3 Holder (org.apache.servicecomb.foundation.common.Holder)3 MultiMap (io.vertx.core.MultiMap)2 Map (java.util.Map)2 Executor (java.util.concurrent.Executor)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Invocation (org.apache.servicecomb.core.Invocation)2 ReactiveExecutor (org.apache.servicecomb.core.executor.ReactiveExecutor)2 AsyncResultCallback (org.apache.servicecomb.foundation.vertx.AsyncResultCallback)2 AbstractHttpServletResponse (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse)2 Test (org.junit.jupiter.api.Test)2 EventBus (com.google.common.eventbus.EventBus)1