urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu extends GroupAttributesModuleAbstract implements GroupAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { List<String> emails = attribute.valueAsList(); if (emails == null) emails = new ArrayList<>(); AttributesManagerBl am = sess.getPerunBl().getAttributesManagerBl(); try { if (emails.isEmpty()) { Attribute adName = am.getAttribute(sess, group, ADNAME_ATTRIBUTE); if (adName.getValue() != null) { throw new WrongReferenceAttributeValueException(attribute, adName, group, null, group, null, "at least one email must be defined"); } return; } } catch (AttributeNotExistsException e) { throw new InternalErrorException(e.getMessage(), e); } try { AttributesManagerBl attributesManagerBl = sess.getPerunBl().getAttributesManagerBl(); Attribute memberO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE)); memberO365EmailAddresses.setValue(emails); Set<Pair<Integer, Integer>> memberPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, memberO365EmailAddresses); if (!memberPairs.isEmpty()) { throw new WrongReferenceAttributeValueException(attribute, memberO365EmailAddresses, group, null, "member " + BeansUtils.getSingleId(memberPairs) + " "); } } catch(AttributeNotExistsException ex) { log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE, attribute); } }  @Override void checkAttributeSyntax(PerunSessionImpl sess, Group group, Attribute attribute); @Override void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute); @Override Attribute fillAttribute(PerunSessionImpl sess, Group group, AttributeDefinition attrDef); @Override List<String> getDependencies(); @Override AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckValueExistIfAdNameSetWithNull() throws Exception { System.out.println("testCheckValueExistIfAdNameSetWithNull()"); attributeToCheck.setValue(null); classInstance.checkAttributeSemantics(session, group, attributeToCheck); }
@Test public void testCheckNullForNullAdName() throws Exception { System.out.println("testCheckNullForNullAdName()"); attributeToCheck.setValue(null); when(adNameAttr.getValue()).thenReturn(null); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session),any(Attribute.class))).thenReturn(Sets.newHashSet()); classInstance.checkAttributeSemantics(session, group, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckValueExistIfAdNameSet() throws Exception { System.out.println("testCheckValueExistIfAdNameSet()"); attributeToCheck.setValue(Lists.newArrayList()); classInstance.checkAttributeSemantics(session, group, attributeToCheck); }
@Test public void testUniqItself() throws Exception { System.out.println("testUniqItself"); attributeToCheck.setValue(new ArrayList<>(Arrays.asList("my@example.com", "my2@google.com"))); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member")))) .thenReturn(Sets.newHashSet()); classInstance.checkAttributeSemantics(session, group, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testUniqClash() throws Exception { System.out.println("testUniqClash"); attributeToCheck.setValue(new ArrayList<>(Arrays.asList("my@example.com", "my2@google.com"))); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member")))) .thenReturn(Sets.newHashSet(new Pair<>(10, 0))); classInstance.checkAttributeSemantics(session, group, attributeToCheck); }