AdminUserResource { @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) public List<User> modify(User user) { try { return useradmin.modifyUser(user); } catch (AuthserviceException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /ukelonn/api/admin/user/modify failed to modify user %d", user.getUserid())); throw new InternalServerErrorException("See log for 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 testModify() { 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; String modifiedUsername = "gandalf"; String modifiedEmailaddress = "wizard@hotmail.com"; String modifiedFirstname = "Gandalf"; String modifiedLastname = "Grey"; User user = new User(1, modifiedUsername, modifiedEmailaddress, modifiedFirstname, modifiedLastname); when(useradmin.modifyUser(user)).thenReturn(Arrays.asList(user)); List<User> updatedUsers = resource.modify(user); User firstUser = updatedUsers.get(0); assertEquals(modifiedUsername, firstUser.getUsername()); assertEquals(modifiedEmailaddress, firstUser.getEmail()); assertEquals(modifiedFirstname, firstUser.getFirstname()); assertEquals(modifiedLastname, firstUser.getLastname()); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testModifyInternalServerError() throws Exception { AdminUserResource resource = new AdminUserResource(); UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); resource.ukelonn = ukelonn; UserManagementServiceProvider useradmin = new UserManagementServiceProvider(); resource.useradmin = useradmin; MockLogService logservice = new MockLogService(); resource.logservice = 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(); resource.modify(user); fail("Should never get here!"); }