UserEditorDriver implements Driver<User, UserEditor> { public boolean flush() { assert this.isEditMode; this.isFlushed = true; userEditor.flush(); userEditor.attributesEditor().flush(); userEditor.groupsExplorer().flush(); userEditor.rolesExplorer().flush(); final String id = userEditor.identifier(); final Map<String, String> properties = userEditor.attributesEditor().getValue(); final Set<Group> groups = userEditor.groupsExplorer().getValue(); final Set<Role> roles = userEditor.rolesExplorer().getValue(); user = new UserImpl(id, roles, groups, properties); Set<ConstraintViolation<User>> violations = userSystemManager.usersValidator().validate(user); userEditor.setViolations(violations); return violations == null || violations.isEmpty(); } @Inject  UserEditorDriver(final ClientUserSystemManager userSystemManager); User createNewUser(final String identifier); void show(final User user,
                     final UserEditor userEditor); void edit(final User user,
                     final UserEditor userEditor); boolean flush(); @Override User getValue();  }
@Test public void testFlush() { final User user = mock(User.class); final Set<Role> roles = new HashSet<Role>(); when(user.getRoles()).thenReturn(roles); final UserEditor userEditor = mock(UserEditor.class); final UserAttributesEditor attributesEditor = mock(UserAttributesEditor.class); final UserAssignedGroupsExplorer groupsExplorer = mock(UserAssignedGroupsExplorer.class); final UserAssignedRolesExplorer rolesExplorer = mock(UserAssignedRolesExplorer.class); when(userEditor.identifier()).thenReturn("user1"); when(userEditor.attributesEditor()).thenReturn(attributesEditor); final Map<String, String> attributes = new HashMap<String, String>(); when(attributesEditor.getValue()).thenReturn(attributes); final Set<Group> groups = new HashSet<Group>(); when(userEditor.groupsExplorer()).thenReturn(groupsExplorer); when(groupsExplorer.getValue()).thenReturn(groups); final Set<Role> _roles = new HashSet<Role>(); when(userEditor.rolesExplorer()).thenReturn(rolesExplorer); when(rolesExplorer.getValue()).thenReturn(_roles); final EntityValidator<User> userEntityValidator = mock(EntityValidator.class); when(userSystemManager.usersValidator()).thenReturn(userEntityValidator); final Set<ConstraintViolation<User>> violations = mock(Set.class); when(violations.isEmpty()).thenReturn(true); when(userEntityValidator.validate(any(User.class))).thenReturn(violations); tested.user = user; tested.userEditor = userEditor; tested.isFlushed = false; tested.isEditMode = true; tested.flush(); User result = tested.getValue(); verify(userEditor, times(1)).flush(); verify(attributesEditor, times(1)).flush(); verify(groupsExplorer, times(1)).flush(); verify(rolesExplorer, times(1)).flush(); verify(attributesEditor, times(1)).getValue(); verify(groupsExplorer, times(1)).getValue(); verify(rolesExplorer, times(1)).getValue(); verify(userEntityValidator, times(1)).validate(any(User.class)); assertEquals("user1", result.getIdentifier()); assertEquals(roles, result.getRoles()); assertEquals(groups, result.getGroups()); assertEquals(_roles, result.getRoles()); assertEquals(attributes, result.getProperties()); }