UserServiceImpl implements UserService { @Override @Transactional(rollbackFor = Throwable.class) public User createUser(User userTemplate) throws FieldValidationException { Preconditions.checkArgument(userTemplate != null, "userTemplate is required"); validateUser(userTemplate); User userToCreate = new User(); userToCreate.setDisplayName(userTemplate.getDisplayName()); userToCreate.setEmail(userTemplate.getEmail()); userToCreate.setIntegrationData(userTemplate.getIntegrationData()); userToCreate.setIsBlocked(userTemplate.getIsBlocked()); userToCreate.setLocale(userTemplate.getLocale()); userToCreate.setRegisteredAt(userTemplate.getRegisteredAt()); userToCreate.setTimeZone(userTemplate.getTimeZone()); if (userTemplate.getUuid() != null) { Preconditions.checkArgument(stringIdGenerator.isValidId(userTemplate.getUuid()), "User id is invalid"); userToCreate.setUuid(userTemplate.getUuid()); } else { userToCreate.setUuid(stringIdGenerator.generateNewId(userTemplate)); } if (userToCreate.getRegisteredAt() == 0) { userToCreate.setRegisteredAt(new Date().getTime()); } if (userToCreate.getLocale() == null) { userToCreate.setLocale(LocaleContextHolder.getLocale().toString()); } if (userToCreate.getTimeZone() == null) { userToCreate.setTimeZone(Calendar.getInstance().getTimeZone().getID()); } try { userDao.createUser(userToCreate); eventBus.post(EntityChangedEvent.added(userToCreate)); } catch (DuplicateKeyException dke) { throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL)); } catch (Throwable t) { String msg = String.format("Failed to create user with email '%s'", userToCreate.getEmail()); throw new UserServiceUnexpectedException(msg, t); } return userToCreate; }  @Override @Transactional(rollbackFor = Throwable.class) User createUser(User userTemplate); @Override User getUserByUuid(String userUuid); @Override User getUserByEmail(String userEmail); @Override PaginatedList<User> findUsersByDisplayNamePartial(String displayNamePartial, PagerParams pagerParams); @Override @Transactional(rollbackFor = Throwable.class) void updateUser(User user); @Override @Transactional(rollbackFor = Throwable.class) void deleteUserByUuid(String userUuid); UserDao getUserDao(); void setUserDao(UserDao userDao); EventBus getEventBus(); @Required void setEventBus(EventBus eventBus); StringIdGenerator getStringIdGenerator(); @Autowired(required = false) void setStringIdGenerator(StringIdGenerator stringIdGenerator);  }
@Test(expected = FieldValidationException.class) public void testCreateUser_blackbox_expectValidationExceptionOnInvalidEmail1() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User user = UserFactory.createNewUserTemplate(); user.setEmail(null); fixture.createUser(user); fail(); }
@Test(expected = IllegalArgumentException.class) public void testCreateUser_defensive_expectIllegalArgumentExceptionInsteadOfNpe() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); fixture.createUser(null); fail(); }
@Test public void testCreateUser_blackbox_expectGuidWillBeCreatedForTheUser() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User result = fixture.createUser(UserFactory.createNewUserTemplate()); assertNotNull(result); assertNotNull(result.getUuid()); }
@Test public void testCreateUser_blackbox_expectAllFieldsEqualsForCreatedUser() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User user = UserFactory.createNewUserTemplate(); User createdUser = fixture.createUser(user); assertNotNull(createdUser); assertEquals(createdUser.getDisplayName(), user.getDisplayName()); assertEquals(createdUser.getEmail(), user.getEmail()); assertEquals(createdUser.getIntegrationData(), user.getIntegrationData()); assertEquals(createdUser.getIsBlocked(), user.getIsBlocked()); assertEquals(createdUser.getLocale(), user.getLocale()); assertEquals(createdUser.getRegisteredAt(), user.getRegisteredAt()); assertEquals(createdUser.getTimeZone(), user.getTimeZone()); }
@Test public void testCreateUser_blackbox_expectRegisteredAtWillBePopulated() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User user = UserFactory.createNewUserTemplate(); user.setRegisteredAt(0); User createdUser = fixture.createUser(user); assertNotNull(createdUser); assertTrue(createdUser.getRegisteredAt() != 0L); }
@Test public void testCreateUser_whitebox_expectReturnedValueIsNotTheSameReferenceAsParameter() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User template = UserFactory.createNewUserTemplate(); User result = fixture.createUser(template); assertNotNull(result); assertNotSame(template, result); }
@Test(expected = FieldValidationException.class) public void testCreateUser_blackbox_expectValidationExceptionOnInvalidEmail() throws Exception { UserServiceImpl fixture = UserServiceImplFactory.createUsersServiceImpl(); User user = UserFactory.createNewUserTemplate(); user.setEmail("abara-cadabara"); fixture.createUser(user); fail(); }