Search in sources :

Example 16 with InvocationContext

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

the class TestPojoV1V1 method addWithContext_add.

@Test
public void addWithContext_add() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(PojoAddV1.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerAddWithContext.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("add").getArgumentsMapper();
    InvocationContext invocationContext = new InvocationContext();
    invocationContext.addContext("k1", "v1");
    invocationContext.addContext("k2", "v2");
    invocationContext.addLocalContext("k3", "v3");
    invocationContext.addLocalContext("k4", "v4");
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("context", invocationContext);
    arguments.put("x", 1);
    arguments.put("y", 2);
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertEquals(1, result.size());
    result = (Map<String, Object>) result.get("addBody");
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(1, result.get("x"));
    Assert.assertEquals(2, result.get("y"));
    Assert.assertEquals(2, invocation.getContext().size());
    Assert.assertEquals("v1", invocation.getContext().get("k1"));
    Assert.assertEquals("v2", invocation.getContext().get("k2"));
    Assert.assertEquals(2, invocation.getLocalContext().size());
    Assert.assertEquals("v3", invocation.getLocalContext().get("k3"));
    Assert.assertEquals("v4", invocation.getLocalContext().get("k4"));
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Example 17 with InvocationContext

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

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 18 with InvocationContext

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

the class CodeFirstSpringmvcBase method cseResponse.

public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    response.addHeader("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    response.addHeader("h2", "h2v " + c2.getContext().toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) User(org.apache.servicecomb.demo.server.User) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 19 with InvocationContext

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

the class CodeFirstSpringmvcBase method responseEntity.

public ResponseEntity<Date> responseEntity(InvocationContext c1, Date date) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.add("h2", "h2v " + c2.getContext().toString());
    return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 20 with InvocationContext

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

the class CodeFirstSpringmvc method responseEntityPATCH.

@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@RequestMapping(path = "/responseEntity", method = RequestMethod.PATCH)
public ResponseEntity<Date> responseEntityPATCH(InvocationContext c1, @RequestAttribute("date") Date date) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE).toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.add("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());
    return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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