TeamControllerTest.java 5.29 KB
package com.dituhui.mp.user;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TeamControllerTest {


    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void whenQueryTeamSuccess() throws Exception {
        String result = mockMvc.perform(
                get("/v1/team/get").param("id", "40288ac9764ba85101764baef86f0000")
                        .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenAddTeamSuccess() throws Exception {
        String content = "{\"name\":\"测试团队001\",\"business\":\"001团队\",\"province\":\"四川省\",\"city\":\"成都市\",\"county\":\"高新区\",\"size\":1000,\"address\":\"新川科技园成都超图大厦\",\"expireDate\":\"2020-12-23 00:00:00\"}";
        String result = mockMvc.perform(post("/v1/team/add").contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenUpdateTeamSuccess() throws Exception {
        String content = "{\"id\":\"40288ac9768e57d101768e5808610000\",\"name\":\"测试团队002\",\"business\":\"002团队\",\"province\":\"四川省\",\"city\":\"成都市\",\"county\":\"高新区\",\"size\":1000,\"address\":\"新川科技园成都超图大厦\"}";
        String result = mockMvc.perform(post("/v1/team/update").contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenDeleteTeamSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/team/delete").param("id", "40288ac9768e540101768e5437aa0000")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenAddTeamUserSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/team/addUser").param("id","40288ac9768e57d101768e5808610000")
                .param("userId", "9a29b842388148eda91eede9e11dddbb")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenDeleteTeamUserSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/team/deleteUser").param("id","40288ac9768e57d101768e5808610000")
                .param("userId", "9a29b842388148eda91eede9e11dddbb")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenGetTeamUserSuccess() throws Exception {
        String result = mockMvc.perform(get("/v1/team/getUsers").param("teamId", "40288ac97673e23f017673e26ed90001")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenGetTeamRoleSuccess() throws Exception {
        String result = mockMvc.perform(get("/v1/team/getRoles").param("teamId", "40288ac97673e23f017673e26ed90001")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


}