@Test public void getCollaboratorsCollaboratorsWithInvalidAccessHaveNoAccess() { List<BaseObject> objects = new ArrayList<>(); when(this.collaboratorObject1.getStringValue(COLLABORATOR_LABEL)).thenReturn(COLLABORATOR_STR); when(this.collaboratorObject1.getStringValue(ACCESS_LABEL)).thenReturn("wrgfhfdhgong"); objects.add(this.collaboratorObject1); when(this.collaboratorObject2.getStringValue(COLLABORATOR_LABEL)).thenReturn(OTHER_USER_STR); when(this.collaboratorObject2.getStringValue(ACCESS_LABEL)).thenReturn(VIEW_LABEL); objects.add(this.collaboratorObject2); when(this.collaboratorObject3.getStringValue(COLLABORATOR_LABEL)).thenReturn(GROUP_STR); when(this.collaboratorObject3.getStringValue(ACCESS_LABEL)).thenReturn(EDIT_LABEL); objects.add(this.collaboratorObject3); when(this.entityDoc.getXObjects(COLLABORATOR_CLASS)).thenReturn(objects); Collection<Collaborator> collaborators = this.component.getCollaborators(this.entity); Assert.assertEquals(3, collaborators.size()); Collaborator c = new DefaultCollaborator(OTHER_USER, VIEW_ACCESS, this.helper); Assert.assertTrue(collaborators.contains(c)); c = new DefaultCollaborator(GROUP, EDIT_ACCESS, this.helper); Assert.assertTrue(collaborators.contains(c)); c = new DefaultCollaborator(COLLABORATOR, this.noAccess, this.helper); Assert.assertTrue(collaborators.contains(c)); }
@Nonnull @Override public Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity) { if (entity == null || entity.getDocumentReference() == null) { return Collections.emptySet(); } final DocumentReference entityRef = entity.getDocumentReference(); try { final XWikiDocument entityDoc = entity.getXDocument(); final DocumentReference classRef = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entityRef); final Collection<BaseObject> storedCollaborators = entityDoc.getXObjects(classRef); final Map<EntityReference, Collaborator> collaborators = storedCollaborators == null ? Collections.emptyMap() : storedCollaborators.stream() .filter(Objects::nonNull) .collect(TreeMap::new, (map, v) -> collectCollaborator(map, v, entityRef), TreeMap::putAll); return collaborators.values(); } catch (Exception e) { this.logger.error("Unexpected exception occurred when retrieving collaborators for entity [{}]", entity); } return Collections.emptySet(); }
DefaultEntityAccessManager implements EntityAccessManager { @Nonnull @Override public Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity) { if (entity == null || entity.getDocumentReference() == null) { return Collections.emptySet(); } final DocumentReference entityRef = entity.getDocumentReference(); try { final XWikiDocument entityDoc = entity.getXDocument(); final DocumentReference classRef = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entityRef); final Collection<BaseObject> storedCollaborators = entityDoc.getXObjects(classRef); final Map<EntityReference, Collaborator> collaborators = storedCollaborators == null ? Collections.emptyMap() : storedCollaborators.stream() .filter(Objects::nonNull) .collect(TreeMap::new, (map, v) -> collectCollaborator(map, v, entityRef), TreeMap::putAll); return collaborators.values(); } catch (Exception e) { this.logger.error("Unexpected exception occurred when retrieving collaborators for entity [{}]", entity); } return Collections.emptySet(); } }
DefaultEntityAccessManager implements EntityAccessManager { @Nonnull @Override public Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity) { if (entity == null || entity.getDocumentReference() == null) { return Collections.emptySet(); } final DocumentReference entityRef = entity.getDocumentReference(); try { final XWikiDocument entityDoc = entity.getXDocument(); final DocumentReference classRef = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entityRef); final Collection<BaseObject> storedCollaborators = entityDoc.getXObjects(classRef); final Map<EntityReference, Collaborator> collaborators = storedCollaborators == null ? Collections.emptyMap() : storedCollaborators.stream() .filter(Objects::nonNull) .collect(TreeMap::new, (map, v) -> collectCollaborator(map, v, entityRef), TreeMap::putAll); return collaborators.values(); } catch (Exception e) { this.logger.error("Unexpected exception occurred when retrieving collaborators for entity [{}]", entity); } return Collections.emptySet(); }  }
DefaultEntityAccessManager implements EntityAccessManager { @Nonnull @Override public Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity) { if (entity == null || entity.getDocumentReference() == null) { return Collections.emptySet(); } final DocumentReference entityRef = entity.getDocumentReference(); try { final XWikiDocument entityDoc = entity.getXDocument(); final DocumentReference classRef = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entityRef); final Collection<BaseObject> storedCollaborators = entityDoc.getXObjects(classRef); final Map<EntityReference, Collaborator> collaborators = storedCollaborators == null ? Collections.emptyMap() : storedCollaborators.stream() .filter(Objects::nonNull) .collect(TreeMap::new, (map, v) -> collectCollaborator(map, v, entityRef), TreeMap::putAll); return collaborators.values(); } catch (Exception e) { this.logger.error("Unexpected exception occurred when retrieving collaborators for entity [{}]", entity); } return Collections.emptySet(); }  @Nonnull @Override Collection<AccessLevel> listAccessLevels(); @Nonnull @Override Collection<AccessLevel> listAllAccessLevels(); @Nonnull @Override AccessLevel resolveAccessLevel(@Nullable final String name); @Nonnull @Override AccessLevel getAccessLevel(@Nullable final PrimaryEntity entity, @Nullable final EntityReference userOrGroup); @Override boolean isAdministrator(@Nullable final PrimaryEntity entity); @Override boolean isAdministrator(@Nullable final PrimaryEntity entity, @Nullable final DocumentReference user); @Nullable @Override Owner getOwner(@Nullable final PrimaryEntity entity); @Override boolean setOwner(@Nullable final PrimaryEntity entity, @Nullable final EntityReference userOrGroup); @Nonnull @Override Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity); @Override boolean setCollaborators(
        @Nullable final PrimaryEntity entity,
        @Nullable final Collection<Collaborator> newCollaborators); @Override boolean addCollaborator(@Nullable final PrimaryEntity entity, @Nullable final Collaborator collaborator); @Override boolean removeCollaborator(@Nullable final PrimaryEntity entity, @Nullable final Collaborator collaborator); }
DefaultEntityAccessManager implements EntityAccessManager { @Nonnull @Override public Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity) { if (entity == null || entity.getDocumentReference() == null) { return Collections.emptySet(); } final DocumentReference entityRef = entity.getDocumentReference(); try { final XWikiDocument entityDoc = entity.getXDocument(); final DocumentReference classRef = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entityRef); final Collection<BaseObject> storedCollaborators = entityDoc.getXObjects(classRef); final Map<EntityReference, Collaborator> collaborators = storedCollaborators == null ? Collections.emptyMap() : storedCollaborators.stream() .filter(Objects::nonNull) .collect(TreeMap::new, (map, v) -> collectCollaborator(map, v, entityRef), TreeMap::putAll); return collaborators.values(); } catch (Exception e) { this.logger.error("Unexpected exception occurred when retrieving collaborators for entity [{}]", entity); } return Collections.emptySet(); }  @Nonnull @Override Collection<AccessLevel> listAccessLevels(); @Nonnull @Override Collection<AccessLevel> listAllAccessLevels(); @Nonnull @Override AccessLevel resolveAccessLevel(@Nullable final String name); @Nonnull @Override AccessLevel getAccessLevel(@Nullable final PrimaryEntity entity, @Nullable final EntityReference userOrGroup); @Override boolean isAdministrator(@Nullable final PrimaryEntity entity); @Override boolean isAdministrator(@Nullable final PrimaryEntity entity, @Nullable final DocumentReference user); @Nullable @Override Owner getOwner(@Nullable final PrimaryEntity entity); @Override boolean setOwner(@Nullable final PrimaryEntity entity, @Nullable final EntityReference userOrGroup); @Nonnull @Override Collection<Collaborator> getCollaborators(@Nullable final PrimaryEntity entity); @Override boolean setCollaborators(
        @Nullable final PrimaryEntity entity,
        @Nullable final Collection<Collaborator> newCollaborators); @Override boolean addCollaborator(@Nullable final PrimaryEntity entity, @Nullable final Collaborator collaborator); @Override boolean removeCollaborator(@Nullable final PrimaryEntity entity, @Nullable final Collaborator collaborator);  }