You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/08/14 08:30:09 UTC

[incubator-servicecomb-java-chassis] 02/03: [SCB-827] add IT to test response decoding error situation

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 5c5869e08157d76fbc04ed545ad98a89cfdd1cd4
Author: yaohaishi <ya...@huawei.com>
AuthorDate: Thu Aug 9 19:49:45 2018 +0800

    [SCB-827] add IT to test response decoding error situation
---
 .../springmvc/client/CodeFirstSpringmvcIntf.java   |  3 ++
 .../demo/springmvc/client/TestResponse.java        | 31 +++++++++++++++++
 .../decoderesponse/DecodeTestResponse.java         | 40 ++++++++++++++++++++++
 .../decoderesponse/DecodeTestResponse.java         | 38 ++++++++++++++++++++
 .../demo/springmvc/server/CodeFirstSpringmvc.java  |  8 +++++
 5 files changed, 120 insertions(+)

diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstSpringmvcIntf.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstSpringmvcIntf.java
index c2f5cc5..509e15c 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstSpringmvcIntf.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstSpringmvcIntf.java
@@ -27,6 +27,7 @@ import org.apache.servicecomb.demo.Generic;
 import org.apache.servicecomb.demo.compute.GenericParam;
 import org.apache.servicecomb.demo.compute.Person;
 import org.apache.servicecomb.demo.server.User;
+import org.apache.servicecomb.demo.springmvc.decoderesponse.DecodeTestResponse;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -72,4 +73,6 @@ public interface CodeFirstSpringmvcIntf {
   String testDelay();
 
   String testAbort();
+
+  DecodeTestResponse testDecodeResponseError();
 }
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestResponse.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestResponse.java
index e243a2f..844b562 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestResponse.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestResponse.java
@@ -17,7 +17,9 @@
 package org.apache.servicecomb.demo.springmvc.client;
 
 import java.util.Date;
+import java.util.Objects;
 
+import org.apache.servicecomb.core.exception.CseException;
 import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.demo.compute.GenericParam;
 import org.apache.servicecomb.demo.compute.Person;
@@ -27,6 +29,8 @@ import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.springframework.http.ResponseEntity;
 
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
+
 public class TestResponse {
   private CodeFirstSpringmvcIntf intf;
 
@@ -39,6 +43,7 @@ public class TestResponse {
     checkQueryGenericString();
     testDelay();
     testAbort();
+    testDecodeResponseError();
   }
 
   public void runHighway() {
@@ -130,4 +135,30 @@ public class TestResponse {
             + "OK|InvocationException: code=421;msg=CommonExceptionData [message=aborted by fault inject]|",
         result.toString());
   }
+
+  private void testDecodeResponseError() {
+    InvocationException exception = null;
+    try {
+      intf.testDecodeResponseError();
+    } catch (InvocationException e) {
+      // 1. InvocationException: wrapper exception
+      exception = e;
+    }
+    Objects.requireNonNull(exception);
+    // 2. CseException: bizKeeper exception
+    Throwable wrappedException = exception.getCause();
+    TestMgr.check(CseException.class, wrappedException.getClass());
+    // 3. InvocationException: decoder wrapping exception
+    wrappedException = wrappedException.getCause();
+    TestMgr.check(InvocationException.class, wrappedException.getClass());
+    // 4. InvalidFormatException: decode exception
+    Object errorData = ((InvocationException) wrappedException).getErrorData();
+    TestMgr.check(InvalidFormatException.class, errorData.getClass());
+    TestMgr.check(
+        "Cannot deserialize value of type `java.util.Date` from String \"returnOK\": not a valid representation "
+            + "(error: Failed to parse Date value 'returnOK': Failed to parse date \"returnOK\": Invalid number: retu)\n"
+            + " at [Source: (org.apache.servicecomb.foundation.vertx.stream.BufferInputStream); line: 1, column: 12] "
+            + "(through reference chain: org.apache.servicecomb.demo.springmvc.decoderesponse.DecodeTestResponse[\"content\"])",
+        ((InvalidFormatException) errorData).getMessage());
+  }
 }
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java
new file mode 100644
index 0000000..f27c15d
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.demo.springmvc.decoderesponse;
+
+import java.util.Date;
+
+public class DecodeTestResponse {
+  private Date content;
+
+  public Date getContent() {
+    return content;
+  }
+
+  public void setContent(Date content) {
+    this.content = content;
+  }
+
+  @Override
+  public String toString() {
+    final StringBuilder sb = new StringBuilder("DecodeTestResponse{");
+    sb.append("content=").append(content);
+    sb.append('}');
+    return sb.toString();
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java
new file mode 100644
index 0000000..9a19dc1
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/decoderesponse/DecodeTestResponse.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.demo.springmvc.decoderesponse;
+
+public class DecodeTestResponse {
+  private String content;
+
+  public String getContent() {
+    return content;
+  }
+
+  public void setContent(String content) {
+    this.content = content;
+  }
+
+  @Override
+  public String toString() {
+    final StringBuilder sb = new StringBuilder("DecodeTestResponse{");
+    sb.append("content='").append(content).append('\'');
+    sb.append('}');
+    return sb.toString();
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
index 74f8b11..6ec2bc5 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvc.java
@@ -44,6 +44,7 @@ import org.apache.servicecomb.demo.ignore.InputModelForTestIgnore;
 import org.apache.servicecomb.demo.ignore.OutputModelForTestIgnore;
 import org.apache.servicecomb.demo.jaxbbean.JAXBPerson;
 import org.apache.servicecomb.demo.server.User;
+import org.apache.servicecomb.demo.springmvc.decoderesponse.DecodeTestResponse;
 import org.apache.servicecomb.provider.rest.common.RestSchema;
 import org.apache.servicecomb.swagger.extend.annotations.RawJsonRequestBody;
 import org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders;
@@ -524,4 +525,11 @@ public class CodeFirstSpringmvc {
     LOGGER.info("testAbort() is called!");
     return "OK";
   }
+
+  @GetMapping(path = "/testDecodeResponseError")
+  public DecodeTestResponse testDecodeResponseError() {
+    DecodeTestResponse response = new DecodeTestResponse();
+    response.setContent("returnOK");
+    return response;
+  }
 }