urn_perun_user_attribute_def_def_uid_namespace extends UserAttributesModuleAbstract implements UserAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { Integer uid = attribute.valueAsInteger(); String uidNamespace = attribute.getFriendlyNameParameter(); if (uid == null) { throw new WrongReferenceAttributeValueException(attribute, "Attribute was not filled, therefore there is nothing to be checked."); } Attribute minUidAttribute; Attribute maxUidAttribute; try { minUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_minUID); maxUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_maxUID); } catch (AttributeNotExistsException e) { throw new ConsistencyErrorException("minUid and maxUid attributes are required", e); } Integer min = minUidAttribute.valueAsInteger(); Integer max = maxUidAttribute.valueAsInteger(); if(min == null) { throw new WrongReferenceAttributeValueException(attribute, minUidAttribute); } if (max == null) { throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute); } if (uid < min) { throw new WrongReferenceAttributeValueException(attribute, minUidAttribute, user, null, uidNamespace, null, "UID " + uid + " is lesser than min " + min); } if (uid > max) { throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute, user, null, uidNamespace, null, "UID " + uid + " is higher than max " + max); } List<User> usersWithUid = sess.getPerunBl().getUsersManagerBl().getUsersByAttribute(sess, attribute); usersWithUid.remove(user); if (!usersWithUid.isEmpty()) { if(usersWithUid.size() > 1) throw new ConsistencyErrorException("FATAL ERROR: Duplicated UID detected." + attribute + " " + usersWithUid); throw new WrongReferenceAttributeValueException(attribute, "UID " + attribute.getValue() + " is already occupied by " + usersWithUid.get(0) + ". We can't set it for " + user + "."); } }  @Override void checkAttributeSemantics(PerunSessionImpl sess, User user, Attribute attribute); @Override Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute); @Override List<String> getDependencies();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithNullValue() throws Exception { System.out.println("testCheckWithNullValue()"); attributeToCheck.setValue(null); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithNullWithNullMinUid() throws Exception { System.out.println("testCheckWithNullWithNullMinUid()"); minUid.setValue(null); maxUid.setValue(6); attributeToCheck.setValue(5); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithNullWithNullMaxUid() throws Exception { System.out.println("testCheckWithNullWithNullMaxUid()"); minUid.setValue(1); maxUid.setValue(null); attributeToCheck.setValue(5); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithUidLesserThanMin() throws Exception { System.out.println("testCheckWithUidLesserThanMin()"); minUid.setValue(2); maxUid.setValue(6); attributeToCheck.setValue(1); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithUidHigherThanMax() throws Exception { System.out.println("testCheckWithUidHigherThanMax()"); minUid.setValue(2); maxUid.setValue(6); attributeToCheck.setValue(7); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testCheckWithDuplicate() throws Exception { System.out.println("testCheckWithDuplicate()"); minUid.setValue(2); maxUid.setValue(6); attributeToCheck.setValue(5); List<User> list = new ArrayList<>(); list.add(user); list.add(new User()); when(session.getPerunBl().getUsersManagerBl().getUsersByAttribute(session, attributeToCheck)).thenReturn(list); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); minUid.setValue(2); maxUid.setValue(6); attributeToCheck.setValue(5); classInstance.checkAttributeSemantics(session, user, attributeToCheck); }