@SuppressWarnings("unchecked") @Test void testValidateRoleMembershipGroupOtherExistingRole() { RoleMembership roleMembership = when(mock(RoleMembership.class).getId()).thenReturn("roleMembershipId").getMock(); User user = mock(User.class); when(roleMembership.getUser()).thenReturn(user); Role roleWithGroup = mock(Role.class); Group group = when(mock(Group.class).getId()).thenReturn("groupId").getMock(); when(roleWithGroup.getGroup()).thenReturn(group); when(roleMembership.getRole()).thenReturn(roleWithGroup); when(roleMembership.getUser()).thenReturn(user); RoleMembership otherRoleMembership = when(mock(RoleMembership.class).getId()).thenReturn("otherRoleMembershipId").getMock(); Role otherRole = mock(Role.class); when(otherRole.getGroup()).thenReturn(group); when(otherRoleMembership.getRole()).thenReturn(otherRole); Query<RoleMembership> query = mock(Query.class, RETURNS_SELF); when(dataService.query(RoleMembershipMetadata.ROLE_MEMBERSHIP, RoleMembership.class)) .thenReturn(query); when(query.eq(RoleMembershipMetadata.USER, user).findAll()) .thenReturn(Stream.of(otherRoleMembership)); assertThrows( RoleMembershipValidationException.class, () -> roleMembershipValidatorImpl.validate(roleMembership)); }
@Override public void validate(RoleMembership roleMembership) { Group group = roleMembership.getRole().getGroup(); if (group != null) { getUserRoleMemberships(roleMembership.getUser()) .filter( userRoleMembership -> isDifferentRoleMembership(userRoleMembership, roleMembership)) .forEach(otherUserRoleMembership -> validateGroup(otherUserRoleMembership, group)); } }
RoleMembershipValidatorImpl implements RoleMembershipValidator { @Override public void validate(RoleMembership roleMembership) { Group group = roleMembership.getRole().getGroup(); if (group != null) { getUserRoleMemberships(roleMembership.getUser()) .filter( userRoleMembership -> isDifferentRoleMembership(userRoleMembership, roleMembership)) .forEach(otherUserRoleMembership -> validateGroup(otherUserRoleMembership, group)); } } }
RoleMembershipValidatorImpl implements RoleMembershipValidator { @Override public void validate(RoleMembership roleMembership) { Group group = roleMembership.getRole().getGroup(); if (group != null) { getUserRoleMemberships(roleMembership.getUser()) .filter( userRoleMembership -> isDifferentRoleMembership(userRoleMembership, roleMembership)) .forEach(otherUserRoleMembership -> validateGroup(otherUserRoleMembership, group)); } } RoleMembershipValidatorImpl(DataService dataService); }
RoleMembershipValidatorImpl implements RoleMembershipValidator { @Override public void validate(RoleMembership roleMembership) { Group group = roleMembership.getRole().getGroup(); if (group != null) { getUserRoleMemberships(roleMembership.getUser()) .filter( userRoleMembership -> isDifferentRoleMembership(userRoleMembership, roleMembership)) .forEach(otherUserRoleMembership -> validateGroup(otherUserRoleMembership, group)); } } RoleMembershipValidatorImpl(DataService dataService); @Override void validate(RoleMembership roleMembership); }
RoleMembershipValidatorImpl implements RoleMembershipValidator { @Override public void validate(RoleMembership roleMembership) { Group group = roleMembership.getRole().getGroup(); if (group != null) { getUserRoleMemberships(roleMembership.getUser()) .filter( userRoleMembership -> isDifferentRoleMembership(userRoleMembership, roleMembership)) .forEach(otherUserRoleMembership -> validateGroup(otherUserRoleMembership, group)); } } RoleMembershipValidatorImpl(DataService dataService); @Override void validate(RoleMembership roleMembership);  }