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 06442ae6f4fe8538ba40d219142ff75c0d9dc006 Author: yaohaishi <[email protected]> AuthorDate: Wed Sep 19 16:41:01 2018 +0800 [SCB-926] add StaticMicroserviceVersionMeta, generate and register schema --- .../org/apache/servicecomb/core/CseContext.java | 12 ++ .../core/definition/MicroserviceVersionMeta.java | 7 +- ...eta.java => StaticMicroserviceVersionMeta.java} | 22 ++- .../StaticMicroserviceVersionMetaFactory.java | 2 +- .../definition/schema/AbstractSchemaFactory.java | 14 ++ .../definition/schema/ProducerSchemaFactory.java | 15 +- .../definition/schema/StaticSchemaFactory.java | 61 ++++++++ .../StaticMicroserviceVersionMetaTest.java | 95 ++++++++++++ .../definition/schema/StaticSchemaFactoryTest.java | 167 +++++++++++++++++++++ .../consumer/MicroserviceVersion.java | 5 + .../consumer/TestMicroserviceVersion.java | 14 +- 11 files changed, 384 insertions(+), 30 deletions(-) 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 45b8415..3aa9e3e 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseContext.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseContext.java @@ -22,6 +22,7 @@ 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; @@ -40,6 +41,8 @@ public class CseContext { private ConsumerSchemaFactory consumerSchemaFactory; + private StaticSchemaFactory staticSchemaFactory; + private ConsumerProviderManager consumerProviderManager; private ProducerProviderManager producerProviderManager; @@ -76,6 +79,10 @@ public class CseContext { return swaggerEnvironment; } + public StaticSchemaFactory getStaticSchemaFactory() { + return staticSchemaFactory; + } + @Inject public void setSwaggerEnvironment(SwaggerEnvironment swaggerEnvironment) { this.swaggerEnvironment = swaggerEnvironment; @@ -110,4 +117,9 @@ 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/definition/MicroserviceVersionMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java index a5eff58..e4e2763 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java @@ -20,10 +20,15 @@ package org.apache.servicecomb.core.definition; import org.apache.servicecomb.core.CseContext; import org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoaderFactory; import org.apache.servicecomb.serviceregistry.api.Const; +import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion; public class MicroserviceVersionMeta extends MicroserviceVersion { - private MicroserviceMeta microserviceMeta; + MicroserviceMeta microserviceMeta; + + MicroserviceVersionMeta(Microservice microservice) { + super(microservice); + } public MicroserviceVersionMeta(String microserviceName, String microserviceId, MicroserviceClassLoaderFactory classLoaderFactory) { diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java similarity index 65% copy from core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java copy to core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java index a5eff58..4ef88b0 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceVersionMeta.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMeta.java @@ -20,28 +20,24 @@ package org.apache.servicecomb.core.definition; import org.apache.servicecomb.core.CseContext; import org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoaderFactory; import org.apache.servicecomb.serviceregistry.api.Const; -import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion; +import org.apache.servicecomb.serviceregistry.api.registry.StaticMicroservice; -public class MicroserviceVersionMeta extends MicroserviceVersion { - private MicroserviceMeta microserviceMeta; +public class StaticMicroserviceVersionMeta extends MicroserviceVersionMeta { - public MicroserviceVersionMeta(String microserviceName, String microserviceId, + public StaticMicroserviceVersionMeta(StaticMicroservice microservice, MicroserviceClassLoaderFactory classLoaderFactory) { - super(microserviceId); + super(microservice); - this.microserviceMeta = new MicroserviceMeta(microserviceName); + this.microserviceMeta = new MicroserviceMeta(microservice.getServiceName()); this.microserviceMeta.setClassLoader( - classLoaderFactory.create(microservice.getAppId(), microserviceName, microservice.getVersion())); - if (Const.REGISTRY_APP_ID.equals(microservice.getAppId()) && Const.REGISTRY_SERVICE_NAME.equals(microserviceName)) { + classLoaderFactory.create(microservice.getAppId(), microservice.getServiceName(), microservice.getVersion())); + if (Const.REGISTRY_APP_ID.equals(microservice.getAppId()) + && Const.REGISTRY_SERVICE_NAME.equals(microservice.getServiceName())) { // do not load service center schemas return; } - CseContext.getInstance().getConsumerSchemaFactory().createConsumerSchema(microserviceMeta, microservice); + CseContext.getInstance().getStaticSchemaFactory().loadSchema(microserviceMeta, microservice); CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(microserviceMeta); } - - public MicroserviceMeta getMicroserviceMeta() { - return microserviceMeta; - } } diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaFactory.java b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaFactory.java index 9fd8e5e..e10dc9b 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaFactory.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaFactory.java @@ -28,6 +28,6 @@ public class StaticMicroserviceVersionMetaFactory implements StaticMicroserviceV @Override public MicroserviceVersion create(StaticMicroservice microservice) { - return null; + return new StaticMicroserviceVersionMeta(microservice, classLoaderFactory); } } 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 0db01dc..f2a6575 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,7 +31,11 @@ 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发起的契约注册 @@ -45,6 +49,8 @@ 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; @@ -103,4 +109,12 @@ 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 5b1d0c8..8546f5d 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 @@ -45,13 +45,10 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectWriter; import com.netflix.config.DynamicPropertyFactory; import io.swagger.models.Operation; import io.swagger.models.Swagger; -import io.swagger.util.Yaml; @Component public class ProducerSchemaFactory extends AbstractSchemaFactory<ProducerSchemaContext> { @@ -60,22 +57,12 @@ public class ProducerSchemaFactory extends AbstractSchemaFactory<ProducerSchemaC @Inject private SwaggerEnvironment swaggerEnv; - private ObjectWriter writer = Yaml.pretty(); - - private String getSwaggerContent(Swagger swagger) { - try { - return writer.writeValueAsString(swagger); - } catch (JsonProcessingException e) { - throw new Error(e); - } - } - public void setSwaggerEnv(SwaggerEnvironment swaggerEnv) { this.swaggerEnv = swaggerEnv; } // 只会在启动流程中调用 - public SchemaMeta getOrCreateProducerSchema( String schemaId, + public SchemaMeta getOrCreateProducerSchema(String schemaId, Class<?> producerClass, Object producerInstance) { MicroserviceMeta microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta(); 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 new file mode 100644 index 0000000..9c4e00a --- /dev/null +++ b/core/src/main/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactory.java @@ -0,0 +1,61 @@ +/* + * 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.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.generator.core.SwaggerGenerator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import io.swagger.models.Swagger; + +@Component +public class StaticSchemaFactory extends AbstractSchemaFactory<SchemaContext> { + + private static final Logger LOGGER = LoggerFactory.getLogger(StaticSchemaFactory.class); + + public void loadSchema(MicroserviceMeta microserviceMeta, StaticMicroservice microservice) { + SchemaContext context = new SchemaContext(); + context.setMicroserviceMeta(microserviceMeta); + // use microservice name as schemaId, since currently we only allow one schema per 3rd party microservice + context.setSchemaId(microserviceMeta.getShortName()); + context.setProviderClass(microservice.getSchemaIntfCls()); + + getOrCreateSchema(context); + } + + @Override + protected SchemaMeta createSchema(SchemaContext context) { + // generate schema according to producer interface + Swagger swagger; + SwaggerGenerator generator = generateSwagger(context); + swagger = generator.getSwagger(); + String swaggerContent = getSwaggerContent(swagger); + LOGGER.info("generate swagger for {}/{}/{}, swagger: {}", + context.getMicroserviceMeta().getAppId(), + context.getMicroserviceName(), + context.getSchemaId(), + swaggerContent); + + // register swagger schema + return schemaLoader.registerSchema(context.getMicroserviceMeta(), context.getSchemaId(), swagger); + } +} 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 new file mode 100644 index 0000000..d9fe118 --- /dev/null +++ b/core/src/test/java/org/apache/servicecomb/core/definition/StaticMicroserviceVersionMetaTest.java @@ -0,0 +1,95 @@ +/* + * 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.core.definition; + +import javax.xml.ws.Holder; + +import org.apache.servicecomb.core.CseContext; +import org.apache.servicecomb.core.definition.classloader.MicroserviceClassLoaderFactory; +import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; +import org.apache.servicecomb.core.definition.schema.StaticSchemaFactory; +import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.api.registry.StaticMicroservice; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +import mockit.Mock; +import mockit.MockUp; + +public class StaticMicroserviceVersionMetaTest { + + private static final String APP_ID_FROM_REGISTRY_UTIL = "appIdFromRegistryUtil"; + + @BeforeClass + public static void beforeClass() { + new MockUp<RegistryUtils>() { + @Mock + String getAppId() { + return APP_ID_FROM_REGISTRY_UTIL; + } + }; + } + + @Test + public void testConstruct() { + StaticMicroservice staticMicroservice = new StaticMicroservice(); + String appId = "testAppId"; + String serviceName = "testServiceName"; + String version = "1.2.1"; + staticMicroservice.setAppId(appId); + staticMicroservice.setServiceName(serviceName); + staticMicroservice.setVersion(version); + + MicroserviceClassLoaderFactory classLoaderFactory = Mockito.mock(MicroserviceClassLoaderFactory.class); + ClassLoader classLoader = Mockito.mock(ClassLoader.class); + Mockito.when(classLoaderFactory.create(appId, serviceName, version)).thenReturn(classLoader); + + Holder<Boolean> schemaLoaded = new Holder<>(false); + CseContext.getInstance().setStaticSchemaFactory(new MockUp<StaticSchemaFactory>() { + @Mock + void loadSchema(MicroserviceMeta microserviceMeta, StaticMicroservice microservice) { + Assert.assertSame(classLoader, microserviceMeta.getClassLoader()); + Assert.assertSame(APP_ID_FROM_REGISTRY_UTIL, microserviceMeta.getAppId()); + Assert.assertSame(serviceName, microserviceMeta.getName()); + Assert.assertSame(serviceName, microserviceMeta.getShortName()); + Assert.assertSame(staticMicroservice, microservice); + schemaLoaded.value = true; + } + }.getMockInstance()); + Holder<Boolean> listenerNotified = new Holder<>(false); + CseContext.getInstance().setSchemaListenerManager(new MockUp<SchemaListenerManager>() { + @Mock + void notifySchemaListener(MicroserviceMeta... microserviceMetas) { + Assert.assertEquals(1, microserviceMetas.length); + MicroserviceMeta microserviceMeta = microserviceMetas[0]; + Assert.assertEquals(serviceName, microserviceMeta.getShortName()); + listenerNotified.value = true; + } + }.getMockInstance()); + + StaticMicroserviceVersionMeta staticMicroserviceVersionMeta = new StaticMicroserviceVersionMeta(staticMicroservice, + classLoaderFactory); + + Assert.assertTrue(schemaLoaded.value); + Assert.assertTrue(listenerNotified.value); + Assert.assertSame(staticMicroservice, staticMicroserviceVersionMeta.getMicroservice()); + Assert.assertEquals(serviceName, staticMicroserviceVersionMeta.getMicroserviceMeta().getName()); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..39b4a1b --- /dev/null +++ b/core/src/test/java/org/apache/servicecomb/core/definition/schema/StaticSchemaFactoryTest.java @@ -0,0 +1,167 @@ +/* + * 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.core.definition.schema; + +import java.lang.reflect.Method; +import java.util.concurrent.CompletableFuture; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +import org.apache.servicecomb.core.definition.MicroserviceMeta; +import org.apache.servicecomb.core.definition.OperationMeta; +import org.apache.servicecomb.core.definition.SchemaMeta; +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.generator.core.CompositeSwaggerGeneratorContext; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class StaticSchemaFactoryTest { + + private static final StaticSchemaFactory staticSchemaFactory = new StaticSchemaFactory(); + + private static final String APPLICATION_ID_KEY = "APPLICATION_ID"; + + private static final String APP_ID_VALUE = "appIdTest"; + + @BeforeClass + public static void beforeClass() { + System.setProperty(APPLICATION_ID_KEY, APP_ID_VALUE); + new UnitTestMeta(); + + CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext(); + ReflectUtils.setField(staticSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext); + + SchemaLoader schemaLoader = new SchemaLoader(); + ReflectUtils.setField(staticSchemaFactory, "schemaLoader", schemaLoader); + } + + @AfterClass + public static void afterClass() { + TestProducerSchemaFactory.teardown(); + System.clearProperty(APPLICATION_ID_KEY); + } + + @Test + public void testLoadSchema() { + String serviceAndSchemaName = "3rdPartyService"; + StaticMicroservice staticMicroservice = new StaticMicroservice(); + staticMicroservice.setSchemaIntfCls(Test3rdPartyServiceIntf.class); + MicroserviceMeta microserviceMeta = new MicroserviceMeta(serviceAndSchemaName); + staticSchemaFactory.loadSchema(microserviceMeta, staticMicroservice); + + SchemaMeta schemaMeta = microserviceMeta.findSchemaMeta(serviceAndSchemaName); + Assert.assertEquals(EXPECTED_SCHEMA_CONTENT, + staticSchemaFactory.getSwaggerContent(schemaMeta.getSwagger())); + + Assert.assertEquals(2, schemaMeta.getOperations().size()); + OperationMeta operationMeta = schemaMeta.ensureFindOperation("add"); + Assert.assertEquals("add", operationMeta.getOperationId()); + Method swaggerProducerMethod = operationMeta.getMethod(); + Class<?>[] parameterTypes = swaggerProducerMethod.getParameterTypes(); + Assert.assertEquals(2, parameterTypes.length); + Assert.assertEquals(Integer.class, parameterTypes[0]); + Assert.assertEquals(Integer.class, parameterTypes[1]); + Assert.assertEquals(Integer.class, swaggerProducerMethod.getGenericReturnType()); + + operationMeta = schemaMeta.ensureFindOperation("addAsync"); + Assert.assertEquals("addAsync", operationMeta.getOperationId()); + swaggerProducerMethod = operationMeta.getMethod(); + parameterTypes = swaggerProducerMethod.getParameterTypes(); + Assert.assertEquals(2, parameterTypes.length); + Assert.assertEquals(Integer.class, parameterTypes[0]); + Assert.assertEquals(Integer.class, parameterTypes[1]); + // ensure reactive operation's return type is set correctly, not CompletableFuture<T> + Assert.assertEquals(Integer.class, swaggerProducerMethod.getGenericReturnType()); + } + + @Path("/3rdParty") + interface Test3rdPartyServiceIntf { + + @Path("/add") + @GET + int add(@QueryParam("x") int x, @QueryParam("y") int y); + + @Path("/addAsync") + @GET + CompletableFuture<Integer> addAsync(@QueryParam("x") int x, @QueryParam("y") int y); + } + + private static final String EXPECTED_SCHEMA_CONTENT = "---\n" + + "swagger: \"2.0\"\n" + + "info:\n" + + " version: \"1.0.0\"\n" + + " title: \"swagger definition for org.apache.servicecomb.core.definition.schema.StaticSchemaFactoryTest$Test3rdPartyServiceIntf\"\n" + + " x-java-interface: \"cse.gen.appIdTest._3rdPartyService._3rdPartyService.Test3rdPartyServiceIntfIntf\"\n" + + "basePath: \"/3rdParty\"\n" + + "consumes:\n" + + "- \"application/json\"\n" + + "produces:\n" + + "- \"application/json\"\n" + + "paths:\n" + + " /add:\n" + + " get:\n" + + " operationId: \"add\"\n" + + " parameters:\n" + + " - name: \"x\"\n" + + " in: \"query\"\n" + + " required: false\n" + + " type: \"integer\"\n" + + " default: 0\n" + + " format: \"int32\"\n" + + " - name: \"y\"\n" + + " in: \"query\"\n" + + " required: false\n" + + " type: \"integer\"\n" + + " default: 0\n" + + " format: \"int32\"\n" + + " responses:\n" + + " 200:\n" + + " description: \"response of 200\"\n" + + " schema:\n" + + " type: \"integer\"\n" + + " format: \"int32\"\n" + + " /addAsync:\n" + + " get:\n" + + " operationId: \"addAsync\"\n" + + " parameters:\n" + + " - name: \"x\"\n" + + " in: \"query\"\n" + + " required: false\n" + + " type: \"integer\"\n" + + " default: 0\n" + + " format: \"int32\"\n" + + " - name: \"y\"\n" + + " in: \"query\"\n" + + " required: false\n" + + " type: \"integer\"\n" + + " default: 0\n" + + " format: \"int32\"\n" + + " responses:\n" + + " 200:\n" + + " description: \"response of 200\"\n" + + " schema:\n" + + " type: \"integer\"\n" + + " format: \"int32\"\n"; +} \ No newline at end of file diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersion.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersion.java index 68ce8c8..b956a66 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersion.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersion.java @@ -35,6 +35,11 @@ public class MicroserviceVersion { this.version = new Version(microservice.getVersion()); } + public MicroserviceVersion(Microservice microservice) { + this.microservice = microservice; + this.version = new Version(microservice.getVersion()); + } + public Microservice getMicroservice() { return microservice; } diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersion.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersion.java index 5b5f23a..02b7ff5 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersion.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersion.java @@ -17,8 +17,9 @@ package org.apache.servicecomb.serviceregistry.consumer; - import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.api.registry.Microservice; +import org.apache.servicecomb.serviceregistry.version.Version; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Rule; @@ -51,4 +52,15 @@ public class TestMicroserviceVersion { Assert.assertEquals("1", microserviceVersion.getMicroservice().getServiceId()); Assert.assertEquals("1.0.0", microserviceVersion.getVersion().getVersion()); } + + @Test + public void constructByMicroservice() { + Microservice microservice = new Microservice(); + String version = "1.2.1"; + microservice.setVersion(version); + MicroserviceVersion microserviceVersion = new MicroserviceVersion(microservice); + + Assert.assertSame(microservice, microserviceVersion.getMicroservice()); + Assert.assertEquals(new Version(version), microserviceVersion.getVersion()); + } }
