urn_perun_entityless_attribute_def_def_orgAups 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) { String value = map.get(mapKey); if (value == null || value.isEmpty()) throw new WrongAttributeValueException(attribute, "AUPs for key: '"+mapKey+"' can't be empty."); try { JSONArray array = new JSONArray(value); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); if (!object.has("version")) throw new WrongAttributeValueException(attribute, "AUP for key: '"+mapKey+"' is missing key version in JSON."); if (!object.has("date")) throw new WrongAttributeValueException(attribute, "AUP for key: '"+mapKey+"' is missing key date in JSON."); if (!object.has("link")) throw new WrongAttributeValueException(attribute, "AUP for key: '"+mapKey+"' is missing key link in JSON."); if (!object.has("text")) throw new WrongAttributeValueException(attribute, "AUP for key: '"+mapKey+"' is missing key text in JSON."); } } catch (JSONException ex) { throw new WrongAttributeValueException(attribute, "AUP for key: '"+mapKey+"' is not valid JSON.", ex); } } }  @Override void checkAttributeSyntax(PerunSessionImpl perunSession, String key, Attribute attribute); @Override AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithEmptyValue() throws Exception { System.out.println("testCheckValueWithEmptyValue()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", ""); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithMissingVersion() throws Exception { System.out.println("testCheckValueWithMissingVersion()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", "[{date: date, link: link, text: text}]"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithMissingDate() throws Exception { System.out.println("testCheckValueWithMissingDate()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", "[{version: version, link: link, text: text}]"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithMissingLink() throws Exception { System.out.println("testCheckValueWithMissingLink()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", "[{version: version, date: date, text: text}]"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckValueWithMissingText() throws Exception { System.out.println("testCheckValueWithMissingText()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", "[{version: version, date: date, link: link}]"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }
@Test public void testCorrectSyntax() throws Exception { System.out.println("testCorrectSyntax()"); Map<String, String> value = new LinkedHashMap<>(); value.put("key", "[{version: version, date: date, link: link, text: text}]"); attributeToCheck.setValue(value); classInstance.checkAttributeSyntax(session, "key", attributeToCheck); }