tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package com.cloudroam.service.impl;
 
import com.cloudroam.bo.GroupPermissionBO;
import com.cloudroam.dto.admin.*;
import com.cloudroam.mapper.*;
import com.cloudroam.model.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudroam.dto.admin.*;
import com.cloudroam.mapper.*;
import com.cloudroam.model.*;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import com.cloudroam.common.enumeration.GroupLevelEnum;
import com.cloudroam.dto.admin.*;
import com.cloudroam.dto.user.RegisterDTO;
import com.cloudroam.mapper.*;
import com.cloudroam.model.*;
import com.cloudroam.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
import static org.junit.jupiter.api.Assertions.*;
 
 
@SpringBootTest
@Transactional
@Rollback
@Slf4j
@ActiveProfiles("test")
public class AdminServiceImplTest {
 
    @Autowired
    private AdminServiceImpl adminService;
 
    @Autowired
    private UserMapper userMapper;
 
    @Autowired
    private GroupMapper groupMapper;
 
    @Autowired
    private UserGroupMapper userGroupMapper;
 
    @Autowired
    private PermissionMapper permissionMapper;
 
    @Autowired
    private GroupPermissionMapper groupPermissionMapper;
 
    @Autowired
    private UserServiceImpl userService;
 
    @Autowired
    private UserIdentityServiceImpl userIdentityService;
 
    @Autowired
    private GroupService groupService;
 
    public UserDO mockData() {
        UserDO user = UserDO.builder().nickname("pedro大大").username("pedro大大").build();
        GroupDO group1 = GroupDO.builder().name("测试分组12").info("just for test").build();
        GroupDO group2 = GroupDO.builder().name("测试分组11").info("just for test").build();
        userMapper.insert(user);
        groupMapper.insert(group1);
        groupMapper.insert(group2);
        List<UserGroupDO> relations = new ArrayList<>();
        UserGroupDO relation1 = new UserGroupDO(user.getId(), group1.getId());
        UserGroupDO relation2 = new UserGroupDO(user.getId(), group2.getId());
        relations.add(relation1);
        relations.add(relation2);
        userGroupMapper.insertBatch(relations);
        return user;
    }
 
    public GroupDO mockData1() {
        UserDO user1 = UserDO.builder().nickname("pedro大大").username("pedro大大").build();
        UserDO user2 = UserDO.builder().nickname("pedro小小").username("pedro小小").build();
        GroupDO group = GroupDO.builder().name("测试分组11").info("just for test").build();
        userMapper.insert(user1);
        userMapper.insert(user2);
        groupMapper.insert(group);
        List<UserGroupDO> relations = new ArrayList<>();
        UserGroupDO relation1 = new UserGroupDO(user1.getId(), group.getId());
        UserGroupDO relation2 = new UserGroupDO(user2.getId(), group.getId());
        relations.add(relation1);
        relations.add(relation2);
        userGroupMapper.insertBatch(relations);
        return group;
    }
 
    @Test
    public void getUserPageByGroupId() {
        GroupDO group = mockData1();
        IPage<UserDO> iPage = adminService.getUserPageByGroupId(null, 10, 0);
        assertTrue(iPage.getTotal() > 0);
        assertEquals(10, iPage.getSize());
        assertEquals(0, iPage.getCurrent());
        boolean anyMatch = iPage.getRecords().stream().anyMatch(it -> it.getUsername().equals(
                "pedro大大"));
        assertTrue(anyMatch);
    }
 
    @Test
    public void getUserPageByGroupId1() {
        GroupDO group = mockData1();
        IPage<UserDO> iPage = adminService.getUserPageByGroupId(group.getId(), 10, 0);
        assertTrue(iPage.getTotal() > 0);
        boolean anyMatch = iPage.getRecords().stream().anyMatch(it -> it.getUsername().equals(
                "pedro大大"));
        assertTrue(anyMatch);
        anyMatch = iPage.getRecords().stream().anyMatch(it -> it.getNickname().equals("pedro小小"));
        assertTrue(anyMatch);
    }
 
    @Test
    public void changeUserPassword() {
        RegisterDTO dto = new RegisterDTO();
        dto.setUsername("pedro&佩德罗");
        dto.setPassword("123456");
        dto.setConfirmPassword("123456");
        UserDO user = userService.createUser(dto);
        assertEquals("pedro&佩德罗", user.getUsername());
        boolean valid = userIdentityService.verifyUsernamePassword(user.getId(), "pedro&佩德罗",
                "123456");
        assertTrue(valid);
 
        ResetPasswordDTO dto1 = new ResetPasswordDTO();
        dto1.setNewPassword("147258");
        dto1.setConfirmPassword("147258");
        boolean b = adminService.changeUserPassword(user.getId(), dto1);
        assertTrue(b);
 
        valid = userIdentityService.verifyUsernamePassword(user.getId(), "pedro&佩德罗", "147258");
        assertTrue(valid);
    }
 
    @Test
    public void changeUserPassword1() {
        assertThrows(NotFoundException.class, () -> {
            Random random = new Random();
            ResetPasswordDTO dto1 = new ResetPasswordDTO();
            dto1.setNewPassword("147258");
            dto1.setConfirmPassword("147258");
            boolean b = adminService.changeUserPassword(random.nextInt(), dto1);
            assertFalse(b);
        });
    }
 
    @Test
    public void deleteUser() {
        RegisterDTO dto = new RegisterDTO();
        dto.setUsername("pedro&佩德罗");
        dto.setPassword("123456");
        dto.setConfirmPassword("123456");
        UserDO user = userService.createUser(dto);
        assertEquals("pedro&佩德罗", user.getUsername());
        boolean b = true;
        try {
            b = adminService.deleteUser(user.getId());
        } catch (ForbiddenException ignored) {
        }
        assertTrue(b);
 
        UserDO selected = userMapper.selectById(user.getId());
        assertNull(selected);
    }
 
    //    @Test(expected = NotFoundException.class)
    public void deleteUser1() {
        Random random = new Random();
        boolean b = adminService.deleteUser(random.nextInt());
        assertFalse(b);
    }
 
    //    @Test(expected = ForbiddenException.class)
    public void updateUserInfo() {
        UserDO user1 = UserDO.builder().nickname("pedro大大").username("pedro大大").build();
        userMapper.insert(user1);
        Random random = new Random();
        UpdateUserInfoDTO dto = new UpdateUserInfoDTO();
        Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT);
        dto.setGroupIds(Arrays.asList(rootGroupId, random.nextInt(100)));
        boolean b = adminService.updateUserInfo(user1.getId(), dto);
        assertFalse(b);
    }
 
    @Test
    public void updateUserInfo1() {
        assertThrows(ForbiddenException.class, () -> {
            UserDO user1 = UserDO.builder().nickname("pedro大大").username("pedro大大").build();
            userMapper.insert(user1);
            Random random = new Random();
            UpdateUserInfoDTO dto = new UpdateUserInfoDTO();
            dto.setGroupIds(Arrays.asList(random.nextInt(100), random.nextInt(100)));
            boolean b = adminService.updateUserInfo(user1.getId(), dto);
            assertFalse(b);
        });
    }
 
    @Test
    public void updateUserInfo2() {
        UserDO user = UserDO.builder().nickname("pedro大大").username("pedro大大").build();
        GroupDO group1 = GroupDO.builder().name("测试分组12").info("just for test").build();
        GroupDO group2 = GroupDO.builder().name("测试分组11").info("just for test").build();
        userMapper.insert(user);
        groupMapper.insert(group1);
        groupMapper.insert(group2);
        List<UserGroupDO> relations = new ArrayList<>();
        UserGroupDO relation1 = new UserGroupDO(user.getId(), group1.getId());
        relations.add(relation1);
        userGroupMapper.insertBatch(relations);
 
        UpdateUserInfoDTO dto = new UpdateUserInfoDTO();
        dto.setGroupIds(Collections.singletonList(group2.getId()));
        boolean b = adminService.updateUserInfo(user.getId(), dto);
        assertTrue(b);
 
        QueryWrapper<UserGroupDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserGroupDO::getUserId, user.getId())
                .eq(UserGroupDO::getGroupId, group1.getId());
        UserGroupDO rel1 = userGroupMapper.selectOne(wrapper);
        assertNull(rel1);
 
        QueryWrapper<UserGroupDO> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().eq(UserGroupDO::getUserId, user.getId())
                .eq(UserGroupDO::getGroupId, group2.getId());
        UserGroupDO rel2 = userGroupMapper.selectOne(wrapper1);
        assertEquals(rel2.getGroupId(), group2.getId());
    }
 
    @Test
    public void getGroupPage() {
        GroupDO group1 = GroupDO.builder().name("测试分组12").info("just for test").build();
        GroupDO group2 = GroupDO.builder().name("测试分组11").info("just for test").build();
        groupMapper.insert(group1);
        groupMapper.insert(group2);
        IPage<GroupDO> iPage = adminService.getGroupPage(0, 10);
        assertTrue(iPage.getTotal() > 0);
        assertEquals(0, iPage.getCurrent());
        assertEquals(10, iPage.getSize());
        boolean anyMatch = iPage.getRecords().stream().anyMatch(it -> it.getName().equals("测试分组12"
        ));
        assertTrue(anyMatch);
    }
 
    @Test
    public void getGroup() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        groupMapper.insert(group);
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
        List<GroupPermissionDO> relations = new ArrayList<>();
        GroupPermissionDO relation1 = new GroupPermissionDO(group.getId(), permission1.getId());
        GroupPermissionDO relation2 = new GroupPermissionDO(group.getId(), permission2.getId());
        relations.add(relation1);
        relations.add(relation2);
        groupPermissionMapper.insertBatch(relations);
 
        GroupPermissionBO group1 = adminService.getGroup(group.getId());
        assertEquals("测试分组1", group1.getName());
        assertNotNull(group1.getId());
        boolean anyMatch = group1.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限1");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void getGroup1() {
        assertThrows(NotFoundException.class, () -> {
            Random random = new Random();
            GroupPermissionBO group1 = adminService.getGroup(random.nextInt(100));
            assertNull(group1);
        });
    }
 
    @Test
    public void createGroup() {
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
 
        NewGroupDTO dto = new NewGroupDTO();
        dto.setName("测试分组1");
        dto.setInfo("just for test");
        dto.setPermissionIds(Arrays.asList(permission1.getId(), permission2.getId()));
        boolean ok = adminService.createGroup(dto);
        assertTrue(ok);
 
        QueryWrapper<GroupDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GroupDO::getName, "测试分组1");
        GroupDO group = groupMapper.selectOne(wrapper);
        assertEquals("测试分组1", group.getName());
        assertEquals("just for test", group.getInfo());
 
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        boolean anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限1");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void createGroup1() {
        NewGroupDTO dto = new NewGroupDTO();
        dto.setName("测试分组1");
        dto.setInfo("just for test");
        boolean ok = adminService.createGroup(dto);
        assertTrue(ok);
 
        QueryWrapper<GroupDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GroupDO::getName, "测试分组1");
        GroupDO group = groupMapper.selectOne(wrapper);
        assertEquals("测试分组1", group.getName());
        assertEquals("just for test", group.getInfo());
 
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        assertEquals("测试分组1", groupPermissions.getName());
        assertEquals(groupPermissions.getPermissions().size(), 0);
    }
 
    @Test
    public void updateGroup() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        groupMapper.insert(group);
 
        UpdateGroupDTO dto = new UpdateGroupDTO();
        dto.setName("测试分组1儿子");
        dto.setInfo("测试分组1儿子info");
        boolean ok = adminService.updateGroup(group.getId(), dto);
        assertTrue(ok);
        GroupDO selected = groupMapper.selectById(group.getId());
        assertEquals(selected.getName(), "测试分组1儿子");
        assertEquals(selected.getInfo(), "测试分组1儿子info");
    }
 
    @Test
    public void updateGroup1() {
        assertThrows(ForbiddenException.class, () -> {
            GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
            GroupDO group1 = GroupDO.builder().name("测试分组2").info("just for test").build();
            groupMapper.insert(group);
            groupMapper.insert(group1);
 
            UpdateGroupDTO dto = new UpdateGroupDTO();
            dto.setName("测试分组2");
            dto.setInfo("测试分组2info");
            boolean ok = adminService.updateGroup(group.getId(), dto);
            assertFalse(ok);
        });
    }
 
    @Test
    public void updateGroup2() {
        assertThrows(NotFoundException.class, () -> {
            GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
            groupMapper.insert(group);
 
            UpdateGroupDTO dto = new UpdateGroupDTO();
            dto.setName("测试分组2");
            dto.setInfo("测试分组2info");
            Random random = new Random();
            boolean ok = adminService.updateGroup(random.nextInt(100) + 10, dto);
            assertFalse(ok);
        });
    }
 
    @Test
    public void deleteGroup() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        groupMapper.insert(group);
 
        boolean ok = adminService.deleteGroup(group.getId());
        assertTrue(ok);
        GroupDO selected = groupMapper.selectById(group.getId());
        assertNull(selected);
    }
 
    @Test
    public void deleteGroup1() {
        assertThrows(NotFoundException.class, () -> {
            Random random = new Random();
            boolean ok = adminService.deleteGroup(random.nextInt(1000));
            assertFalse(ok);
        });
    }
 
    @Test
    public void dispatchPermission() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        groupMapper.insert(group);
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
        List<GroupPermissionDO> relations = new ArrayList<>();
        GroupPermissionDO relation1 = new GroupPermissionDO(group.getId(), permission1.getId());
        GroupPermissionDO relation2 = new GroupPermissionDO(group.getId(), permission2.getId());
        relations.add(relation1);
        relations.add(relation2);
        groupPermissionMapper.insertBatch(relations);
 
        PermissionDO permission3 = PermissionDO.builder().name("权限3").module("炉石传说").build();
        permissionMapper.insert(permission3);
        DispatchPermissionDTO dto = new DispatchPermissionDTO();
        dto.setGroupId(group.getId());
        dto.setPermissionId(permission3.getId());
        boolean ok = adminService.dispatchPermission(dto);
        assertTrue(ok);
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        boolean anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限3") && p.getModule().equals("炉石传说");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void dispatchPermissions() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        groupMapper.insert(group);
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
        List<GroupPermissionDO> relations = new ArrayList<>();
        GroupPermissionDO relation1 = new GroupPermissionDO(group.getId(), permission1.getId());
        GroupPermissionDO relation2 = new GroupPermissionDO(group.getId(), permission2.getId());
        relations.add(relation1);
        relations.add(relation2);
        groupPermissionMapper.insertBatch(relations);
 
        PermissionDO permission3 = PermissionDO.builder().name("权限3").module("炉石传说").build();
        permissionMapper.insert(permission3);
        PermissionDO permission4 = PermissionDO.builder().name("权限4").module("炉石传说").build();
        permissionMapper.insert(permission4);
 
        DispatchPermissionsDTO dto = new DispatchPermissionsDTO();
        dto.setGroupId(group.getId());
        dto.setPermissionIds(Arrays.asList(permission3.getId(), permission4.getId()));
        boolean ok = adminService.dispatchPermissions(dto);
        assertTrue(ok);
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        boolean anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限3") && p.getModule().equals("炉石传说");
        });
        assertTrue(anyMatch);
 
        anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限4") && p.getModule().equals("炉石传说");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void removePermissions() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        PermissionDO permission3 = PermissionDO.builder().name("权限3").module("炉石传说").build();
        groupMapper.insert(group);
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
        permissionMapper.insert(permission3);
        List<GroupPermissionDO> relations = new ArrayList<>();
        GroupPermissionDO relation1 = new GroupPermissionDO(group.getId(), permission1.getId());
        GroupPermissionDO relation2 = new GroupPermissionDO(group.getId(), permission2.getId());
        GroupPermissionDO relation3 = new GroupPermissionDO(group.getId(), permission3.getId());
        relations.add(relation1);
        relations.add(relation2);
        relations.add(relation3);
        groupPermissionMapper.insertBatch(relations);
 
        RemovePermissionsDTO dto = new RemovePermissionsDTO();
        dto.setGroupId(group.getId());
        // 2, 3
        dto.setPermissionIds(Arrays.asList(permission3.getId(), permission2.getId()));
        boolean ok = adminService.removePermissions(dto);
        assertTrue(ok);
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        boolean anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限3") && p.getModule().equals("炉石传说");
        });
        assertFalse(anyMatch);
 
        anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限1") && p.getModule().equals("炉石传说");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void removePermissions1() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        PermissionDO permission1 = PermissionDO.builder().name("权限1").module("炉石传说").build();
        PermissionDO permission2 = PermissionDO.builder().name("权限2").module("炉石传说").build();
        PermissionDO permission3 = PermissionDO.builder().name("权限3").module("炉石传说").build();
        groupMapper.insert(group);
        permissionMapper.insert(permission1);
        permissionMapper.insert(permission2);
        permissionMapper.insert(permission3);
        List<GroupPermissionDO> relations = new ArrayList<>();
        GroupPermissionDO relation1 = new GroupPermissionDO(group.getId(), permission1.getId());
        GroupPermissionDO relation2 = new GroupPermissionDO(group.getId(), permission2.getId());
        GroupPermissionDO relation3 = new GroupPermissionDO(group.getId(), permission3.getId());
        relations.add(relation1);
        relations.add(relation2);
        relations.add(relation3);
        groupPermissionMapper.insertBatch(relations);
 
        RemovePermissionsDTO dto = new RemovePermissionsDTO();
        dto.setGroupId(group.getId());
        // 3
        dto.setPermissionIds(Collections.singletonList(permission3.getId()));
        boolean ok = adminService.removePermissions(dto);
        assertTrue(ok);
        GroupPermissionBO groupPermissions = adminService.getGroup(group.getId());
        boolean anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限3") && p.getModule().equals("炉石传说");
        });
        assertFalse(anyMatch);
 
        anyMatch = groupPermissions.getPermissions().stream().anyMatch(it -> {
            PermissionDO p = it;
            return p.getName().equals("权限1") && p.getModule().equals("炉石传说");
        });
        assertTrue(anyMatch);
    }
 
    @Test
    public void getAllGroups() {
        GroupDO group = GroupDO.builder().name("测试分组1").info("just for test").build();
        groupMapper.insert(group);
        List<GroupDO> groups = adminService.getAllGroups();
        assertTrue(groups.size() > 0);
        boolean anyMatch = groups.stream().anyMatch(it -> it.getName().equals("测试分组1"));
        assertTrue(anyMatch);
    }
 
    @Test
    public void createGroupAndDeleteGroup() {
        NewGroupDTO dto = new NewGroupDTO();
        dto.setName("测试分组1");
        dto.setInfo("just for test");
 
        try {
            boolean ok = adminService.createGroup(dto);
            assertTrue(ok);
        } catch (ForbiddenException e) {
            log.warn("", e);
        }
 
        QueryWrapper<GroupDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GroupDO::getName, "测试分组1");
        GroupDO group = groupMapper.selectOne(wrapper);
        assertEquals("测试分组1", group.getName());
        assertEquals("just for test", group.getInfo());
 
        try {
            boolean b = adminService.deleteGroup(group.getId());
            assertTrue(b);
        } catch (ForbiddenException e) {
            assertTrue(group.getId() <= 2);
        }
 
        try {
            boolean ok = adminService.createGroup(dto);
            assertTrue(ok);
            boolean ok1 = adminService.createGroup(dto);
            assertTrue(ok1);
 
            group = groupMapper.selectOne(wrapper);
            assertEquals("测试分组1", group.getName());
            assertEquals("just for test", group.getInfo());
        } catch (ForbiddenException e) {
            log.warn("", e);
        }
    }
}