urn_perun_group_resource_attribute_def_def_o365EmailAddresses_mu extends GroupResourceAttributesModuleAbstract implements GroupResourceAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Resource resource, 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, resource, group, ADNAME_ATTRIBUTE); if (adName.getValue() != null) { throw new WrongReferenceAttributeValueException(attribute, adName, resource, group, resource, group, "at least one email must be defined"); } return; } } catch (AttributeNotExistsException | GroupResourceMismatchException e) { throw new InternalErrorException(e.getMessage(), e); } AttributesManagerBl attributesManagerBl = sess.getPerunBl().getAttributesManagerBl(); Set<Pair<Integer, Integer>> groupResourcePairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, attribute); groupResourcePairs.remove(new Pair<>(group.getId(), resource.getId())); if (!groupResourcePairs.isEmpty()) { throw new WrongReferenceAttributeValueException(attribute, attribute, group, resource, "some of the email addresses are already assigned to the following group_resource pairs: " + groupResourcePairs); } Attribute memberO365EmailAddresses = new Attribute(new urn_perun_member_attribute_def_def_o365EmailAddresses_mu().getAttributeDefinition()); memberO365EmailAddresses.setValue(emails); Set<Pair<Integer, Integer>> memberPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, memberO365EmailAddresses); if (!memberPairs.isEmpty()) { throw new WrongReferenceAttributeValueException(attribute, memberO365EmailAddresses, group, resource, "member " + BeansUtils.getSingleId(memberPairs) + " "); } }  @Override void checkAttributeSyntax(PerunSessionImpl sess, Group group, Resource resource, Attribute attribute); @Override void checkAttributeSemantics(PerunSessionImpl sess, Group group, Resource resource, Attribute attribute); @Override Attribute fillAttribute(PerunSessionImpl sess, Group group, Resource resource, 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, resource, 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, resource, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckValueExistIfAdNameSet() throws Exception { System.out.println("testCheckValueExistIfAdNameSet()"); attributeToCheck.setValue(Lists.newArrayList()); classInstance.checkAttributeSemantics(session, group, resource, 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("group_resource")))) .thenReturn(Sets.newHashSet(new Pair<>(group.getId(),resource.getId()))); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member")))) .thenReturn(Sets.newHashSet()); classInstance.checkAttributeSemantics(session, group, resource, 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("group_resource")))) .thenReturn(Sets.newHashSet(new Pair<>(1000,2000))); when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member")))) .thenReturn(Sets.newHashSet()); classInstance.checkAttributeSemantics(session, group, resource, attributeToCheck); }