This is an automated email from the ASF dual-hosted git repository.
zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new b9f98279c feat:add TagController and TagRelationController unit test
(#4188)
b9f98279c is described below
commit b9f98279c6e0de2fd214071f678191b31490d48f
Author: mahaitao <[email protected]>
AuthorDate: Mon Nov 14 19:18:28 2022 +0800
feat:add TagController and TagRelationController unit test (#4188)
* feat:add TagController and TagRelationController unit test
* feat:fix checkstyle violations
* feat:remove unused package
Co-authored-by: mahaitao617 <[email protected]>
---
.../shenyu/admin/controller/TagController.java | 14 +-
.../admin/controller/TagRelationController.java | 10 +-
.../shenyu/admin/controller/TagControllerTest.java | 177 +++++++++++++++++++++
.../controller/TagRelationControllerTest.java | 130 +++++++++++++++
4 files changed, 319 insertions(+), 12 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagController.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagController.java
index 2a3b222e3..6ef5c2d53 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagController.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagController.java
@@ -23,7 +23,7 @@ import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
-import org.apache.shenyu.admin.mapper.RuleMapper;
+import org.apache.shenyu.admin.mapper.TagMapper;
import org.apache.shenyu.admin.model.dto.TagDTO;
import org.apache.shenyu.admin.model.result.ShenyuAdminResult;
import org.apache.shenyu.admin.model.vo.TagVO;
@@ -69,7 +69,7 @@ public class TagController {
*/
@GetMapping("/queryRootTag")
public ShenyuAdminResult queryRootTag() {
- return ShenyuAdminResult.success(ShenyuResultMessage.CREATE_SUCCESS,
tagService.findByParentTagId("0"));
+ return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS,
tagService.findByParentTagId("0"));
}
/**
@@ -78,9 +78,9 @@ public class TagController {
* @param id tag name.
* @return {@linkplain ShenyuAdminResult}
*/
- @GetMapping("/name/{name}")
- public ShenyuAdminResult detailTag(@PathVariable("id") @Valid
- @Existed(provider = RuleMapper.class,
+ @GetMapping("/id/{id}")
+ public ShenyuAdminResult queryById(@PathVariable("id") @Valid
+ @Existed(provider = TagMapper.class,
message = "tag is not
existed") final String id) {
TagVO tagVO = tagService.findById(id);
return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS,
tagVO);
@@ -104,7 +104,7 @@ public class TagController {
* @param name tag name.
* @return {@linkplain ShenyuAdminResult}
*/
- @GetMapping("/id/{id}")
+ @GetMapping("/name/{name}")
public ShenyuAdminResult queryByName(@PathVariable("name") @Valid final
String name) {
List<TagVO> tagVO = tagService.findByQuery(name);
return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS,
tagVO);
@@ -131,7 +131,7 @@ public class TagController {
* @param ids primary key.
* @return {@linkplain ShenyuAdminResult}
*/
- @DeleteMapping("/batch")
+ @DeleteMapping("/batchDelete")
public ShenyuAdminResult deleteTags(@RequestBody @NotEmpty final
List<@NotBlank String> ids) {
Integer deleteCount = tagService.delete(ids);
return ShenyuAdminResult.success(ShenyuResultMessage.DELETE_SUCCESS,
deleteCount);
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagRelationController.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagRelationController.java
index 7869ee28d..588b2bf47 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagRelationController.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/TagRelationController.java
@@ -54,10 +54,10 @@ public class TagRelationController {
* @param tagId tag tagId.
* @return {@linkplain ShenyuAdminResult}
*/
- @GetMapping("/{tagId}")
+ @GetMapping("/tagId/{tagId}")
public ShenyuAdminResult queryApiByTagId(@PathVariable("tagId") @Valid
final String tagId) {
- List<TagRelationDO> tagRelationVOs =
Optional.ofNullable(tagRelationService.findByTagId(tagId)).orElse(Lists.newArrayList());
- return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS,
tagRelationVOs);
+ List<TagRelationDO> tagRelationDOS =
Optional.ofNullable(tagRelationService.findByTagId(tagId)).orElse(Lists.newArrayList());
+ return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS,
tagRelationDOS);
}
/**
@@ -67,7 +67,7 @@ public class TagRelationController {
* @param tagRelationDTO tagRelationDTO.
* @return {@linkplain ShenyuAdminResult}
*/
- @PutMapping("/{id}")
+ @PutMapping("/id/{id}")
public ShenyuAdminResult updateTagRelation(@PathVariable("id") @Valid
final String id,
@Valid @RequestBody final
TagRelationDTO tagRelationDTO) {
tagRelationDTO.setId(id);
@@ -81,7 +81,7 @@ public class TagRelationController {
* @param ids primary key.
* @return {@linkplain ShenyuAdminResult}
*/
- @DeleteMapping("/batch")
+ @DeleteMapping("/batchDelete")
public ShenyuAdminResult deleteTagRelation(@RequestBody @NotEmpty final
List<@NotBlank String> ids) {
Integer deleteCount = tagRelationService.delete(ids);
return ShenyuAdminResult.success(ShenyuResultMessage.DELETE_SUCCESS,
deleteCount);
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagControllerTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagControllerTest.java
new file mode 100644
index 000000000..bca79e453
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagControllerTest.java
@@ -0,0 +1,177 @@
+/*
+ * 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.shenyu.admin.controller;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import org.apache.shenyu.admin.exception.ExceptionHandlers;
+import org.apache.shenyu.admin.model.dto.TagDTO;
+import org.apache.shenyu.admin.model.vo.TagVO;
+import org.apache.shenyu.admin.service.TagService;
+import org.apache.shenyu.admin.utils.ShenyuResultMessage;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.hamcrest.core.Is.is;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.BDDMockito.given;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * Test cases for {@link TagController}.
+ */
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+public final class TagControllerTest {
+
+ private MockMvc mockMvc;
+
+ @InjectMocks
+ private TagController tagController;
+
+ @Mock
+ private TagService tagService;
+
+ @BeforeEach
+ public void setUp() {
+ this.mockMvc = MockMvcBuilders.standaloneSetup(tagController)
+ .setControllerAdvice(new ExceptionHandlers())
+ .build();
+ }
+
+ @Test
+ public void testCreateTag() throws Exception {
+ TagDTO tagDTO = buildTagDTO();
+ given(tagService.create(tagDTO)).willReturn(1);
+ this.mockMvc.perform(MockMvcRequestBuilders.post("/tag")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(GsonUtils.getInstance().toJson(tagDTO)))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.CREATE_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testQueryRootTag() throws Exception {
+ List<TagVO> tagVOS = new ArrayList<>();
+ given(tagService.findByParentTagId("0")).willReturn(tagVOS);
+ this.mockMvc.perform(MockMvcRequestBuilders.get("/tag/queryRootTag"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.QUERY_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testqueryById() throws Exception {
+ given(tagService.findById("123")).willReturn(buildTagVO());
+ this.mockMvc.perform(MockMvcRequestBuilders.get("/tag/id/{id}", "123"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DETAIL_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testQueryListByParentTagId() throws Exception {
+ List<TagVO> list = new ArrayList<>();
+ list.add(buildTagVO());
+ given(tagService.findByParentTagId(anyString())).willReturn(list);
+
this.mockMvc.perform(MockMvcRequestBuilders.get("/tag/parentTagId/{parentTagId}",
"123"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DETAIL_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testQueryByName() throws Exception {
+ List<TagVO> list = new ArrayList<>();
+ list.add(buildTagVO());
+ given(tagService.findByQuery(anyString())).willReturn(list);
+ this.mockMvc.perform(MockMvcRequestBuilders.get("/tag/name/{name}",
"123"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DETAIL_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testUpdateTag() throws Exception {
+ TagDTO tagDTO = buildTagDTO();
+ given(tagService.update(tagDTO)).willReturn(1);
+ this.mockMvc.perform(MockMvcRequestBuilders.put("/tag/id/{id}", "123")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param("id", "123")
+ .content(GsonUtils.getInstance().toJson(tagDTO)))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.UPDATE_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testDeleteTags() throws Exception {
+ List<String> ids = new ArrayList<>();
+ ids.add("123");
+ ids.add("456");
+ given(tagService.delete(ids)).willReturn(ids.size());
+ this.mockMvc.perform(MockMvcRequestBuilders.delete("/tag/batchDelete")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(GsonUtils.getInstance().toJson(ids)))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DELETE_SUCCESS)))
+ .andExpect(jsonPath("$.data", is(2)))
+ .andReturn();
+ }
+
+ /**
+ * buildTagDTO.
+ * @return tagDTO
+ */
+ public TagDTO buildTagDTO() {
+ TagDTO tagDTO = new TagDTO();
+ tagDTO.setTagDesc("this is a tag");
+ tagDTO.setName("test tag");
+ tagDTO.setId(null);
+ tagDTO.setParentTagId("123");
+ return tagDTO;
+ }
+
+ /**
+ * buildTagVO.
+ * @return TagVO
+ */
+ public TagVO buildTagVO() {
+ TagVO tagVO = new TagVO();
+ tagVO.setTagDesc("123");
+ tagVO.setName("test tag");
+ tagVO.setDateCreated(new Date().toString());
+ tagVO.setDateUpdated(new Date().toString());
+ return tagVO;
+ }
+
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagRelationControllerTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagRelationControllerTest.java
new file mode 100644
index 000000000..bd839b727
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/TagRelationControllerTest.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.shenyu.admin.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.shenyu.admin.exception.ExceptionHandlers;
+import org.apache.shenyu.admin.model.dto.TagRelationDTO;
+import org.apache.shenyu.admin.model.entity.TagRelationDO;
+import org.apache.shenyu.admin.service.TagRelationService;
+import org.apache.shenyu.admin.utils.ShenyuResultMessage;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.hamcrest.core.Is.is;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.BDDMockito.given;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * Test cases for {@link TagRelationController}.
+ */
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+public final class TagRelationControllerTest {
+
+ private MockMvc mockMvc;
+
+ @InjectMocks
+ private TagRelationController tagRelationController;
+
+ @Mock
+ private TagRelationService tagRelationService;
+
+ @BeforeEach
+ public void setUp() {
+ this.mockMvc = MockMvcBuilders.standaloneSetup(tagRelationController)
+ .setControllerAdvice(new ExceptionHandlers())
+ .build();
+ }
+
+ @Test
+ public void testQueryApiByTagId() throws Exception {
+ List<TagRelationDO> tagRelationDOS = new ArrayList<>();
+ tagRelationDOS.add(buildTagRelationDO());
+
given(tagRelationService.findByTagId(anyString())).willReturn(tagRelationDOS);
+
this.mockMvc.perform(MockMvcRequestBuilders.get("/tag-relation/tagId/{tagId}",
"123"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DETAIL_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testUpdateTagRelation() throws Exception {
+ TagRelationDTO tagRelationDTO = buildTagRelationDTO();
+ given(tagRelationService.update(any())).willReturn(1);
+ this.mockMvc.perform(MockMvcRequestBuilders.put("/tag-relation/id/123")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param("id", "123")
+
.content(GsonUtils.getInstance().toJson(tagRelationDTO)))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.UPDATE_SUCCESS)))
+ .andReturn();
+ }
+
+ @Test
+ public void testDeleteTagRelation() throws Exception {
+ List<String> ids = new ArrayList<>();
+ ids.add("123");
+ ids.add("456");
+ given(tagRelationService.delete(ids)).willReturn(ids.size());
+
this.mockMvc.perform(MockMvcRequestBuilders.delete("/tag-relation/batchDelete")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(GsonUtils.getInstance().toJson(ids)))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.message",
is(ShenyuResultMessage.DELETE_SUCCESS)))
+ .andExpect(jsonPath("$.data", is(2)))
+ .andReturn();
+ }
+
+ /**
+ * buildTagRelationDO.
+ * @return TagRelationDO
+ */
+ private TagRelationDO buildTagRelationDO() {
+ TagRelationDO tagRelationDO = new TagRelationDO();
+ tagRelationDO.setTagId("123");
+ tagRelationDO.setApiId("124");
+ return tagRelationDO;
+ }
+
+ /**
+ * buildTagRelationDTO.
+ * @return TagRelationDTO
+ */
+ private TagRelationDTO buildTagRelationDTO() {
+ TagRelationDTO tagRelationDTO = new TagRelationDTO();
+ tagRelationDTO.setTagId("123");
+ tagRelationDTO.setApiId("12334");
+ return tagRelationDTO;
+ }
+}