Java Code Examples for org.apache.servicecomb.swagger.invocation.Response#getResult()

The following examples show how to use org.apache.servicecomb.swagger.invocation.Response#getResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void downloadSchemas_failed() {
  SchemaFormat format = SchemaFormat.SWAGGER;
  new Expectations(format) {
    {
      format.getSuffix();
      result = new RuntimeExceptionWithoutStackTrace("zip failed.");
    }
  };

  try (LogCollector logCollector = new LogCollector()) {
    Response response = inspector.downloadSchemas(format);

    Assert.assertEquals("failed to create schemas zip file, format=SWAGGER.",
        logCollector.getLastEvents().getMessage());

    InvocationException invocationException = response.getResult();
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR, invocationException.getStatus());
    Assert.assertEquals("failed to create schemas zip file, format=SWAGGER.",
        ((CommonExceptionData) invocationException.getErrorData()).getMessage());
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode());
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), response.getReasonPhrase());
  }
}
 
Example 2
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void downloadSchemas_html(@Mocked Microservice microservice) throws IOException {
  new Expectations(RegistrationManager.INSTANCE) {
    {
      RegistrationManager.INSTANCE.getMicroservice();
      result = microservice;
      microservice.getServiceName();
      result = "ms";
    }
  };
  Response response = inspector.downloadSchemas(SchemaFormat.HTML);
  Part part = response.getResult();
  Assert.assertEquals("ms.html.zip", part.getSubmittedFileName());

  try (InputStream is = part.getInputStream()) {
    Map<String, String> unziped = unzip(is);

    Assert.assertEquals(schemas.size(), unziped.size());
    Assert.assertTrue(unziped.get("schema1.html").endsWith("</html>"));
    Assert.assertTrue(unziped.get("schema2.html").endsWith("</html>"));
  }
}
 
Example 3
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private void testDownloadSchemasSwagger(Microservice microservice, SchemaFormat format) throws IOException {
  new Expectations(RegistrationManager.INSTANCE) {
    {
      RegistrationManager.INSTANCE.getMicroservice();
      result = microservice;
      microservice.getServiceName();
      result = "ms";
    }
  };

  Response response = inspector.downloadSchemas(format);
  Part part = response.getResult();
  Assert.assertEquals("ms.yaml.zip", part.getSubmittedFileName());

  try (InputStream is = part.getInputStream()) {
    Map<String, String> unziped = unzip(is);

    Assert.assertEquals(schemas.size(), unziped.size());
    Assert.assertEquals(schemas.get("schema1"), unziped.get("schema1.yaml"));
    Assert.assertEquals(schemas.get("schema2"), unziped.get("schema2.yaml"));
  }
}
 
Example 4
Source File: TestConfig.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testResponse() {
  Response response = Response.create(400, "test", null);
  InvocationException exception = response.getResult();
  Assert.assertEquals(null, exception.getErrorData());

  response = Response.create(400, "test", "errorData");
  exception = response.getResult();
  Assert.assertEquals("errorData", exception.getErrorData());
}
 
Example 5
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void getStaticResource() throws IOException {
  Response response = inspector.getStaticResource("index.html");

  Part part = response.getResult();
  Assert.assertEquals("inline", response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
  Assert.assertEquals(MediaType.TEXT_HTML, response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));

  try (InputStream is = part.getInputStream()) {
    Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("</html>"));
  }
}
 
Example 6
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void testDownloadHtmlById(String schemaId) throws IOException {
  Response response = inspector.getSchemaContentById(schemaId, SchemaFormat.HTML, true);

  Part part = response.getResult();
  Assert.assertEquals(schemaId + ".html", part.getSubmittedFileName());
  Assert.assertNull(response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
  Assert.assertEquals(MediaType.TEXT_HTML, response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));

  try (InputStream is = part.getInputStream()) {
    Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("</html>"));
  }
}
 
Example 7
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void testViewHtmlById(String schemaId) throws IOException {
  Response response = inspector.getSchemaContentById(schemaId, SchemaFormat.HTML, false);

  Part part = response.getResult();
  Assert.assertEquals(schemaId + ".html", part.getSubmittedFileName());
  Assert.assertEquals("inline", response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
  Assert.assertEquals(MediaType.TEXT_HTML, response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));

  try (InputStream is = part.getInputStream()) {
    Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("</html>"));
  }
}
 
Example 8
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void testDownloadSwaggerById(String schemaId, SchemaFormat format) throws IOException {
  Response response = inspector.getSchemaContentById(schemaId, format, true);

  Part part = response.getResult();
  Assert.assertEquals(schemaId + ".yaml", part.getSubmittedFileName());
  Assert.assertNull(response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
  Assert.assertEquals(MediaType.TEXT_HTML, response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));

  try (InputStream is = part.getInputStream()) {
    Assert.assertEquals(schemas.get(schemaId), IOUtils.toString(is, StandardCharsets.UTF_8));
  }
}
 
Example 9
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void testViewSwaggerById(String schemaId, SchemaFormat format) throws IOException {
  Response response = inspector.getSchemaContentById(schemaId, format, false);

  Part part = response.getResult();
  Assert.assertEquals(schemaId + ".yaml", part.getSubmittedFileName());
  Assert.assertEquals("inline", response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
  Assert.assertEquals(MediaType.TEXT_HTML, response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));

  try (InputStream is = part.getInputStream()) {
    Assert.assertEquals(schemas.get(schemaId), IOUtils.toString(is, StandardCharsets.UTF_8));
  }
}
 
Example 10
Source File: TestInspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void getSchemaContentById_notExist() {
  Response response = inspector.getSchemaContentById("notExist", null, false);

  InvocationException invocationException = response.getResult();
  Assert.assertEquals(Status.NOT_FOUND, invocationException.getStatus());
  Assert.assertEquals(Status.NOT_FOUND.getReasonPhrase(),
      ((CommonExceptionData) invocationException.getErrorData()).getMessage());
  Assert.assertEquals(404, response.getStatusCode());
  Assert.assertEquals("Not Found", response.getReasonPhrase());
}
 
Example 11
Source File: TestClassPathStaticResourceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void attack() {
  Response response = handler.handle("../microservice.yaml");

  InvocationException invocationException = response.getResult();
  Assert.assertEquals(Status.NOT_FOUND, invocationException.getStatus());
  Assert.assertEquals(Status.NOT_FOUND.getReasonPhrase(),
      ((CommonExceptionData) invocationException.getErrorData()).getMessage());
  Assert.assertEquals(404, response.getStatusCode());
  Assert.assertEquals("Not Found", response.getReasonPhrase());
}
 
Example 12
Source File: TestClassPathStaticResourceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void notExist() {
  Response response = handler.handle("notExist.html");

  InvocationException invocationException = response.getResult();
  Assert.assertEquals(Status.NOT_FOUND, invocationException.getStatus());
  Assert.assertEquals(Status.NOT_FOUND.getReasonPhrase(),
      ((CommonExceptionData) invocationException.getErrorData()).getMessage());
  Assert.assertEquals(404, response.getStatusCode());
  Assert.assertEquals("Not Found", response.getReasonPhrase());
}
 
Example 13
Source File: TestClassPathStaticResourceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void normal() throws IOException {
  Response response = handler.handle("index.html");
  Part part = response.getResult();

  try (InputStream is = part.getInputStream()) {
    Assert.assertTrue(IOUtils.toString(is, StandardCharsets.UTF_8).endsWith("<html></html>"));
  }
  Assert.assertEquals("text/html", part.getContentType());
  Assert.assertEquals("text/html", response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
  Assert.assertEquals("inline", response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
}
 
Example 14
Source File: InvokerUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
/**
 * This is an internal API, caller make sure already invoked SCBEngine.ensureStatusUp
 * @param invocation
 * @return contract result
 * @throws InvocationException
 */
public static Object syncInvoke(Invocation invocation) throws InvocationException {
  Response response = innerSyncInvoke(invocation);
  if (response.isSuccessed()) {
    return response.getResult();
  }
  throw ExceptionFactory.convertConsumerException(response.getResult());
}
 
Example 15
Source File: HighwayServerInvoke.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void sendResponse(Map<String, String> context, Response response) {
  invocation.getInvocationStageTrace().finishHandlersResponse();

  ResponseHeader header = new ResponseHeader();
  header.setStatusCode(response.getStatusCode());
  header.setReasonPhrase(response.getReasonPhrase());
  header.setContext(context);
  header.setHeaders(response.getHeaders());

  ResponseRootSerializer bodySchema = operationProtobuf.findResponseRootSerializer(response.getStatusCode());
  Object body = response.getResult();
  if (response.isFailed()) {
    body = ((InvocationException) body).getErrorData();
  }

  try {
    Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
    invocation.getInvocationStageTrace().finishServerFiltersResponse();
    connection.write(respBuffer.getByteBuf());
  } catch (Exception e) {
    // keep highway performance and simple, this encoding/decoding error not need handle by client
    String msg = String.format("encode response failed, %s, msgId=%d",
        invocation.getOperationMeta().getMicroserviceQualifiedName(),
        msgId);
    LOGGER.error(msg, e);
  } finally {
    if (invocation != null) {
      invocation.onFinish(response);
    }
  }
}
 
Example 16
Source File: TestDefaultHttpClientFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAfterReceiveResponseNullProduceProcessor(@Mocked Invocation invocation,
    @Mocked HttpServletResponseEx responseEx,
    @Mocked OperationMeta operationMeta,
    @Mocked RestOperationMeta swaggerRestOperation) throws Exception {
  CommonExceptionData data = new CommonExceptionData("abcd");
  new Expectations() {
    {
      invocation.getOperationMeta();
      result = operationMeta;
      operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
      result = swaggerRestOperation;
      invocation.findResponseType(403);
      result = SimpleType.constructUnsafe(CommonExceptionData.class);
      responseEx.getStatus();
      result = 403;
      responseEx.getStatusType();
      result = Status.FORBIDDEN;
      responseEx.getBodyBuffer();
      result = Buffer.buffer(JsonUtils.writeValueAsString(data).getBytes());
    }
  };

  Response response = filter.afterReceiveResponse(invocation, responseEx);
  Assert.assertEquals(403, response.getStatusCode());
  Assert.assertEquals("Forbidden", response.getReasonPhrase());
  Assert.assertEquals(InvocationException.class, response.<InvocationException>getResult().getClass());
  InvocationException invocationException = response.getResult();
  Assert.assertEquals(
      403,
      invocationException.getStatusCode());
  Assert.assertEquals(
      "CommonExceptionData [message=abcd]",
      invocationException.getErrorData().toString());
}
 
Example 17
Source File: ConsumerBizkeeperCommand.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isFailedResponse(Response resp) {
  if (resp.isFailed()) {
    if (InvocationException.class.isInstance(resp.getResult())) {
      InvocationException e = (InvocationException) resp.getResult();
      return e.getStatusCode() == ExceptionFactory.CONSUMER_INNER_STATUS_CODE;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 18
Source File: FilterNode.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private Response rethrowExceptionInResponse(Response response) {
  if (response.isFailed() && response.getResult() instanceof Throwable) {
    AsyncUtils.rethrow(response.getResult());
  }

  return response;
}
 
Example 19
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected boolean isFailedResponse(Response resp) {
  if (resp.isFailed()) {
    if (InvocationException.class.isInstance(resp.getResult())) {
      InvocationException e = (InvocationException) resp.getResult();
      return e.getStatusCode() == ExceptionFactory.CONSUMER_INNER_STATUS_CODE
          || e.getStatusCode() == 503;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 20
Source File: DefaultConsumerResponseMapper.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Object mapResponse(Response response) {
  return response.getResult();
}