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/06/25 01:56:21 UTC

[incubator-servicecomb-java-chassis] 01/02: [SCB-684]Following swagger conventions. Adding and change some test cases.

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 3872df0596ff176dbd629953017a3ece1262fc3c
Author: liubao <ba...@huawei.com>
AuthorDate: Fri Jun 22 14:59:13 2018 +0800

    [SCB-684]Following swagger conventions. Adding and change some test cases.
---
 .../java/org/apache/servicecomb/demo/TestMgr.java  |  4 ++
 .../src/main/resources/microservice.yaml           |  1 +
 .../server/CodeFirstSpringmvcForSchema.java        | 67 ++++++++++++++++++
 .../server/ProducerSchemaFactoryHolder.java        | 80 ++++++++++++++++++++++
 .../META-INF/spring/springmvc.server.bean.xml      | 25 +++++++
 .../src/main/resources/microservice.yaml           |  1 +
 .../tests/endpoints/CodeFirstSpringmvc.java        |  8 +--
 7 files changed, 182 insertions(+), 4 deletions(-)

diff --git a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/TestMgr.java b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/TestMgr.java
index 80c5bf0..f2218bc 100644
--- a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/TestMgr.java
+++ b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/TestMgr.java
@@ -74,6 +74,10 @@ public class TestMgr {
     errorList.add(error);
   }
 
+  public static boolean isSuccess() {
+    return errorList.isEmpty();
+  }
+
   public static void summary() {
     if (errorList.isEmpty()) {
       LOGGER.info("............. test finished ............");
diff --git a/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml
index 8dc4824..09a4d0e 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-client/src/main/resources/microservice.yaml
@@ -18,6 +18,7 @@
 APPLICATION_ID: springmvctest
 service_description:
   name: springmvcClient
+  environment: development
   version: 0.0.1
 servicecomb:
   service:
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvcForSchema.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvcForSchema.java
new file mode 100644
index 0000000..fd5ddce
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CodeFirstSpringmvcForSchema.java
@@ -0,0 +1,67 @@
+/*
+ * 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 org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * Created for testing schema to accommodate swagger api specification. When change this class, please use
+ * http://editor.swagger.io/ to validate the generated schema and should not give any errors. In test case,
+ * only checksum is validated to make sure schema is not changed.
+ */
+@RestSchema(schemaId = "CodeFirstSpringmvcForSchema")
+@RequestMapping(path = "/forScheam")
+public class CodeFirstSpringmvcForSchema {
+  /*
+   * Using http://editor.swagger.io/ . Listing errors not handled:
+   *
+   * #1. Should NOT have additional properties additionalProperty: type, format, name, in, required
+   /reduce:
+    get:
+      operationId: "reduce"
+      parameters:
+      - name: "b"
+        in: "cookie"
+        required: false
+   This schema gives error, but according to https://swagger.io/docs/specification/describing-parameters/#cookie-parameters
+   This should supported.
+
+
+   * #2. Operations with Parameters of "in: formData" must include "application/x-www-form-urlencoded" or "multipart/form-data" in their "consumes" property
+   This error can be fixed by user code.
+
+
+   *
+   */
+
+
+  @RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
+      consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
+  @ResponseBody
+  public boolean uploadAwardFile(@RequestParam("fileType") String fileType, @RequestParam("zoneId") String zoneId,
+      @RequestPart("file") MultipartFile file) {
+    throw new UnsupportedOperationException("only for testing schema");
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerSchemaFactoryHolder.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerSchemaFactoryHolder.java
new file mode 100644
index 0000000..766d078
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerSchemaFactoryHolder.java
@@ -0,0 +1,80 @@
+/*
+ * 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 org.apache.servicecomb.core.BootListener;
+import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.core.definition.schema.ProducerSchemaFactory;
+import org.apache.servicecomb.demo.TestMgr;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.google.common.base.Charsets;
+import com.google.common.hash.Hashing;
+
+import io.swagger.models.Swagger;
+import io.swagger.util.Yaml;
+
+/**
+ * Created by Administrator on 2018/6/22.
+ */
+public class ProducerSchemaFactoryHolder implements BootListener {
+  private static final Logger LOGGER = LoggerFactory.getLogger(ProducerSchemaFactoryHolder.class);
+
+  private ObjectWriter writer = Yaml.pretty();
+
+  @Autowired
+  private ProducerSchemaFactory factory;
+
+  public void test() {
+    LOGGER.info("ProducerSchemaFactoryHolder testing start");
+    SchemaMeta meta =
+        factory.getOrCreateProducerSchema("customer-service",
+            "test1",
+            CodeFirstSpringmvcForSchema.class,
+            new CodeFirstSpringmvcForSchema());
+    String codeFirst = getSwaggerContent(meta.getSwagger());
+    TestMgr.check("07a48acef4cc1a7f2387d695923c49e98951a974e4f51cf1356d6878db48888f",
+        Hashing.sha256().newHasher().putString(codeFirst, Charsets.UTF_8).hash().toString());
+    TestMgr.check(codeFirst.length(), 899);
+
+    if (!TestMgr.isSuccess()) {
+      TestMgr.summary();
+      throw new IllegalStateException("schema not the same. ");
+    }
+  }
+
+  private String getSwaggerContent(Swagger swagger) {
+    try {
+      return writer.writeValueAsString(swagger);
+    } catch (JsonProcessingException e) {
+      throw new Error(e);
+    }
+  }
+
+  @Override
+  public void onBootEvent(BootEvent event) {
+    if (event.getEventType() == BootListener.EventType.AFTER_REGISTRY) {
+      test();
+    }
+  }
+}
+
diff --git a/demo/demo-springmvc/springmvc-server/src/main/resources/META-INF/spring/springmvc.server.bean.xml b/demo/demo-springmvc/springmvc-server/src/main/resources/META-INF/spring/springmvc.server.bean.xml
new file mode 100644
index 0000000..7b05f28
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/META-INF/spring/springmvc.server.bean.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd">
+
+  <bean id="ProducerSchemaFactoryHolder"
+    class="org.apache.servicecomb.demo.springmvc.server.ProducerSchemaFactoryHolder"></bean>
+</beans>
diff --git a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index b9c9638..c2a2087 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -21,6 +21,7 @@ APPLICATION_ID: springmvctest
 service_description:
   name: springmvc
   version: 0.0.3
+  environment: development
   paths:
     - path: /test1/testpath
       property:
diff --git a/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/endpoints/CodeFirstSpringmvc.java b/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/endpoints/CodeFirstSpringmvc.java
index 7748ff6..c235b79 100644
--- a/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/endpoints/CodeFirstSpringmvc.java
+++ b/integration-tests/springmvc-tests/springmvc-tests-common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/endpoints/CodeFirstSpringmvc.java
@@ -56,7 +56,7 @@ import io.swagger.annotations.ResponseHeader;
 public class CodeFirstSpringmvc extends CodeFirstSpringmvcBase {
   @ResponseHeaders({@ResponseHeader(name = "h1", response = String.class),
       @ResponseHeader(name = "h2", response = String.class)})
-  @RequestMapping(path = "/responseEntity", method = RequestMethod.POST)
+  @RequestMapping(path = "/responseEntity", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
   @Override
   public ResponseEntity<Date> responseEntity(InvocationContext c1, @RequestAttribute("date") Date date) {
     return super.responseEntity(c1, date);
@@ -89,14 +89,14 @@ public class CodeFirstSpringmvc extends CodeFirstSpringmvcBase {
     return super.bytes(input);
   }
 
-  @RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
+  @RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   @Override
   public String fileUpload(@RequestPart(name = "file1") MultipartFile file1,
       @RequestPart(name = "someFile") MultipartFile file2, @RequestAttribute("name") String name) {
     return super.fileUpload(file1, file2, name);
   }
 
-  @RequestMapping(path = "/uploadWithoutAnnotation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
+  @RequestMapping(path = "/uploadWithoutAnnotation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
   public String fileUploadWithoutAnnotation(MultipartFile file1, MultipartFile file2,
       @RequestAttribute("name") String name) {
     return super.fileUpload(file1, file2, name);
@@ -114,7 +114,7 @@ public class CodeFirstSpringmvc extends CodeFirstSpringmvcBase {
     return a;
   }
 
-  @RequestMapping(path = "/add", method = RequestMethod.POST)
+  @RequestMapping(path = "/add", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
   @Override
   public int add(@RequestAttribute("a") int a, @RequestAttribute("b") int b) {
     return super.add(a, b);