This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit eadd9238938601faffdfb2f11c127bdc43f8201a Author: yaohaishi <[email protected]> AuthorDate: Thu Sep 20 17:17:21 2018 +0800 [SCB-926] only allow initialize 3rd party services' instances for once, move StaticSchemaFactory into SCBEngine --- .../servicecomb/core/CseApplicationListener.java | 2 + .../org/apache/servicecomb/core/CseContext.java | 12 --- .../org/apache/servicecomb/core/SCBEngine.java | 11 +++ .../definition/StaticMicroserviceVersionMeta.java | 3 +- .../definition/schema/AbstractSchemaFactory.java | 14 ---- .../definition/schema/ProducerSchemaFactory.java | 3 +- .../definition/schema/StaticSchemaFactory.java | 3 +- .../StaticMicroserviceVersionMetaTest.java | 3 +- .../definition/schema/StaticSchemaFactoryTest.java | 3 +- .../thirdparty/Test3rdPartyInvocation.java | 4 +- .../serviceregistry/ServiceRegistry.java | 15 +++- .../registry/AbstractServiceRegistry.java | 20 ++--- .../registry/TestAbstractServiceRegistry.java | 88 +++++++++++----------- 13 files changed, 92 insertions(+), 89 deletions(-) diff --git a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java index 70c66f8..5ed1759 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java @@ -19,6 +19,7 @@ package org.apache.servicecomb.core; import org.apache.servicecomb.core.definition.MicroserviceMeta; import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; +import org.apache.servicecomb.core.definition.schema.StaticSchemaFactory; import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager; import org.apache.servicecomb.core.provider.producer.ProducerProviderManager; import org.apache.servicecomb.core.transport.TransportManager; @@ -74,6 +75,7 @@ public class CseApplicationListener SCBEngine.getInstance().setTransportManager(applicationContext.getBean(TransportManager.class)); SCBEngine.getInstance().setSchemaListenerManager(applicationContext.getBean(SchemaListenerManager.class)); SCBEngine.getInstance().setBootListenerList(applicationContext.getBeansOfType(BootListener.class).values()); + SCBEngine.getInstance().setStaticSchemaFactory(applicationContext.getBean(StaticSchemaFactory.class)); } SCBEngine.getInstance().init(); diff --git a/core/src/main/java/org/apache/servicecomb/core/CseContext.java b/core/src/main/java/org/apache/servicecomb/core/CseContext.java index 3aa9e3e..45b8415 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseContext.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseContext.java @@ -22,7 +22,6 @@ import javax.inject.Inject; import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; import org.apache.servicecomb.core.definition.loader.SchemaLoader; import org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory; -import org.apache.servicecomb.core.definition.schema.StaticSchemaFactory; import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager; import org.apache.servicecomb.core.provider.producer.ProducerProviderManager; import org.apache.servicecomb.core.transport.TransportManager; @@ -41,8 +40,6 @@ public class CseContext { private ConsumerSchemaFactory consumerSchemaFactory; - private StaticSchemaFactory staticSchemaFactory; - private ConsumerProviderManager consumerProviderManager; private ProducerProviderManager producerProviderManager; @@ -79,10 +76,6 @@ public class CseContext { return swaggerEnvironment; } - public StaticSchemaFactory getStaticSchemaFactory() { - return staticSchemaFactory; - } - @Inject public void setSwaggerEnvironment(SwaggerEnvironment swaggerEnvironment) { this.swaggerEnvironment = swaggerEnvironment; @@ -117,9 +110,4 @@ public class CseContext { public void setTransportManager(TransportManager transportManager) { this.transportManager = transportManager; } - - @Inject - public void setStaticSchemaFactory(StaticSchemaFactory staticSchemaFactory) { - this.staticSchemaFactory = staticSchemaFactory; - } } diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java index 4be7e7c..f364d1c 100644 --- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java +++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java @@ -31,6 +31,7 @@ import org.apache.servicecomb.core.BootListener.BootEvent; import org.apache.servicecomb.core.BootListener.EventType; import org.apache.servicecomb.core.definition.MicroserviceMeta; import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; +import org.apache.servicecomb.core.definition.schema.StaticSchemaFactory; import org.apache.servicecomb.core.endpoint.AbstractEndpointsCache; import org.apache.servicecomb.core.event.InvocationFinishEvent; import org.apache.servicecomb.core.event.InvocationStartEvent; @@ -82,6 +83,8 @@ public class SCBEngine { private EventBus eventBus = EventManager.getEventBus(); + private StaticSchemaFactory staticSchemaFactory; + private static final SCBEngine INSTANCE = new SCBEngine(); public void setStatus(SCBStatus status) { @@ -374,4 +377,12 @@ public class SCBEngine { } } } + + public StaticSchemaFactory getStaticSchemaFactory() { + return staticSchemaFactory; + } + + public void setStaticSchemaFactory(StaticSchemaFactory staticSchemaFactory) { + this.staticSchemaFactory = staticSchemaFactory; + } } diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java index 4ef88b0..0fd866b 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java @@ -18,6 +18,7 @@ package org.apache.servicecomb.core.definition; import org.apache.servicecomb.core.CseContext; +import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoaderFactory; import org.apache.servicecomb.serviceregistry.api.Const; import org.apache.servicecomb.serviceregistry.api.registry.StaticMicroservice; @@ -37,7 +38,7 @@ public class StaticMicroserviceVersionMeta extends MicroserviceVersionMeta { return; } - CseContext.getInstance().getStaticSchemaFactory().loadSchema(microserviceMeta, microservice); + SCBEngine.getInstance().getStaticSchemaFactory().loadSchema(microserviceMeta, microservice); CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(microserviceMeta); } } diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/schema/AbstractSchemaFactory.java b/core/src/main/java/org/apache/servicecomb/core/definition/schema/AbstractSchemaFactory.java index f2a6575..0db01dc 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/schema/AbstractSchemaFactory.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/schema/AbstractSchemaFactory.java @@ -31,11 +31,7 @@ import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorCo import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator; import org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectWriter; - import io.swagger.models.Swagger; -import io.swagger.util.Yaml; /** * 由consumer或producer发起的契约注册 @@ -49,8 +45,6 @@ public abstract class AbstractSchemaFactory<CONTEXT extends SchemaContext> { @Inject protected CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext; - private ObjectWriter writer = Yaml.pretty(); - @Inject public void setSchemaLoader(SchemaLoader schemaLoader) { this.schemaLoader = schemaLoader; @@ -109,12 +103,4 @@ public abstract class AbstractSchemaFactory<CONTEXT extends SchemaContext> { return generator; } - - protected String getSwaggerContent(Swagger swagger) { - try { - return writer.writeValueAsString(swagger); - } catch (JsonProcessingException e) { - throw new Error(e); - } - } } diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java index 8546f5d..59888d4 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java @@ -36,6 +36,7 @@ import org.apache.servicecomb.core.definition.SchemaMeta; import org.apache.servicecomb.core.executor.ExecutorManager; import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.swagger.SwaggerUtils; import org.apache.servicecomb.swagger.engine.SwaggerEnvironment; import org.apache.servicecomb.swagger.engine.SwaggerProducer; import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation; @@ -113,7 +114,7 @@ public class ProducerSchemaFactory extends AbstractSchemaFactory<ProducerSchemaC if (swagger == null) { SwaggerGenerator generator = generateSwagger(context); swagger = generator.getSwagger(); - String swaggerContent = getSwaggerContent(swagger); + String swaggerContent = SwaggerUtils.swaggerToString(swagger); LOGGER.info("generate swagger for {}/{}/{}, swagger: {}", context.getMicroserviceMeta().getAppId(), context.getMicroserviceName(), diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactory.java b/core/src/main/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactory.java index 9c4e00a..0606e46 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactory.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactory.java @@ -20,6 +20,7 @@ package org.apache.servicecomb.core.definition.schema; import org.apache.servicecomb.core.definition.MicroserviceMeta; import org.apache.servicecomb.core.definition.SchemaMeta; import org.apache.servicecomb.serviceregistry.api.registry.StaticMicroservice; +import org.apache.servicecomb.swagger.SwaggerUtils; import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,7 +49,7 @@ public class StaticSchemaFactory extends AbstractSchemaFactory<SchemaContext> { Swagger swagger; SwaggerGenerator generator = generateSwagger(context); swagger = generator.getSwagger(); - String swaggerContent = getSwaggerContent(swagger); + String swaggerContent = SwaggerUtils.swaggerToString(swagger); LOGGER.info("generate swagger for {}/{}/{}, swagger: {}", context.getMicroserviceMeta().getAppId(), context.getMicroserviceName(), diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaTest.java b/core/src/test/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaTest.java index d9fe118..a06f087 100644 --- a/core/src/test/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaTest.java +++ b/core/src/test/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaTest.java @@ -20,6 +20,7 @@ package org.apache.servicecomb.core.definition; import javax.xml.ws.Holder; import org.apache.servicecomb.core.CseContext; +import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoaderFactory; import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; import org.apache.servicecomb.core.definition.schema.StaticSchemaFactory; @@ -62,7 +63,7 @@ public class StaticMicroserviceVersionMetaTest { Mockito.when(classLoaderFactory.create(appId, serviceName, version)).thenReturn(classLoader); Holder<Boolean> schemaLoaded = new Holder<>(false); - CseContext.getInstance().setStaticSchemaFactory(new MockUp<StaticSchemaFactory>() { + SCBEngine.getInstance().setStaticSchemaFactory(new MockUp<StaticSchemaFactory>() { @Mock void loadSchema(MicroserviceMeta microserviceMeta, StaticMicroservice microservice) { Assert.assertSame(classLoader, microserviceMeta.getClassLoader()); diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactoryTest.java b/core/src/test/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactoryTest.java index 39b4a1b..2f254ae 100644 --- a/core/src/test/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactoryTest.java +++ b/core/src/test/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactoryTest.java @@ -31,6 +31,7 @@ import org.apache.servicecomb.core.definition.loader.SchemaLoader; import org.apache.servicecomb.core.unittest.UnitTestMeta; import org.apache.servicecomb.foundation.common.utils.ReflectUtils; import org.apache.servicecomb.serviceregistry.api.registry.StaticMicroservice; +import org.apache.servicecomb.swagger.SwaggerUtils; import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext; import org.junit.AfterClass; import org.junit.Assert; @@ -73,7 +74,7 @@ public class StaticSchemaFactoryTest { SchemaMeta schemaMeta = microserviceMeta.findSchemaMeta(serviceAndSchemaName); Assert.assertEquals(EXPECTED_SCHEMA_CONTENT, - staticSchemaFactory.getSwaggerContent(schemaMeta.getSwagger())); + SwaggerUtils.swaggerToString(schemaMeta.getSwagger())); Assert.assertEquals(2, schemaMeta.getOperations().size()); OperationMeta operationMeta = schemaMeta.ensureFindOperation("add"); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java index 36a298f..4f4dfaa 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java @@ -66,14 +66,14 @@ public class Test3rdPartyInvocation { int endIndex = urlPrefix.indexOf("/", beginIndex + 3); String endpoint = "rest:" + urlPrefix.substring(beginIndex, endIndex); RegistryUtils.getServiceRegistry() - .registryMicroserviceMappingByEndpoints( + .registerMicroserviceMappingByEndpoints( THIRD_PARTY_MICROSERVICE_NAME, "1.2.1", DataTypeJaxrsSchemaIntf.class, Collections.singletonList(endpoint)); MicroserviceInstance instance = new MicroserviceInstance(); instance.setEndpoints(Collections.singletonList(endpoint)); RegistryUtils.getServiceRegistry() - .registryMicroserviceMapping( + .registerMicroserviceMapping( ASYNC_THIRD_PARTY_MICROSERVICE_NAME, "1.1.1", DataTypeJaxrsSchemaAsyncIntf.class, Collections.singletonList(instance) ); diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java index bd79dd3..2b46e74 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java @@ -62,13 +62,18 @@ public interface ServiceRegistry { /** * <p> - * Register a third party service if not registered before, and add it's instances into + * Register a third party service if not registered before, and set it's instances into * {@linkplain org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersions StaticMicroserviceVersions}. * </p> * <p> * The registered third party service has the same {@code appId} and {@code environment} as this microservice instance has, * and there is only one schema represented by {@code schemaIntfCls}, whose name is the same as {@code microserviceName}. * </p> + * <em> + * This method is for initializing 3rd party service endpoint config. + * i.e. If this service has not been registered before, this service will be registered and the instances will be set; + * otherwise, NOTHING will happen. + * </em> * * @param microserviceName name of the 3rd party service, and this param also specifies the schemaId * @param version version of this 3rd party service @@ -77,13 +82,15 @@ public interface ServiceRegistry { * @param instances the instances of this 3rd party service. Users only need to specify the endpoint information, other * necessary information will be generate and set in the implementation of this method. */ - void registryMicroserviceMapping(String microserviceName, String version, Class<?> schemaIntfCls, + void registerMicroserviceMapping(String microserviceName, String version, Class<?> schemaIntfCls, List<MicroserviceInstance> instances); /** - * @see #registryMicroserviceMapping(String, String, Class, List) + * @see #registerMicroserviceMapping(String, String, Class, List) * @param endpoints the endpoints of 3rd party service. Each of endpoints will be treated as a separated instance. + * Format of the endpoints is the same as the endpoints that ServiceComb microservices register in service-center, + * like {@code rest://127.0.0.1:8080} */ - void registryMicroserviceMappingByEndpoints(String microserviceName, String version, + void registerMicroserviceMappingByEndpoints(String microserviceName, String version, Class<?> schemaIntfCls, List<String> endpoints); } diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java index c9e310d..bae8815 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java @@ -309,20 +309,22 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry { } @Override - public void registryMicroserviceMapping(String microserviceName, String version, Class<?> schemaIntfCls, + public void registerMicroserviceMapping(String microserviceName, String version, Class<?> schemaIntfCls, List<MicroserviceInstance> instances) { String app = RegistryUtils.getAppId(); MicroserviceManager microserviceManager = appManager.getOrCreateMicroserviceManager(app); - StaticMicroserviceVersions microserviceVersions = - (StaticMicroserviceVersions) microserviceManager.getVersionsByName() - .computeIfAbsent(microserviceName, - svcName -> new StaticMicroserviceVersions(this.appManager, app, microserviceName, schemaIntfCls)); - - microserviceVersions.addInstances(version, instances); + microserviceManager.getVersionsByName() + .computeIfAbsent(microserviceName, + svcName -> { + StaticMicroserviceVersions microserviceVersions = new StaticMicroserviceVersions(this.appManager, + app, microserviceName, schemaIntfCls); + microserviceVersions.addInstances(version, instances); + return microserviceVersions; + }); } @Override - public void registryMicroserviceMappingByEndpoints(String microserviceName, String version, + public void registerMicroserviceMappingByEndpoints(String microserviceName, String version, Class<?> schemaIntfCls, List<String> endpoints) { ArrayList<MicroserviceInstance> microserviceInstances = new ArrayList<>(); for (String endpoint : endpoints) { @@ -331,6 +333,6 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry { microserviceInstances.add(instance); } - registryMicroserviceMapping(microserviceName, version, schemaIntfCls, microserviceInstances); + registerMicroserviceMapping(microserviceName, version, schemaIntfCls, microserviceInstances); } } diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java index d4582c9..4fabb6d 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java @@ -22,8 +22,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; -import javax.xml.ws.Holder; - import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; @@ -31,8 +29,9 @@ import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig; import org.apache.servicecomb.serviceregistry.consumer.AppManager; import org.apache.servicecomb.serviceregistry.consumer.DefaultMicroserviceVersionFactory; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager; +import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions; -import org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersions; +import org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersionFactory; import org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition; import org.hamcrest.Matchers; import org.junit.After; @@ -46,9 +45,8 @@ import org.mockito.Mockito; import com.google.common.eventbus.EventBus; +import mockit.Deencapsulation; import mockit.Expectations; -import mockit.Mock; -import mockit.MockUp; import mockit.Mocked; public class TestAbstractServiceRegistry { @@ -143,26 +141,27 @@ public class TestAbstractServiceRegistry { String testServiceName = "testService"; final String testVersion = "1.0.1"; - registry.appManager = Mockito.mock(AppManager.class); - MicroserviceManager microserviceManager = Mockito.mock(MicroserviceManager.class); - Mockito.when(registry.appManager.getOrCreateMicroserviceManager(this.appId)).thenReturn(microserviceManager); - HashMap<String, MicroserviceVersions> versionsByName = new HashMap<>(); - Mockito.when(microserviceManager.getVersionsByName()).thenReturn(versionsByName); + HashMap<String, MicroserviceVersions> versionsByName = prepareForMicroserviceMappingRegistry(); ArrayList<MicroserviceInstance> instancesParam = new ArrayList<>(); - Holder<Boolean> checked = new Holder<>(false); - StaticMicroserviceVersions microserviceVersions = new MockUp<StaticMicroserviceVersions>() { - @Mock - void addInstances(String version, List<MicroserviceInstance> instances) { - Assert.assertEquals(version, version); - Assert.assertSame(instancesParam, instances); - checked.value = true; - } - }.getMockInstance(); - versionsByName.put(testServiceName, microserviceVersions); - - registry.registryMicroserviceMapping(testServiceName, testVersion, Test3rdPartyServiceIntf.class, instancesParam); - Assert.assertTrue(checked.value); + instancesParam.add(new MicroserviceInstance()); + registry.registerMicroserviceMapping(testServiceName, testVersion, Test3rdPartyServiceIntf.class, instancesParam); + + MicroserviceVersions microserviceVersions = versionsByName.get(testServiceName); + List<MicroserviceInstance> instances = Deencapsulation.getField(microserviceVersions, "instances"); + Assert.assertEquals(1, instances.size()); + Assert.assertSame(instancesParam.get(0), instances.get(0)); + + // nothing will happen if register repeatedly + List<MicroserviceInstance> newInstancesParam = new ArrayList<>(); + newInstancesParam.add(new MicroserviceInstance()); + registry.registerMicroserviceMapping( + testServiceName, testVersion, Test3rdPartyServiceIntf.class, newInstancesParam); + + microserviceVersions = versionsByName.get(testServiceName); + instances = Deencapsulation.getField(microserviceVersions, "instances"); + Assert.assertEquals(1, instances.size()); + Assert.assertSame(instancesParam.get(0), instances.get(0)); } @Test @@ -170,31 +169,34 @@ public class TestAbstractServiceRegistry { String testServiceName = "testService"; final String testVersion = "1.0.1"; + HashMap<String, MicroserviceVersions> versionByName = prepareForMicroserviceMappingRegistry(); + + registry.registerMicroserviceMappingByEndpoints(testServiceName, testVersion, Test3rdPartyServiceIntf.class, + Arrays.asList("cse://127.0.0.1:8080", "cse://127.0.0.1:8081")); + + MicroserviceVersions microserviceVersions = versionByName.get(testServiceName); + List<MicroserviceInstance> instances = Deencapsulation.getField(microserviceVersions, "instances"); + Assert.assertEquals(2, instances.size()); + Assert.assertEquals("cse://127.0.0.1:8080", instances.get(0).getEndpoints().get(0)); + Assert.assertEquals("cse://127.0.0.1:8081", instances.get(1).getEndpoints().get(0)); + } + + private HashMap<String, MicroserviceVersions> prepareForMicroserviceMappingRegistry() { registry.appManager = Mockito.mock(AppManager.class); MicroserviceManager microserviceManager = Mockito.mock(MicroserviceManager.class); - Mockito.when(registry.appManager.getOrCreateMicroserviceManager(this.appId)).thenReturn(microserviceManager); + StaticMicroserviceVersionFactory staticMicroserviceVersionFactory = + Mockito.mock(StaticMicroserviceVersionFactory.class); HashMap<String, MicroserviceVersions> versionsByName = new HashMap<>(); - Mockito.when(microserviceManager.getVersionsByName()).thenReturn(versionsByName); - ArrayList<MicroserviceInstance> instancesParam = new ArrayList<>(); - Holder<Boolean> checked = new Holder<>(false); - StaticMicroserviceVersions microserviceVersions = new MockUp<StaticMicroserviceVersions>() { - @Mock - void addInstances(String version, List<MicroserviceInstance> instances) { - Assert.assertEquals("1.0.1", version); - Assert.assertEquals(2, instances.size()); - Assert.assertEquals(1, instances.get(0).getEndpoints().size()); - Assert.assertEquals("http://127.0.0.1:8080", instances.get(0).getEndpoints().get(0)); - Assert.assertEquals(1, instances.get(1).getEndpoints().size()); - Assert.assertEquals("http://127.0.0.1:8081", instances.get(1).getEndpoints().get(0)); - checked.value = true; - } - }.getMockInstance(); - versionsByName.put(testServiceName, microserviceVersions); + Mockito.when(registry.appManager.getOrCreateMicroserviceManager(this.appId)).thenReturn(microserviceManager); + Mockito.when(registry.appManager.getEventBus()).thenReturn(Mockito.mock(EventBus.class)); + Mockito.when(registry.appManager.getStaticMicroserviceVersionFactory()) + .thenReturn(staticMicroserviceVersionFactory); + Mockito.when(staticMicroserviceVersionFactory.create(Mockito.any())) + .thenReturn(Mockito.mock(MicroserviceVersion.class)); - registry.registryMicroserviceMappingByEndpoints("testService", "1.0.1", Test3rdPartyServiceIntf.class, - Arrays.asList("http://127.0.0.1:8080", "http://127.0.0.1:8081")); - Assert.assertTrue(checked.value); + Mockito.when(microserviceManager.getVersionsByName()).thenReturn(versionsByName); + return versionsByName; } private interface Test3rdPartyServiceIntf {
