urn_perun_user_attribute_def_def_userPreferredCertDN extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongAttributeAssignmentException, WrongReferenceAttributeValueException { Attribute userCertDNs; try { userCertDNs = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs"); } catch (AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } Map<String, String> certDNsValue; if(userCertDNs.getValue() != null) { certDNsValue = userCertDNs.valueAsMap(); } else { if(attribute.getValue() != null) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "There is no certificates for this user so preferred certificate can't be choose."); else return; } if(attribute.getValue() == null) { if(certDNsValue != null && !certDNsValue.isEmpty()) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "This attribute value can't be null because of notNull attribute userCertDNs"); } else { String preferredCertDNValue = attribute.valueAsString(); if(!certDNsValue.containsKey(preferredCertDNValue)) throw new WrongReferenceAttributeValueException(attribute, userCertDNs, user, null, user, null, "This attribute value must be one of exsiting keys in userCertDNs."); } }  @Override void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute); @Override Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute); @Override List<String> getDependencies(); @Override AttributeDefinition getAttributeDefinition(); @Override void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute);  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithPreferredValueButNoCertificates() throws Exception { System.out.println("testCheckWithPreferredValueButNoCertificates()"); reqAttribute.setValue(null); attributeToCheck.setValue("example"); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithNullPreferredValue() throws Exception { System.out.println("testCheckWithNullPreferredValue()"); Map<String, String> value = new LinkedHashMap<>(); value.put("example", "example"); reqAttribute.setValue(value); attributeToCheck.setValue(null); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithInvalidPreferredValue() throws Exception { System.out.println("testCheckWithInvalidPreferredValue()"); Map<String, String> value = new LinkedHashMap<>(); value.put("example", "example"); reqAttribute.setValue(value); attributeToCheck.setValue("bad_example"); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); Map<String, String> value = new LinkedHashMap<>(); value.put("example", "example"); reqAttribute.setValue(value); attributeToCheck.setValue("example"); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }