“mitao”
2026-01-20 4e5beb844f03daec12f2b039365730b0683f8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.estate.expert.admin.service;
 
import com.alibaba.fastjson.JSONObject;
import com.estate.expert.admin.domain.user.User;
import com.estate.expert.admin.domain.user.UserDto;
import com.estate.expert.admin.domain.user.UserState;
import com.estate.expert.admin.domain.common.request.PageReq;
import com.estate.expert.common.utils.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 用户服务测试
 **/
@Slf4j
@SpringBootTest
@ExtendWith(SpringExtension.class)
@Transactional
public class UserServiceTest {
 
    @Autowired
    private UserService userService;
 
    private User currentUser;
 
    @BeforeEach
    public void setUp() {
        User request = new User();
        request.setName("Lily");
        request.setMobile("13814085622");
        request.setDepartmentPath("K");
        currentUser = userService.add(null , request);
    }
 
    @AfterEach
    public void tearDown() {
        userService.deleteAllById(List.of(currentUser.getId()));
    }
 
    @Test
    public void testList() {
        var response = userService.list("", PageReq.defaultPageReq());
        Assertions.assertTrue(response.getTotalElements() > 0);
 
        response = userService.list("Lily", PageReq.defaultPageReq());
        Assertions.assertTrue(response.getTotalElements() > 0);
 
        response = userService.list("xxccsd", PageReq.defaultPageReq());
        Assertions.assertEquals(0, response.getTotalElements());
        log.info("response: {}", JSONObject.toJSONString(response));
    }
 
    @Test
    public void testOptions(){
        var response = userService.options(null);
        Assertions.assertFalse(response.isEmpty());
        log.info("response: {}", JSONObject.toJSONString(response));
    }
 
    @Test
    public void testEditUser() {
        UserDto request = UserDto.builder()
                .id(currentUser.getId())
                .name("小小木")
                .mobile("18501373727")
                .state(UserState.DISABLE)
                .departmentPath("K")
                .avatar("avatar").build();
        userService.edit(request);
 
        User user = userService.get(currentUser.getId());
        Assertions.assertEquals("小小木", user.getName());
        // mobile 不可修改
        Assertions.assertNotEquals("18501373727", user.getMobile());
    }
 
    @Test
    public void testResetPass() {
        userService.resetPass(currentUser.getId());
    }
 
    @Test
    public void testModifyState() {
        userService.modifyState(currentUser.getId(), UserState.DISABLE);
        User user = userService.get(currentUser.getId());
        Assertions.assertEquals(UserState.DISABLE, user.getState());
    }
 
    @Test
    public void testPass() {
        String defaultPass = "123456";
        System.out.println(PasswordUtils.generate(defaultPass));
    }
 
}