Search in sources :

Example 36 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestJaxrsProducerResponseMapper method mapResponse_withHeaders.

@Test
public void mapResponse_withHeaders() {
    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.add("h", "v");
    new Expectations() {

        {
            jaxrsResponse.getStatusInfo();
            result = Status.OK;
            jaxrsResponse.getEntity();
            result = "result";
            jaxrsResponse.getHeaders();
            result = headers;
        }
    };
    Response response = mapper.mapResponse(null, jaxrsResponse);
    Assert.assertEquals(Status.OK, response.getStatus());
    Assert.assertEquals("result", response.getResult());
    Assert.assertEquals(1, response.getHeaders().getHeaderMap().size());
    Assert.assertThat(response.getHeaders().getHeader("h"), Matchers.contains("v"));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Expectations(mockit.Expectations) Response(org.apache.servicecomb.swagger.invocation.Response) Test(org.junit.Test)

Example 37 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class CodeFirstSpringmvc method cseResponse.

@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@RequestMapping(path = "/cseResponse", method = RequestMethod.GET)
public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE).toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ApiResponse(io.swagger.annotations.ApiResponse) User(org.apache.servicecomb.demo.server.User) HttpHeaders(org.springframework.http.HttpHeaders) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class CseClientHttpRequest method invoke.

private CseClientHttpResponse invoke(Object[] args) {
    Invocation invocation = InvocationFactory.forConsumer(requestMeta.getReferenceConfig(), requestMeta.getOperationMeta(), args);
    invocation.getHandlerContext().put(RestConst.REST_CLIENT_REQUEST_PATH, path + "?" + this.uri.getRawQuery());
    if (context != null) {
        invocation.addContext(context);
    }
    invocation.getHandlerContext().put(RestConst.CONSUMER_HEADER, httpHeaders);
    Response response = doInvoke(invocation);
    if (response.isSuccessed()) {
        return new CseClientHttpResponse(response);
    }
    throw ExceptionFactory.convertConsumerException((Throwable) response.getResult());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Invocation(org.apache.servicecomb.core.Invocation)

Example 39 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestInvoker method completableFutureInvoke_failed.

@Test
public void completableFutureInvoke_failed(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Throwable error = new Error("failed");
    Response response = Response.createConsumerFail(error);
    new MockUp<InvokerUtils>() {

        @Mock
        void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
            asyncResp.handle(response);
            ;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
    future.whenComplete((result, ex) -> {
        Assert.assertEquals(null, result);
        Assert.assertSame(error, ex);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 40 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayClient method doTestSend.

private Object doTestSend(Vertx vertx, HighwayClientConnectionPool pool, HighwayClientConnection tcpClient, Object decodedResponse) throws Exception {
    new MockUp<VertxUtils>() {

        @Mock
        <VERTICLE extends AbstractVerticle> boolean blockDeploy(Vertx vertx, Class<VERTICLE> cls, DeploymentOptions options) throws InterruptedException {
            return true;
        }
    };
    new MockUp<ClientPoolManager<HighwayClientConnectionPool>>() {

        @Mock
        public HighwayClientConnectionPool findClientPool(boolean sync) {
            return pool;
        }
    };
    new MockUp<ProtobufManager>() {

        @Mock
        public OperationProtobuf getOrCreateOperation(OperationMeta operationMeta) throws Exception {
            return operationProtobuf;
        }
    };
    new MockUp<HighwayClientConnectionPool>() {

        @Mock
        HighwayClientConnection findOrCreateClient(String endpoint) {
            return tcpClient;
        }
    };
    new MockUp<HighwayCodec>() {

        @Mock
        public Buffer encodeRequest(Invocation invocation, OperationProtobuf operationProtobuf, long msgId) throws Exception {
            return null;
        }

        @Mock
        Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData, ProtobufFeature protobufFeature) throws Throwable {
            if (Response.class.isInstance(decodedResponse)) {
                return (Response) decodedResponse;
            }
            throw (Throwable) decodedResponse;
        }
    };
    client.init(vertx);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
    Mockito.when(invocation.getEndpoint().getEndpoint()).thenReturn("endpoint");
    Mockito.when(invocation.getResponseExecutor()).thenReturn(new ReactiveExecutor());
    Holder<Object> result = new Holder<>();
    client.send(invocation, ar -> {
        result.value = ar.getResult();
    });
    return result.value;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) MockUp(mockit.MockUp) Vertx(io.vertx.core.Vertx) AbstractVerticle(io.vertx.core.AbstractVerticle) TcpData(org.apache.servicecomb.foundation.vertx.client.tcp.TcpData) ProtobufFeature(io.protostuff.runtime.ProtobufFeature) Response(org.apache.servicecomb.swagger.invocation.Response) DeploymentOptions(io.vertx.core.DeploymentOptions) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) BeforeClass(org.junit.BeforeClass) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Aggregations

Response (org.apache.servicecomb.swagger.invocation.Response)65 Test (org.junit.Test)44 Invocation (org.apache.servicecomb.core.Invocation)17 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)14 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)11 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)8 Expectations (mockit.Expectations)7 ResponseEntity (org.springframework.http.ResponseEntity)7 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)6 MockUp (mockit.MockUp)6 Method (java.lang.reflect.Method)5 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)5 Holder (javax.xml.ws.Holder)4 User (org.apache.servicecomb.demo.server.User)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ApiResponse (io.swagger.annotations.ApiResponse)3 List (java.util.List)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)3