urn_perun_resource_attribute_def_def_unixGID_namespace extends ResourceAttributesModuleAbstract implements ResourceAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Resource resource, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException{ try{ String gidNamespace = attribute.getFriendlyNameParameter(); Integer attrValue; if(attribute.getValue() == null) { throw new WrongReferenceAttributeValueException(attribute, null, resource, null, "Unix GID must be set"); } else { attrValue = attribute.valueAsInteger(); } try { sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute); } catch (WrongAttributeValueException ex) { throw new WrongReferenceAttributeValueException(ex); } Attribute usedGids = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, gidNamespace, A_E_usedGids); if(usedGids.getValue() != null) { Map<String, String> usedGidsValue = usedGids.valueAsMap(); if(usedGidsValue.containsKey("D" + attrValue.toString())) { throw new WrongReferenceAttributeValueException(attribute, usedGids, resource, null, gidNamespace, null, "This GID is already depleted."); } } Attribute resourceGIDAttribute = attribute; Attribute groupGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_G_unixGID_namespace + ":" + gidNamespace)); groupGIDAttribute.setValue(resourceGIDAttribute.getValue()); List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute)); List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute)); allResourcesWithSameGIDInSameNamespace.remove(resource); List <Attribute> groupNamesOfResource = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, resource, A_R_unixGroupName_namespace + ":"); if(!allGroupsWithSameGIDInSameNamespace.isEmpty()) { for(Group g: allGroupsWithSameGIDInSameNamespace) { for(Attribute a: groupNamesOfResource) { Attribute groupGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_G_unixGroupName_namespace + ":" + a.getFriendlyNameParameter())); groupGroupName.setValue(a.getValue()); int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, groupGroupName); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, "There is a group with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + g + " " + resource); } } } } if(!allResourcesWithSameGIDInSameNamespace.isEmpty()) { for(Resource r: allResourcesWithSameGIDInSameNamespace) { for(Attribute a: groupNamesOfResource) { int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, a); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, "There is a resource with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + r + " " + resource); } } } } } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } }  @Override Attribute fillAttribute(PerunSessionImpl sess, Resource resource, AttributeDefinition attributeDefinition); @Override void checkAttributeSemantics(PerunSessionImpl sess, Resource resource, Attribute attribute); @Override void changedAttributeHook(PerunSessionImpl session, Resource resource, Attribute attribute); @Override List<String> getDependencies();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testUnixGroupNameSet() throws Exception { System.out.println("testUnixGroupNameSet()"); Set<String> set = new HashSet<>(); set.add(attributeToCheck.getNamespace()); when(sess.getPerunBl().getModulesUtilsBl().getSetOfGroupNameNamespacesWhereFacilitiesHasTheSameGIDNamespace(sess, new ArrayList<>(), attributeToCheck)).thenReturn(set); reqAttribute.setValue("value"); classInstance.checkAttributeSemantics(sess, resource, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testDepletedGIDValue() throws Exception { System.out.println("testDepletedGIDValue()"); attributeToCheck.setValue(5); Map<String, String> map = new LinkedHashMap<>(); map.put("D5", "value"); reqAttribute.setValue(map); classInstance.checkAttributeSemantics(sess, resource, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testResourceWithSameGID() throws Exception { System.out.println("testResourceWithSameGID()"); attributeToCheck.setValue(5); Resource resource2 = new Resource(); when(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, attributeToCheck)).thenReturn(Arrays.asList(resource, resource2)); when(sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGroupName-namespace:")).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, resource, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, resource, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testGroupWithSameGID() throws Exception { System.out.println("testGroupWithSameGID()"); attributeToCheck.setValue(5); Group group = new Group(); when(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, attributeToCheck)).thenReturn(Collections.singletonList(group)); when(sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGroupName-namespace:")).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, group, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, resource, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); attributeToCheck.setValue(5); classInstance.checkAttributeSemantics(sess, resource, attributeToCheck); }