AdminUserResource { @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) public List<User> create(UserAndPasswords passwords) { try { List<User> users = useradmin.addUser(passwords); String username = passwords.getUser().getUsername(); Optional<User> createdUser = users.stream().filter(u -> username.equals(u.getUsername())).findFirst(); if (!createdUser.isPresent()) { throw new UkelonnException(String.format("Found no user matching %s in the users table", username)); } no.priv.bang.ukelonn.beans.User user = new no.priv.bang.ukelonn.beans.User(createdUser.get().getUserid(), username, createdUser.get().getEmail(), createdUser.get().getFirstname(), createdUser.get().getLastname()); ukelonn.addAccount(user); return users; } catch (AuthserviceException e) { logservice.log(LogService.LOG_ERROR, "REST endpoint /ukelonn/api/admin/user/create got error from the database", e); throw new InternalServerErrorException("See log for error cause"); } }  @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 testCreate() { AdminUserResource resource = new AdminUserResource(); UserManagementService useradmin = mock(UserManagementService.class); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; int originalUserCount = getUsers().size(); List<User> originalUsersPlusOne = new ArrayList<>(getUsersForUserManagement()); String newUsername = "aragorn"; String newEmailaddress = "strider@hotmail.com"; String newFirstname = "Aragorn"; String newLastname = "McArathorn"; User user = new User(0, newUsername, newEmailaddress, newFirstname, newLastname); originalUsersPlusOne.add(user); when(useradmin.addUser(any())).thenReturn(originalUsersPlusOne); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "zecret", false); List<User> updatedUsers = resource.create(passwords); assertThat(updatedUsers.size()).isGreaterThan(originalUserCount); User lastUser = updatedUsers.get(updatedUsers.size() - 1); assertEquals(newUsername, lastUser.getUsername()); assertEquals(newEmailaddress, lastUser.getEmail()); assertEquals(newFirstname, lastUser.getFirstname()); assertEquals(newLastname, lastUser.getLastname()); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testCreatePasswordsNotIdentical() throws Exception { AdminUserResource resource = new AdminUserResource(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); DataSource authservicedatasource = mock(DataSource.class); when(authservicedatasource.getConnection()).thenThrow(SQLException.class); useradmin.setDataSource(authservicedatasource); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; useradmin.setLogservice(logservice); int originalUserCount = getUsers().size(); String newUsername = "aragorn"; String newEmailaddress = "strider@hotmail.com"; String newFirstname = "Aragorn"; String newLastname = "McArathorn"; User user = new User(0, newUsername, newEmailaddress, newFirstname, newLastname); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "secret", true); List<User> updatedUsers = resource.create(passwords); assertThat(updatedUsers.size()).isGreaterThan(originalUserCount); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testCreateDatabaseException() throws Exception { AdminUserResource resource = new AdminUserResource(); UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); 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); MockLogService logservice = new MockLogService(); useradmin.setLogservice(logservice); resource.ukelonn = ukelonn; resource.useradmin = useradmin; resource.logservice = logservice; String newUsername = "aragorn"; String newEmailaddress = "strider@hotmail.com"; String newFirstname = "Aragorn"; String newLastname = "McArathorn"; User user = new User(0, newUsername, newEmailaddress, newFirstname, newLastname); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "zecret", false); resource.create(passwords); fail("Should never get here"); }
@Test(expected=InternalServerErrorException.class) public void testCreateWhenUseridToCreateAccountCantBeFound() throws Exception { AdminUserResource resource = new AdminUserResource(); UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); DataSource authservicedatasource = mock(DataSource.class); Connection connection = mock(Connection.class); PreparedStatement statement = mock(PreparedStatement.class); ResultSet results = mock(ResultSet.class); when(statement.executeQuery()).thenReturn(results); when(connection.prepareStatement(anyString())).thenReturn(statement); when(authservicedatasource.getConnection()).thenReturn(connection); useradmin.setDataSource(authservicedatasource); resource.useradmin = useradmin; UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; useradmin.setLogservice(logservice); String newUsername = "aragorn"; String newEmailaddress = "strider@hotmail.com"; String newFirstname = "Aragorn"; String newLastname = "McArathorn"; User user = new User(0, newUsername, newEmailaddress, newFirstname, newLastname); UserAndPasswords passwords = new UserAndPasswords(user, "zecret", "zecret", false); List<User> updatedUsers = resource.create(passwords); assertThat(updatedUsers).isNotEmpty(); }