UserManager { public User create(User newUser, boolean isTemporary) throws ConflictException, ServerException { requireNonNull(newUser, "Required non-null user"); if (reservedNames.contains(newUser.getName().toLowerCase())) { throw new ConflictException(String.format("Username '%s' is reserved", newUser.getName())); } final String userId = newUser.getId() != null ? newUser.getId() : generate("user", ID_LENGTH); final UserImpl user = new UserImpl( userId, newUser.getEmail(), newUser.getName(), firstNonNull(newUser.getPassword(), generate("", PASSWORD_LENGTH)), newUser.getAliases()); doCreate(user, isTemporary); eventService.publish(new UserCreatedEvent(user)); return user; } @Inject  UserManager(
      UserDao userDao,
      ProfileDao profileDao,
      PreferenceDao preferencesDao,
      EventService eventService,
      @Named("che.auth.reserved_user_names") String[] reservedNames); User create(User newUser, boolean isTemporary); void update(User user); User getById(String id); User getByAlias(String alias); User getByName(String name); User getByEmail(String email); Page<UserImpl> getAll(int maxItems, long skipCount); Page<? extends User> getByEmailPart(String emailPart, int maxItems, long skipCount); Page<? extends User> getByNamePart(String namePart, int maxItems, long skipCount); long getTotalCount(); void remove(String id); static final String PERSONAL_ACCOUNT; }
@Test public void shouldCreateAccountAndProfileAndPreferencesOnUserCreation() throws Exception { final UserImpl user = new UserImpl(null, "test@email.com", "testName", null, null); manager.create(user, false); verify(userDao).create(any(UserImpl.class)); verify(profileDao).create(any(ProfileImpl.class)); verify(preferencesDao).setPreferences(anyString(), anyMapOf(String.class, String.class)); }
@Test public void shouldGeneratePasswordWhenCreatingUserAndItIsMissing() throws Exception { final User user = new UserImpl(null, "test@email.com", "testName", null, null); manager.create(user, false); final ArgumentCaptor<UserImpl> userCaptor = ArgumentCaptor.forClass(UserImpl.class); verify(userDao).create(userCaptor.capture()); assertNotNull(userCaptor.getValue().getPassword()); }
@Test public void shouldGenerateIdentifierWhenCreatingUserWithNullId() throws Exception { final User user = new UserImpl(null, "test@email.com", "testName", null, null); manager.create(user, false); final ArgumentCaptor<UserImpl> userCaptor = ArgumentCaptor.forClass(UserImpl.class); verify(userDao).create(userCaptor.capture()); final String id = userCaptor.getValue().getId(); assertNotNull(id); }
@Test public void shouldNotGenerateIdentifierWhenCreatingUserWithNotNullId() throws Exception { final User user = new UserImpl("identifier", "test@email.com", "testName", null, null); manager.create(user, false); final ArgumentCaptor<UserImpl> userCaptor = ArgumentCaptor.forClass(UserImpl.class); verify(userDao).create(userCaptor.capture()); final String id = userCaptor.getValue().getId(); assertNotNull(id); assertEquals(id, "identifier"); }
@Test(expectedExceptions = ConflictException.class) public void shouldThrowConflictExceptionOnCreationIfUserNameIsReserved() throws Exception { final User user = new UserImpl("id", "test@email.com", "reserved"); manager.create(user, false); }
@Test public void shouldFirePostUserPersistedEventNewUserCreatedAndBeforeCommit() throws Exception { final UserImpl user = new UserImpl( "identifier", "test@email.com", "testName", "password", Collections.singletonList("alias")); manager.create(user, false); ArgumentCaptor<Object> firedEvents = ArgumentCaptor.forClass(Object.class); verify(eventService, times(2)).publish(firedEvents.capture()); Object event = firedEvents.getAllValues().get(0); assertTrue(event instanceof PostUserPersistedEvent, "Not a PostUserPersistedEvent"); assertEquals(((PostUserPersistedEvent) event).getUser(), user); }
@Test public void shouldFireUserCreatedEventOnNewUserCreated() throws Exception { final UserImpl user = new UserImpl( "identifier", "test@email.com", "testName", "password", Collections.singletonList("alias")); manager.create(user, false); ArgumentCaptor<Object> firedEvents = ArgumentCaptor.forClass(Object.class); verify(eventService, times(2)).publish(firedEvents.capture()); Object event = firedEvents.getAllValues().get(1); assertTrue(event instanceof UserCreatedEvent, "Not a UserCreatedEvent"); assertEquals(((UserCreatedEvent) event).getUser(), user); }
@Test public void shouldNotCreteUserWhenSubscriberThrowsExceptionOnCreatingNewUser() throws Exception { final UserImpl user = new UserImpl( "identifier", "test@email.com", "testName", "password", Collections.singletonList("alias")); doThrow(new ServerException("error")).when(postUserPersistedEvent).propagateException(); try { manager.create(user, false); fail("ServerException expected."); } catch (ServerException ignored) { } ArgumentCaptor<Object> firedEvents = ArgumentCaptor.forClass(Object.class); verify(eventService, times(1)).publish(firedEvents.capture()); assertTrue( firedEvents.getValue() instanceof PostUserPersistedEvent, "Not a PostUserPersistedEvent"); }