UserWS { @RequestMapping(value = "/api/users", method = RequestMethod.GET) public List<User> getUsers(@RequestParam(value = "username", required = false) String username) { List<User> users = userRepository.findAll( where(ifParamNotNull(usernameEquals(username))) .and(enabledEquals(true)), Sort.by(Sort.Direction.ASC, "username")); return users; }  @RequestMapping(value = "/api/users", method = RequestMethod.GET) List<User> getUsers(@RequestParam(value = "username", required = false) String username); @RequestMapping(value = "api/users", method = RequestMethod.POST) ResponseEntity<User> createUser(@RequestBody User user); @RequestMapping(value = "/api/users/{userId}", method = RequestMethod.DELETE) void deleteUserByUserId(@PathVariable Long userId); @RequestMapping(value = "/api/users/{userId}", method = RequestMethod.PATCH) void updateUserByUserId(@PathVariable Long userId, @RequestBody User user);  }
@Test public void testGetUsers() { List<User> users = userClient.getUsers(null); assertFalse(users.isEmpty()); for (User user : users) { logger.debug("{}: {}", user.getUsername(), authoritiesToString(user.getAuthorities())); } }
@Test public void testGetDefaultUser() { List<User> users = userClient.getUsers(bootstrapConfig.getDefaultUser().getUsername()); assertEquals(1, users.size()); assertEquals(bootstrapConfig.getDefaultUser().getUsername(), users.get(0).getUsername()); }
@Test public void testCreateAndDeleteUser() throws ResourceNotCreatedException, ResourceNotFoundException { String username = testIdWatcher.getEntityName("user"); User user = createTestUser(username); List<User> users = userClient.getUsers(username); assertEquals(username, users.get(0).getUsername()); boolean shouldFailToCreateWithExistingUsername = false; try { createTestUser(username); } catch (ResourceNotCreatedException ex) { shouldFailToCreateWithExistingUsername = true; } assertTrue(shouldFailToCreateWithExistingUsername); users = userClient.getUsers(username); assertFalse(users.isEmpty()); userClient.deleteUserByUsername(username); users = userClient.getUsers(username); assertTrue(users.isEmpty()); boolean shouldFailToDeleteNonExistingUsername = false; try { userClient.deleteUserByUsername(username); } catch (ResourceNotFoundException ex) { shouldFailToDeleteNonExistingUsername = true; } assertTrue(shouldFailToDeleteNonExistingUsername); }
@Test public void testUpdateUser() throws ResourceNotCreatedException, ResourceNotFoundException { String username = testIdWatcher.getEntityName("user"); User user = createTestUser(username); String password = "test_updated"; Role role = Role.ADMIN; String surname = "Mojito_updated"; String givenName = "Test_updated"; String commonName = "Test_updated Mojito_updated"; userClient.updateUserByUsername(username, password, role, surname, givenName, commonName); List<User> users = userClient.getUsers(username); assertFalse(users.isEmpty()); User updatedUser = users.get(0); assertEquals(user.getId(), updatedUser.getId()); assertEquals(username, updatedUser.getUsername()); assertEquals(1, user.getAuthorities().size()); assertEquals("ROLE_" + role, updatedUser.getAuthorities().iterator().next().getAuthority()); assertEquals(surname, updatedUser.getSurname()); assertEquals(givenName, updatedUser.getGivenName()); assertEquals(commonName, updatedUser.getCommonName()); }