Search in sources :

Example 6 with MultiResponse200

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse200 in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCodeWithHeaderJAXRS.

private static void testErrorCodeWithHeaderJAXRS() {
    MultiRequest request = new MultiRequest();
    request.setCode(200);
    request.setMessage("success result");
    ResponseEntity<MultiResponse200> result = template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
    TestMgr.check(result.getBody().getCode(), 200);
    TestMgr.check(result.getHeaders().getFirst("x-code"), 200);
    request.setCode(400);
    request.setMessage("bad request");
    MultiResponse400 t400 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t400 = (MultiResponse400) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.BAD_REQUEST.getStatusCode());
    }
    TestMgr.check(t400.getCode(), 400);
    TestMgr.check(t400.getMessage(), "bad request");
    request.setCode(500);
    request.setMessage("internal error");
    MultiResponse500 t500 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeaderJAXRS", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t500 = (MultiResponse500) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    TestMgr.check(t500.getCode(), 500);
    TestMgr.check(t500.getMessage(), "internal error");
}
Also used : MultiRequest(org.apache.servicecomb.demo.multiErrorCode.MultiRequest) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)

Example 7 with MultiResponse200

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse200 in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCodeWithHeader.

private static void testErrorCodeWithHeader() {
    MultiRequest request = new MultiRequest();
    request.setCode(200);
    ResponseEntity<MultiResponse200> result = template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeader", request, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
    TestMgr.check(result.getBody().getCode(), 200);
    TestMgr.check(result.getHeaders().getFirst("x-code"), 200);
    request.setCode(400);
    MultiResponse400 t400 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeader", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t400 = (MultiResponse400) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.BAD_REQUEST.getStatusCode());
    }
    TestMgr.check(t400.getCode(), 400);
    TestMgr.check(t400.getMessage(), "bad request");
    request.setCode(500);
    MultiResponse500 t500 = null;
    try {
        template.postForEntity(SERVER + "/MultiErrorCodeService/errorCodeWithHeader", request, MultiResponse400.class);
    } catch (InvocationException e) {
        t500 = (MultiResponse500) e.getErrorData();
        TestMgr.check(e.getStatus().getStatusCode(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    TestMgr.check(t500.getCode(), 500);
    TestMgr.check(t500.getMessage(), "internal error");
}
Also used : MultiRequest(org.apache.servicecomb.demo.multiErrorCode.MultiRequest) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) MultiResponse400(org.apache.servicecomb.demo.multiErrorCode.MultiResponse400) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MultiResponse500(org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)

Example 8 with MultiResponse200

use of org.apache.servicecomb.demo.multiErrorCode.MultiResponse200 in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCodeWrongType.

private static void testErrorCodeWrongType() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    String body = "{\"message\":\"hello\",\"code\":\"wrongType\"";
    HttpEntity<String> entity = new HttpEntity<>(body, headers);
    ResponseEntity<MultiResponse200> result;
    try {
        template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
    } catch (HttpClientErrorException e) {
        TestMgr.check(e.getRawStatusCode(), 400);
        TestMgr.check(e.getMessage(), "400 Bad Request: \"{\"message\":\"Parameter is not valid for operation [jaxrs.MultiErrorCodeService.errorCode]." + " Parameter is [request]. Processor is [body].\"}\"");
    }
    entity = new HttpEntity<>(null, headers);
    try {
        result = template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
        TestMgr.check(590, 200);
    } catch (HttpServerErrorException e) {
        TestMgr.check(e.getRawStatusCode(), 500);
    }
    // not recommend
    body = "{\"message\":\"hello\",\"code\":\"200\"}";
    entity = new HttpEntity<>(body, headers);
    result = template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Aggregations

MultiResponse200 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse200)8 MultiResponse400 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse400)6 MultiResponse500 (org.apache.servicecomb.demo.multiErrorCode.MultiResponse500)6 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)6 MultiRequest (org.apache.servicecomb.demo.multiErrorCode.MultiRequest)4 ApiResponses (io.swagger.annotations.ApiResponses)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)2 ApiResponse (io.swagger.annotations.ApiResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 Response (org.apache.servicecomb.swagger.invocation.Response)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1