urn_perun_user_attribute_def_virt_userOrganizations extends UserVirtualAttributesModuleAbstract implements UserVirtualAttributesModuleImplApi { @Override public Attribute getAttributeValue(PerunSessionImpl perunSession, User user, AttributeDefinition attributeDefinition) { Map<String, String> userOrganizations = new LinkedHashMap<>(); List<Vo> vos = perunSession.getPerunBl().getUsersManagerBl().getVosWhereUserIsMember(perunSession, user); for (Vo vo : vos) { try { Member member = perunSession.getPerunBl().getMembersManagerBl().getMemberByUser(perunSession, vo, user); String value = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, member, A_M_organization).valueAsString(); if (value != null) userOrganizations.put(vo.getShortName(), value); } catch (MemberNotExistsException e) { throw new ConsistencyErrorException(e); } catch (WrongAttributeAssignmentException | AttributeNotExistsException e) { throw new InternalErrorException(e); } } Attribute attribute = new Attribute(attributeDefinition); attribute.setValue(userOrganizations); return attribute; }  @Override Attribute getAttributeValue(PerunSessionImpl perunSession, User user, AttributeDefinition attributeDefinition); @Override AttributeDefinition getAttributeDefinition();  }
@Test public void getAttributeValue() throws Exception { Attribute organization = perun.getAttributesManagerBl().getAttribute(sess, member, A_M_organization); organization.setValue("some value"); perun.getAttributesManager().setAttribute(sess, member, organization); organization = perun.getAttributesManagerBl().getAttribute(sess, member2, A_M_organization); organization.setValue("different value"); perun.getAttributesManager().setAttribute(sess, member2, organization); AttributeDefinition attr = new AttributeDefinition(); attr.setNamespace(AttributesManager.NS_USER_ATTR_VIRT); attr.setFriendlyName("userOrganizations"); attr.setType(HashMap.class.getName()); Attribute attribute = new Attribute(attr); perun.getAttributesManager().createAttribute(sess, attribute); Map<String, String> value = classInstance.getAttributeValue((PerunSessionImpl) sess, user, attribute).valueAsMap(); assertEquals("Attribute should have 2 elements.", 2, value.size()); assertTrue("Attribute should contain short name of first vo as a key.", value.containsKey(vo.getShortName())); assertTrue("Attribute should contain short name of second vo as a key.", value.containsKey(vo2.getShortName())); assertEquals("some value should be a value to short name of first vo.", "some value", value.get(vo.getShortName())); assertEquals("different value should be a value to short name of second vo.", "different value", value.get(vo2.getShortName())); }