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 be33086dbde190ceec425aeea3ae5662fbf6b633
Author: yaohaishi <[email protected]>
AuthorDate: Fri Aug 24 00:31:31 2018 +0800

    [SCB-206] add IT on @Api
---
 .../client/CodeFirstRestTemplateSpringmvc.java     |  6 +-
 .../demo/springmvc/client/TestContentType.java     | 90 ++++++++++++++++++++++
 .../springmvc/server/ContentTypeSpringmvc.java     | 51 ++++++++++++
 .../server/ContentTypeSpringmvcOverwrite.java      | 36 +++++++++
 .../springmvc/server/ProducerTestsAfterBootup.java |  7 +-
 5 files changed, 184 insertions(+), 6 deletions(-)

diff --git 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
index e34b340..d3760e3 100644
--- 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
+++ 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/CodeFirstRestTemplateSpringmvc.java
@@ -30,7 +30,6 @@ import javax.servlet.http.Part;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.servicecomb.bizkeeper.BizkeeperExceptionUtils;
-import org.apache.servicecomb.core.exception.CseException;
 import org.apache.servicecomb.demo.CodeFirstRestTemplate;
 import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.foundation.common.part.FilePart;
@@ -75,6 +74,8 @@ public class CodeFirstRestTemplateSpringmvc extends 
CodeFirstRestTemplate {
 
   private TestRestTemplate testRestTemplate = new TestRestTemplate();
 
+  private TestContentType testContentType = new TestContentType();
+
   @Override
   protected void testOnlyRest(RestTemplate template, String cseUrlPrefix) {
     testDownload.runRest();
@@ -89,6 +90,7 @@ public class CodeFirstRestTemplateSpringmvc extends 
CodeFirstRestTemplate {
     testObject.runRest();
     testGeneric.runRest();
     testRestTemplate.runRest();
+    testContentType.runAllTest();
 
     super.testOnlyRest(template, cseUrlPrefix);
   }
@@ -185,7 +187,7 @@ public class CodeFirstRestTemplateSpringmvc extends 
CodeFirstRestTemplate {
       result = template.getForObject(cseUrlPrefix + 
"/fallback/throwexception/throwexception", String.class);
       TestMgr.check(false, true);
     } catch (Exception e) {
-      TestMgr.check(((CseException) e.getCause()).getMessage(),
+      TestMgr.check(e.getCause().getMessage(),
           
BizkeeperExceptionUtils.createBizkeeperException(BizkeeperExceptionUtils.SERVICECOMB_BIZKEEPER_FALLBACK,
               null,
               "springmvc.codeFirst.fallbackThrowException").getMessage());
diff --git 
a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestContentType.java
 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestContentType.java
new file mode 100644
index 0000000..65fc146
--- /dev/null
+++ 
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestContentType.java
@@ -0,0 +1,90 @@
+/*
+ * 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.client;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity;
+import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+public class TestContentType {
+
+  private RestTemplate restTemplate = RestTemplateBuilder.create();
+
+  public void runAllTest() {
+    testGlobalSetting();
+    testApiOperation();
+    testRequestMapping();
+    testResponseTypeOverwrite();
+  }
+
+  private void testGlobalSetting() {
+    HttpHeaders requestHeaders = new HttpHeaders();
+    requestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
+    CseHttpEntity<String> requestEntity = new CseHttpEntity<>("from 
testGlobalSetting", requestHeaders);
+    ResponseEntity<String> responseEntity = restTemplate
+        .exchange("cse://springmvc/contentTypeSpringmvc/testGlobalSetting", 
HttpMethod.POST,
+            requestEntity, String.class);
+    TestMgr.check(
+        "testGlobalSetting: name=[from testGlobalSetting], request 
content-type=[" + MediaType.TEXT_PLAIN + "]",
+        responseEntity.getBody());
+    TestMgr.check(MediaType.TEXT_PLAIN, 
extractContentType(responseEntity.getHeaders().getContentType()));
+  }
+
+  private void testApiOperation() {
+    HttpHeaders requestHeaders = new HttpHeaders();
+    requestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
+    CseHttpEntity<String> requestEntity = new CseHttpEntity<>("from 
testApiOperation", requestHeaders);
+    ResponseEntity<String> responseEntity = restTemplate
+        .exchange("cse://springmvc/contentTypeSpringmvc/testApiOperation", 
HttpMethod.POST,
+            requestEntity, String.class);
+    TestMgr.check(
+        "testApiOperation: name=[from testApiOperation], request 
content-type=[" + MediaType.APPLICATION_JSON + "]",
+        responseEntity.getBody());
+    TestMgr.check(MediaType.APPLICATION_JSON, 
extractContentType(responseEntity.getHeaders().getContentType()));
+  }
+
+  private void testRequestMapping() {
+    HttpHeaders requestHeaders = new HttpHeaders();
+    requestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
+    CseHttpEntity<String> requestEntity = new CseHttpEntity<>("from 
testRequestMapping", requestHeaders);
+    ResponseEntity<String> responseEntity = restTemplate
+        .exchange("cse://springmvc/contentTypeSpringmvc/testRequestMapping", 
HttpMethod.POST,
+            requestEntity, String.class);
+    TestMgr.check(
+        "testRequestMapping: name=[from testRequestMapping], request 
content-type=[" + MediaType.APPLICATION_JSON + "]",
+        responseEntity.getBody());
+    TestMgr.check(MediaType.APPLICATION_JSON, 
extractContentType(responseEntity.getHeaders().getContentType()));
+  }
+
+  private void testResponseTypeOverwrite() {
+    ResponseEntity<String> responseEntity = restTemplate
+        
.getForEntity("cse://springmvc/contentTypeSpringmvcOverwrite/testResponseTypeOverwrite",
 String.class);
+    TestMgr.check("testResponseTypeOverwrite: OK", responseEntity.getBody());
+    TestMgr.check(MediaType.TEXT_PLAIN, 
extractContentType(responseEntity.getHeaders().getContentType()));
+  }
+
+  private String extractContentType(org.springframework.http.MediaType 
mediaType) {
+    return mediaType.getType() + "/" + mediaType.getSubtype();
+  }
+}
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvc.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvc.java
new file mode 100644
index 0000000..94281ca
--- /dev/null
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvc.java
@@ -0,0 +1,51 @@
+/*
+ * 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.server;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+
+@RestSchema(schemaId = "contentTypeSpringmvc")
+@RequestMapping("/contentTypeSpringmvc")
+@Api(consumes = MediaType.TEXT_PLAIN, produces = MediaType.TEXT_PLAIN)
+public class ContentTypeSpringmvc {
+  @RequestMapping(path = "/testGlobalSetting", method = RequestMethod.POST)
+  public String testGlobalSetting(@RequestBody String name, HttpServletRequest 
request) {
+    return String.format("testGlobalSetting: name=[%s], request 
content-type=[%s]", name, request.getContentType());
+  }
+
+  @RequestMapping(path = "/testApiOperation", method = RequestMethod.POST)
+  @ApiOperation(value = "testApiOperation desc", consumes = 
MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
+  public String testApiOperation(@RequestBody String name, HttpServletRequest 
request) {
+    return String.format("testApiOperation: name=[%s], request 
content-type=[%s]", name, request.getContentType());
+  }
+
+  @RequestMapping(path = "/testRequestMapping", method = RequestMethod.POST,
+      consumes = MediaType.APPLICATION_JSON, produces = 
MediaType.APPLICATION_JSON)
+  public String testRequestMapping(@RequestBody String name, 
HttpServletRequest request) {
+    return String.format("testRequestMapping: name=[%s], request 
content-type=[%s]", name, request.getContentType());
+  }
+}
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvcOverwrite.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvcOverwrite.java
new file mode 100644
index 0000000..74b2d13
--- /dev/null
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ContentTypeSpringmvcOverwrite.java
@@ -0,0 +1,36 @@
+/*
+ * 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.server;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import io.swagger.annotations.Api;
+
+@RestSchema(schemaId = "contentTypeSpringmvcOverwrite")
+@RequestMapping(value = "/contentTypeSpringmvcOverwrite", produces = 
MediaType.TEXT_PLAIN)
+@Api(produces = MediaType.APPLICATION_JSON)
+public class ContentTypeSpringmvcOverwrite {
+  @RequestMapping(value = "/testResponseTypeOverwrite", method = 
RequestMethod.GET)
+  public String testResponseTypeOverwrite() {
+    return "testResponseTypeOverwrite: OK";
+  }
+}
diff --git 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
index 7420815..6023d1d 100644
--- 
a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
+++ 
b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
@@ -56,11 +56,10 @@ public class ProducerTestsAfterBootup implements 
BootListener {
     
TestMgr.check("2986daa46b229ec125443122dd7b51ee9a64879f1750d0996f948ce0718685c7",
         RegistryUtils.calcSchemaSummary(codeFirst));
     TestMgr.check(codeFirst.length(), 889);
-
   }
 
-  public void testRegisterPath() {
-    TestMgr.check(RegistryUtils.getMicroservice().getPaths().size(), 10);
+  public void testRegisteredBasePath() {
+    TestMgr.check(12, RegistryUtils.getMicroservice().getPaths().size());
   }
 
   private String getSwaggerContent(Swagger swagger) {
@@ -75,7 +74,7 @@ public class ProducerTestsAfterBootup implements BootListener 
{
   public void onBootEvent(BootEvent event) {
     if (event.getEventType() == BootListener.EventType.AFTER_REGISTRY) {
       testSchemaNotChange();
-      testRegisterPath();
+      testRegisteredBasePath();
       if (!TestMgr.isSuccess()) {
         TestMgr.summary();
         throw new IllegalStateException("some tests are failed. ");

Reply via email to