Skip to content

Commit

Permalink
Merge pull request #60 from is0xjh25/Testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
Lingxiao108105 authored Sep 24, 2021
2 parents 14a66b2 + ee33b0c commit 538eddc
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crm-server.iml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-mail:2.4.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.9" level="project" />
<orderEntry type="library" name="Maven: com.sun.mail:jakarta.mail:1.6.7" level="project" />
<orderEntry type="library" name="Maven: com.sun.activation:jakarta.activation:1.2.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.4.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.4.9" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.4.9" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Page<ContactDTO> getContactByOrgIdAndDepartmentId(Page<ContactDTO> page,

QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
Page<Contact> contacts = new Page<>();
// User加入的departmentId
// User's joined department
List<Permission> permissionList = permissionService.getPermissionByUserId(userId, PermissionLevel.DISPLAY);
List<Integer> departmentIdJoinList = new ArrayList<>();
for (Permission permission : permissionList) {
Expand All @@ -122,7 +122,7 @@ public Page<ContactDTO> getContactByOrgIdAndDepartmentId(Page<ContactDTO> page,

if (departmentId == null) {

// Org的departmentId
// Org's department
List<Integer> departmentIdList = departmentService.getDepartmentIdByOrganization(organizationId);

List<Integer> departmentInOrgAndJoin = new ArrayList<>();
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/db/data-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
@@ -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\": \"[email protected]\",\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 = 2;

String email = "[email protected]";
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token)
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"department_id\": \"2\", \n" +
" \"email\": \"[email protected]\", \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<Contact> 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 = 2;


String email = "[email protected]";

String updateEmail = "[email protected]";

MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token)
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"department_id\": \"2\", \n" +
" \"email\": \"[email protected]\", \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<Contact> 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\": \"2\", \n" +
" \"email\": \"[email protected]\", \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<Contact> 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 = 2;


String email = "[email protected]";

String updateEmail = "[email protected]";

MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token)
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"department_id\": \"2\", \n" +
" \"email\": \"[email protected]\", \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<Contact> 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 = 2;


String email = "[email protected]";

MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/contact").header(SecurityConstants.TOKEN_HEADER,token)
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"department_id\": \"2\", \n" +
" \"email\": \"[email protected]\", \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<Contact> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,6 +45,9 @@ public class DepartmentControllerTest {
@Autowired
private PermissionService permissionService;

@Autowired
private DepartmentService departmentService;

/**
* login before test
* @throws Exception
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 538eddc

Please sign in to comment.