urn_perun_member_attribute_def_def_o365EmailAddresses_mu extends MemberAttributesModuleAbstract { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Member member, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { log.trace("checkAttributeSemantics(member={},attribute={})", member, attribute); List<String> emails; if (attribute.getValue() == null) { throw new WrongReferenceAttributeValueException(attribute, "can't be null."); } else { emails = attribute.valueAsList(); } Attribute attrUCO = getUserUco(sess, member); String UCO = attrUCO.valueAsString(); if(UCO == null) { throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, UCO_ATTRIBUTE + " has null value!"); } String ucoEmail = UCO + "@muni.cz"; if (!emails.contains(ucoEmail)) { throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, member, null, "does not contain " + ucoEmail); } try { Attribute groupO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR)); groupO365EmailAddresses.setValue(emails); Set<Integer> groupsWithSameAttributeValueSet = sess.getPerunBl().getAttributesManagerBl().getPerunBeanIdsForUniqueAttributeValue(sess, groupO365EmailAddresses).stream() .map(Pair::getLeft) .collect(Collectors.toSet()); if (!groupsWithSameAttributeValueSet.isEmpty()) { try { Group groupWithDuplicateEmail = sess.getPerunBl().getGroupsManagerBl().getGroupById(sess, groupsWithSameAttributeValueSet.iterator().next()); throw new WrongReferenceAttributeValueException(attribute, groupO365EmailAddresses, member, null, groupWithDuplicateEmail, null, "some of the email addresses are already assigned."); } catch (GroupNotExistsException e) { throw new ConsistencyErrorException(e); } } } catch (AttributeNotExistsException ex) { log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR, attribute); } }  void checkAttributeSyntax(PerunSessionImpl perunSession, Member member, Attribute attribute); @Override void checkAttributeSemantics(PerunSessionImpl sess, Member member, Attribute attribute); @Override List<String> getDependencies(); @Override Attribute fillAttribute(PerunSessionImpl sess, Member member, AttributeDefinition attrDef); @Override AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckNull() throws Exception { System.out.println("testCheckNull()"); attributeToCheck.setValue(null); classInstance.checkAttributeSemantics(session, member, attributeToCheck); }
@Test public void testCheckMissingUco() throws Exception { System.out.println("testCheckMissingUco()"); attributeToCheck.setValue(Lists.newArrayList("my@example.com", "aaa@bbb.com")); try { classInstance.checkAttributeSemantics(session, member, attributeToCheck); fail("should throw WrongReferenceAttributeValueException"); } catch (WrongReferenceAttributeValueException ex) { assertThat(ex.getMessage(), endsWith("does not contain 123456@muni.cz")); } }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); attributeToCheck.setValue(Lists.newArrayList("my@example.com", "aaa@bbb.com", uco + "@muni.cz")); classInstance.checkAttributeSemantics(session, member, attributeToCheck); }
@Test public void testClashGroupResource() throws Exception { System.out.println("testClashGroupResource()"); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("group")))) .thenReturn(Sets.newHashSet(new Pair<>(55, 0))); Group groupWithDuplicateEmail = mock(Group.class); GroupsManagerBl groupsManagerBl = mock(GroupsManagerBl.class); when(session.getPerunBl().getGroupsManagerBl()).thenReturn(groupsManagerBl); when(session.getPerunBl().getGroupsManagerBl().getGroupById(session, 55)).thenReturn(groupWithDuplicateEmail); attributeToCheck.setValue(Lists.newArrayList("my@example.com", uco + "@muni.cz")); try { classInstance.checkAttributeSemantics(session, member, attributeToCheck); fail("should throw WrongReferenceAttributeValueException"); } catch (WrongReferenceAttributeValueException ex) { assertThat(ex.getMessage(), endsWith("some of the email addresses are already assigned.")); } }