AdminUserResource { @Path("password") @POST @Consumes(MediaType.APPLICATION_JSON) public List<User> password(UserAndPasswords passwords) { try { return useradmin.updatePassword(passwords); } catch (AuthservicePasswordEmptyException e) { logservice.log(LogService.LOG_WARNING, "REST endpoint /ukelonn/api/admin/user/password received empty password"); throw new BadRequestException(e.getMessage()); } catch (AuthservicePasswordsNotIdenticalException e) { logservice.log(LogService.LOG_WARNING, "REST endpoint /ukelonn/api/admin/user/password received passwords that weren't identical"); throw new BadRequestException(e.getMessage()); } catch (AuthserviceException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /ukelonn/api/admin/user/password got bad request: %s", e.getMessage())); throw new InternalServerErrorException("See log for error details"); } }  @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) List<User> modify(User user); @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) List<User> create(UserAndPasswords passwords); @Path("password") @POST @Consumes(MediaType.APPLICATION_JSON) List<User> password(UserAndPasswords passwords);  }
@Test public void testPassword() { AdminUserResource resource = new AdminUserResource(); UserManagementService useradmin = mock(UserManagementService.class); when(useradmin.updatePassword(any())).thenReturn(getUsersForUserManagement()); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; List<User> users = getUsersForUserManagement(); User user = users.get(0); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "zecret", false); List<User> updatedUsers = resource.password(passwords); assertEquals(users.size(), updatedUsers.size()); }
@Test(expected=BadRequestException.class) public void testPasswordWithEmptyUsername() { AdminUserResource resource = new AdminUserResource(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; useradmin.setLogservice(logservice); UserAndPasswords passwords = new UserAndPasswords(null, "zecret", "zecret", false); resource.password(passwords); fail("Should never get here!"); }
@Test(expected=BadRequestException.class) public void testPasswordWhenPasswordsDontMatch() { AdminUserResource resource = new AdminUserResource(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; useradmin.setLogservice(logservice); List<User> users = getUsersForUserManagement(); User user = users.get(0); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "secret", true); resource.password(passwords); fail("Should never get here!"); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testPasswordDatabaseException() throws Exception { AdminUserResource resource = new AdminUserResource(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); resource.useradmin = useradmin; UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; ukelonn.setLogservice(logservice); useradmin.setLogservice(logservice); DataSource datasource = mock(DataSource.class); Connection connection = mock(Connection.class); when(datasource.getConnection()).thenReturn(connection); PreparedStatement statement = mock(PreparedStatement.class); when(connection.prepareStatement(anyString())).thenReturn(statement); when(statement.executeUpdate()).thenThrow(SQLException.class); useradmin.setDataSource(datasource); User user = new User(0, "validusername", null, null, null); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "zecret", false); resource.password(passwords); fail("Should never get here"); }