urn_perun_user_attribute_def_def_cnCeitecAD extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl perunSession, User user, Attribute attribute) throws WrongReferenceAttributeValueException { if (attribute.getValue() == null) { throw new WrongReferenceAttributeValueException(attribute, null, user, null, "Value can't be null"); } Set<User> usersWithSameCN = new HashSet<>(perunSession.getPerunBl().getUsersManagerBl().getUsersByAttribute(perunSession, attribute)); String normalizedValue = java.text.Normalizer.normalize((String)attribute.getValue(), java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+",""); if (!Objects.equals(normalizedValue, attribute.getValue())) { Attribute changedAttribute = new Attribute(attribute); changedAttribute.setValue(normalizedValue); usersWithSameCN.addAll(perunSession.getPerunBl().getUsersManagerBl().getUsersByAttribute(perunSession, changedAttribute)); } usersWithSameCN.remove(user); if (!usersWithSameCN.isEmpty()) { if(usersWithSameCN.size() > 1) throw new ConsistencyErrorException("FATAL ERROR: Duplicated CN detected." + attribute + " " + usersWithSameCN); throw new WrongReferenceAttributeValueException(attribute, attribute, user, null, usersWithSameCN.iterator().next(), null, "This CN " + attribute.getValue() + " is already occupied for CEITEC AD"); } }  @Override void checkAttributeSemantics(PerunSessionImpl perunSession, User user, Attribute attribute); @Override Attribute fillAttribute(PerunSessionImpl session, User user, AttributeDefinition attribute); @Override AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testNullValue() throws Exception { System.out.println("testNullValue()"); attributeToCheck.setValue(null); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckAlreadyOccupiedValue() throws Exception { System.out.println("testCheckAlreadyOccupiedValue()"); when(session.getPerunBl().getUsersManagerBl().getUsersByAttribute(session, attributeToCheck)).thenReturn(Arrays.asList(user, secondUser)); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); when(session.getPerunBl().getUsersManagerBl().getUsersByAttribute(session, attributeToCheck)).thenReturn(Collections.singletonList(user)); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }