RoleControllerTest.java 4.51 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 RoleControllerTest {


    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

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

    @Test
    public void whenAddRoleSuccess() throws Exception {
        String content = "{\"name\":\"角色1\",\"teamId\":\"40288ac9768e57d101768e5808610000\",\"extra\":\"1\"}";
        String result = mockMvc.perform(post("/v1/role/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 whenDeleteRoleSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/role/delete").param("id", "40288ac97697e591017697e5be060000")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }

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


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

    @Test
    public void whenAddResourceSuccess() throws Exception {
        String content = "{\"name\":\"资源一\",\"teamId\":\"40288ac9768e57d101768e5808610000\",\"extra\":\"1\"}";
        String result = mockMvc.perform(post("/v1/resource/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 whenDeleteResourceSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/resource/delete").param("id","40288ac97698777801769877a5220000").contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


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

}