urn_perun_user_attribute_def_def_IPAddresses extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi { @Override public void checkAttributeSyntax(PerunSessionImpl perunSession, User user, Attribute attribute) throws WrongAttributeValueException { List<String> value = attribute.valueAsList(); if (value != null) { for (String address : value) { Matcher matcherIPv4 = IPv4_PATTERN.matcher(address); Matcher matcherIPv6 = IPv6_PATTERN.matcher(address); Matcher matcherIPv6Short = IPv6_PATTERN_SHORT.matcher(address); if (!matcherIPv4.matches() && !matcherIPv6.matches() && !matcherIPv6Short.matches()) throw new WrongAttributeValueException(attribute, "IP address is not in the correct format."); } } }  @Override void checkAttributeSyntax(PerunSessionImpl perunSession, User user, Attribute attribute); @Override AttributeDefinition getAttributeDefinition();  }
@Test public void testCheckAttributeSyntax() throws Exception { System.out.println("testCheckAttributeSyntax()"); Attribute attributeToCheck = new Attribute(); List<String> ipAddresses = new ArrayList<>(); ipAddresses.add("255.255.255.255"); ipAddresses.add("192.168.1.0"); ipAddresses.add("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); ipAddresses.add("2001:db8:85a3:8d3:1319:8a2e:370:7348"); ipAddresses.add("2001:db8::2:1"); attributeToCheck.setValue(ipAddresses); classInstance.checkAttributeSyntax(session, user, attributeToCheck); }
@Test(expected = WrongAttributeValueException.class) public void testCheckAttributeSyntaxWithWrongValue() throws Exception { System.out.println("testCheckAttributeSyntaxWithWrongValue()"); Attribute attributeToCheck = new Attribute(); List<String> ipAddresses = new ArrayList<>(); ipAddresses.add("123"); attributeToCheck.setValue(ipAddresses); classInstance.checkAttributeSyntax(session, user, attributeToCheck); }