urn_perun_entityless_attribute_def_def_usedGids extends EntitylessAttributesModuleAbstract implements EntitylessAttributesModuleImplApi { @Override public void checkAttributeSyntax(PerunSessionImpl perunSession, String key, Attribute attribute) throws WrongAttributeValueException { if(attribute.getValue() == null) return; Map<String, String> map = attribute.valueAsMap(); Set<String> mapKeys = map.keySet(); for(String mapKey: mapKeys) { if(mapKey == null) throw new WrongAttributeValueException(attribute, key, "Key in usedGids can't be null."); Matcher keyMatcher = keyPattern.matcher(mapKey); if(!keyMatcher.matches()) throw new WrongAttributeValueException(attribute, key, "Key in usedGids can be only in format 'Rx', 'Gx', 'Dx' where 'x' is positive integer."); String value = map.get(mapKey); if(value == null) throw new WrongAttributeValueException(attribute, key, "Value in usedGids can't be null."); Matcher valueMatcher = valuePattern.matcher(value); if(!valueMatcher.matches()) throw new WrongAttributeValueException(attribute, key, "Key in usedGids can be only positive integer."); } for(String mapKey: mapKeys) { if(mapKey.startsWith("D")) continue; String value = map.get(mapKey); if(map.containsKey("D" + value)) throw new WrongAttributeValueException(attribute, key, "This gid can't be depleted and used at the same time!"); } }  @Override void checkAttributeSyntax(PerunSessionImpl perunSession, String key, Attribute attribute); @Override AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithNullKeyInMap() throws Exception { System.out.println("testCheckValueWithNullKeyInMap()"); Map<String, String> value = new LinkedHashMap<>(); value.put(null, ""); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithWrongKeyInMap() throws Exception { System.out.println("testCheckValueWithWrongKeyInMap()"); Map<String, String> value = new LinkedHashMap<>(); value.put("bad value", ""); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithNullValueInMap() throws Exception { System.out.println("testCheckValueWithNullValueInMap()"); Map<String, String> value = new LinkedHashMap<>(); value.put("R11", null); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithWrongValueInMap() throws Exception { System.out.println("testCheckValueWithWrongValueInMap()"); Map<String, String> value = new LinkedHashMap<>(); value.put("R11", "bad value"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithDepletedAndUsedValue() throws Exception { System.out.println("testCheckValueWithNullKey()"); Map<String, String> value = new LinkedHashMap<>(); value.put("R11", "11"); value.put("D11", "11"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test public void testCorrectSyntax() throws Exception { System.out.println("testCheckValueWithNullKey()"); Map<String, String> value = new LinkedHashMap<>(); value.put("R11", "11"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }