You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/05/20 01:38:11 UTC

[GitHub] [servicecomb-java-chassis] liubao68 commented on a change in pull request #1766: [SCB-1922] implemented core logic of zero-config registry

liubao68 commented on a change in pull request #1766:
URL: https://github.com/apache/servicecomb-java-chassis/pull/1766#discussion_r427693412



##########
File path: service-registry/registry-zero-config/src/test/java/org/apache/servicecomb/serviceregistry/client/TestZeroConfigRegistryClientImpl.java
##########
@@ -0,0 +1,388 @@
+/*
+ * 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;
+
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstanceStatus;
+import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
+import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
+import org.apache.servicecomb.serviceregistry.server.ServerMicroserviceInstance;
+import org.mockito.Mock;
+
+import static org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.INSTANCE_ID;
+import static org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.SCHEMA_CONTENT_ENDPOINT_BASE_PATH;
+import static org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.SCHEMA_CONTENT_ENDPOINT_QUERY_KEYWORD;
+import static org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.SCHEMA_CONTENT_ENDPOINT_SUBPATH;
+import static org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.INSTANCE_HEARTBEAT_RESPONSE_MESSAGE_OK;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.server.ZeroConfigRegistryService;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.IOException;
+import java.net.MulticastSocket;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+public class TestZeroConfigRegistryClientImpl {
+
+    ZeroConfigRegistryClientImpl target;
+
+    @Mock
+    ZeroConfigRegistryService zeroConfigRegistryService;
+
+    @Mock
+    MulticastSocket multicastSocket;
+
+    @Mock
+    RestTemplate restTemplate;
+
+    // testing data
+    String selfServiceId = "123";
+    String selfInstanceId = "instanceId";
+    String otherServiceId = "456";
+    String appId = "appId";
+    String serviceName = "serviceName";
+    String version = "0.0.0.1";
+    String status = "UP";
+    String host = "host";
+    String schemaId1 = "schemaId1";
+    String schemaContent1 = "schemaContent1";
+    String newSchemaId = "newSchemaId";
+    String newSchemaContent = "newSchemaContent";
+    String nonExistSchemaId = "nonExistSchemaId";
+    String endpoint1 = "endpoint1";
+    String strVersionRule = "0.0.0.0+";
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        target = new ZeroConfigRegistryClientImpl(zeroConfigRegistryService, multicastSocket, restTemplate);
+    }
+
+    private Microservice prepareService(String serviceId, boolean withId){
+        Microservice microservice = new Microservice();
+        if (withId){
+            microservice.setServiceId(serviceId);
+        }
+        microservice.setServiceName(serviceName);
+        microservice.setAppId(appId);
+        microservice.setVersion(version);
+        microservice.setStatus(status);
+        List<String> schemas = new ArrayList<>();
+        schemas.add(schemaId1);
+        microservice.setSchemas(schemas);
+        microservice.addSchema(schemaId1, schemaContent1);
+        return microservice;
+    }
+
+    private MicroserviceInstance prepareInstance(String instanceId, boolean withId){
+        MicroserviceInstance instance = new MicroserviceInstance();
+        if (withId){
+            instance.setInstanceId(instanceId);
+        }
+        instance.setServiceId(selfServiceId);
+        List<String> endpointList = new ArrayList<>();
+        endpointList.add(endpoint1);
+        instance.setEndpoints(endpointList);
+        instance.setStatus(MicroserviceInstanceStatus.UP);
+        instance.setHostName(host);
+        return instance;
+    }
+
+    private ServerMicroserviceInstance prepareServerServiceInstance(boolean withEndpoint){
+        ServerMicroserviceInstance serverServiceInstance = new ServerMicroserviceInstance();
+        serverServiceInstance.setServiceId(otherServiceId);
+        serverServiceInstance.setInstanceId(selfInstanceId);
+        serverServiceInstance.setServiceName(serviceName);
+        serverServiceInstance.setAppId(appId);
+        serverServiceInstance.setVersion(version);
+        serverServiceInstance.setStatus(status);
+        List<String> schemas = new ArrayList<>();
+        schemas.add(schemaId1);
+        serverServiceInstance.setSchemas(schemas);
+        if (withEndpoint) {
+            List<String> endpointList = new ArrayList<>();
+            endpointList.add(endpoint1);
+            serverServiceInstance.setEndpoints(endpointList);
+        }
+        serverServiceInstance.setHostName(host);
+        return serverServiceInstance;
+    }
+
+    @After
+    public void tearDown() throws Exception {
+
+    }
+
+    @Test
+    public void test_registerMicroservice_withID_shouldReturnSameID(){
+        Microservice selfService = prepareService(selfServiceId, true);
+        ClientUtil.microserviceSelf = selfService;
+
+        String returnedServiceId = target.registerMicroservice(selfService);
+
+        Assert.assertEquals(selfServiceId, returnedServiceId);
+    }
+
+    @Test
+    public void test_registerMicroservice_withoutID_shouldReturnGeneratedID(){
+        Microservice serviceWithoutID = prepareService(selfServiceId, false);
+        ClientUtil.microserviceSelf = serviceWithoutID;
+
+        String returnedServiceId = target.registerMicroservice(serviceWithoutID);
+
+        Assert.assertEquals(ClientUtil.generateServiceId(serviceWithoutID), returnedServiceId);
+    }
+
+    @Test
+    public void test_getMicroservice_forItself_shouldReturnItself_And_NotCallZeroConfigRegistryService (){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);;
+
+        Microservice microservice = target.getMicroservice(selfServiceId);
+
+        Assert.assertEquals(microservice.getServiceId(), ClientUtil.microserviceSelf.getServiceId());
+        verifyZeroInteractions(zeroConfigRegistryService);
+    }
+
+    @Test
+    public void test_getMicroservice_forItself_shouldReturnOtherService_And_CallZeroConfigRegistryService (){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);
+
+        when(zeroConfigRegistryService.getMicroservice(otherServiceId)).thenReturn(prepareServerServiceInstance(true));
+
+        Microservice returnedMicroservice = target.getMicroservice(otherServiceId);
+
+        Assert.assertEquals(otherServiceId, returnedMicroservice.getServiceId());
+        verify(zeroConfigRegistryService, times(1)).getMicroservice(otherServiceId);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void test_isSchemaExist_whenServiceIdIsNull_shouldThrowIllegalArgumentException(){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, false);
+
+        target.isSchemaExist(selfServiceId, schemaId1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void test_isSchemaExist_whenServiceIdIsNotItselfId_shouldThrowIllegalArgumentException(){
+        ClientUtil.microserviceSelf = prepareService(otherServiceId, true);
+
+        target.isSchemaExist(selfServiceId, schemaId1);
+    }
+
+    @Test
+    public void test_isSchemaExist_whenServiceIdIsItselfId_shouldReturnTrue(){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);
+
+        boolean returnedResult = target.isSchemaExist(selfServiceId, schemaId1);
+        Assert.assertTrue(returnedResult);
+    }
+
+    @Test
+    public void test_isSchemaExist_whenSchemaNotExist_shouldReturnFalse(){
+        ClientUtil.microserviceSelf =  prepareService(selfServiceId, true);
+
+        boolean returnedResult = target.isSchemaExist(selfServiceId, nonExistSchemaId);
+        Assert.assertFalse(returnedResult);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void test_registerSchema_whenServiceIdIsNull_shouldThrowIllegalArgumentException(){
+        ClientUtil.microserviceSelf =  prepareService(selfServiceId, false);
+
+        target.registerSchema(selfServiceId, schemaId1, schemaContent1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void test_registerSchema_whenServiceIdIsNotItSelfId_shouldThrowIllegalArgumentException(){
+        ClientUtil.microserviceSelf =  prepareService(selfServiceId, true);
+
+        target.registerSchema(otherServiceId, schemaId1, schemaContent1);
+    }
+
+    @Test
+    public void test_registerSchema_whenReigsterForItself_shouldSuceed(){
+        ClientUtil.microserviceSelf =  prepareService(selfServiceId, true);
+
+        boolean returnedResult = target.registerSchema(selfServiceId, newSchemaId, newSchemaContent);
+        Assert.assertTrue(returnedResult);
+        Assert.assertEquals(newSchemaContent, ClientUtil.microserviceSelf.getSchemaMap().computeIfPresent(newSchemaId, (k, v) -> {
+            return v;
+        }));
+    }
+
+    @Test
+    public void test_getSchema_whenForSelfMicroservice_shouldSuceed(){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);
+
+        String returnedSchemaContent = target.getSchema(selfServiceId, schemaId1);
+        Assert.assertEquals(schemaContent1, returnedSchemaContent);
+    }
+
+    @Test
+    public void test_getSchema_whenForSelfMicroservice_shouldNotCallZeroConfigRegistryServiceAndSucceed(){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);
+
+        String returnedSchemaContent = target.getSchema(selfServiceId, schemaId1);
+
+        Assert.assertEquals(schemaContent1, returnedSchemaContent);
+        verifyZeroInteractions(zeroConfigRegistryService);
+    }
+
+    @Test
+    public void test_getSchema_whenForOtherMicroservice_shouldCallZeroConfigRegistryService(){
+        ClientUtil.microserviceSelf = prepareService(selfServiceId, true);
+        when(zeroConfigRegistryService.getMicroservice(otherServiceId)).thenReturn(prepareServerServiceInstance(true));
+        String schemaContentEndpoint = endpoint1 + SCHEMA_CONTENT_ENDPOINT_BASE_PATH + SCHEMA_CONTENT_ENDPOINT_SUBPATH + "?" + SCHEMA_CONTENT_ENDPOINT_QUERY_KEYWORD + "=" + schemaId1;

Review comment:
       This line is too long. use etc/*xml code template to format files. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org