urn_perun_group_attribute_def_def_unixGID_namespace extends GroupAttributesModuleAbstract implements GroupAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException{ try{ String gidNamespace = attribute.getFriendlyNameParameter(); if(attribute.getValue() == null) { List<Facility> groupFacilities = new ArrayList<>(); for(Resource r: sess.getPerunBl().getResourcesManagerBl().getAssignedResources(sess, group)) { groupFacilities.add(sess.getPerunBl().getResourcesManagerBl().getFacility(sess, r)); } Set<String> namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere = sess.getPerunBl().getModulesUtilsBl().getSetOfGroupNameNamespacesWhereFacilitiesHasTheSameGIDNamespace(sess, groupFacilities, attribute); for(String namespace : namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere) { Attribute unixGroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, A_G_unixGroupName_namespace + ":" + namespace); if(unixGroupName.getValue() != null) { throw new WrongReferenceAttributeValueException(attribute, unixGroupName, group, null, group, null, "Group is propagated to the facility where it have set unix group name so it must have unix GID too."); } } return; } Integer attrValue = attribute.valueAsInteger(); 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, group, null, gidNamespace, null, "This GID is already depleted."); } } try { sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute); } catch (WrongAttributeValueException e) { throw new WrongReferenceAttributeValueException(e); } Attribute groupGIDAttribute = attribute; Attribute resourceGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGID_namespace + ":" + gidNamespace)); resourceGIDAttribute.setValue(groupGIDAttribute.getValue()); List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute)); List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute)); allGroupsWithSameGIDInSameNamespace.remove(group); List <Attribute> groupNamesOfGroup = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGroupName_namespace + ":"); if(!allGroupsWithSameGIDInSameNamespace.isEmpty()) { for(Group g: allGroupsWithSameGIDInSameNamespace) { for(Attribute a: groupNamesOfGroup) { int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, group, null, g, null, "There is a group with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + g + " " + group); } } } } if(!allResourcesWithSameGIDInSameNamespace.isEmpty()) { for(Resource r: allResourcesWithSameGIDInSameNamespace) { for(Attribute a: groupNamesOfGroup) { Attribute resourceGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + a.getFriendlyNameParameter())); resourceGroupName.setValue(a.getValue()); int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, resourceGroupName); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, group, null, r, null, "There is a resource with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + r + " " + group); } } } } } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } }  @Override Attribute fillAttribute(PerunSessionImpl sess, Group group, AttributeDefinition attributeDefinition); @Override void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute); @Override void changedAttributeHook(PerunSessionImpl session, Group group, 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, group, 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, group, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testGroupWithSameGID() throws Exception { System.out.println("testGroupWithSameGID()"); attributeToCheck.setValue(5); Group group2 = new Group(2,"group2","Group 2",null,null,null,null,0,0); when(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, attributeToCheck)).thenReturn(Arrays.asList(group, group2)); when(sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGroupName-namespace:")).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, group2, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testResourceWithSameGID() throws Exception { System.out.println("testResourceWithSameGID()"); attributeToCheck.setValue(5); Resource resource = mock(Resource.class); when(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, attributeToCheck)).thenReturn(Collections.singletonList(resource)); when(sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGroupName-namespace:")).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, resource, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); attributeToCheck.setValue(5); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }