From 90c90b25b0da0f4a7e70fecb1f6f79ca5296feaf Mon Sep 17 00:00:00 2001 From: Yichao Yang <1048262223@qq.com> Date: Mon, 13 Jul 2020 08:48:59 +0800 Subject: [PATCH] [Hotfix-3131][api] Fix the new tenant already exists prompt (#3132) * Bugfix: Fix the new tenant already exists prompt * Feature: Add test cases * Update TenantServiceTest.java Co-authored-by: dailidong Co-authored-by: qiaozhanwei --- .../dolphinscheduler/api/enums/Status.java | 2 +- .../api/service/TenantService.java | 6 ++-- .../api/controller/TenantControllerTest.java | 16 ++++++++++ .../api/service/TenantServiceTest.java | 30 ++++++++++++------- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java index 49dac8711a..9bb8e65e4b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java @@ -35,7 +35,7 @@ public enum Status { USER_NAME_NULL(10004,"user name is null", "用户名不能为空"), HDFS_OPERATION_ERROR(10006, "hdfs operation error", "hdfs操作错误"), TASK_INSTANCE_NOT_FOUND(10008, "task instance not found", "任务实例不存在"), - TENANT_NAME_EXIST(10009, "tenant code already exists", "租户编码不能为空"), + TENANT_NAME_EXIST(10009, "tenant code {0} already exists", "租户编码[{0}]已存在"), USER_NOT_EXIST(10010, "user {0} not exists", "用户[{0}]不存在"), ALERT_GROUP_NOT_EXIST(10011, "alarm group not found", "告警组不存在"), ALERT_GROUP_EXIST(10012, "alarm group already exists", "告警组名称已存在"), diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java index 2fded4d32f..a78c951d34 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java @@ -327,11 +327,11 @@ public class TenantService extends BaseService{ * @return true if tenant code can user, otherwise return false */ public Result verifyTenantCode(String tenantCode) { - Result result=new Result(); + Result result = new Result(); if (checkTenantExists(tenantCode)) { logger.error("tenant {} has exist, can't create again.", tenantCode); - putMsg(result, Status.TENANT_NAME_EXIST); - }else{ + putMsg(result, Status.TENANT_NAME_EXIST, tenantCode); + } else { putMsg(result, Status.SUCCESS); } return result; diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TenantControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TenantControllerTest.java index b3beacfb01..60516b5a83 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TenantControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/TenantControllerTest.java @@ -120,7 +120,23 @@ public class TenantControllerTest extends AbstractControllerTest{ } + @Test + public void testVerifyTenantCodeExists() throws Exception { + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("tenantCode", "tenantCode"); + MvcResult mvcResult = mockMvc.perform(get("/tenant/verify-tenant-code") + .header(SESSION_ID, sessionId) + .params(paramsMap)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andReturn(); + + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + Assert.assertEquals(Status.TENANT_NAME_EXIST.getCode(), result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + + } @Test public void testQueryTenantlist() throws Exception { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java index d6fb6b219c..f7f506b69b 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java @@ -16,8 +16,11 @@ */ package org.apache.dolphinscheduler.api.service; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -41,10 +44,10 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.i18n.LocaleContextHolder; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @RunWith(MockitoJUnitRunner.class) public class TenantServiceTest { @@ -61,8 +64,8 @@ public class TenantServiceTest { @Mock private UserMapper userMapper; - private String tenantCode ="TenantServiceTest"; - private String tenantName ="TenantServiceTest"; + private String tenantCode = "TenantServiceTest"; + private String tenantName = "TenantServiceTest"; @Test @@ -85,6 +88,7 @@ public class TenantServiceTest { result = tenantService.createTenant(loginUser, "test", "test", 1, "TenantServiceTest"); logger.info(result.toString()); Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + } catch (Exception e) { logger.error("create tenant error",e); Assert.assertTrue(false); @@ -192,11 +196,17 @@ public class TenantServiceTest { // tenantCode not exist Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf"); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg()); + Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); // tenantCode exist result = tenantService.verifyTenantCode(getTenant().getTenantCode()); + String resultString; + if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())) { + resultString = "租户编码[TenantServiceTest]已存在"; + } else { + resultString = "tenant code TenantServiceTest already exists"; + } logger.info(result.toString()); - Assert.assertEquals(Status.TENANT_NAME_EXIST.getMsg(),result.getMsg()); + Assert.assertEquals(resultString, result.getMsg()); } @@ -261,4 +271,4 @@ public class TenantServiceTest { } -} \ No newline at end of file +}