CaseRolesValidations { public List<String> validateRolesAssignments(final CaseDefinitionSummary caseDefinition, final List<CaseRoleAssignmentSummary> assignments) { final List<String> errors = new ArrayList<>(); assignments.forEach(a -> { final Integer roleCardinality = caseDefinition.getRoles().get(a.getName()); if (roleCardinality == -1) { return; } final Integer roleAssignments = a.getUsers().size() + a.getGroups().size(); if (roleAssignments > roleCardinality) { errors.add(translationService.format(INVALID_ROLE_ASSIGNMENT, a.getName(), roleCardinality)); } }); return errors; }  List<String> validateRolesAssignments(final CaseDefinitionSummary caseDefinition,
                                                 final List<CaseRoleAssignmentSummary> assignments);  }
@Test public void testValidateRolesAssignments_roleCardinalityReached() { final Map<String, Integer> role = Collections.singletonMap("test", 1); final CaseDefinitionSummary cds = CaseDefinitionSummary.builder().roles(role).build(); final List<CaseRoleAssignmentSummary> roles = singletonList(CaseRoleAssignmentSummary.builder().name("test").users(singletonList("user1")).build()); final List<String> errors = caseRolesValidations.validateRolesAssignments(cds, roles); verify(translationService, never()).format(anyString(), anyString(), anyInt()); assertThat(errors).isEmpty(); }
@Test public void testValidateRolesAssignments_roleCardinalityExceeded() { final String caseRole = "test"; final Integer caseRoleCardinality = 1; final Map<String, Integer> role = Collections.singletonMap(caseRole, caseRoleCardinality); final CaseDefinitionSummary cds = CaseDefinitionSummary.builder().roles(role).build(); final List<CaseRoleAssignmentSummary> roles = singletonList(CaseRoleAssignmentSummary.builder().name("test").users(singletonList("user1")).groups(singletonList("group1")).build()); final List<String> errors = caseRolesValidations.validateRolesAssignments(cds, roles); verify(translationService).format(eq("InvalidRoleAssignment"), eq(caseRole), eq(caseRoleCardinality)); assertThat(errors).hasSize(1); }
@Test public void testValidateRolesAssignments_multipleRolesCardinalityExceeded() { final Map<String, Integer> roles = new HashMap<>(); final Integer roleCardinality = 1; roles.put("caseRole1", roleCardinality); roles.put("caseRole2", roleCardinality); final CaseDefinitionSummary cds = CaseDefinitionSummary.builder().roles(roles).build(); final List<CaseRoleAssignmentSummary> roleAssignments = new ArrayList<>(); roleAssignments.add(CaseRoleAssignmentSummary.builder().name("caseRole1").users(singletonList("user1")).groups(singletonList("group1")).build()); final List<String> assignedUsers = new ArrayList<>(); assignedUsers.add("user2"); assignedUsers.add("user3"); roleAssignments.add(CaseRoleAssignmentSummary.builder().name("caseRole2").users(assignedUsers).build()); final List<String> errors = caseRolesValidations.validateRolesAssignments(cds, roleAssignments); verify(translationService, times(2)).format(eq("InvalidRoleAssignment"), anyString(), eq(roleCardinality)); assertThat(errors).hasSize(2); }
@Test public void testValidateRolesAssignments_roleWithInfiniteCardinality() { final String caseRole = "test"; final Integer caseRoleCardinality = -1; final Map<String, Integer> role = Collections.singletonMap(caseRole, caseRoleCardinality); final CaseDefinitionSummary cds = CaseDefinitionSummary.builder().roles(role).build(); final CaseRoleAssignmentSummary caseRoleAssignmentSummaryMock = mock(CaseRoleAssignmentSummary.class); final List<CaseRoleAssignmentSummary> roles = singletonList(caseRoleAssignmentSummaryMock); when(caseRoleAssignmentSummaryMock.getName()).thenReturn(caseRole); when(caseRoleAssignmentSummaryMock.getUsers()).thenReturn(null); try{ caseRolesValidations.validateRolesAssignments(cds, roles); } catch (NullPointerException e) { Assert.fail("Case role cardinality = " + caseRoleCardinality + ", role assignments check should be skipped"); } }