weichao666 closed pull request #450: [JAV-429] modify the startup policy of SDK
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/450
 
 
   

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/src/main/java/io/servicecomb/serviceregistry/api/Const.java 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/Const.java
index 9c4d590f1..25cbbdbc9 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/Const.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/Const.java
@@ -150,4 +150,8 @@ private Const() {
   public static final String URL_PREFIX = "urlPrefix";
   
   public static final String INSTANCE_PUBKEY_PRO = "publickey";
+
+  public static final String SERVICECENTER_ENVIRONMENT = "/version";
+
+  public static final String SERVICECENTER_RUNMODE_DEV = "dev";
 }
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/registry/ServiceCenterEnvironment.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/registry/ServiceCenterEnvironment.java
new file mode 100644
index 000000000..4580bc789
--- /dev/null
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/registry/ServiceCenterEnvironment.java
@@ -0,0 +1,64 @@
+/*
+ * 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 io.servicecomb.serviceregistry.api.registry;
+
+/**
+ * Created by on 2017/12/19.
+ */
+public class ServiceCenterEnvironment {
+  private String version;
+
+  private String buildTag;
+
+  private String runMode;
+
+  private String apiVersion;
+
+  public String getVersion() {
+    return version;
+  }
+
+  public void setVersion(String version) {
+    this.version = version;
+  }
+
+  public String getBuildTag() {
+    return buildTag;
+  }
+
+  public void setBuildTag(String buildTag) {
+    this.buildTag = buildTag;
+  }
+
+  public String getRunMode() {
+    return runMode;
+  }
+
+  public void setRunMode(String runMode) {
+    this.runMode = runMode;
+  }
+
+  public String getApiVersion() {
+    return apiVersion;
+  }
+
+  public void setApiVersion(String apiVersion) {
+    this.apiVersion = apiVersion;
+  }
+
+}
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
index b5695d620..8cb164e8a 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
@@ -17,9 +17,14 @@
 
 package io.servicecomb.serviceregistry.api.request;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 public class CreateSchemaRequest {
   private String schema;
 
+  @JsonProperty(value = "summary")
+  private String signature;
+
   public String getSchema() {
     return schema;
   }
@@ -27,4 +32,13 @@ public String getSchema() {
   public void setSchema(String schema) {
     this.schema = schema;
   }
+
+  public String getSignature() {
+    return signature;
+  }
+
+  public void setSignature(String signature) {
+    this.signature = signature;
+  }
+
 }
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
index 0069cb57d..edd7b2994 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
@@ -20,6 +20,8 @@
 public class GetSchemaResponse {
   private String schema;
 
+  private String schemaSummary;
+
   public String getSchema() {
     return schema;
   }
@@ -27,4 +29,13 @@ public String getSchema() {
   public void setSchema(String schema) {
     this.schema = schema;
   }
+
+  public String getSchemaSummary() {
+    return schemaSummary;
+  }
+
+  public void setSchemaSummary(String schemaSummary) {
+    this.schemaSummary = schemaSummary;
+  }
+
 }
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
index 0691af14d..5cdcec146 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
@@ -23,6 +23,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -35,9 +36,13 @@
 import org.springframework.util.StringUtils;
 import org.yaml.snakeyaml.Yaml;
 
+import com.google.common.hash.Hashing;
+
 import io.servicecomb.foundation.vertx.AsyncResultCallback;
+import io.servicecomb.serviceregistry.api.Const;
 import io.servicecomb.serviceregistry.api.registry.Microservice;
 import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.serviceregistry.api.registry.ServiceCenterEnvironment;
 import io.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import 
io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
 import io.servicecomb.serviceregistry.version.Version;
@@ -59,6 +64,8 @@
   // second key is instance id
   private Map<String, Map<String, MicroserviceInstance>> 
microserviceInstanceMap = new ConcurrentHashMap<>();
 
+  private ServiceCenterEnvironment serviceCenterEnvironment = new 
ServiceCenterEnvironment();
+
   public LocalServiceRegistryClientImpl() {
     if (StringUtils.isEmpty(LOCAL_REGISTRY_FILE)) {
       LOGGER.info("create empty local registry.");
@@ -123,6 +130,10 @@ private void initFromData(Map<String, Object> data) {
         microserviceInstanceMap.put(microservice.getServiceId(), instanceMap);
       }
     }
+    serviceCenterEnvironment.setVersion("0.4.1");
+    
serviceCenterEnvironment.setBuildTag("20171201150624.1055.ad31520ef590c7183932664d05f3ebffd25c3714");
+    serviceCenterEnvironment.setRunMode(Const.SERVICECENTER_RUNMODE_DEV);
+    serviceCenterEnvironment.setApiVersion("3.0.0");
   }
 
   @Override
@@ -338,6 +349,20 @@ public MicroserviceInstance findServiceInstance(String 
serviceId, String instanc
     Map<String, MicroserviceInstance> instances = 
microserviceInstanceMap.get(serviceId);
     return instances.get(instanceId);
   }
-  
-  
+
+  @Override
+  public ServiceCenterEnvironment getServiceCenterEnvironment() {
+    return serviceCenterEnvironment;
+  }
+
+  @Override
+  public String getSchemaSummary(String microserviceId, String schemaId) {
+    Microservice microservice = microserviceIdMap.get(microserviceId);
+    if (microservice == null) {
+      throw new IllegalArgumentException("Invalid serviceId, serviceId=" + 
microserviceId);
+    }
+    String schemaSummary = 
Hashing.sha256().newHasher().putString(microservice.getSchemaMap().get(schemaId),
 StandardCharsets.UTF_8).hash().toString();
+    return schemaSummary;
+  }
+
 }
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/ServiceRegistryClient.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/ServiceRegistryClient.java
index 1c9b750b8..480a3dda4 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/ServiceRegistryClient.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/ServiceRegistryClient.java
@@ -23,12 +23,19 @@
 import io.servicecomb.foundation.vertx.AsyncResultCallback;
 import io.servicecomb.serviceregistry.api.registry.Microservice;
 import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.serviceregistry.api.registry.ServiceCenterEnvironment;
 import io.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import 
io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
 
 public interface ServiceRegistryClient {
   void init();
 
+  /**
+  *
+  * get servicecenter Info
+  */
+  ServiceCenterEnvironment getServiceCenterEnvironment();
+
   /**
    * get all microservices
    */
@@ -75,6 +82,12 @@
    */
   String getSchema(String microserviceId, String schemaId);
 
+  /**
+  *
+  * get schema summary
+  */
+  String getSchemaSummary(String microserviceId, String schemaId);
+
   /**
    *
    * ???????
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index 53ffe5c10..64f8ae167 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -20,6 +20,7 @@
 import static java.util.Collections.emptyList;
 
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -32,12 +33,15 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.hash.Hashing;
+
 import io.servicecomb.foundation.common.net.IpPort;
 import io.servicecomb.foundation.common.utils.JsonUtils;
 import io.servicecomb.foundation.vertx.AsyncResultCallback;
 import io.servicecomb.serviceregistry.api.Const;
 import io.servicecomb.serviceregistry.api.registry.Microservice;
 import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.serviceregistry.api.registry.ServiceCenterEnvironment;
 import io.servicecomb.serviceregistry.api.request.CreateSchemaRequest;
 import io.servicecomb.serviceregistry.api.request.CreateServiceRequest;
 import io.servicecomb.serviceregistry.api.request.RegisterInstanceRequest;
@@ -151,6 +155,28 @@ private void retry(RequestContext requestContext, 
Handler<RestResponse> response
     };
   }
 
+  @Override
+  public ServiceCenterEnvironment getServiceCenterEnvironment() {
+    Holder<ServiceCenterEnvironment> holder = new Holder<>();
+    IpPort ipPort = ipPortManager.getAvailableAddress(false);
+    
+    CountDownLatch countDownLatch = new CountDownLatch(1);
+    
+    RestUtils.get(ipPort,
+        Const.SERVICECENTER_ENVIRONMENT,
+        new RequestParam(),
+        syncHandler(countDownLatch, ServiceCenterEnvironment.class, holder));
+    try {
+      countDownLatch.await();
+      if (holder.value != null) {
+        return holder.value;
+      }
+    } catch (Exception e) {
+      LOGGER.error("query servicecenterEnvironment failed", e);
+    }
+    return null;
+  }
+
   @Override
   public List<Microservice> getAllMicroservices() {
     Holder<GetAllServicesResponse> holder = new Holder<>();
@@ -231,6 +257,8 @@ public boolean registerSchema(String microserviceId, String 
schemaId, String sch
     try {
       CreateSchemaRequest request = new CreateSchemaRequest();
       request.setSchema(schemaContent);
+      String summary = Hashing.sha256().newHasher().putString(schemaContent, 
StandardCharsets.UTF_8).hash().toString();
+      request.setSignature(summary);
       byte[] body = JsonUtils.writeValueAsBytes(request);
 
       CountDownLatch countDownLatch = new CountDownLatch(1);
@@ -293,6 +321,53 @@ public String getSchema(String microserviceId, String 
schemaId) {
     return null;
   }
 
+  @Override
+  public String getSchemaSummary(String microserviceId, String schemaId) {
+    IpPort ipPort = ipPortManager.getAvailableAddress(false);
+    GetSchemaResponse getSchemaResponse = new GetSchemaResponse();
+    
+    CountDownLatch countDownLatch = new CountDownLatch(1);
+    RestUtils.get(ipPort,
+        Const.REGISTRY_API.MICROSERVICE_EXISTENCE,
+        new RequestParam().addQueryParam("type", "schema")
+            .addQueryParam("serviceId", microserviceId)
+            .addQueryParam("schemaId", schemaId),
+        new Handler<RestResponse>() {
+            @Override
+            public void handle(RestResponse restResponse) {
+              RequestContext requestContext = restResponse.getRequestContext();
+              HttpClientResponse response = restResponse.getResponse();
+              if (response == null) {
+                //Request failure, to trigger request other instances of the SC
+                if (!requestContext.isRetry()) {
+                  retry(requestContext, this);
+                } else {
+                  countDownLatch.countDown();
+                }
+                return;
+              }
+              response.bodyHandler(
+                bodyBuffer -> {
+                  try {
+                    
getSchemaResponse.setSchemaSummary(response.getHeader("X-Schema-Summary"));
+                  } catch (Exception e) {
+                    LOGGER.warn(bodyBuffer.toString(), e);
+                  }
+                  countDownLatch.countDown();
+                });
+            }
+        });
+    try {
+      countDownLatch.await();
+    } catch (Exception e) {
+      LOGGER.error("query schemasummary exist {}/{} failed",
+          microserviceId,
+          schemaId,
+          e);
+    }
+    return getSchemaResponse.getSchemaSummary();
+  }
+
   @Override
   public String registerMicroservice(Microservice microservice) {
     Holder<CreateServiceResponse> holder = new Holder<>();
diff --git 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 20d195695..18e7ced69 100644
--- 
a/service-registry/src/main/java/io/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ 
b/service-registry/src/main/java/io/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -16,6 +16,7 @@
  */
 package io.servicecomb.serviceregistry.task;
 
+import java.nio.charset.StandardCharsets;
 import java.util.HashSet;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -26,7 +27,9 @@
 
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
+import com.google.common.hash.Hashing;
 
+import io.servicecomb.serviceregistry.api.Const;
 import io.servicecomb.serviceregistry.api.registry.Microservice;
 import io.servicecomb.serviceregistry.client.ServiceRegistryClient;
 
@@ -35,6 +38,8 @@
 
   private boolean schemaIdSetMatch;
 
+  private boolean scEnvIsDev = false;
+
   public MicroserviceRegisterTask(EventBus eventBus, ServiceRegistryClient 
srClient, Microservice microservice) {
     super(eventBus, srClient, microservice);
     this.taskStatus = TaskStatus.READY;
@@ -122,6 +127,9 @@ private boolean checkSchemaIdSet() {
           microservice.getVersion(),
           localSchemas,
           existSchemas);
+      if (!serviceCenterEnvIsDev()) {
+        return false;
+      }
       return true;
     }
 
@@ -146,9 +154,43 @@ private boolean registerSchemas() {
         if (!srClient.registerSchema(microservice.getServiceId(), schemaId, 
content)) {
           return false;
         }
+      } else {
+        if (!checkSchemaSummary(schemaId, content)) {
+          return false;
+        }
       }
     }
 
     return true;
   }
+
+  private boolean checkSchemaSummary(String schemaId, String content) {
+    //query if schemaSummary exist, if not exist, register schema&summary in SC
+    String serviceId = microservice.getServiceId();
+    String schemaSummary = srClient.getSchemaSummary(serviceId, schemaId);
+    if (null == schemaSummary) {
+      return srClient.registerSchema(serviceId, schemaId, content);
+    } else {
+      //compare to localschema, if don't match,when SC runMode is dev,register 
schema&summary in SC
+      String localSummary = Hashing.sha256().newHasher().putString(content, 
StandardCharsets.UTF_8).hash().toString();
+      if (!localSummary.equals(schemaSummary)) {
+        if (scEnvIsDev || serviceCenterEnvIsDev()) {
+          return srClient.registerSchema(serviceId, schemaId, content);
+        } else {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
+
+  private boolean serviceCenterEnvIsDev() {
+    if 
(Const.SERVICECENTER_RUNMODE_DEV.equals(srClient.getServiceCenterEnvironment().getRunMode()))
 {
+      LOGGER.warn("The current servicecenter environment runmode is {}", 
Const.SERVICECENTER_RUNMODE_DEV);
+      scEnvIsDev = true;
+      return true;
+    }
+    LOGGER.error("The current servicecenter environment runmode isn't {}, 
schema info can't be modified.", Const.SERVICECENTER_RUNMODE_DEV);
+    return false;
+  }
 }
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/api/registry/TestServiceCenterEnvironment.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/registry/TestServiceCenterEnvironment.java
new file mode 100644
index 000000000..517830c89
--- /dev/null
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/registry/TestServiceCenterEnvironment.java
@@ -0,0 +1,62 @@
+/*
+ * 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 io.servicecomb.serviceregistry.api.registry;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestServiceCenterEnvironment {
+
+  ServiceCenterEnvironment serviceCenterEnvironment = null;
+
+  @Before
+  public void setUp() throws Exception {
+    serviceCenterEnvironment = new ServiceCenterEnvironment();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    serviceCenterEnvironment = null;
+  }
+
+  @Test
+  public void testDefaultValues() {
+    Assert.assertNull(serviceCenterEnvironment.getVersion());
+    Assert.assertNull(serviceCenterEnvironment.getBuildTag());
+    Assert.assertNull(serviceCenterEnvironment.getRunMode());
+    Assert.assertNull(serviceCenterEnvironment.getApiVersion());
+  }
+
+  @Test
+  public void testInitializedValues() {
+    initServiceCenterEnvironment();
+    Assert.assertEquals("0.4.1", serviceCenterEnvironment.getVersion());
+    Assert.assertEquals("2017", serviceCenterEnvironment.getBuildTag());
+    Assert.assertEquals("dev", serviceCenterEnvironment.getRunMode());
+    Assert.assertEquals("3.0.0", serviceCenterEnvironment.getApiVersion());
+  }
+
+  private void initServiceCenterEnvironment() {
+    serviceCenterEnvironment.setVersion("0.4.1");
+    serviceCenterEnvironment.setBuildTag("2017");
+    serviceCenterEnvironment.setRunMode("dev");
+    serviceCenterEnvironment.setApiVersion("3.0.0");
+  }
+}
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/api/request/TestCreateSchemaRequest.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/request/TestCreateSchemaRequest.java
index 0df2c5e47..47e6ee6c1 100644
--- 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/api/request/TestCreateSchemaRequest.java
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/request/TestCreateSchemaRequest.java
@@ -16,17 +16,39 @@
  */
 package io.servicecomb.serviceregistry.api.request;
 
+import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class TestCreateSchemaRequest {
+  CreateSchemaRequest createSchemaRequest = null;
+
+  @Before
+  public void setUp() throws Exception {
+    createSchemaRequest = new CreateSchemaRequest();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    createSchemaRequest = null;
+  }
+
   @Test
-  public void testProperties() {
-    CreateSchemaRequest request = new CreateSchemaRequest();
+  public void testDefaultValues() {
+    Assert.assertNull(createSchemaRequest.getSchema());
+    Assert.assertNull(createSchemaRequest.getSignature());
+  }
 
-    Assert.assertEquals(null, request.getSchema());
+  @Test
+  public void testInitializedValues() {
+    initCreateSchemaRequest();
+    Assert.assertEquals("schema", createSchemaRequest.getSchema());
+    Assert.assertEquals("summary", createSchemaRequest.getSignature());
+  }
 
-    request.setSchema("schema");
-    Assert.assertEquals("schema", request.getSchema());
+  private void initCreateSchemaRequest() {
+    createSchemaRequest.setSchema("schema");
+    createSchemaRequest.setSignature("summary");
   }
 }
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/api/response/TestGetSchemaResponse.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/response/TestGetSchemaResponse.java
new file mode 100644
index 000000000..9464f8db7
--- /dev/null
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/api/response/TestGetSchemaResponse.java
@@ -0,0 +1,62 @@
+/*
+ * 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 io.servicecomb.serviceregistry.api.response;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class TestGetSchemaResponse {
+
+  GetSchemaResponse getSchemaResponse = null;
+
+  @Before
+  public void setUp() throws Exception {
+    getSchemaResponse = new GetSchemaResponse();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    getSchemaResponse = null;
+  }
+
+  @Test
+  public void testDefaultValues() {
+    Assert.assertNull(getSchemaResponse.getSchema());
+    Assert.assertNull(getSchemaResponse.getSchemaSummary());
+  }
+
+  @Test
+  public void testInitializedValues() {
+    initGetSchemaResponse(); //Initialize the Object
+    Assert.assertEquals("schema", getSchemaResponse.getSchema());
+    Assert.assertEquals("schemaSummary", getSchemaResponse.getSchemaSummary());
+  }
+
+  private void initGetSchemaResponse() {
+    getSchemaResponse.setSchema("schema");
+    getSchemaResponse.setSchemaSummary("schemaSummary");
+  }
+}
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
index cde6179d0..6256a0287 100644
--- 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
@@ -18,6 +18,7 @@
 package io.servicecomb.serviceregistry.client;
 
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 
 import org.hamcrest.Matchers;
@@ -28,8 +29,11 @@
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import com.google.common.hash.Hashing;
+
 import io.servicecomb.serviceregistry.api.registry.Microservice;
 import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.serviceregistry.api.registry.ServiceCenterEnvironment;
 import io.servicecomb.serviceregistry.definition.DefinitionConst;
 
 public class LocalServiceRegistryClientImplTest {
@@ -156,4 +160,21 @@ public void testFindServiceInstance()
     String instanceId = registryClient.registerMicroserviceInstance(instance);
     
Assert.assertNotNull(registryClient.findServiceInstance(microservice.getServiceId(),
 instanceId));
   }
+
+  @Test
+  public void testServiceCenterEnvironment() {
+    ServiceCenterEnvironment serviceCenterEnvironment = 
registryClient.getServiceCenterEnvironment();
+    Assert.assertEquals("0.4.1", serviceCenterEnvironment.getVersion());
+    
Assert.assertEquals("20171201150624.1055.ad31520ef590c7183932664d05f3ebffd25c3714",
 serviceCenterEnvironment.getBuildTag());
+    Assert.assertEquals("dev", serviceCenterEnvironment.getRunMode());
+    Assert.assertEquals("3.0.0", serviceCenterEnvironment.getApiVersion());
+  }
+
+  @Test
+  public void testGetSchemaSummary() {
+    Microservice microservice = mockRegisterMicroservice(appId, 
microserviceName, "1.0.0");
+    registryClient.registerSchema(microservice.getServiceId(), "sid", 
"content");
+    String schemaSummary = Hashing.sha256().newHasher().putString("content", 
StandardCharsets.UTF_8).hash().toString();
+    Assert.assertEquals(schemaSummary, 
registryClient.getSchemaSummary(microservice.getServiceId(), "sid"));
+  }
 }
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
index 364cc031c..5898ffb7f 100644
--- 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
@@ -256,4 +256,82 @@ public void testFindServiceInstance()
   {
     Assert.assertNull(oClient.findServiceInstance("aaa","bbb"));
   }
+
+  @Test
+  public void testGetServiceCenterEnvironmentSuccess() {
+    new MockUp<ServiceRegistryClientImpl>() {
+        @Mock
+        <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, 
Class<T> cls, Holder<T> holder) {
+          return restResponse -> {
+            HttpClientResponse response = 
Mockito.mock(HttpClientResponse.class);
+            Mockito.when(response.statusCode()).thenReturn(400);
+
+            try {
+              holder.value = cls.newInstance();
+            } catch (Exception e) {
+              e.printStackTrace();
+            }
+          };
+        }
+      };
+    new MockUp<RestUtils>() {
+      @Mock
+      void httpDo(RequestContext requestContext, Handler<RestResponse> 
responseHandler) {
+        responseHandler.handle(null);
+      }
+    };
+    Assert.assertNotNull(oClient.getServiceCenterEnvironment());
+  }
+
+  @Test
+  public void testGetServiceCenterEnvironmentException() {
+    InterruptedException e = new InterruptedException();
+    new MockUp<CountDownLatch>() {
+      @Mock
+      public void await() throws InterruptedException {
+        throw e;
+      }
+    };
+
+    new RegisterSchemaTester() {
+      void doRun(java.util.List<LoggingEvent> events) {
+        oClient.getServiceCenterEnvironment();
+        Assert.assertEquals(
+            "query servicecenterEnvironment failed",
+            events.get(0).getMessage());
+        Assert.assertEquals(e, 
events.get(0).getThrowableInformation().getThrowable());
+      }
+    }.run();
+  }
+
+  @Test
+  public void testGetServiceCenterEnvironmentNoResponse() {
+    Assert.assertNull(oClient.getServiceCenterEnvironment());
+  }
+
+  @Test
+  public void testGetSchemaSummaryNoResponse() {
+    Assert.assertNull(oClient.getSchemaSummary("microserviceId", "schemaId"));
+  }
+
+  @Test
+  public void testGetSchemaSummaryException() {
+    InterruptedException e = new InterruptedException();
+    new MockUp<CountDownLatch>() {
+      @Mock
+      public void await() throws InterruptedException {
+        throw e;
+      }
+    };
+
+    new RegisterSchemaTester() {
+      void doRun(java.util.List<LoggingEvent> events) {
+        oClient.getSchemaSummary("microserviceId", "schemaId");
+        Assert.assertEquals(
+            "query schemasummary exist microserviceId/schemaId failed",
+            events.get(0).getMessage());
+        Assert.assertEquals(e, 
events.get(0).getThrowableInformation().getThrowable());
+      }
+    }.run();
+  }
 }
diff --git 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index aacc5b490..9b3480699 100644
--- 
a/service-registry/src/test/java/io/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++ 
b/service-registry/src/test/java/io/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -112,8 +112,6 @@ public void testRegisterSchemaFailed(@Mocked 
ServiceRegistryClient srClient) {
         result = null;
         srClient.registerMicroservice((Microservice) any);
         result = "serviceId";
-        srClient.isSchemaExist("serviceId", "exist");
-        result = true;
         srClient.isSchemaExist(anyString, anyString);
         result = false;
         srClient.registerSchema(anyString, anyString, anyString);
@@ -192,6 +190,8 @@ public void 
testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClie
         result = "serviceId";
         srClient.getMicroservice(anyString);
         result = otherMicroservice;
+        srClient.getServiceCenterEnvironment().getRunMode();
+        result = "dev";
       }
     };
 
@@ -229,4 +229,62 @@ public void 
testAlreadyRegisteredGetSchemaIdSetFailed(@Mocked ServiceRegistryCli
     Assert.assertEquals("serviceId", microservice.getServiceId());
     Assert.assertEquals(1, taskList.size());
   }
+
+  @Test
+  public void testSchemaSummaryNotExist(@Mocked ServiceRegistryClient 
srClient) {
+    microservice.addSchema("exist", "");
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString);
+        result = null;
+        srClient.registerMicroservice((Microservice) any);
+        result = "serviceId";
+        srClient.isSchemaExist(anyString, anyString);
+        result = true;
+        srClient.getSchemaSummary(anyString, anyString);
+        result = null;
+        srClient.registerSchema(anyString, anyString, anyString);
+        result = true;
+      }
+    };
+
+    MicroserviceRegisterTask registerTask = new 
MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+
+    Assert.assertEquals(true, registerTask.isRegistered());
+    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals("serviceId", microservice.getServiceId());
+    Assert.assertEquals("serviceId", 
microservice.getInstance().getServiceId());
+    Assert.assertEquals(1, taskList.size());
+  }
+
+  @Test
+  public void testSchemaSummaryNotMatch(@Mocked ServiceRegistryClient 
srClient) {
+    microservice.addSchema("exist", "");
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString);
+        result = null;
+        srClient.registerMicroservice((Microservice) any);
+        result = "serviceId";
+        srClient.isSchemaExist(anyString, anyString);
+        result = true;
+        srClient.getSchemaSummary(anyString, anyString);
+        result = "schemaSummary";
+        srClient.getServiceCenterEnvironment().getRunMode();
+        result = "dev";
+        srClient.registerSchema(anyString, anyString, anyString);
+        result = true;
+      }
+    };
+
+    MicroserviceRegisterTask registerTask = new 
MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+
+    Assert.assertEquals(true, registerTask.isRegistered());
+    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals("serviceId", microservice.getServiceId());
+    Assert.assertEquals("serviceId", 
microservice.getInstance().getServiceId());
+    Assert.assertEquals(1, taskList.size());
+  }
 }


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to