From da768c9519f0dc6c9b5ccd62b06194b5e8f4b357 Mon Sep 17 00:00:00 2001 From: harryqin99 Date: Wed, 22 Sep 2021 21:54:43 +0800 Subject: [PATCH 1/5] Add get User's permission level in a department --- .../controller/PermissionController.java | 15 +++++++++++++++ .../crmserver/service/PermissionService.java | 8 ++++++++ .../service/impl/PermissionServiceImpl.java | 17 +++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/main/java/tech/crm/crmserver/controller/PermissionController.java b/src/main/java/tech/crm/crmserver/controller/PermissionController.java index ee337ad..57b6cbc 100644 --- a/src/main/java/tech/crm/crmserver/controller/PermissionController.java +++ b/src/main/java/tech/crm/crmserver/controller/PermissionController.java @@ -106,5 +106,20 @@ public ResponseResult updatePermission(@RequestParam("department_id") In } return ResponseResult.suc("No pending"); } + + /** + * Get user's permission of a particular department + * @param departmentId the department to get permission + * @return the ResponseResult with msg, with the boolean value about if the there is pending request + */ + @GetMapping("/myPermission") + public ResponseResult getUserPermissisonInADepartment(@RequestParam(value = "department_id") Integer departmentId) { + Integer userId = userService.getId(); + Permission permission = permissionService.getPermissionByUserIdAndDepartmentId(userId, departmentId); + if (permission == null) { + ResponseResult.fail("No permission, not join the department yet"); + } + return ResponseResult.suc("success", permission); + } } diff --git a/src/main/java/tech/crm/crmserver/service/PermissionService.java b/src/main/java/tech/crm/crmserver/service/PermissionService.java index c3597f3..6488b9f 100644 --- a/src/main/java/tech/crm/crmserver/service/PermissionService.java +++ b/src/main/java/tech/crm/crmserver/service/PermissionService.java @@ -79,6 +79,14 @@ public interface PermissionService extends IService { */ public List getPermissionByUserId(Integer userId, PermissionLevel permissionLevel); + /** + * Get a user's permission in a department + * + * @param userId the user id to delete + * @param departmentId the departmentId to delete + */ + public Permission getPermissionByUserIdAndDepartmentId(Integer userId, Integer departmentId); + /** * Get all the permission based on userId * diff --git a/src/main/java/tech/crm/crmserver/service/impl/PermissionServiceImpl.java b/src/main/java/tech/crm/crmserver/service/impl/PermissionServiceImpl.java index 9c09620..245dd7e 100644 --- a/src/main/java/tech/crm/crmserver/service/impl/PermissionServiceImpl.java +++ b/src/main/java/tech/crm/crmserver/service/impl/PermissionServiceImpl.java @@ -287,4 +287,21 @@ public void deleteMember(Integer userId, Integer departmentId) { throw new NotEnoughPermissionException(); } } + + /** + * Get a user's permission in a department + * + * @param userId the user id to delete + * @param departmentId the departmentId to delete + */ + @Override + public Permission getPermissionByUserIdAndDepartmentId(Integer userId, Integer departmentId) { + List permissionByUserId = getPermissionByUserId(userId); + for (Permission permission : permissionByUserId) { + if (permission.getDepartmentId().equals(departmentId)) { + return permission; + } + } + return null; + } } From 05829384aa382208abd4730adfc158b653f7e4ed Mon Sep 17 00:00:00 2001 From: harryqin99 Date: Thu, 23 Sep 2021 01:31:14 +0800 Subject: [PATCH 2/5] Add contactController test --- crm-server.iml | 4 + .../controller/ContactController.java | 2 + .../service/impl/ContactServiceImpl.java | 4 +- src/main/resources/db/data-test.sql | 3 +- .../ContactControllerTest.java | 229 ++++++++++++++++++ .../DepartmentControllerTest.java | 28 ++- 6 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java diff --git a/crm-server.iml b/crm-server.iml index 72b5f9f..0b716bb 100644 --- a/crm-server.iml +++ b/crm-server.iml @@ -25,6 +25,10 @@ + + + + diff --git a/src/main/java/tech/crm/crmserver/controller/ContactController.java b/src/main/java/tech/crm/crmserver/controller/ContactController.java index 73a3be1..df3386f 100644 --- a/src/main/java/tech/crm/crmserver/controller/ContactController.java +++ b/src/main/java/tech/crm/crmserver/controller/ContactController.java @@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import tech.crm.crmserver.common.enums.PermissionLevel; diff --git a/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java b/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java index 50dbcec..9a9288a 100644 --- a/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java +++ b/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java @@ -111,7 +111,7 @@ public Page getContactByOrgIdAndDepartmentId(Page page, Intege QueryWrapper queryWrapper = new QueryWrapper<>(); Page contacts = new Page<>(); - // User加入的departmentId + // User's joined department List permissionList = permissionService.getPermissionByUserId(userId, PermissionLevel.DISPLAY); List departmentIdJoinList = new ArrayList<>(); for (Permission permission : permissionList) { @@ -120,7 +120,7 @@ public Page getContactByOrgIdAndDepartmentId(Page page, Intege if (departmentId == null) { - // Org的departmentId + // Org's department List departmentIdList = departmentService.getDepartmentIdByOrganization(organizationId); List departmentInOrgAndJoin = new ArrayList<>(); diff --git a/src/main/resources/db/data-test.sql b/src/main/resources/db/data-test.sql index 45478ff..ba265db 100644 --- a/src/main/resources/db/data-test.sql +++ b/src/main/resources/db/data-test.sql @@ -53,4 +53,5 @@ INSERT INTO `permission` (id, user_id, department_id, authority_level) VALUES ( 6, 4, 1, 2), ( 7, 5, 1, 0), ( 8, 6, 1, 4), -( 9, 5, 2, 0); +( 9, 5, 2, 0), +( 10, 2, 2, 2); diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java new file mode 100644 index 0000000..a22d371 --- /dev/null +++ b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java @@ -0,0 +1,229 @@ +package tech.crm.crmserver.controllerTesting; + +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.transaction.annotation.Transactional; +import tech.crm.crmserver.common.constants.ExceptionMessageConstants; +import tech.crm.crmserver.common.constants.SecurityConstants; +import tech.crm.crmserver.common.enums.PermissionLevel; +import tech.crm.crmserver.dao.Contact; +import tech.crm.crmserver.dao.Department; +import tech.crm.crmserver.dao.Permission; +import tech.crm.crmserver.dto.LoginRequest; +import tech.crm.crmserver.service.ContactService; +import tech.crm.crmserver.service.DepartmentService; +import tech.crm.crmserver.service.PermissionService; +import tech.crm.crmserver.service.UserService; + +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ContactControllerTest { + + @Autowired + private MockMvc mvc; + + private static String token; + + @Autowired + private PermissionService permissionService; + + @Autowired + private DepartmentService departmentService; + + @Autowired + private ContactService contactService; + + private int contactId = 10; + + /** + * login before test + * @throws Exception + */ + @Before + public void loginTest() throws Exception { + if(token != null){ + return; + } + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/user/login") + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"email\": \"lingxiao1@student.unimelb.edu.au\",\n" + + " \"password\": \"123456\"\n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + token = mvcResult.getResponse().getHeader(SecurityConstants.TOKEN_HEADER); + } + + /** + * Test add a contact into the department + * @throws Exception + */ + @Test + public void testAAddNewContact() throws Exception { + int departmentId = 3; + + String email = "test@gamil.com"; + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"test@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + assert (contactBasedOnSomeConditionFromDB.size() == 1); + } + + /** + * Test update a contact + * @throws Exception + */ + @Test + public void testBUpdatingContact() throws Exception { + int departmentId = 3; + + + String email = "te@gamil.com"; + + String updateEmail = "updateEmail@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"te@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.put("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"id\": \"" +String.valueOf(contactId) + "\", \n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"updateEmail@gamil.com\", \n" + + " \"first_name\": \"updateFirstName\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + List contactBasedOnSomeConditionFromDB2 = contactService.getContactBasedOnSomeConditionFromDB(departmentId, updateEmail, null, null, null, null, null, null, null); + assert (contactBasedOnSomeConditionFromDB2.get(0).getFirstName().equals("updateFirstName")); + assert (contactBasedOnSomeConditionFromDB2.size() == 1); + } + + + /** + * Test delete a contact + * @throws Exception + */ + @Test + public void testCDeleteContact() throws Exception { + int departmentId = 3; + + + String email = "testDelete@gamil.com"; + + String updateEmail = "updateEmail@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"testDelete@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.delete("/contact").param("contact_id", String.valueOf(contactId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + Contact contact = contactService.getById(contactId); + assert (contact == null); + } + + /** + * Test get a contact by id + * @throws Exception + */ + @Test + public void testDGetContactDetails() throws Exception { + int departmentId = 3; + + + String email = "testGetContact@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"testGetContact@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.get("/contact/detail").param("contact_id", String.valueOf(contactId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + Contact contact = contactService.getById(contactId); + assert (contact != null); + } + +} \ No newline at end of file diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java index bb273e2..d08efa2 100644 --- a/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java +++ b/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java @@ -22,8 +22,10 @@ import tech.crm.crmserver.common.constants.ExceptionMessageConstants; import tech.crm.crmserver.common.constants.SecurityConstants; import tech.crm.crmserver.common.enums.PermissionLevel; +import tech.crm.crmserver.dao.Department; import tech.crm.crmserver.dao.Permission; import tech.crm.crmserver.dto.LoginRequest; +import tech.crm.crmserver.service.DepartmentService; import tech.crm.crmserver.service.PermissionService; import tech.crm.crmserver.service.UserService; @@ -43,6 +45,9 @@ public class DepartmentControllerTest { @Autowired private PermissionService permissionService; + @Autowired + private DepartmentService departmentService; + /** * login before test * @throws Exception @@ -93,12 +98,29 @@ public void testBJoinDepartment() throws Exception { */ @Test public void testAGetMemberofDepartment() throws Exception{ - int departmentId = 3; - mvc.perform(MockMvcRequestBuilders.post("/department/join").param("department_id", String.valueOf(departmentId)).header(SecurityConstants.TOKEN_HEADER,token)) + int departmentId = 2; + mvc.perform(MockMvcRequestBuilders.get("/department/member").param("department_id", String.valueOf(departmentId)).param("size", "4").param("current", "1").header(SecurityConstants.TOKEN_HEADER,token)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) - .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Successfully create permission!")) + .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Success")) .andReturn(); } + /** + * Test delete department by departmentId + * @throws Exception + */ + @Test + public void testCDeleteDepartment() throws Exception{ + int departmentId = 2; + mvc.perform(MockMvcRequestBuilders.delete("/department").param("department_id", String.valueOf(departmentId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Successfully delete the department!")) + .andReturn(); + + Department department = departmentService.getById(departmentId); + assert (department == null); + } + } From 3f3d43e61d60ee5515079b4d661b2d48d40104fe Mon Sep 17 00:00:00 2001 From: harryqin99 Date: Thu, 23 Sep 2021 01:31:14 +0800 Subject: [PATCH 3/5] Add contactController test --- crm-server.iml | 4 + .../controller/ContactController.java | 2 + .../service/impl/ContactServiceImpl.java | 4 +- src/main/resources/db/data-test.sql | 3 +- .../ContactControllerTest.java | 229 ++++++++++++++++++ .../DepartmentControllerTest.java | 28 ++- 6 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java diff --git a/crm-server.iml b/crm-server.iml index 72b5f9f..0b716bb 100644 --- a/crm-server.iml +++ b/crm-server.iml @@ -25,6 +25,10 @@ + + + + diff --git a/src/main/java/tech/crm/crmserver/controller/ContactController.java b/src/main/java/tech/crm/crmserver/controller/ContactController.java index 0ec9294..48705de 100644 --- a/src/main/java/tech/crm/crmserver/controller/ContactController.java +++ b/src/main/java/tech/crm/crmserver/controller/ContactController.java @@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import tech.crm.crmserver.common.enums.PermissionLevel; diff --git a/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java b/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java index 990ba6d..399c595 100644 --- a/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java +++ b/src/main/java/tech/crm/crmserver/service/impl/ContactServiceImpl.java @@ -113,7 +113,7 @@ public Page getContactByOrgIdAndDepartmentId(Page page, QueryWrapper queryWrapper = new QueryWrapper<>(); Page contacts = new Page<>(); - // User加入的departmentId + // User's joined department List permissionList = permissionService.getPermissionByUserId(userId, PermissionLevel.DISPLAY); List departmentIdJoinList = new ArrayList<>(); for (Permission permission : permissionList) { @@ -122,7 +122,7 @@ public Page getContactByOrgIdAndDepartmentId(Page page, if (departmentId == null) { - // Org的departmentId + // Org's department List departmentIdList = departmentService.getDepartmentIdByOrganization(organizationId); List departmentInOrgAndJoin = new ArrayList<>(); diff --git a/src/main/resources/db/data-test.sql b/src/main/resources/db/data-test.sql index 45478ff..ba265db 100644 --- a/src/main/resources/db/data-test.sql +++ b/src/main/resources/db/data-test.sql @@ -53,4 +53,5 @@ INSERT INTO `permission` (id, user_id, department_id, authority_level) VALUES ( 6, 4, 1, 2), ( 7, 5, 1, 0), ( 8, 6, 1, 4), -( 9, 5, 2, 0); +( 9, 5, 2, 0), +( 10, 2, 2, 2); diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java new file mode 100644 index 0000000..a22d371 --- /dev/null +++ b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java @@ -0,0 +1,229 @@ +package tech.crm.crmserver.controllerTesting; + +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.transaction.annotation.Transactional; +import tech.crm.crmserver.common.constants.ExceptionMessageConstants; +import tech.crm.crmserver.common.constants.SecurityConstants; +import tech.crm.crmserver.common.enums.PermissionLevel; +import tech.crm.crmserver.dao.Contact; +import tech.crm.crmserver.dao.Department; +import tech.crm.crmserver.dao.Permission; +import tech.crm.crmserver.dto.LoginRequest; +import tech.crm.crmserver.service.ContactService; +import tech.crm.crmserver.service.DepartmentService; +import tech.crm.crmserver.service.PermissionService; +import tech.crm.crmserver.service.UserService; + +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ContactControllerTest { + + @Autowired + private MockMvc mvc; + + private static String token; + + @Autowired + private PermissionService permissionService; + + @Autowired + private DepartmentService departmentService; + + @Autowired + private ContactService contactService; + + private int contactId = 10; + + /** + * login before test + * @throws Exception + */ + @Before + public void loginTest() throws Exception { + if(token != null){ + return; + } + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/user/login") + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"email\": \"lingxiao1@student.unimelb.edu.au\",\n" + + " \"password\": \"123456\"\n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + token = mvcResult.getResponse().getHeader(SecurityConstants.TOKEN_HEADER); + } + + /** + * Test add a contact into the department + * @throws Exception + */ + @Test + public void testAAddNewContact() throws Exception { + int departmentId = 3; + + String email = "test@gamil.com"; + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"test@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + assert (contactBasedOnSomeConditionFromDB.size() == 1); + } + + /** + * Test update a contact + * @throws Exception + */ + @Test + public void testBUpdatingContact() throws Exception { + int departmentId = 3; + + + String email = "te@gamil.com"; + + String updateEmail = "updateEmail@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"te@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.put("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"id\": \"" +String.valueOf(contactId) + "\", \n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"updateEmail@gamil.com\", \n" + + " \"first_name\": \"updateFirstName\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + List contactBasedOnSomeConditionFromDB2 = contactService.getContactBasedOnSomeConditionFromDB(departmentId, updateEmail, null, null, null, null, null, null, null); + assert (contactBasedOnSomeConditionFromDB2.get(0).getFirstName().equals("updateFirstName")); + assert (contactBasedOnSomeConditionFromDB2.size() == 1); + } + + + /** + * Test delete a contact + * @throws Exception + */ + @Test + public void testCDeleteContact() throws Exception { + int departmentId = 3; + + + String email = "testDelete@gamil.com"; + + String updateEmail = "updateEmail@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"testDelete@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.delete("/contact").param("contact_id", String.valueOf(contactId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + Contact contact = contactService.getById(contactId); + assert (contact == null); + } + + /** + * Test get a contact by id + * @throws Exception + */ + @Test + public void testDGetContactDetails() throws Exception { + int departmentId = 3; + + + String email = "testGetContact@gamil.com"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) + .content("{\n" + + " \"department_id\": \"3\", \n" + + " \"email\": \"testGetContact@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + + " \"birthday\": \"2020-01-01\", \n" + + " \"status\": \"active\" \n" + + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); + + MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.get("/contact/detail").param("contact_id", String.valueOf(contactId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + Contact contact = contactService.getById(contactId); + assert (contact != null); + } + +} \ No newline at end of file diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java index bb273e2..d08efa2 100644 --- a/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java +++ b/src/test/java/tech/crm/crmserver/controllerTesting/DepartmentControllerTest.java @@ -22,8 +22,10 @@ import tech.crm.crmserver.common.constants.ExceptionMessageConstants; import tech.crm.crmserver.common.constants.SecurityConstants; import tech.crm.crmserver.common.enums.PermissionLevel; +import tech.crm.crmserver.dao.Department; import tech.crm.crmserver.dao.Permission; import tech.crm.crmserver.dto.LoginRequest; +import tech.crm.crmserver.service.DepartmentService; import tech.crm.crmserver.service.PermissionService; import tech.crm.crmserver.service.UserService; @@ -43,6 +45,9 @@ public class DepartmentControllerTest { @Autowired private PermissionService permissionService; + @Autowired + private DepartmentService departmentService; + /** * login before test * @throws Exception @@ -93,12 +98,29 @@ public void testBJoinDepartment() throws Exception { */ @Test public void testAGetMemberofDepartment() throws Exception{ - int departmentId = 3; - mvc.perform(MockMvcRequestBuilders.post("/department/join").param("department_id", String.valueOf(departmentId)).header(SecurityConstants.TOKEN_HEADER,token)) + int departmentId = 2; + mvc.perform(MockMvcRequestBuilders.get("/department/member").param("department_id", String.valueOf(departmentId)).param("size", "4").param("current", "1").header(SecurityConstants.TOKEN_HEADER,token)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) - .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Successfully create permission!")) + .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Success")) .andReturn(); } + /** + * Test delete department by departmentId + * @throws Exception + */ + @Test + public void testCDeleteDepartment() throws Exception{ + int departmentId = 2; + mvc.perform(MockMvcRequestBuilders.delete("/department").param("department_id", String.valueOf(departmentId)).header(SecurityConstants.TOKEN_HEADER,token)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value("Successfully delete the department!")) + .andReturn(); + + Department department = departmentService.getById(departmentId); + assert (department == null); + } + } From 7479deeb76d323bb36405e8a84090dd0c4a86b9c Mon Sep 17 00:00:00 2001 From: harryqin99 Date: Fri, 24 Sep 2021 19:02:35 +0800 Subject: [PATCH 4/5] Add contactController test, fix bug on permission --- .../ContactControllerTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java index a22d371..c2ca464 100644 --- a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java +++ b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java @@ -83,13 +83,13 @@ public void loginTest() throws Exception { */ @Test public void testAAddNewContact() throws Exception { - int departmentId = 3; + int departmentId = 2; String email = "test@gamil.com"; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) .contentType(MediaType.APPLICATION_JSON) .content("{\n" + - " \"department_id\": \"3\", \n" + + " \"department_id\": \"2\", \n" + " \"email\": \"test@gamil.com\", \n" + " \"first_name\": \"testFirst\", \n" + " \"last_name\": \"testLast\", \n" + @@ -110,7 +110,7 @@ public void testAAddNewContact() throws Exception { */ @Test public void testBUpdatingContact() throws Exception { - int departmentId = 3; + int departmentId = 2; String email = "te@gamil.com"; @@ -120,7 +120,7 @@ public void testBUpdatingContact() throws Exception { MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) .contentType(MediaType.APPLICATION_JSON) .content("{\n" + - " \"department_id\": \"3\", \n" + + " \"department_id\": \"2\", \n" + " \"email\": \"te@gamil.com\", \n" + " \"first_name\": \"testFirst\", \n" + " \"last_name\": \"testLast\", \n" + @@ -138,7 +138,7 @@ public void testBUpdatingContact() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content("{\n" + " \"id\": \"" +String.valueOf(contactId) + "\", \n" + - " \"department_id\": \"3\", \n" + + " \"department_id\": \"2\", \n" + " \"email\": \"updateEmail@gamil.com\", \n" + " \"first_name\": \"updateFirstName\", \n" + " \"last_name\": \"testLast\", \n" + @@ -160,7 +160,7 @@ public void testBUpdatingContact() throws Exception { */ @Test public void testCDeleteContact() throws Exception { - int departmentId = 3; + int departmentId = 2; String email = "testDelete@gamil.com"; @@ -170,7 +170,7 @@ public void testCDeleteContact() throws Exception { MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) .contentType(MediaType.APPLICATION_JSON) .content("{\n" + - " \"department_id\": \"3\", \n" + + " \"department_id\": \"2\", \n" + " \"email\": \"testDelete@gamil.com\", \n" + " \"first_name\": \"testFirst\", \n" + " \"last_name\": \"testLast\", \n" + @@ -197,7 +197,7 @@ public void testCDeleteContact() throws Exception { */ @Test public void testDGetContactDetails() throws Exception { - int departmentId = 3; + int departmentId = 2; String email = "testGetContact@gamil.com"; @@ -205,7 +205,7 @@ public void testDGetContactDetails() throws Exception { MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) .contentType(MediaType.APPLICATION_JSON) .content("{\n" + - " \"department_id\": \"3\", \n" + + " \"department_id\": \"2\", \n" + " \"email\": \"testGetContact@gamil.com\", \n" + " \"first_name\": \"testFirst\", \n" + " \"last_name\": \"testLast\", \n" + From ee33b0c34116f5d1c30e4f87ec3e9b504735cb5f Mon Sep 17 00:00:00 2001 From: Lingxiao108105 <71265122+Lingxiao108105@users.noreply.github.com> Date: Fri, 24 Sep 2021 21:26:39 +1000 Subject: [PATCH 5/5] Update ContactControllerTest.java --- .../controllerTesting/ContactControllerTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java index 10b29c5..c2ca464 100644 --- a/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java +++ b/src/test/java/tech/crm/crmserver/controllerTesting/ContactControllerTest.java @@ -90,7 +90,10 @@ public void testAAddNewContact() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content("{\n" + " \"department_id\": \"2\", \n" + + " \"email\": \"test@gamil.com\", \n" + + " \"first_name\": \"testFirst\", \n" + " \"last_name\": \"testLast\", \n" + + " \"gender\": \"male\", \n" + " \"birthday\": \"2020-01-01\", \n" + " \"status\": \"active\" \n" + "}")) @@ -111,7 +114,11 @@ public void testBUpdatingContact() throws Exception { String email = "te@gamil.com"; + + String updateEmail = "updateEmail@gamil.com"; + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token) + .contentType(MediaType.APPLICATION_JSON) .content("{\n" + " \"department_id\": \"2\", \n" + " \"email\": \"te@gamil.com\", \n" + @@ -121,7 +128,11 @@ public void testBUpdatingContact() throws Exception { " \"birthday\": \"2020-01-01\", \n" + " \"status\": \"active\" \n" + "}")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + List contactBasedOnSomeConditionFromDB = contactService.getContactBasedOnSomeConditionFromDB(departmentId, email, null, null, null, null, null, null, null); + contactId = contactBasedOnSomeConditionFromDB.get(0).getId(); MvcResult mvcResult1 = mvc.perform(MockMvcRequestBuilders.put("/contact").header(SecurityConstants.TOKEN_HEADER,token) .contentType(MediaType.APPLICATION_JSON)