package com.mx.cneeds.server.user.web;

import com.mx.cneeds.server.entity.SysUserEntity;
import com.mx.cneeds.server.user.service.SysUserDeptService;
import com.mx.cneeds.server.user.service.SysUserFuncService;
import com.mx.cneeds.server.user.service.SysUserRoleService;
import com.mx.cneeds.server.user.service.SysUserService;
import junit.framework.TestCase;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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.boot.test.mock.mockito.MockBean;
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.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SysUserControllerTest extends TestCase {


    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private SysUserService sysUserService;

    @MockBean
    private SysUserRoleService sysUserRoleService;

    @MockBean
    private SysUserDeptService sysUserDeptService;

    @MockBean
    private SysUserFuncService sysUserFuncService;


    public void setUp() throws Exception {
        super.setUp();
    }

    public void tearDown() throws Exception {
    }

    @Test
    public void testList() {
        System.out.println("1111");
    }

    @Test
    public void testInfoByUsername() throws Exception {
        List<Long> longList = new LinkedList<>();
        longList.add(1L);
        longList.add(2L);
        longList.add(3L);


        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUserId(1L);
        sysUserEntity.setUsername("123456");
        Mockito.when(sysUserService.queryUserByUsername("123456")).thenReturn(sysUserEntity);

        Mockito.when(sysUserRoleService.queryRoleId(1L)).thenReturn(1L);

        Mockito.when(sysUserDeptService.queryDeptIdList(1L)).thenReturn(longList);

        Mockito.when(sysUserFuncService.queryFuncIdList(1L)).thenReturn(longList);

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/sys/user/userinfo").param("username","123456"));

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());


       resultActions.andExpect(MockMvcResultMatchers.jsonPath("userId",IsEqual.equalTo(1)));
       resultActions.andExpect(MockMvcResultMatchers.jsonPath("username",IsEqual.equalTo("123456")));


    }
}