This is an automated email from the ASF dual-hosted git repository.

min pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo-ops.git


The following commit(s) were added to refs/heads/develop by this push:
     new a762a0f  add more integration tests (#267)
a762a0f is described below

commit a762a0f65aecdbb2e9d1a96974c07b898f13c0d5
Author: kezhenxu94 <[email protected]>
AuthorDate: Tue Jan 22 16:48:53 2019 +0800

    add more integration tests (#267)
    
    * add more integration tests
    
    * fix ci failure
---
 .../controller/ConditionRoutesControllerTest.java  | 274 +++++++++++++++++++++
 .../admin/controller/ManagementControllerTest.java |   4 +-
 .../admin/controller/ServiceControllerTest.java    |  17 +-
 3 files changed, 286 insertions(+), 9 deletions(-)

diff --git 
a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ConditionRoutesControllerTest.java
 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ConditionRoutesControllerTest.java
new file mode 100644
index 0000000..5ed756c
--- /dev/null
+++ 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ConditionRoutesControllerTest.java
@@ -0,0 +1,274 @@
+package org.apache.dubbo.admin.controller;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.dubbo.admin.AbstractSpringIntegrationTest;
+import org.apache.dubbo.admin.common.util.YamlParser;
+import org.apache.dubbo.admin.model.dto.ConditionRouteDTO;
+import org.apache.dubbo.admin.model.store.RoutingRule;
+import org.apache.dubbo.admin.service.ProviderService;
+import org.junit.After;
+import org.junit.Test;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+public class ConditionRoutesControllerTest extends 
AbstractSpringIntegrationTest {
+  private final String env = "whatever";
+
+  @MockBean
+  private ProviderService providerService;
+
+  @After
+  public void tearDown() throws Exception {
+    if (zkClient.checkExists().forPath("/dubbo") != null) {
+      zkClient.delete().deletingChildrenIfNeeded().forPath("/dubbo");
+    }
+  }
+
+  @Test
+  public void shouldThrowWhenParamInvalid() {
+    String uuid = UUID.randomUUID().toString();
+
+    ConditionRouteDTO dto = new ConditionRouteDTO();
+    ResponseEntity<String> responseEntity = restTemplate.postForEntity(
+        url("/api/{env}/rules/route/condition"), dto, String.class, env
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.BAD_REQUEST));
+    assertThat(responseEntity.getBody(), containsString("serviceName and app 
is Empty!"));
+
+    dto.setApplication("application" + uuid);
+    
when(providerService.findVersionInApplication(dto.getApplication())).thenReturn("2.6");
+    responseEntity = restTemplate.postForEntity(
+        url("/api/{env}/rules/route/condition"), dto, String.class, env
+    );
+    assertThat(responseEntity.getStatusCode(), 
is(HttpStatus.INTERNAL_SERVER_ERROR));
+    assertThat(responseEntity.getBody(), containsString("dubbo 2.6 does not 
support application scope routing rule"));
+  }
+
+  @Test
+  public void shouldCreateRule() {
+    String uuid = UUID.randomUUID().toString();
+    String application = "application" + uuid;
+    String service = "service" + uuid;
+    List<String> conditions = Collections.singletonList("=> host != 
172.22.3.91");
+
+    ConditionRouteDTO dto = new ConditionRouteDTO();
+    dto.setService(service);
+    dto.setConditions(conditions);
+
+    ResponseEntity<String> responseEntity = restTemplate.postForEntity(
+        url("/api/{env}/rules/route/condition"), dto, String.class, env
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.CREATED));
+
+    dto.setApplication(application);
+    
when(providerService.findVersionInApplication(dto.getApplication())).thenReturn("2.7");
+
+    responseEntity = restTemplate.postForEntity(
+        url("/api/{env}/rules/route/condition"), dto, String.class, env
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.CREATED));
+  }
+
+  @Test
+  public void shouldUpdateRule() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: true\n"
+        + "force: true\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    List<String> newConditions = Arrays.asList("=> host != 172.22.3.211", "=> 
host != 172.22.3.212");
+
+    ConditionRouteDTO dto = new ConditionRouteDTO();
+    dto.setConditions(newConditions);
+    dto.setService(service);
+
+    ResponseEntity<String> responseEntity = restTemplate.exchange(
+        url("/api/{env}/rules/route/condition/{service}"), HttpMethod.PUT,
+        new HttpEntity<>(dto, null), String.class, env, service
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
+
+    byte[] bytes = zkClient.getData().forPath(path);
+    String updatedConfig = new String(bytes);
+    RoutingRule rule = YamlParser.loadObject(updatedConfig, RoutingRule.class);
+    assertThat(rule.getConditions(), 
containsInAnyOrder(newConditions.toArray()));
+  }
+
+  @Test
+  public void shouldGetServiceRule() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: true\n"
+        + "force: true\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    ResponseEntity<List<ConditionRouteDTO>> responseEntity = 
restTemplate.exchange(
+        url("/api/{env}/rules/route/condition/?service={service}"), 
HttpMethod.GET,
+        null, new ParameterizedTypeReference<List<ConditionRouteDTO>>() {
+        }, env, service
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
+    assertThat(responseEntity.getBody(), hasSize(1));
+    List<String> conditions = responseEntity.getBody()
+        .stream()
+        .flatMap(it -> it.getConditions().stream())
+        .collect(Collectors.toList());
+    assertThat(conditions, hasSize(2));
+    assertThat(conditions, containsInAnyOrder("=> host != 172.22.3.111", "=> 
host != 172.22.3.112"));
+  }
+
+  @Test
+  public void shouldDeleteRule() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: true\n"
+        + "force: true\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    assertNotNull("zk path should not be null before deleting", 
zkClient.checkExists().forPath(path));
+
+    ResponseEntity<String> responseEntity = restTemplate.exchange(
+        url("/api/{env}/rules/route/condition/{service}"), HttpMethod.DELETE,
+        null, String.class, env, service
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
+
+    assertNull(zkClient.checkExists().forPath(path));
+  }
+
+  @Test
+  public void shouldThrowWhenDetailRouteWithUnknownId() {
+    ResponseEntity<String> responseEntity = restTemplate.getForEntity(
+        url("/api/{env}/rules/route/condition/{id}"), String.class, env, 
"non-existed-service"
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.NOT_FOUND));
+  }
+
+  @Test
+  public void shouldGetRouteDetail() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: true\n"
+        + "force: true\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    ResponseEntity<ConditionRouteDTO> responseEntity = 
restTemplate.getForEntity(
+        url("/api/{env}/rules/route/condition/{id}"), ConditionRouteDTO.class, 
env, service
+    );
+    assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
+
+    ConditionRouteDTO conditionRouteDTO = responseEntity.getBody();
+    assertNotNull(conditionRouteDTO);
+    assertThat(conditionRouteDTO.getConditions(), hasSize(2));
+    assertThat(conditionRouteDTO.getConditions(), containsInAnyOrder("=> host 
!= 172.22.3.111", "=> host != 172.22.3.112"));
+  }
+
+  @Test
+  public void shouldEnableRoute() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: false\n"
+        + "force: true\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    byte[] bytes = zkClient.getData().forPath(path);
+    String updatedConfig = new String(bytes);
+    RoutingRule rule = YamlParser.loadObject(updatedConfig, RoutingRule.class);
+    assertFalse(rule.isEnabled());
+
+    restTemplate.put(url("/api/{env}/rules/route/condition/enable/{id}"), 
null, env, service);
+
+    bytes = zkClient.getData().forPath(path);
+    updatedConfig = new String(bytes);
+    rule = YamlParser.loadObject(updatedConfig, RoutingRule.class);
+    assertTrue(rule.isEnabled());
+  }
+
+  @Test
+  public void shouldDisableRoute() throws Exception {
+    String service = "org.apache.dubbo.demo.DemoService";
+    String content = "conditions:\n"
+        + "- => host != 172.22.3.111\n"
+        + "- => host != 172.22.3.112\n"
+        + "enabled: true\n"
+        + "force: false\n"
+        + "key: " + service + "\n"
+        + "priority: 0\n"
+        + "runtime: false\n"
+        + "scope: service";
+    String path = "/dubbo/config/" + service + "/condition-router";
+    zkClient.create().creatingParentContainersIfNeeded().forPath(path);
+    zkClient.setData().forPath(path, content.getBytes());
+
+    byte[] bytes = zkClient.getData().forPath(path);
+    String updatedConfig = new String(bytes);
+    RoutingRule rule = YamlParser.loadObject(updatedConfig, RoutingRule.class);
+    assertTrue(rule.isEnabled());
+
+    restTemplate.put(url("/api/{env}/rules/route/condition/disable/{id}"), 
null, env, service);
+
+    bytes = zkClient.getData().forPath(path);
+    updatedConfig = new String(bytes);
+    rule = YamlParser.loadObject(updatedConfig, RoutingRule.class);
+    assertFalse(rule.isEnabled());
+  }
+}
diff --git 
a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ManagementControllerTest.java
 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ManagementControllerTest.java
index 5683df0..f4114ba 100644
--- 
a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ManagementControllerTest.java
+++ 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ManagementControllerTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.dubbo.admin.controller;
 
+import java.util.UUID;
 import org.apache.dubbo.admin.AbstractSpringIntegrationTest;
 import org.apache.dubbo.admin.common.util.Constants;
 import org.apache.dubbo.admin.model.dto.ConfigDTO;
@@ -64,7 +65,8 @@ public class ManagementControllerTest extends 
AbstractSpringIntegrationTest {
 
   @Test
   public void shouldCreateApplicationConfig() throws Exception {
-    String application = "dubbo-admin";
+    String uuid = UUID.randomUUID().toString();
+    String application = "dubbo-admin" + uuid;
     ConfigDTO configDTO = new ConfigDTO();
     configDTO.setKey(application);
     configDTO.setConfig("key1=val1\nkey2=val2");
diff --git 
a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ServiceControllerTest.java
 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ServiceControllerTest.java
index ba5f41b..1079140 100644
--- 
a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ServiceControllerTest.java
+++ 
b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/ServiceControllerTest.java
@@ -17,23 +17,21 @@
 
 package org.apache.dubbo.admin.controller;
 
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
 import org.apache.dubbo.admin.AbstractSpringIntegrationTest;
 import org.apache.dubbo.admin.common.util.Constants;
 import org.apache.dubbo.admin.model.dto.ServiceDTO;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.registry.Registry;
 import org.apache.dubbo.registry.support.AbstractRegistry;
-import org.junit.Before;
+import org.junit.After;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.ParameterizedTypeReference;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
@@ -42,11 +40,14 @@ public class ServiceControllerTest extends 
AbstractSpringIntegrationTest {
   @Autowired
   private Registry registry;
 
-  @Before
-  public void setUp() throws Exception {
+  @After
+  public void tearDown() throws Exception {
     final Set<URL> registered = ((AbstractRegistry) registry).getRegistered();
     for (final URL url : registered) {
-      registry.unregister(url);
+      try {
+        registry.unregister(url);
+      } catch (Exception ignored) {
+      }
     }
     TimeUnit.SECONDS.sleep(1);
   }

Reply via email to