Commit 340cde10235446dfa6e3a110d017e51d66f3c065
1 parent
a75b8d36
feat(usr): user update error branches REQ-USR-002
Showing
1 changed file
with
72 additions
and
0 deletions
backend/src/test/java/com/xly/erp/module/usr/service/UserServiceImplTest.java
| ... | ... | @@ -244,6 +244,78 @@ class UserServiceImplTest { |
| 244 | 244 | assertThat(uc.getValue().getBCanModifyDocs()).isFalse(); |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | + @Test | |
| 248 | + void updateWithInvalidUserType_throws40001() { | |
| 249 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 250 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 251 | + dto.setSUserType("火星"); | |
| 252 | + assertThatThrownBy(() -> service.update(10, dto)) | |
| 253 | + .isInstanceOf(BizException.class) | |
| 254 | + .hasFieldOrPropertyWithValue("code", 40001); | |
| 255 | + verify(userMapper, never()).updateById(any(User.class)); | |
| 256 | + } | |
| 257 | + | |
| 258 | + @Test | |
| 259 | + void updateWithInvalidLanguage_throws40001() { | |
| 260 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 261 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 262 | + dto.setSLanguage("ja"); | |
| 263 | + assertThatThrownBy(() -> service.update(10, dto)) | |
| 264 | + .isInstanceOf(BizException.class) | |
| 265 | + .hasFieldOrPropertyWithValue("code", 40001); | |
| 266 | + verify(userMapper, never()).updateById(any(User.class)); | |
| 267 | + } | |
| 268 | + | |
| 269 | + @Test | |
| 270 | + void updateWithStaffNotFound_throws40022() { | |
| 271 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 272 | + when(staffMapper.existsActiveById(99)).thenReturn(false); | |
| 273 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 274 | + dto.setIStaffId(99); | |
| 275 | + assertThatThrownBy(() -> service.update(10, dto)) | |
| 276 | + .isInstanceOf(BizException.class) | |
| 277 | + .hasFieldOrPropertyWithValue("code", 40022); | |
| 278 | + verify(userMapper, never()).updateById(any(User.class)); | |
| 279 | + } | |
| 280 | + | |
| 281 | + @Test | |
| 282 | + void updateWithSomeInvalidPermissionIds_throws40023() { | |
| 283 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 284 | + when(permissionCategoryMapper.countActiveByIds(List.of(1, 2, 3))).thenReturn(2); | |
| 285 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 286 | + dto.setPermissionCategoryIds(List.of(1, 2, 3)); | |
| 287 | + assertThatThrownBy(() -> service.update(10, dto)) | |
| 288 | + .isInstanceOf(BizException.class) | |
| 289 | + .hasFieldOrPropertyWithValue("code", 40023); | |
| 290 | + verify(userMapper, never()).updateById(any(User.class)); | |
| 291 | + } | |
| 292 | + | |
| 293 | + @Test | |
| 294 | + void updateWithDuplicateUserNo_throws40020() { | |
| 295 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 296 | + when(userMapper.updateById(any(User.class))) | |
| 297 | + .thenThrow(new DuplicateKeyException("uk_user_no")); | |
| 298 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 299 | + assertThatThrownBy(() -> service.update(10, dto)) | |
| 300 | + .isInstanceOf(BizException.class) | |
| 301 | + .hasFieldOrPropertyWithValue("code", 40020); | |
| 302 | + verify(userPermissionMapper, never()).deleteByUserId(any()); | |
| 303 | + } | |
| 304 | + | |
| 305 | + @Test | |
| 306 | + void updateWithEmptyPermissionIds_clearsExisting() { | |
| 307 | + when(userMapper.selectById(10)).thenReturn(stubExistingUser(10)); | |
| 308 | + when(userMapper.updateById(any(User.class))).thenReturn(1); | |
| 309 | + when(userPermissionMapper.deleteByUserId(10)).thenReturn(2); | |
| 310 | + UpdateUserDTO dto = baseUpdateDto(); | |
| 311 | + dto.setPermissionCategoryIds(null); | |
| 312 | + | |
| 313 | + service.update(10, dto); | |
| 314 | + | |
| 315 | + verify(userPermissionMapper, times(1)).deleteByUserId(10); | |
| 316 | + verify(userPermissionMapper, never()).insert(any(UserPermission.class)); | |
| 317 | + } | |
| 318 | + | |
| 247 | 319 | private UpdateUserDTO baseUpdateDto() { |
| 248 | 320 | UpdateUserDTO dto = new UpdateUserDTO(); |
| 249 | 321 | dto.setSUserNo("u_new"); | ... | ... |