You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by mi...@apache.org on 2019/01/25 02:39:04 UTC
[incubator-dubbo-ops] branch develop updated: Unit test (#274)
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 5b05caf Unit test (#274)
5b05caf is described below
commit 5b05caf4318a7dfa7e8fb067bb9be1bd7a212635
Author: 孙不服 <su...@163.com>
AuthorDate: Fri Jan 25 10:38:59 2019 +0800
Unit test (#274)
* unit test
* add Apache license & remove *
* AccessesController's unit test
---
.../dubbo/admin/controller/AccessesController.java | 2 +-
.../admin/controller/AccessesControllerTest.java | 130 +++++++++++++++++++++
2 files changed, 131 insertions(+), 1 deletion(-)
diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java
index ac350c5..9fde35b 100644
--- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java
+++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java
@@ -89,7 +89,7 @@ public class AccessesController {
throw new ParamValidationException("Either Service or application is required.");
}
String application = accessDTO.getApplication();
- if (StringUtils.isNotEmpty(application) && this.providerService.findVersionInApplication(application).equals("2.6")) {
+ if (StringUtils.isNotEmpty(application) && "2.6".equals(providerService.findVersionInApplication(application))) {
throw new VersionValidationException("dubbo 2.6 does not support application scope blackwhite list config");
}
if (accessDTO.getBlacklist() == null && accessDTO.getWhitelist() == null) {
diff --git a/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/AccessesControllerTest.java b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/AccessesControllerTest.java
new file mode 100644
index 0000000..fc494ba
--- /dev/null
+++ b/dubbo-admin-backend/src/test/java/org/apache/dubbo/admin/controller/AccessesControllerTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.admin.controller;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.dubbo.admin.AbstractSpringIntegrationTest;
+import org.apache.dubbo.admin.model.dto.AccessDTO;
+import org.apache.dubbo.admin.model.dto.ConditionRouteDTO;
+import org.apache.dubbo.admin.service.ProviderService;
+import org.apache.dubbo.admin.service.RouteService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.ResponseEntity;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class AccessesControllerTest extends AbstractSpringIntegrationTest {
+
+ private final String env = "whatever";
+
+ @MockBean
+ private RouteService routeService;
+ @MockBean
+ private ProviderService providerService;
+ @Autowired
+ private ObjectMapper objectMapper;
+
+ @Test
+ public void searchAccess() throws IOException {
+ AccessDTO accessDTO = new AccessDTO();
+ ResponseEntity<String> response;
+ String exceptResponseBody;
+ // application and service is all empty
+ response = restTemplate.getForEntity(url("/api/{env}/rules/access"), String.class, env);
+ Map map = objectMapper.readValue(response.getBody(), Map.class);
+ assertFalse("should return a fail response", (Boolean) map.get("success"));
+
+ // when application is present
+ String application = "applicationName";
+ when(routeService.findAccess(application)).thenReturn(accessDTO);
+ response = restTemplate.getForEntity(url("/api/{env}/rules/access?application={application}"), String.class, env, application);
+ exceptResponseBody = objectMapper.writeValueAsString(Collections.singletonList(accessDTO));
+ assertEquals(exceptResponseBody, response.getBody());
+
+ // when service is present
+ String service = "serviceName";
+ when(routeService.findAccess(service)).thenReturn(accessDTO);
+ response = restTemplate.getForEntity(url("/api/{env}/rules/access?service={service}"), String.class, env, service);
+ exceptResponseBody = objectMapper.writeValueAsString(Collections.singletonList(accessDTO));
+ assertEquals(exceptResponseBody, response.getBody());
+ }
+
+ @Test
+ public void detailAccess() throws JsonProcessingException {
+ String id = "1";
+ AccessDTO accessDTO = new AccessDTO();
+ when(routeService.findAccess(id)).thenReturn(accessDTO);
+ ResponseEntity<String> response = restTemplate.getForEntity(url("/api/{env}/rules/access/{id}"), String.class, env, id);
+ String exceptResponseBody = objectMapper.writeValueAsString(accessDTO);
+ assertEquals(exceptResponseBody, response.getBody());
+ }
+
+ @Test
+ public void deleteAccess() {
+ String id = "1";
+ restTemplate.delete(url("/api/{env}/rules/access/{id}"), env, id);
+ verify(routeService).deleteAccess(id);
+ }
+
+ @Test
+ public void createAccess() {
+ AccessDTO accessDTO = new AccessDTO();
+ String application = "applicationName";
+
+ restTemplate.postForLocation(url("/api/{env}/rules/access"), accessDTO, env);
+ // when application is present & dubbo's version is 2.6
+ accessDTO.setApplication(application);
+ when(providerService.findVersionInApplication(application)).thenReturn("2.6");
+ restTemplate.postForLocation(url("/api/{env}/rules/access"), accessDTO, env);
+ // dubbo's version is 2.7
+ when(providerService.findVersionInApplication(application)).thenReturn("2.7");
+ restTemplate.postForLocation(url("/api/{env}/rules/access"), accessDTO, env);
+ // black white list is not null
+ accessDTO.setBlacklist(new HashSet<>());
+ accessDTO.setWhitelist(new HashSet<>());
+ restTemplate.postForLocation(url("/api/{env}/rules/access"), accessDTO, env);
+ verify(routeService).createAccess(any(AccessDTO.class));
+ }
+
+ @Test
+ public void updateAccess() throws IOException {
+ AccessDTO accessDTO = new AccessDTO();
+ String id = "1";
+ // when id is 'Unknown ID'
+ restTemplate.put(url("/api/{env}/rules/access/{id}"), accessDTO, env, id);
+ verify(routeService).findConditionRoute(id);
+ //
+ ConditionRouteDTO conditionRouteDTO = mock(ConditionRouteDTO.class);
+ when(routeService.findConditionRoute(id)).thenReturn(conditionRouteDTO);
+ restTemplate.put(url("/api/{env}/rules/access/{id}"), accessDTO, env, id);
+ verify(routeService).updateAccess(any(AccessDTO.class));
+ }
+}
\ No newline at end of file