干燥机配套车间生产管理系统/云平台服务端
baoshiwei
2023-03-10 58d42ccf875b120f40fddce63752298e916e0b0b
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.jeecg.modules.system.test;
 
import com.alibaba.fastjson.JSONObject;
import org.jeecg.JeecgSystemApplication;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
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.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
 
/**
 * 系统用户单元测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = JeecgSystemApplication.class)
@SuppressWarnings({"FieldCanBeLocal", "SpringJavaAutowiredMembersInspection"})
public class SysUserTest {
    /**
     * 测试地址:实际使用时替换成你自己的地址
     */
    private final String BASE_URL = "http://localhost:8080/jeecg-boot/sys/user/";
    //测试:用户名和密码
    private final String USERNAME = "admin";
    private final String PASSWORD = "123456";
    @Autowired
    private RedisUtil redisUtil;
 
    /**
     * 测试用例:查询记录
     */
    @Test
    public void testQuery() {
        // 请求地址
        String url = BASE_URL + "list";
        // 请求 Header (用于传递Token)
        HttpHeaders headers = this.getHeaders();
        // 请求方式是 GET 代表获取数据
        HttpMethod method = HttpMethod.GET;
 
        System.out.println("请求地址:" + url);
        System.out.println("请求方式:" + method);
 
        // 利用 RestUtil 请求该url
        ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, null, JSONObject.class);
        if (result != null && result.getBody() != null) {
            System.out.println("返回结果:" + result.getBody().toJSONString());
        } else {
            System.out.println("查询失败");
        }
    }
 
    /**
     * 测试用例:新增
     */
    @Test
    public void testAdd() {
        // 请求地址
        String url = BASE_URL + "add" ;
        // 请求 Header (用于传递Token)
        HttpHeaders headers = this.getHeaders();
        // 请求方式是 POST 代表提交新增数据
        HttpMethod method = HttpMethod.POST;
 
        System.out.println("请求地址:" + url);
        System.out.println("请求方式:" + method);
 
        JSONObject params = new JSONObject();
        params.put("username", "wangwuTest");
        params.put("password", "123456");
        params.put("confirmpassword","123456");
        params.put("realname", "单元测试");
        params.put("activitiSync", "1");
        params.put("userIdentity","1");
        params.put("workNo","0025");
 
        System.out.println("请求参数:" + params.toJSONString());
 
        // 利用 RestUtil 请求该url
        ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, params, JSONObject.class);
        if (result != null && result.getBody() != null) {
            System.out.println("返回结果:" + result.getBody().toJSONString());
        } else {
            System.out.println("查询失败");
        }
    }
 
 
    /**
     * 测试用例:修改
     */
    @Test
    public void testEdit() {
        // 数据Id
        String dataId = "1331795062924374018";
        // 请求地址
        String url = BASE_URL + "edit";
        // 请求 Header (用于传递Token)
        HttpHeaders headers = this.getHeaders();
        // 请求方式是 PUT 代表提交修改数据
        HttpMethod method = HttpMethod.PUT;
 
        System.out.println("请求地址:" + url);
        System.out.println("请求方式:" + method);
 
        JSONObject params = new JSONObject();
        params.put("username", "wangwuTest");
        params.put("realname", "单元测试1111");
        params.put("activitiSync", "1");
        params.put("userIdentity","1");
        params.put("workNo","0025");
        params.put("id",dataId);
 
        System.out.println("请求参数:" + params.toJSONString());
 
        // 利用 RestUtil 请求该url
        ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, params, JSONObject.class);
        if (result != null && result.getBody() != null) {
            System.out.println("返回结果:" + result.getBody().toJSONString());
        } else {
            System.out.println("查询失败");
        }
    }
 
 
    /**
     * 测试用例:删除
     */
    @Test
    public void testDelete() {
        // 数据Id
        String dataId = "1331795062924374018";
        // 请求地址
        String url = BASE_URL + "delete" + "?id=" + dataId;
        // 请求 Header (用于传递Token)
        HttpHeaders headers = this.getHeaders();
        // 请求方式是 DELETE 代表删除数据
        HttpMethod method = HttpMethod.DELETE;
 
        System.out.println("请求地址:" + url);
        System.out.println("请求方式:" + method);
 
        // 利用 RestUtil 请求该url
        ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, null, JSONObject.class);
        if (result != null && result.getBody() != null) {
            System.out.println("返回结果:" + result.getBody().toJSONString());
        } else {
            System.out.println("查询失败");
        }
    }
 
 
    private String getToken() {
        String token = JwtUtil.sign(USERNAME, PASSWORD);
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 60);
        return token;
    }
 
    private HttpHeaders getHeaders() {
        String token = this.getToken();
        System.out.println("请求Token:" + token);
 
        HttpHeaders headers = new HttpHeaders();
        String mediaType = MediaType.APPLICATION_JSON_VALUE;
        headers.setContentType(MediaType.parseMediaType(mediaType));
        headers.set("Accept", mediaType);
        headers.set("X-Access-Token", token);
        return headers;
    }
}