UserService { public User createBasicUser(String username, String givenName, String surname, String commonName, boolean partiallyCreated) { logger.debug("Creating user: {}", username); String randomPassword = RandomStringUtils.randomAlphanumeric(15); User userWithRole = createUserWithRole(username, randomPassword, Role.USER, givenName, surname, commonName, partiallyCreated); logger.debug("Manually setting created by user to system user because at this point, there isn't an authenticated user context"); updateCreatedByUserToSystemUser(userWithRole); return userWithRole; }  User createUserWithRole(String username, String password, Role role, String givenName, String surname, String commonName, boolean partiallyCreated); @Transactional User saveUserWithRole(User user, String password, Role role, String givenName, String surname, String commonName, boolean partiallyCreated); User createUserWithRole(String username, String password, Role role); String createAuthorityName(Role role); User findSystemUser(); @Transactional void updateCreatedByUserToSystemUser(User userToUpdate); @Transactional void deleteUser(User user); User createBasicUser(String username, String givenName, String surname, String commonName, boolean partiallyCreated); User createOrUpdateBasicUser(User user, String username, String givenName, String surname, String commonName); User getOrCreatePartialBasicUser(String username); User getOrCreateOrUpdateBasicUser(String username, String givenName, String surname, String commonName); static final String SYSTEM_USERNAME; }
@Test public void testCreateBasicUser() { String username = testIdWatcher.getEntityName("testUser"); String pwd = "testPwd1234"; String surname = "surname"; String givenName = "givenName"; String commonName = "commonName"; Role userRole = Role.USER; String expectedAuthorityName = userService.createAuthorityName(userRole); User userWithRole = userService.createUserWithRole(username, pwd, userRole, givenName, surname, commonName, false); User byUsername = userRepository.findByUsername(username); assertEquals("ID should be the same", userWithRole.getId(), byUsername.getId()); assertNotSame("Password should not be plain", pwd, byUsername.getPassword()); assertFalse("Should have at least one authority", byUsername.getAuthorities().isEmpty()); assertEquals("Should have user role", expectedAuthorityName, byUsername.getAuthorities().iterator().next().getAuthority()); assertEquals(surname, byUsername.getSurname()); assertEquals(givenName, byUsername.getGivenName()); assertEquals(commonName, byUsername.getCommonName()); }