From c9f58ea31a25bbdcc7272f6e4961505304d21393 Mon Sep 17 00:00:00 2001 From: BurningCN <1015773611@qq.com> Date: Thu, 23 Sep 2021 23:11:03 +0800 Subject: [PATCH 1/3] Add unit test for StandardMetadataServiceURLBuilder --- .../metadata/MetadataServiceURLBuilder.java | 2 +- .../MetadataServiceURLBuilderTest.java | 4 +- ...StandardMetadataServiceURLBuilderTest.java | 53 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilder.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilder.java index 7f77bdcfc9f..8d1aa8aabbc 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilder.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilder.java @@ -34,7 +34,7 @@ public interface MetadataServiceURLBuilder { * Build the {@link URL URLs} from the specified {@link ServiceInstance} * * @param serviceInstance {@link ServiceInstance} - * @return TODO, usually, we generate one metadata url from one instance. There's no scenario to return a metadta url list. + * @return TODO, usually, we generate one metadata url from one instance. There's no scenario to return a metadata url list. */ List build(ServiceInstance serviceInstance); } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java index c8032b8b458..5fe36c31e72 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java @@ -27,11 +27,11 @@ */ public class MetadataServiceURLBuilderTest { - static ServiceInstance serviceInstance = new DefaultServiceInstance("127.0.0.1", "test", 8080, ApplicationModel.defaultModel()); + static ServiceInstance serviceInstance = new DefaultServiceInstance("test", "127.0.0.1", 8080, ApplicationModel.defaultModel()); static { serviceInstance.getMetadata().put("dubbo.metadata-service.urls", "[ \"dubbo://192.168.0.102:20881/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=spring-cloud-alibaba-dubbo-provider&bind.ip=192.168.0.102&bind.port=20881&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=spring-cloud-alibaba-dubbo-provider&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=17134&qos.enable=false®ister=true&release=2.7.3&revision=1.0.0&side=provider×tamp=1564826098503&version=1.0.0\" ]"); - serviceInstance.getMetadata().put("dubbo.metadata-service.url-params", "{\"dubbo\":{\"application\":\"dubbo-provider-demo\",\"deprecated\":\"false\",\"group\":\"dubbo-provider-demo\",\"version\":\"1.0.0\",\"timestamp\":\"1564845042651\",\"dubbo\":\"2.0.2\",\"provider.host\":\"192.168.0.102\",\"provider.port\":\"20880\"}}"); + serviceInstance.getMetadata().put("dubbo.metadata-service.url-params", "{\"application\":\"dubbo-provider-demo\",\"protocol\":\"dubbo\",\"group\":\"dubbo-provider-demo\",\"version\":\"1.0.0\",\"timestamp\":\"1564845042651\",\"dubbo\":\"2.0.2\",\"host\":\"192.168.0.102\",\"port\":\"20880\"}"); } // @Test diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java new file mode 100644 index 00000000000..12522d7f52b --- /dev/null +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java @@ -0,0 +1,53 @@ +/* + * 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.dubbo.registry.client.metadata; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.registry.client.DefaultServiceInstance; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.junit.jupiter.api.Test; +import java.util.List; + +import static org.apache.dubbo.registry.client.metadata.MetadataServiceURLBuilderTest.serviceInstance; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.spy; + +/** + * {@link StandardMetadataServiceURLBuilder} Test + */ +public class StandardMetadataServiceURLBuilderTest { + + private StandardMetadataServiceURLBuilder builder = new StandardMetadataServiceURLBuilder(); + + @Test + public void testBuild() { + ApplicationConfig applicationConfig = new ApplicationConfig(); + applicationConfig.setMetadataServicePort(7001); + ApplicationModel applicationModel = spy(ApplicationModel.defaultModel()); + applicationModel.getApplicationConfigManager().setApplication(applicationConfig); + + List urls = builder.build(new DefaultServiceInstance("test", "127.0.0.1", 8080, ApplicationModel.defaultModel())); + assertEquals(1, urls.size()); + assertEquals(urls.get(0).toString(), "dubbo://127.0.0.1:7001/org.apache.dubbo.metadata.MetadataService?getAndListenInstanceMetadata.1.callback=true&group=test&reconnect=false&side=consumer&timeout=5000&version=1.0.0"); + + urls = builder.build(serviceInstance); + assertEquals(1, urls.size()); + assertEquals(urls.get(0).toString(), "dubbo://127.0.0.1:20880/org.apache.dubbo.metadata.MetadataService?application=dubbo-provider-demo&dubbo=2.0.2&group=test&host=192.168.0.102&port=20880&protocol=dubbo&side=consumer&timeout=5000×tamp=1564845042651&version=1.0.0"); + } + +} From f30bbeb153255e188819eb4ad5ca2276512094fc Mon Sep 17 00:00:00 2001 From: BurningCN <1015773611@qq.com> Date: Fri, 24 Sep 2021 10:38:11 +0800 Subject: [PATCH 2/3] FIX UT --- .../MetadataServiceURLBuilderTest.java | 8 +----- ...StandardMetadataServiceURLBuilderTest.java | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java index 5fe36c31e72..1dd2d98eb76 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLBuilderTest.java @@ -31,13 +31,7 @@ public class MetadataServiceURLBuilderTest { static { serviceInstance.getMetadata().put("dubbo.metadata-service.urls", "[ \"dubbo://192.168.0.102:20881/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=spring-cloud-alibaba-dubbo-provider&bind.ip=192.168.0.102&bind.port=20881&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&group=spring-cloud-alibaba-dubbo-provider&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=17134&qos.enable=false®ister=true&release=2.7.3&revision=1.0.0&side=provider×tamp=1564826098503&version=1.0.0\" ]"); - serviceInstance.getMetadata().put("dubbo.metadata-service.url-params", "{\"application\":\"dubbo-provider-demo\",\"protocol\":\"dubbo\",\"group\":\"dubbo-provider-demo\",\"version\":\"1.0.0\",\"timestamp\":\"1564845042651\",\"dubbo\":\"2.0.2\",\"host\":\"192.168.0.102\",\"port\":\"20880\"}"); + serviceInstance.getMetadata().put("dubbo.metadata-service.url-params", "{\"application\":\"dubbo-provider-demo\",\"protocol\":\"rest\",\"group\":\"dubbo-provider-demo\",\"version\":\"1.0.0\",\"timestamp\":\"1564845042651\",\"dubbo\":\"2.0.2\",\"host\":\"192.168.0.102\",\"port\":\"20880\"}"); } -// @Test -// public void testBuild() { -// assertTrue(composite().build(null).isEmpty()); -// assertTrue(composite().build(new DefaultServiceInstance("127.0.0.1", "test", 8080)).isEmpty()); -// assertEquals(2, composite().build(serviceInstance).size()); -// } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java index 12522d7f52b..89470a3a343 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.registry.client.DefaultServiceInstance; import org.apache.dubbo.rpc.model.ApplicationModel; import org.junit.jupiter.api.Test; @@ -41,13 +42,34 @@ public void testBuild() { ApplicationModel applicationModel = spy(ApplicationModel.defaultModel()); applicationModel.getApplicationConfigManager().setApplication(applicationConfig); + // test generateUrlWithoutMetadata List urls = builder.build(new DefaultServiceInstance("test", "127.0.0.1", 8080, ApplicationModel.defaultModel())); assertEquals(1, urls.size()); - assertEquals(urls.get(0).toString(), "dubbo://127.0.0.1:7001/org.apache.dubbo.metadata.MetadataService?getAndListenInstanceMetadata.1.callback=true&group=test&reconnect=false&side=consumer&timeout=5000&version=1.0.0"); + URL url = urls.get(0); + assertEquals(url.getProtocol(), "dubbo"); + assertEquals(url.getHost(), "127.0.0.1"); + assertEquals(url.getPort(), 7001); + assertEquals(url.getServiceInterface(), MetadataService.class.getName()); + assertEquals(url.getGroup(), "test"); + assertEquals(url.getSide(), "consumer"); + assertEquals(url.getVersion(), "1.0.0"); + assertEquals(url.getParameters().get("getAndListenInstanceMetadata.1.callback"), "true"); + assertEquals(url.getParameters().get("reconnect"), "false"); + assertEquals(url.getParameters().get("timeout"), "5000"); + // test generateWithMetadata urls = builder.build(serviceInstance); assertEquals(1, urls.size()); - assertEquals(urls.get(0).toString(), "dubbo://127.0.0.1:20880/org.apache.dubbo.metadata.MetadataService?application=dubbo-provider-demo&dubbo=2.0.2&group=test&host=192.168.0.102&port=20880&protocol=dubbo&side=consumer&timeout=5000×tamp=1564845042651&version=1.0.0"); + url = urls.get(0); + assertEquals(url.getProtocol(), "rest"); + assertEquals(url.getHost(), "127.0.0.1"); + assertEquals(url.getPort(), 20880); + assertEquals(url.getServiceInterface(), MetadataService.class.getName()); + assertEquals(url.getGroup(), "test"); + assertEquals(url.getSide(), "consumer"); + assertEquals(url.getVersion(), "1.0.0"); + assertEquals(url.getApplication(), "dubbo-provider-demo"); + assertEquals(url.getParameters().get("timeout"), "5000"); } } From d84f22994e6f02e19ad8cc629bae3ad6d2ef33e0 Mon Sep 17 00:00:00 2001 From: BurningCN <1015773611@qq.com> Date: Fri, 24 Sep 2021 23:16:02 +0800 Subject: [PATCH 3/3] ApplicationModel reset --- ...StandardMetadataServiceURLBuilderTest.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java index 89470a3a343..22e5832333c 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/metadata/StandardMetadataServiceURLBuilderTest.java @@ -21,6 +21,8 @@ import org.apache.dubbo.metadata.MetadataService; import org.apache.dubbo.registry.client.DefaultServiceInstance; import org.apache.dubbo.rpc.model.ApplicationModel; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.List; @@ -35,13 +37,21 @@ public class StandardMetadataServiceURLBuilderTest { private StandardMetadataServiceURLBuilder builder = new StandardMetadataServiceURLBuilder(); - @Test - public void testBuild() { - ApplicationConfig applicationConfig = new ApplicationConfig(); + + @BeforeAll + public static void setUp() { + ApplicationConfig applicationConfig = new ApplicationConfig("demo"); applicationConfig.setMetadataServicePort(7001); - ApplicationModel applicationModel = spy(ApplicationModel.defaultModel()); - applicationModel.getApplicationConfigManager().setApplication(applicationConfig); + ApplicationModel.defaultModel().getApplicationConfigManager().setApplication(applicationConfig); + } + @AfterAll + public static void clearUp() { + ApplicationModel.reset(); + } + + @Test + public void testBuild() { // test generateUrlWithoutMetadata List urls = builder.build(new DefaultServiceInstance("test", "127.0.0.1", 8080, ApplicationModel.defaultModel())); assertEquals(1, urls.size());