wujimin closed pull request #801: [SCB-727] Local generated schema and service 
center schema are different then print the diff content to the user
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/801
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/service-registry/pom.xml b/service-registry/pom.xml
index b2fbcdd16..72e26f48d 100644
--- a/service-registry/pom.xml
+++ b/service-registry/pom.xml
@@ -56,5 +56,9 @@
       <groupId>org.apache.servicecomb</groupId>
       <artifactId>foundation-test-scaffolding</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 363668286..35ee1e636 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -33,10 +33,10 @@
 import org.apache.servicecomb.serviceregistry.client.http.Holder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.util.StringUtils;
 
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
+import org.apache.commons.lang3.StringUtils;
 
 public class MicroserviceRegisterTask extends AbstractRegisterTask {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(MicroserviceRegisterTask.class);
@@ -191,7 +191,9 @@ private boolean registerSchema(Map<String, 
GetSchemaResponse> scSchemaMap,
     GetSchemaResponse scSchema = scSchemaMap.get(localSchemaEntry.getKey());
 
     boolean onlineSchemaExists = scSchema != null;
-    LOGGER.info("schemaId [{}] exists [{}], summary exists [{}]", 
localSchemaEntry.getKey(), onlineSchemaExists,
+    LOGGER.info("schemaId [{}] exists [{}], summary exists [{}]",
+        localSchemaEntry.getKey(),
+        onlineSchemaExists,
         scSchema != null && scSchema.getSummary() != null);
     if (!onlineSchemaExists) {
       // local > sc
@@ -250,10 +252,26 @@ private boolean compareAndReRegisterSchema(Entry<String, 
String> localSchemaEntr
       if (onlineSchemaIsModifiable()) {
         LOGGER.info(
             "schema[{}]'s content is changed and the current environment is 
[{}], so re-register it!",
-            localSchemaEntry.getKey(), 
ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV);
+            localSchemaEntry.getKey(),
+            ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV);
         return registerSingleSchema(localSchemaEntry.getKey(), 
localSchemaEntry.getValue());
       }
 
+      //if local schema and service center schema is different then print the 
both schemas and print difference in local schema.
+      String scSchemaContent = srClient.getSchema(microservice.getServiceId(), 
scSchema.getSchemaId());
+      String localSchemaContent = localSchemaEntry.getValue();
+
+      LOGGER.warn(
+          "service center schema and local schema both are different:\n 
service center schema:\n[{}\n local schema:\n[{}]",
+          scSchemaContent,
+          localSchemaContent);
+      String diffStringLocal = StringUtils.difference(scSchemaContent, 
localSchemaContent);
+      if (diffStringLocal.equals("")) {
+        LOGGER.warn("Some APIs are deleted in local schema which are present 
in service center schema \n");
+      } else {
+        LOGGER.warn("The difference in local schema:\n[{}]", diffStringLocal);
+      }
+
       // env is not development, throw an exception and break the init 
procedure
       throw new IllegalStateException(
           "The schema(id=[" + localSchemaEntry.getKey()
@@ -307,7 +325,7 @@ private void checkRemainingSchema(Map<String, 
GetSchemaResponse> scSchemaMap) {
 
       // Currently nothing to do but print a warning
       LOGGER.warn("There are schemas only existing in service center: {}, 
which means there are interfaces changed. "
-              + "It's recommended to increment microservice version before 
deploying.",
+          + "It's recommended to increment microservice version before 
deploying.",
           scSchemaMap.keySet());
       LOGGER.warn("ATTENTION: The schemas in new version are less than the old 
version, "
           + "which may cause compatibility problems.");
diff --git 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index a7944c83a..5c44312c9 100644
--- 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++ 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -18,13 +18,17 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
+import org.apache.log4j.spi.LoggingEvent;
 import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
+import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.apache.servicecomb.serviceregistry.client.http.Holder;
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -42,8 +46,11 @@
 
   private List<MicroserviceRegisterTask> taskList;
 
+  LogCollector collector;
+
   @Before
   public void setup() {
+    collector = new LogCollector();
     eventBus = new EventBus();
 
     taskList = new ArrayList<>();
@@ -61,6 +68,11 @@ public void onEvent(MicroserviceRegisterTask task) {
     microservice.setInstance(new MicroserviceInstance());
   }
 
+  @After
+  public void teardown() {
+    collector.teardown();
+  }
+
   @Test
   public void testNewRegisterFailed(@Mocked ServiceRegistryClient srClient) {
     new Expectations() {
@@ -471,4 +483,226 @@ public void 
testReRegisterForDevAndLocalSchemasAreLess(@Mocked ServiceRegistryCl
     Assert.assertEquals("serviceId", microservice.getServiceId());
     Assert.assertEquals(1, taskList.size());
   }
+
+  /**
+   * There is schema in service center which is different from local schema.
+   */
+  @Test
+  public void testLocalSchemaAndServiceCenterSchemaDiff(@Mocked 
ServiceRegistryClient srClient) {
+
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", "abcd");
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    resp.setSummary("c1188d709631a9038874f9efc6eb894f");
+    list.add(resp);
+    Holder<List<GetSchemaResponse>> onlineSchemasHolder = new Holder<>();
+    onlineSchemasHolder.setValue(list).setStatusCode(200);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = onlineSchemasHolder;
+        srClient.getSchema(anyString, anyString);
+        result = "swagger: \"2.0\"\n" +
+            "info:\n" +
+            "  version: \"1.0.0\"\n" +
+            "  title: \"swagger definition for 
org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
+            "  x-java-interface: 
\"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
+            "basePath: \"/clientreqtimeout\"\n" +
+            "consumes:\n" +
+            "- \"application/json\"\n" +
+            "produces:\n" +
+            "- \"application/json\"\n" +
+            "paths:\n" +
+            "  /sayhello:\n" +
+            "    post:\n" +
+            "      operationId: \"sayHello\"\n" +
+            "      parameters:\n" +
+            "      - in: \"body\"\n" +
+            "        name: \"student\"\n" +
+            "        required: false\n" +
+            "        schema:\n" +
+            "          $ref: \"#/definitions/Student\"\n" +
+            "      responses:\n" +
+            "        200:\n" +
+            "          description: \"response of 200\"\n" +
+            "          schema:\n" +
+            "            $ref: \"#/definitions/Student\"\n" +
+            "definitions:\n" +
+            "  Student:\n" +
+            "    type: \"object\"\n" +
+            "    required:\n" +
+            "    - \"name\"\n" +
+            "    properties:\n" +
+            "      name:\n" +
+            "        type: \"string\"\n" +
+            "      age:\n" +
+            "        type: \"integer\"\n" +
+            "        format: \"int32\"\n" +
+            "        maximum: 20\n" +
+            "    x-java-class: 
\"org.apache.servicecomb.demo.validator.Student\"";
+      }
+    };
+
+    microservice.addSchema("s1",
+        "swagger: \"2.0\"\n" +
+            "info:\n" +
+            "  version: \"1.0.0\"\n" +
+            "  title: \"swagger definition for 
org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
+            "  x-java-interface: 
\"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
+            "basePath: \"/clientreqtimeout\"\n" +
+            "consumes:\n" +
+            "- \"application/json\"\n" +
+            "produces:\n" +
+            "- \"application/json\"\n" +
+            "paths:\n" +
+            "  /sayhello:\n" +
+            "    post:\n" +
+            "      operationId: \"sayHello\"\n" +
+            "      parameters:\n" +
+            "      - in: \"body\"\n" +
+            "        name: \"student\"\n" +
+            "        required: false\n" +
+            "        schema:\n" +
+            "          $ref: \"#/definitions/Student\"\n" +
+            "      responses:\n" +
+            "        200:\n" +
+            "          description: \"response of 200\"\n" +
+            "          schema:\n" +
+            "            type: \"string\"\n" +
+            "definitions:\n" +
+            "  Student:\n" +
+            "    type: \"object\"\n" +
+            "    required:\n" +
+            "    - \"name\"\n" +
+            "    properties:\n" +
+            "      name:\n" +
+            "        type: \"string\"\n" +
+            "      age:\n" +
+            "        type: \"integer\"\n" +
+            "        format: \"int32\"\n" +
+            "        maximum: 20\n" +
+            "    x-java-class: 
\"org.apache.servicecomb.demo.validator.Student\"");
+    microservice.setEnvironment("prod");
+    MicroserviceRegisterTask registerTask = new 
MicroserviceRegisterTask(eventBus, srClient, microservice);
+
+    Boolean isIlleagalException = false;
+
+    try {
+      registerTask.run();
+    } catch (IllegalStateException exception) {
+      isIlleagalException = true;
+      List<LoggingEvent> events = collector.getEvents().stream().filter(e -> {
+        return 
MicroserviceRegisterTask.class.getName().equals(e.getLoggerName());
+      }).collect(Collectors.toList());
+
+      Assert.assertEquals("service center schema and local schema both are 
different:\n" +
+          " service center schema:\n" +
+          "[swagger: \"2.0\"\n" +
+          "info:\n" +
+          "  version: \"1.0.0\"\n" +
+          "  title: \"swagger definition for 
org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
+          "  x-java-interface: 
\"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
+          "basePath: \"/clientreqtimeout\"\n" +
+          "consumes:\n" +
+          "- \"application/json\"\n" +
+          "produces:\n" +
+          "- \"application/json\"\n" +
+          "paths:\n" +
+          "  /sayhello:\n" +
+          "    post:\n" +
+          "      operationId: \"sayHello\"\n" +
+          "      parameters:\n" +
+          "      - in: \"body\"\n" +
+          "        name: \"student\"\n" +
+          "        required: false\n" +
+          "        schema:\n" +
+          "          $ref: \"#/definitions/Student\"\n" +
+          "      responses:\n" +
+          "        200:\n" +
+          "          description: \"response of 200\"\n" +
+          "          schema:\n" +
+          "            $ref: \"#/definitions/Student\"\n" +
+          "definitions:\n" +
+          "  Student:\n" +
+          "    type: \"object\"\n" +
+          "    required:\n" +
+          "    - \"name\"\n" +
+          "    properties:\n" +
+          "      name:\n" +
+          "        type: \"string\"\n" +
+          "      age:\n" +
+          "        type: \"integer\"\n" +
+          "        format: \"int32\"\n" +
+          "        maximum: 20\n" +
+          "    x-java-class: 
\"org.apache.servicecomb.demo.validator.Student\"\n" +
+          " local schema:\n" +
+          "[swagger: \"2.0\"\n" +
+          "info:\n" +
+          "  version: \"1.0.0\"\n" +
+          "  title: \"swagger definition for 
org.apache.servicecomb.demo.jaxrs.server.RequestClientTimeOut\"\n" +
+          "  x-java-interface: 
\"cse.gen.jaxrstest.jaxrs.clientreqtimeout.RequestClientTimeOutIntf\"\n" +
+          "basePath: \"/clientreqtimeout\"\n" +
+          "consumes:\n" +
+          "- \"application/json\"\n" +
+          "produces:\n" +
+          "- \"application/json\"\n" +
+          "paths:\n" +
+          "  /sayhello:\n" +
+          "    post:\n" +
+          "      operationId: \"sayHello\"\n" +
+          "      parameters:\n" +
+          "      - in: \"body\"\n" +
+          "        name: \"student\"\n" +
+          "        required: false\n" +
+          "        schema:\n" +
+          "          $ref: \"#/definitions/Student\"\n" +
+          "      responses:\n" +
+          "        200:\n" +
+          "          description: \"response of 200\"\n" +
+          "          schema:\n" +
+          "            type: \"string\"\n" +
+          "definitions:\n" +
+          "  Student:\n" +
+          "    type: \"object\"\n" +
+          "    required:\n" +
+          "    - \"name\"\n" +
+          "    properties:\n" +
+          "      name:\n" +
+          "        type: \"string\"\n" +
+          "      age:\n" +
+          "        type: \"integer\"\n" +
+          "        format: \"int32\"\n" +
+          "        maximum: 20\n" +
+          "    x-java-class: 
\"org.apache.servicecomb.demo.validator.Student\"]", 
events.get(4).getMessage());
+
+      Assert.assertEquals("The difference in local schema:\n" +
+          "[type: \"string\"\n" +
+          "definitions:\n" +
+          "  Student:\n" +
+          "    type: \"object\"\n" +
+          "    required:\n" +
+          "    - \"name\"\n" +
+          "    properties:\n" +
+          "      name:\n" +
+          "        type: \"string\"\n" +
+          "      age:\n" +
+          "        type: \"integer\"\n" +
+          "        format: \"int32\"\n" +
+          "        maximum: 20\n" +
+          "    x-java-class: 
\"org.apache.servicecomb.demo.validator.Student\"]", 
events.get(5).getMessage());
+
+    }
+
+    Assert.assertEquals(true, isIlleagalException);
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to