From 6ca3eabfcecc58a5530efc625b17ef8b0bf89359 Mon Sep 17 00:00:00 2001 From: yaohaishi Date: Fri, 1 Jun 2018 16:50:29 +0800 Subject: [PATCH 1/2] [SCB-652] change environment configuration key to service_description.environment and mark @Deprecated onto microserviceInstance.environment --- .../common/base/ServiceCombConstants.java | 4 ++++ .../api/registry/MicroserviceInstance.java | 4 ++++ .../task/MicroserviceRegisterTask.java | 15 +++++++++------ .../task/TestMicroserviceRegisterTask.java | 6 +++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java index 22e4709cc3c..21616781407 100644 --- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java +++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/base/ServiceCombConstants.java @@ -64,4 +64,8 @@ public interface ServiceCombConstants { String SERVICECOMB_ENV = "SERVICECOMB_ENV"; String DEFAULT_SERVICECOMB_ENV = ""; + + String DEVELOPMENT_SERVICECOMB_ENV = "development"; + + String PRODUCTION_SERVICECOMB_ENV = "production"; } diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java index 184aac3ca24..4ce3fb2ffac 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/MicroserviceInstance.java @@ -55,6 +55,10 @@ public class MicroserviceInstance { private HealthCheck healthCheck; + /** + * Will be abandoned, use {@link Microservice#environment} instead + */ + @Deprecated private String environment; private String stage; 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 caa6141f109..497c014d1ad 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 @@ -21,6 +21,7 @@ import java.util.Map.Entry; import java.util.Set; +import org.apache.servicecomb.foundation.common.base.ServiceCombConstants; import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse; import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; @@ -161,13 +162,13 @@ private boolean registerSchemas() { return false; } } else { - String curSchemaSumary = existSchema.getSummary(); + String curSchemaSummary = existSchema.getSummary(); String schemaSummary = Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString(); - if (!schemaSummary.equals(curSchemaSumary)) { - if (microservice.getInstance().getEnvironment().equalsIgnoreCase("development")) { + if (!schemaSummary.equals(curSchemaSummary)) { + if (microservice.getEnvironment().equalsIgnoreCase(ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV)) { LOGGER.info( - "schemaId [{}]'s content changes and the current enviroment is development, so re-register it!", - schemaId); + "schemaId [{}]'s content changes and the current environment is {}, so re-register it!", + schemaId, ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV); if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) { return false; } @@ -175,7 +176,9 @@ private boolean registerSchemas() { throw new IllegalStateException("schemaId [" + schemaId + "] exists in service center, but the content does not match the local content that means there are interface change " + "and you need to increment microservice version before deploying. " - + "Or you can configure instance_description.environment=development to work in development enviroment and ignore this error"); + + "Or you can configure service_description.environment=" + + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV + + " to work in development environment and ignore this error"); } } } 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 7932d689390..e85af2e58b5 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 @@ -229,7 +229,7 @@ public void testAlreadyRegisteredGetSchemaIdSetFailed(@Mocked ServiceRegistryCli @Test public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) { ArchaiusUtils.resetConfig(); - ArchaiusUtils.setProperty("instance_description.environment", "development"); + ArchaiusUtils.setProperty("service_description.environment", "development"); Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); @@ -255,7 +255,7 @@ public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) { }; microservice.addSchema("s1", ""); - microservice.getInstance().setEnvironment("development"); + microservice.setEnvironment("development"); MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); registerTask.run(); @@ -327,7 +327,7 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { }; microservice.addSchema("s1", ""); - microservice.getInstance().setEnvironment("prod"); + microservice.setEnvironment("prod"); MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); registerTask.run(); } From e12bb34416848664edfe24fba7fa6268a938fa70 Mon Sep 17 00:00:00 2001 From: yaohaishi Date: Thu, 7 Jun 2018 01:08:09 +0800 Subject: [PATCH 2/2] [SCB-652] refactor schema register logic --- .../serviceregistry/RegistryUtils.java | 6 + .../LocalServiceRegistryClientImpl.java | 9 +- .../client/ServiceRegistryClient.java | 3 +- .../serviceregistry/client/http/Holder.java | 67 +++++ .../http/ServiceRegistryClientImpl.java | 24 +- .../task/MicroserviceRegisterTask.java | 247 ++++++++++++++---- .../LocalServiceRegistryClientImplTest.java | 10 + .../http/TestServiceRegistryClientImpl.java | 38 ++- .../task/TestMicroserviceRegisterTask.java | 180 +++++++++++-- 9 files changed, 491 insertions(+), 93 deletions(-) create mode 100644 service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java index 321ff24a570..20306787adb 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java @@ -40,6 +40,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Charsets; +import com.google.common.hash.Hashing; import com.netflix.config.DynamicPropertyFactory; public final class RegistryUtils { @@ -221,4 +223,8 @@ public static MicroserviceInstances findServiceInstances(String appId, String se String versionRule, String revision) { return serviceRegistry.findServiceInstances(appId, serviceName, versionRule, revision); } + + public static String calcSchemaSummary(String schemaContent) { + return Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString(); + } } diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java index 5e78abcd803..84c15169d4c 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java @@ -31,6 +31,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import javax.ws.rs.core.Response.Status; + import org.apache.servicecomb.foundation.vertx.AsyncResultCallback; import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; @@ -40,6 +42,7 @@ import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse; import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse; import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent; +import org.apache.servicecomb.serviceregistry.client.http.Holder; import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; import org.apache.servicecomb.serviceregistry.version.Version; import org.apache.servicecomb.serviceregistry.version.VersionRule; @@ -342,7 +345,7 @@ public String getSchema(String microserviceId, String schemaId) { } @Override - public List getSchemas(String microserviceId) { + public Holder> getSchemas(String microserviceId) { Microservice microservice = microserviceIdMap.get(microserviceId); if (microservice == null) { throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId); @@ -355,7 +358,9 @@ public List getSchemas(String microserviceId) { schema.setSummary(Hashing.sha256().newHasher().putString(val, Charsets.UTF_8).hash().toString()); schemas.add(schema); }); - return schemas; + Holder> resultHolder = new Holder<>(); + resultHolder.setStatusCode(Status.OK.getStatusCode()).setValue(schemas); + return resultHolder; } @Override diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java index 9b2e1001e2e..4af6d70877d 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java @@ -27,6 +27,7 @@ import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse; import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse; import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent; +import org.apache.servicecomb.serviceregistry.client.http.Holder; import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; public interface ServiceRegistryClient { @@ -82,7 +83,7 @@ public interface ServiceRegistryClient { * * 批量获取schemas内容 */ - List getSchemas(String microserviceId); + Holder> getSchemas(String microserviceId); /** * diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.java new file mode 100644 index 00000000000..e63911a9336 --- /dev/null +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/Holder.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.serviceregistry.client.http; + +/** + * To carry the rest response information. + * @param Type of response body + */ +public class Holder { + T value; + + int statusCode; + + Throwable throwable; + + public T getValue() { + return value; + } + + public Holder setValue(T value) { + this.value = value; + return this; + } + + public int getStatusCode() { + return statusCode; + } + + public Holder setStatusCode(int statusCode) { + this.statusCode = statusCode; + return this; + } + + public Throwable getThrowable() { + return throwable; + } + + public Holder setThrowable(Throwable throwable) { + this.throwable = throwable; + return this; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Holder{"); + sb.append("value=").append(value); + sb.append(", statusCode=").append(statusCode); + sb.append(", throwable=").append(throwable); + sb.append('}'); + return sb.toString(); + } +} diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java index f379d931849..30f4200721f 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java @@ -27,11 +27,11 @@ import java.util.concurrent.CountDownLatch; import javax.ws.rs.core.Response.Status; -import javax.xml.ws.Holder; import org.apache.servicecomb.foundation.common.net.IpPort; import org.apache.servicecomb.foundation.common.utils.JsonUtils; import org.apache.servicecomb.foundation.vertx.AsyncResultCallback; +import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.api.Const; import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; @@ -60,8 +60,6 @@ import org.slf4j.LoggerFactory; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import com.google.common.hash.Hashing; import io.netty.handler.codec.http.HttpStatusClass; import io.vertx.core.Handler; @@ -108,6 +106,7 @@ protected Handler syncHandler(CountDownLatch countDownLatch, C } return; } + holder.setStatusCode(response.statusCode()); response.bodyHandler( bodyBuffer -> { if (cls.getName().equals(HttpClientResponse.class.getName())) { @@ -115,6 +114,11 @@ protected Handler syncHandler(CountDownLatch countDownLatch, C countDownLatch.countDown(); return; } + if (cls.equals(String.class)) { + holder.setValue((T) bodyBuffer.toString()); + countDownLatch.countDown(); + return; + } // no need to support 304 in this place if (!HttpStatusClass.SUCCESS.equals(HttpStatusClass.valueOf(response.statusCode()))) { @@ -131,6 +135,7 @@ protected Handler syncHandler(CountDownLatch countDownLatch, C holder.value = JsonUtils.readValue(bodyBuffer.getBytes(), cls); } catch (Exception e) { + holder.setStatusCode(0).setThrowable(e); LOGGER.warn("read value failed and response message is {}", bodyBuffer.toString()); } @@ -291,7 +296,7 @@ public boolean registerSchema(String microserviceId, String schemaId, String sch try { CreateSchemaRequest request = new CreateSchemaRequest(); request.setSchema(schemaContent); - request.setSummary(Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString()); + request.setSummary(RegistryUtils.calcSchemaSummary(schemaContent)); byte[] body = JsonUtils.writeValueAsBytes(request); CountDownLatch countDownLatch = new CountDownLatch(1); @@ -355,9 +360,10 @@ public String getSchema(String microserviceId, String schemaId) { } @Override - public List getSchemas(String microserviceId) { + public Holder> getSchemas(String microserviceId) { Holder holder = new Holder<>(); IpPort ipPort = ipPortManager.getAvailableAddress(); + Holder> resultHolder = new Holder<>(); CountDownLatch countDownLatch = new CountDownLatch(1); // default not return schema content, just return summary! @@ -372,11 +378,15 @@ public List getSchemas(String microserviceId) { microserviceId, e); } + resultHolder.setStatusCode(holder.getStatusCode()).setThrowable(holder.getThrowable()); if (holder.value != null) { - return holder.value.getSchema() != null ? holder.value.getSchema() : holder.value.getSchemas(); + return resultHolder.setValue( + holder.value.getSchema() != null ? + holder.value.getSchema() : + holder.value.getSchemas()); } - return null; + return resultHolder; } @Override 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 497c014d1ad..9e7be8ae72e 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 @@ -16,23 +16,27 @@ */ package org.apache.servicecomb.serviceregistry.task; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import javax.ws.rs.core.Response.Status; + import org.apache.servicecomb.foundation.common.base.ServiceCombConstants; +import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.api.registry.Microservice; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; -import com.google.common.base.Charsets; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; -import com.google.common.hash.Hashing; public class MicroserviceRegisterTask extends AbstractRegisterTask { private static final Logger LOGGER = LoggerFactory.getLogger(MicroserviceRegisterTask.class); @@ -74,14 +78,15 @@ protected boolean doRegister() { microservice.getVersion(), microservice.getEnvironment()); if (!StringUtils.isEmpty(serviceId)) { - // 已经注册过了,不需要重新注册 + // This microservice has been registered, so we just use the serviceId gotten from service center microservice.setServiceId(serviceId); LOGGER.info( - "Microservice exists in service center, no need to register. id={} appId={}, name={}, version={}", + "Microservice exists in service center, no need to register. id=[{}] appId=[{}], name=[{}], version=[{}], env=[{}]", serviceId, microservice.getAppId(), microservice.getServiceName(), - microservice.getVersion()); + microservice.getVersion(), + microservice.getEnvironment()); if (!checkSchemaIdSet()) { return false; @@ -90,24 +95,25 @@ protected boolean doRegister() { serviceId = srClient.registerMicroservice(microservice); if (StringUtils.isEmpty(serviceId)) { LOGGER.error( - "Registry microservice failed. appId={}, name={}, version={}", + "Registry microservice failed. appId=[{}], name=[{}], version=[{}], env=[{}]", microservice.getAppId(), microservice.getServiceName(), - microservice.getVersion()); + microservice.getVersion(), + microservice.getEnvironment()); return false; } - schemaIdSetMatch = true; - // 重新注册服务场景下,instanceId不应该缓存 + // In re-register microservice case, the old instanceId should not be cached microservice.getInstance().setInstanceId(null); LOGGER.info( - "Registry Microservice successfully. id={} appId={}, name={}, version={}, schemaIds={}", + "Registry Microservice successfully. id=[{}] appId=[{}], name=[{}], version=[{}], schemaIds={}, env=[{}]", serviceId, microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), - microservice.getSchemas()); + microservice.getSchemas(), + microservice.getEnvironment()); } microservice.setServiceId(serviceId); @@ -127,76 +133,207 @@ private boolean checkSchemaIdSet() { schemaIdSetMatch = existSchemas.equals(localSchemas); if (!schemaIdSetMatch) { - LOGGER.error( - "SchemaIds is different between local and service center. Please change microservice version. " - + "id={} appId={}, name={}, version={}, local schemaIds={}, service center schemaIds={}", + LOGGER.warn( + "SchemaIds is different between local and service center. " + + "serviceId=[{}] appId=[{}], name=[{}], version=[{}], env=[{}], local schemaIds={}, service center schemaIds={}", microservice.getServiceId(), microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), + microservice.getEnvironment(), localSchemas, existSchemas); return true; } LOGGER.info( - "SchemaIds is equals to service center. id={} appId={}, name={}, version={}, schemaIds={}", + "SchemaIds are equals to service center. serviceId=[{}], appId=[{}], name=[{}], version=[{}], env=[{}], schemaIds={}", microservice.getServiceId(), microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), + microservice.getEnvironment(), localSchemas); return true; } private boolean registerSchemas() { - List existSchemas = srClient.getSchemas(microservice.getServiceId()); - for (Entry entry : microservice.getSchemaMap().entrySet()) { - String schemaId = entry.getKey(); - String content = entry.getValue(); - GetSchemaResponse existSchema = extractSchema(schemaId, existSchemas); - boolean exists = existSchema != null && existSchema.getSummary() != null; - LOGGER.info("schemaId [{}] exists {}", schemaId, exists); - if (!exists) { - if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) { - return false; - } - } else { - String curSchemaSummary = existSchema.getSummary(); - String schemaSummary = Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString(); - if (!schemaSummary.equals(curSchemaSummary)) { - if (microservice.getEnvironment().equalsIgnoreCase(ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV)) { - LOGGER.info( - "schemaId [{}]'s content changes and the current environment is {}, so re-register it!", - schemaId, ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV); - if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) { - return false; - } - } else { - throw new IllegalStateException("schemaId [" + schemaId - + "] exists in service center, but the content does not match the local content that means there are interface change " - + "and you need to increment microservice version before deploying. " - + "Or you can configure service_description.environment=" - + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV - + " to work in development environment and ignore this error"); - } - } + Holder> scSchemaHolder = srClient.getSchemas(microservice.getServiceId()); + if (Status.OK.getStatusCode() != scSchemaHolder.getStatusCode()) { + LOGGER.error("failed to get schemas from service center, statusCode = [{}]", scSchemaHolder.getStatusCode()); + return false; + } + + Map scSchemaMap = convertScSchemaMap(scSchemaHolder); + // CHECK: local > sc, local != sc + for (Entry localSchemaEntry : microservice.getSchemaMap().entrySet()) { + if (!registerSchema(scSchemaMap, localSchemaEntry)) { + return false; + } + } + + // CHECK: local < sc + checkRemainingSchema(scSchemaMap); + + schemaIdSetMatch = true; + return true; + } + + /** + * Check whether a local schema is equal to a sc schema. + * @return true if the local schema is equal to a sc schema, or be registered to sc successfully; + * false if schema is registered to sc but failed. + * @throws IllegalStateException The environment is not modifiable, and the local schema is different from sc schema + * or not exist in sc. + */ + private boolean registerSchema(Map scSchemaMap, + Entry localSchemaEntry) { + GetSchemaResponse scSchema = scSchemaMap.get(localSchemaEntry.getKey()); + + boolean onlineSchemaExists = scSchema != null; + LOGGER.info("schemaId [{}] exists [{}], summary exists [{}]", localSchemaEntry.getKey(), onlineSchemaExists, + scSchema != null && scSchema.getSummary() != null); + if (!onlineSchemaExists) { + // local > sc + return registerNewSchema(localSchemaEntry); + } + + scSchemaMap.remove(localSchemaEntry.getKey()); + + // local != sc + return compareAndReRegisterSchema(localSchemaEntry, scSchema); + } + + /** + * Try to register a new schema to service center, or throw exception if cannot register. + * @param localSchemaEntry local schema to be registered. + * @return whether local schema is registered successfully. + * @throws IllegalStateException The environment is unmodifiable. + */ + private boolean registerNewSchema(Entry localSchemaEntry) { + // The ids of schemas are contained by microservice registry request, which means once a microservice + // is registered in the service center, the schemas that it contains are determined. + // If we get a microservice but cannot find the given schemaId in it's schemaId list, this means that + // the schemas of this microservice has been changed, and we should decide whether to register this new + // schema according to it's environment configuration. + if (onlineSchemaIsModifiable()) { + return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue()); + } + + throw new IllegalStateException( + "There is a schema only existing in local microservice: [" + localSchemaEntry.getKey() + + "], which means there are interfaces changed. " + + "You need to increment microservice version before deploying, " + + "or you can configure service_description.environment=" + + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV + + " to work in development environment and ignore this error"); + } + + /** + * Compare schema summary and determine whether to re-register schema or throw exception. + * @param localSchemaEntry local schema + * @param scSchema schema in service center + * @return true if the two copies of schema are the same, or local schema is re-registered successfully, + * false if the local schema is re-registered to service center but failed. + * @throws IllegalStateException The two copies of schema are different and the environment is not modifiable. + */ + private boolean compareAndReRegisterSchema(Entry localSchemaEntry, GetSchemaResponse scSchema) { + String scSchemaSummary = getScSchemaSummary(scSchema); + + if (null == scSchemaSummary) { + // cannot get scSchemaSummary, which means there is no schema content in sc, register schema directly + return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue()); + } + + String localSchemaSummary = RegistryUtils.calcSchemaSummary(localSchemaEntry.getValue()); + if (!localSchemaSummary.equals(scSchemaSummary)) { + if (onlineSchemaIsModifiable()) { + LOGGER.info( + "schema[{}]'s content is changed and the current environment is [{}], so re-register it!", + localSchemaEntry.getKey(), ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV); + return registerSingleSchema(localSchemaEntry.getKey(), localSchemaEntry.getValue()); } + + // env is not development, throw an exception and break the init procedure + throw new IllegalStateException( + "The schema(id=[" + localSchemaEntry.getKey() + + "]) content held by this instance and the service center is different. " + + "You need to increment microservice version before deploying. " + + "Or you can configure service_description.environment=" + + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV + + " to work in development environment and ignore this error"); } + + // summaries are the same return true; } - private GetSchemaResponse extractSchema(String schemaId, List schemas) { - if (schemas == null || schemas.isEmpty()) { - return null; + /** + * Try to get or calculate scSchema summary. + * @return summary of scSchema, + * or null if there is no schema content in service center + */ + private String getScSchemaSummary(GetSchemaResponse scSchema) { + String scSchemaSummary = scSchema.getSummary(); + if (null != scSchemaSummary) { + return scSchemaSummary; } - GetSchemaResponse schema = null; - for (GetSchemaResponse tempSchema : schemas) { - if (tempSchema.getSchemaId().equals(schemaId)) { - schema = tempSchema; - break; + + // if there is no online summery, query online schema content directly and calculate summary + String onlineSchemaContent = srClient.getSchema(microservice.getServiceId(), scSchema.getSchemaId()); + if (null != onlineSchemaContent) { + scSchemaSummary = RegistryUtils.calcSchemaSummary(onlineSchemaContent); + } + + return scSchemaSummary; + } + + /** + * Check whether there are schemas remaining in service center but not exist in local microservice. + * @throws IllegalStateException There are schemas only existing in service center, and the environment is unmodifiable. + */ + private void checkRemainingSchema(Map scSchemaMap) { + if (!scSchemaMap.isEmpty()) { + // there are some schemas only exist in service center + if (!onlineSchemaIsModifiable()) { + // env is not development, throw an exception and break the init procedure + throw new IllegalStateException("There are schemas only existing in service center: " + scSchemaMap.keySet() + + ", which means there are interfaces changed. " + + "You need to increment microservice version before deploying, " + + "or if service_description.environment=" + + ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV + + ", you can delete microservice information in service center and restart this instance."); } + + // 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.", scSchemaMap.keySet()); } - return schema; + } + + private boolean onlineSchemaIsModifiable() { + return ServiceCombConstants.DEVELOPMENT_SERVICECOMB_ENV.equalsIgnoreCase(microservice.getEnvironment()); + } + + /** + * Register a schema directly. + * @return true if register success, otherwise false + */ + private boolean registerSingleSchema(String schemaId, String content) { + return srClient.registerSchema(microservice.getServiceId(), schemaId, content); + } + + private Map convertScSchemaMap(Holder> scSchemaHolder) { + Map scSchemaMap = new HashMap<>(); + List scSchemaList = scSchemaHolder.getValue(); + if (null == scSchemaList) { + return scSchemaMap; + } + + for (GetSchemaResponse scSchema : scSchemaList) { + scSchemaMap.put(scSchema.getSchemaId(), scSchema); + } + + return scSchemaMap; } } diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java index 88ba8bfcba0..095aa8b2e1b 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java @@ -23,6 +23,8 @@ import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo; +import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse; +import org.apache.servicecomb.serviceregistry.client.http.Holder; import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; import org.hamcrest.Matchers; @@ -181,4 +183,12 @@ public void testGetServiceCenterInfo() { ServiceCenterInfo serviceCenterInfo = registryClient.getServiceCenterInfo(); Assert.assertEquals("1.0.0", serviceCenterInfo.getVersion()); } + + @Test + public void testGetSchemas() { + Holder> schemasHolder = registryClient.getSchemas("001"); + Assert.assertEquals(200, schemasHolder.getStatusCode()); + Assert.assertTrue(schemasHolder.getValue().isEmpty()); + } } + diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java index dd370e7cb03..cd796fdd287 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java @@ -24,7 +24,6 @@ import java.util.concurrent.CountDownLatch; import javax.ws.rs.core.Response.Status; -import javax.xml.ws.Holder; import org.apache.log4j.Appender; import org.apache.log4j.Logger; @@ -75,7 +74,7 @@ void httpDo(RequestContext requestContext, Handler responseHandler new MockUp() { @Mock - public void await() throws InterruptedException { + public void await() { } }; } @@ -310,12 +309,15 @@ void httpDo(RequestContext requestContext, Handler responseHandler "{\"schema\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"}," + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}", GetSchemasResponse.class); + holder.statusCode = 200; holder.value = schemasResp; } }; - List abc = oClient.getSchemas(microserviceId); - Assert.assertEquals(3, abc.size()); - Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary()); + Holder> schemasHolder = oClient.getSchemas(microserviceId); + List schemaResponses = schemasHolder.getValue(); + Assert.assertEquals(200, schemasHolder.getStatusCode()); + Assert.assertEquals(3, schemaResponses.size()); + Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", schemaResponses.get(1).getSummary()); } @Test @@ -330,12 +332,32 @@ void httpDo(RequestContext requestContext, Handler responseHandler "{\"schemas\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"}," + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}", GetSchemasResponse.class); + holder.statusCode = 200; holder.value = schemasResp; } }; - List abc = oClient.getSchemas(microserviceId); - Assert.assertEquals(3, abc.size()); - Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary()); + Holder> schemasHolder = oClient.getSchemas(microserviceId); + List schemas = schemasHolder.getValue(); + Assert.assertEquals(200, schemasHolder.getStatusCode()); + Assert.assertEquals(3, schemas.size()); + Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", schemas.get(1).getSummary()); + } + + @Test + public void getSchemasFailed() { + String microserviceId = "msId"; + + new MockUp() { + @Mock + void httpDo(RequestContext requestContext, Handler responseHandler) { + Holder holder = Deencapsulation.getField(responseHandler, "arg$4"); + holder.setStatusCode(Status.NOT_FOUND.getStatusCode()); + } + }; + Holder> schemasHolder = oClient.getSchemas(microserviceId); + List schemaResponses = schemasHolder.getValue(); + Assert.assertEquals(404, schemasHolder.getStatusCode()); + Assert.assertNull(schemaResponses); } @Test 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 e85af2e58b5..a7944c83aee 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 @@ -24,6 +24,7 @@ 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.Assert; import org.junit.Before; import org.junit.Test; @@ -82,12 +83,16 @@ public void testNewRegisterFailed(@Mocked ServiceRegistryClient srClient) { @Test public void testNewRegisterSuccess(@Mocked ServiceRegistryClient srClient) { + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200); new Expectations() { { srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = null; srClient.registerMicroservice((Microservice) any); result = "serviceId"; + srClient.getSchemas("serviceId"); + this.result = onlineSchemasHolder; } }; @@ -104,10 +109,23 @@ public void testNewRegisterSuccess(@Mocked ServiceRegistryClient srClient) { Assert.assertEquals(1, taskList.size()); } + /** + * Local schemaId set is consistent with online schemaId set, and schema contents are not registered. + * This service instance try to register schema content but failed. + */ @Test public void testRegisterSchemaFailed(@Mocked ServiceRegistryClient srClient) { microservice.addSchema("s1", ""); microservice.addSchema("exist", ""); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200); + ArrayList schemaResponses = new ArrayList<>(); + onlineSchemasHolder.setValue(schemaResponses); + GetSchemaResponse schemaResponse = new GetSchemaResponse(); + schemaResponse.setSchemaId("s1"); + schemaResponses.add(schemaResponse); + schemaResponse.setSchemaId("exist"); + schemaResponses.add(schemaResponse); new Expectations() { { srClient.getMicroserviceId(anyString, anyString, anyString, anyString); @@ -116,6 +134,8 @@ public void testRegisterSchemaFailed(@Mocked ServiceRegistryClient srClient) { result = "serviceId"; srClient.registerSchema(anyString, anyString, anyString); result = false; + srClient.getSchemas("serviceId"); + this.result = onlineSchemasHolder; } }; @@ -123,23 +143,35 @@ public void testRegisterSchemaFailed(@Mocked ServiceRegistryClient srClient) { registerTask.run(); Assert.assertEquals(false, registerTask.isRegistered()); - Assert.assertEquals(true, registerTask.isSchemaIdSetMatch()); + Assert.assertEquals(false, registerTask.isSchemaIdSetMatch()); Assert.assertEquals("serviceId", microservice.getServiceId()); Assert.assertEquals("serviceId", microservice.getInstance().getServiceId()); Assert.assertEquals(1, taskList.size()); } + /** + * There is no microservice information in service center. + */ @Test public void testRegisterSchemaSuccess(@Mocked ServiceRegistryClient srClient) { - microservice.addSchema("s1", ""); + microservice.addSchema("s1", "s1Content"); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200); + ArrayList schemaResponseList = new ArrayList<>(); + onlineSchemasHolder.setValue(schemaResponseList); + GetSchemaResponse schemaResponse = new GetSchemaResponse(); + schemaResponseList.add(schemaResponse); + schemaResponse.setSchemaId("s1"); new Expectations() { { srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = null; srClient.registerMicroservice((Microservice) any); result = "serviceId"; - srClient.registerSchema(anyString, anyString, anyString); - result = true; + srClient.getSchema("serviceId", "s1"); + result = "s1Content"; + srClient.getSchemas("serviceId"); + result = onlineSchemasHolder; } }; @@ -155,12 +187,16 @@ public void testRegisterSchemaSuccess(@Mocked ServiceRegistryClient srClient) { @Test public void testAlreadyRegisteredSchemaIdSetMatch(@Mocked ServiceRegistryClient srClient) { + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200); new Expectations() { { srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); result = microservice; + srClient.getSchemas("serviceId"); + result = onlineSchemasHolder; } }; @@ -175,30 +211,33 @@ public void testAlreadyRegisteredSchemaIdSetMatch(@Mocked ServiceRegistryClient Assert.assertEquals(1, taskList.size()); } - @Test + @Test(expected = IllegalStateException.class) public void testAlreadyRegisteredSchemaIdSetNotMatch(@Mocked ServiceRegistryClient srClient) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); otherMicroservice.addSchema("s1", ""); - + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200); + ArrayList schemaResponseList = new ArrayList<>(); + onlineSchemasHolder.setValue(schemaResponseList); + GetSchemaResponse schemaResponse = new GetSchemaResponse(); + + schemaResponseList.add(schemaResponse); + schemaResponse.setSchemaId("s1"); new Expectations() { { srClient.getMicroserviceId(anyString, anyString, anyString, anyString); result = "serviceId"; srClient.getMicroservice(anyString); result = otherMicroservice; + srClient.getSchemas("serviceId"); + result = onlineSchemasHolder; } }; MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); registerTask.run(); - - Assert.assertEquals(true, registerTask.isRegistered()); - Assert.assertEquals(false, registerTask.isSchemaIdSetMatch()); - Assert.assertEquals("serviceId", microservice.getServiceId()); - Assert.assertEquals("serviceId", microservice.getInstance().getServiceId()); - Assert.assertEquals(1, taskList.size()); } @Test @@ -240,6 +279,8 @@ public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) { resp.setSchemaId("s1"); resp.setSummary("c1188d709631a9038874f9efc6eb894f"); list.add(resp); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setStatusCode(200).setValue(list); new Expectations() { { @@ -248,7 +289,7 @@ public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) { srClient.getMicroservice(anyString); result = otherMicroservice; srClient.getSchemas(anyString); - result = list; + result = onlineSchemasHolder; srClient.registerSchema(microservice.getServiceId(), anyString, anyString); result = true; } @@ -265,18 +306,22 @@ public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) { Assert.assertEquals(1, taskList.size()); } + /** + * There is microservice information but no schema in service center. + */ @Test public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) { Microservice otherMicroservice = new Microservice(); otherMicroservice.setAppId(microservice.getAppId()); otherMicroservice.setServiceName("ms1"); - otherMicroservice.addSchema("s1", ""); + otherMicroservice.addSchema("s1", null); List list = new ArrayList<>(); GetSchemaResponse resp = new GetSchemaResponse(); resp.setSchemaId("s1"); - resp.setSummary(null); list.add(resp); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setValue(list).setStatusCode(200); new Expectations() { { @@ -285,14 +330,16 @@ public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) { srClient.getMicroservice(anyString); result = otherMicroservice; srClient.getSchemas(anyString); - result = list; - srClient.registerSchema(microservice.getServiceId(), anyString, anyString); + result = onlineSchemasHolder; + srClient.getSchema("serviceId", "s1"); + result = null; + srClient.registerSchema("serviceId", "s1", "s1Content"); result = true; } }; - microservice.addSchema("s1", ""); - microservice.getInstance().setEnvironment("production"); + microservice.addSchema("s1", "s1Content"); + microservice.setEnvironment("production"); MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); registerTask.run(); @@ -302,6 +349,9 @@ public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) { Assert.assertEquals(1, taskList.size()); } + /** + * There is schema in service center which is different from local schema. + */ @Test(expected = IllegalStateException.class) public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { Microservice otherMicroservice = new Microservice(); @@ -314,6 +364,8 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { resp.setSchemaId("s1"); resp.setSummary("c1188d709631a9038874f9efc6eb894f"); list.add(resp); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setValue(list).setStatusCode(200); new Expectations() { { @@ -322,7 +374,7 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { srClient.getMicroservice(anyString); result = otherMicroservice; srClient.getSchemas(anyString); - result = list; + result = onlineSchemasHolder; } }; @@ -331,4 +383,92 @@ public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) { MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); registerTask.run(); } + + /** + * env = production and there are schemas only existing in service center + */ + @Test(expected = IllegalStateException.class) + public void testReRegisterForProductAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) { + Microservice otherMicroservice = new Microservice(); + otherMicroservice.setAppId(microservice.getAppId()); + otherMicroservice.setServiceName("ms1"); + otherMicroservice.addSchema("s1", null); + otherMicroservice.addSchema("s2", null); + + List list = new ArrayList<>(); + GetSchemaResponse resp = new GetSchemaResponse(); + resp.setSchemaId("s1"); + list.add(resp); + resp = new GetSchemaResponse(); + resp.setSchemaId("s2"); + list.add(resp); + Holder> 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("serviceId", "s1"); + result = null; + srClient.registerSchema("serviceId", "s1", "s1Content"); + result = true; + } + }; + + microservice.addSchema("s1", "s1Content"); + microservice.setEnvironment("production"); + MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice); + registerTask.run(); + } + + @Test + public void testReRegisterForDevAndLocalSchemasAreLess(@Mocked ServiceRegistryClient srClient) { + Microservice otherMicroservice = new Microservice(); + otherMicroservice.setAppId(microservice.getAppId()); + otherMicroservice.setServiceName("ms1"); + otherMicroservice.addSchema("s1", null); + otherMicroservice.addSchema("s2", null); + + List list = new ArrayList<>(); + GetSchemaResponse resp = new GetSchemaResponse(); + resp.setSchemaId("s1"); + list.add(resp); + resp = new GetSchemaResponse(); + resp.setSchemaId("s2"); + list.add(resp); + Holder> onlineSchemasHolder = new Holder<>(); + onlineSchemasHolder.setValue(list).setStatusCode(200); + Holder removeSchemaResult = new Holder<>(); + removeSchemaResult.setStatusCode(200); + + new Expectations() { + { + srClient.getMicroserviceId(anyString, anyString, anyString, anyString); + result = "serviceId"; + srClient.getMicroservice(anyString); + result = otherMicroservice; + srClient.getSchemas(anyString); + result = onlineSchemasHolder; + srClient.getSchema("serviceId", "s1"); + result = null; + srClient.registerSchema("serviceId", "s1", "s1Content"); + result = true; + } + }; + + microservice.addSchema("s1", "s1Content"); + microservice.setEnvironment("development"); + 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(1, taskList.size()); + } }