Search in sources :

Example 1 with InvocationContext

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

the class TestConfig method testContextUtils.

@Test
public void testContextUtils() {
    ThreadLocal<InvocationContext> contextMgr = new ThreadLocal<>();
    Assert.assertEquals(contextMgr.get(), ContextUtils.getInvocationContext());
    SwaggerInvocation invocation = new SwaggerInvocation();
    invocation.addContext("test1", "testObject");
    Assert.assertEquals("testObject", invocation.getContext("test1"));
    Map<String, String> context = new HashMap<>();
    context.put("test2", new String("testObject"));
    invocation.setContext(context);
    Assert.assertEquals(context, invocation.getContext());
    invocation.setStatus(Status.OK);
    Assert.assertEquals(200, invocation.getStatus().getStatusCode());
    invocation.setStatus(204);
    Assert.assertEquals(204, invocation.getStatus().getStatusCode());
    invocation.setStatus(Status.OK);
    Assert.assertEquals((Status.OK).getStatusCode(), invocation.getStatus().getStatusCode());
    invocation.setStatus(203, "Done");
    Assert.assertEquals(203, invocation.getStatus().getStatusCode());
    ContextUtils.setInvocationContext(invocation);
    Assert.assertEquals(invocation, ContextUtils.getInvocationContext());
    ContextUtils.removeInvocationContext();
    Assert.assertEquals(null, ContextUtils.getInvocationContext());
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) HashMap(java.util.HashMap) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Test(org.junit.Test)

Example 2 with InvocationContext

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

the class CodeFirstJaxrs method response.

@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@Path("/response")
@GET
public Response response(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().toString());
    return response;
}
Also used : ApiResponse(io.swagger.annotations.ApiResponse) Response(org.apache.servicecomb.swagger.invocation.Response) User(org.apache.servicecomb.demo.server.User) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse)

Example 3 with InvocationContext

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

the class TestPojoConsumerEqualProducer method testContext.

@Test
public void testContext() throws Exception {
    InvocationContext threadContext = new InvocationContext();
    threadContext.addContext("ta", "tvalue");
    ContextUtils.setInvocationContext(threadContext);
    InvocationContext context = new InvocationContext();
    context.addContext("a", "value");
    String result = proxy.testContext(context, "name");
    Object body = invoker.getSwaggerArgument(0);
    Assert.assertEquals(3, invoker.getInvocation().getContext().size());
    Assert.assertEquals("tvalue", invoker.getContext("ta"));
    Assert.assertEquals("value", invoker.getContext("a"));
    Assert.assertEquals("name", invoker.getContext("name"));
    Assert.assertEquals("name", Utils.getFieldValue(body, "name"));
    Assert.assertEquals("name sayhi", result);
}
Also used : InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Test(org.junit.Test)

Example 4 with InvocationContext

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

the class ConsumerInvocationContextMapper method mapArgument.

@Override
public void mapArgument(SwaggerInvocation invocation, Object[] consumerArguments) {
    InvocationContext context = (InvocationContext) consumerArguments[consumerIdx];
    invocation.addContext(context.getContext());
}
Also used : InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 5 with InvocationContext

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

the class PojoClient method testTraceIdOnContextContainsTraceId.

private static void testTraceIdOnContextContainsTraceId() {
    InvocationContext context = new InvocationContext();
    context.addContext(Const.TRACE_ID_NAME, String.valueOf(Long.MIN_VALUE));
    ContextUtils.setInvocationContext(context);
    String traceId = test.testTraceId();
    TestMgr.check(String.valueOf(Long.MIN_VALUE), traceId);
    ContextUtils.removeInvocationContext();
}
Also used : InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Aggregations

InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)35 User (org.apache.servicecomb.demo.server.User)11 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)10 Response (org.apache.servicecomb.swagger.invocation.Response)9 Test (org.junit.Test)8 ApiResponse (io.swagger.annotations.ApiResponse)7 HttpHeaders (org.springframework.http.HttpHeaders)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 HashMap (java.util.HashMap)5 ResponseEntity (org.springframework.http.ResponseEntity)5 Date (java.util.Date)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)4 SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)3 Vertx (io.vertx.core.Vertx)2 LocalDate (java.time.LocalDate)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2