@Test public void setCollaboratorsIgnoresAnyNullCollaborators() throws XWikiException { Collection<Collaborator> collaborators = new HashSet<>(); Collaborator c = new DefaultCollaborator(COLLABORATOR, EDIT_ACCESS, this.helper); collaborators.add(c); c = new DefaultCollaborator(OTHER_USER, VIEW_ACCESS, this.helper); collaborators.add(c); collaborators.add(null); when(this.entityDoc.newXObject(COLLABORATOR_CLASS, this.context)).thenReturn(this.collaboratorObject1); Assert.assertTrue(this.component.setCollaborators(this.entity, collaborators)); verify(this.collaboratorObject1).setStringValue(COLLABORATOR_LABEL, COLLABORATOR_STR); verify(this.collaboratorObject1).setStringValue(ACCESS_LABEL, EDIT_LABEL); verify(this.collaboratorObject1).setStringValue(COLLABORATOR_LABEL, OTHER_USER_STR); verify(this.collaboratorObject1).setStringValue(ACCESS_LABEL, VIEW_LABEL); verify(this.entityDoc, times(1)).removeXObjects(COLLABORATOR_CLASS); verify(this.entityDoc, times(2)).newXObject(COLLABORATOR_CLASS, this.context); verify(this.xwiki).saveDocument(this.entityDoc, "Updated collaborators", true, this.context); }
@Override public boolean setCollaborators( @Nullable final PrimaryEntity entity, @Nullable final Collection<Collaborator> newCollaborators) { if (entity == null || entity.getDocumentReference() == null) { return false; } try { final XWikiDocument patientDoc = entity.getXDocument(); final DocumentReference classReference = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entity.getDocumentReference()); final XWikiContext context = this.xcontextProvider.get(); patientDoc.removeXObjects(classReference); if (newCollaborators != null) { newCollaborators.stream() .filter(Objects::nonNull) .filter(collaborator -> collaborator.getUser() != null) .forEach(collaborator -> saveCollaboratorData(collaborator, patientDoc, classReference, context)); } patientDoc.setAuthorReference(this.helper.getCurrentUser()); patientDoc.setMetaDataDirty(true); context.getWiki().saveDocument(patientDoc, "Updated collaborators", true, context); return true; } catch (Exception e) { this.logger.error("Unexpected exception occurred when setting collaborators [{}] for entity [{}]", newCollaborators, entity); } return false; }
DefaultEntityAccessManager implements EntityAccessManager { @Override public boolean setCollaborators( @Nullable final PrimaryEntity entity, @Nullable final Collection<Collaborator> newCollaborators) { if (entity == null || entity.getDocumentReference() == null) { return false; } try { final XWikiDocument patientDoc = entity.getXDocument(); final DocumentReference classReference = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entity.getDocumentReference()); final XWikiContext context = this.xcontextProvider.get(); patientDoc.removeXObjects(classReference); if (newCollaborators != null) { newCollaborators.stream() .filter(Objects::nonNull) .filter(collaborator -> collaborator.getUser() != null) .forEach(collaborator -> saveCollaboratorData(collaborator, patientDoc, classReference, context)); } patientDoc.setAuthorReference(this.helper.getCurrentUser()); patientDoc.setMetaDataDirty(true); context.getWiki().saveDocument(patientDoc, "Updated collaborators", true, context); return true; } catch (Exception e) { this.logger.error("Unexpected exception occurred when setting collaborators [{}] for entity [{}]", newCollaborators, entity); } return false; } }
DefaultEntityAccessManager implements EntityAccessManager { @Override public boolean setCollaborators( @Nullable final PrimaryEntity entity, @Nullable final Collection<Collaborator> newCollaborators) { if (entity == null || entity.getDocumentReference() == null) { return false; } try { final XWikiDocument patientDoc = entity.getXDocument(); final DocumentReference classReference = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entity.getDocumentReference()); final XWikiContext context = this.xcontextProvider.get(); patientDoc.removeXObjects(classReference); if (newCollaborators != null) { newCollaborators.stream() .filter(Objects::nonNull) .filter(collaborator -> collaborator.getUser() != null) .forEach(collaborator -> saveCollaboratorData(collaborator, patientDoc, classReference, context)); } patientDoc.setAuthorReference(this.helper.getCurrentUser()); patientDoc.setMetaDataDirty(true); context.getWiki().saveDocument(patientDoc, "Updated collaborators", true, context); return true; } catch (Exception e) { this.logger.error("Unexpected exception occurred when setting collaborators [{}] for entity [{}]", newCollaborators, entity); } return false; }  }
DefaultEntityAccessManager implements EntityAccessManager { @Override public boolean setCollaborators( @Nullable final PrimaryEntity entity, @Nullable final Collection<Collaborator> newCollaborators) { if (entity == null || entity.getDocumentReference() == null) { return false; } try { final XWikiDocument patientDoc = entity.getXDocument(); final DocumentReference classReference = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entity.getDocumentReference()); final XWikiContext context = this.xcontextProvider.get(); patientDoc.removeXObjects(classReference); if (newCollaborators != null) { newCollaborators.stream() .filter(Objects::nonNull) .filter(collaborator -> collaborator.getUser() != null) .forEach(collaborator -> saveCollaboratorData(collaborator, patientDoc, classReference, context)); } patientDoc.setAuthorReference(this.helper.getCurrentUser()); patientDoc.setMetaDataDirty(true); context.getWiki().saveDocument(patientDoc, "Updated collaborators", true, context); return true; } catch (Exception e) { this.logger.error("Unexpected exception occurred when setting collaborators [{}] for entity [{}]", newCollaborators, entity); } return false; }  @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 { @Override public boolean setCollaborators( @Nullable final PrimaryEntity entity, @Nullable final Collection<Collaborator> newCollaborators) { if (entity == null || entity.getDocumentReference() == null) { return false; } try { final XWikiDocument patientDoc = entity.getXDocument(); final DocumentReference classReference = this.partialEntityResolver.resolve(Collaborator.CLASS_REFERENCE, entity.getDocumentReference()); final XWikiContext context = this.xcontextProvider.get(); patientDoc.removeXObjects(classReference); if (newCollaborators != null) { newCollaborators.stream() .filter(Objects::nonNull) .filter(collaborator -> collaborator.getUser() != null) .forEach(collaborator -> saveCollaboratorData(collaborator, patientDoc, classReference, context)); } patientDoc.setAuthorReference(this.helper.getCurrentUser()); patientDoc.setMetaDataDirty(true); context.getWiki().saveDocument(patientDoc, "Updated collaborators", true, context); return true; } catch (Exception e) { this.logger.error("Unexpected exception occurred when setting collaborators [{}] for entity [{}]", newCollaborators, entity); } return false; }  @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);  }