@Test public void getGroupsForUser() throws ComponentLookupException, QueryException { User u = mock(User.class); DocumentReference userProfile = new DocumentReference("xwiki", "XWiki", "Admin"); when(u.getProfileDocument()).thenReturn(userProfile); EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki"); when(serializer.serialize(userProfile)).thenReturn("XWiki.Admin"); QueryManager qm = this.mocker.getInstance(QueryManager.class); Query q = mock(Query.class); when(q.bindValue("u", "xwiki:XWiki.Admin")).thenReturn(q); when(q.bindValue("su", "XWiki.Admin")).thenReturn(q); when(qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL)) .thenReturn(q); List<Object> groupNames = new LinkedList<>(); groupNames.add("Groups.Group A"); groupNames.add("Group B Administrators"); when(q.<Object>execute()).thenReturn(groupNames); DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current"); DocumentReference a = new DocumentReference("xwiki", "Groups", "Group A"); when(resolver.resolve(eq("Groups.Group A"), eq(GROUP_SPACE))).thenReturn(a); DocumentReference ba = new DocumentReference("xwiki", "Groups", "Group B Administrators"); when(resolver.resolve(eq("Group B Administrators"), eq(GROUP_SPACE))).thenReturn(ba); q = mock(Query.class); when(q.bindValue(1, "xwiki:Groups.Group A")).thenReturn(q); when(q.bindValue(2, "Groups.Group A")).thenReturn(q); when(q.bindValue(3, "xwiki:Groups.Group B Administrators")).thenReturn(q); when(q.bindValue(4, "Groups.Group B Administrators")).thenReturn(q); when(qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (?1,?2,?3,?4)", Query.XWQL)) .thenReturn(q); groupNames = new LinkedList<>(); groupNames.add("Groups.Group B"); when(q.<Object>execute()).thenReturn(groupNames); q = mock(Query.class); DocumentReference b = new DocumentReference("xwiki", "Groups", "Group B"); when(resolver.resolve(eq("Groups.Group B"), eq(GROUP_SPACE))).thenReturn(b); when(q.bindValue(1, "xwiki:Groups.Group B")).thenReturn(q); when(q.bindValue(2, "Groups.Group B")).thenReturn(q); when(qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (?1,?2)", Query.XWQL)) .thenReturn(q); when(q.<Object>execute()).thenReturn(Collections.emptyList()); q = mock(Query.class); when(qm.createQuery("from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL)).thenReturn(q); groupNames = new LinkedList<>(); groupNames.add("Groups.Group A"); groupNames.add("Groups.Group B"); when(q.<Object>execute()).thenReturn(groupNames); Set<Group> result = this.mocker.getComponentUnderTest().getGroupsForUser(u); Assert.assertEquals(2, result.size()); Iterator<Group> resultGroups = result.iterator(); Assert.assertEquals(a, resultGroups.next().getReference()); Assert.assertEquals(b, resultGroups.next().getReference()); }
@Override public Set<Group> getGroupsForUser(User user) { if (user == null || user.getProfileDocument() == null) { return Collections.emptySet(); } DocumentReference profile = user.getProfileDocument(); Set<Group> result = new LinkedHashSet<>(); try { Query q = this.qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL); q.bindValue("u", profile.toString()); q.bindValue("su", this.compactSerializer.serialize(profile)); List<Object> groups = q.execute(); List<Object> nestedGroups = new ArrayList<>(groups); while (!nestedGroups.isEmpty()) { StringBuilder qs = new StringBuilder("from doc.object(XWiki.XWikiGroups) grp where grp.member in ("); for (int i = 0; i < nestedGroups.size(); ++i) { if (i > 0) { qs.append(','); } qs.append('?').append(i * 2 + 1).append(",?").append(i * 2 + 2); } qs.append(')'); q = this.qm.createQuery(qs.toString(), Query.XWQL); for (int i = 0; i < nestedGroups.size(); ++i) { String formalGroupName = this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE).toString(); String shortGroupName = this.compactSerializer .serialize(this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE)); q.bindValue(i * 2 + 1, formalGroupName); q.bindValue(i * 2 + 2, shortGroupName); } nestedGroups = q.execute(); nestedGroups.removeAll(groups); groups.addAll(nestedGroups); } q = this.qm.createQuery( "from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL); groups.retainAll(q.execute()); for (Object groupName : groups) { result.add(getGroup(String.valueOf(groupName))); } } catch (QueryException ex) { this.logger.warn("Failed to search for user's groups: {}", ex.getMessage()); } return Collections.unmodifiableSet(result); }
DefaultGroupManager implements GroupManager { @Override public Set<Group> getGroupsForUser(User user) { if (user == null || user.getProfileDocument() == null) { return Collections.emptySet(); } DocumentReference profile = user.getProfileDocument(); Set<Group> result = new LinkedHashSet<>(); try { Query q = this.qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL); q.bindValue("u", profile.toString()); q.bindValue("su", this.compactSerializer.serialize(profile)); List<Object> groups = q.execute(); List<Object> nestedGroups = new ArrayList<>(groups); while (!nestedGroups.isEmpty()) { StringBuilder qs = new StringBuilder("from doc.object(XWiki.XWikiGroups) grp where grp.member in ("); for (int i = 0; i < nestedGroups.size(); ++i) { if (i > 0) { qs.append(','); } qs.append('?').append(i * 2 + 1).append(",?").append(i * 2 + 2); } qs.append(')'); q = this.qm.createQuery(qs.toString(), Query.XWQL); for (int i = 0; i < nestedGroups.size(); ++i) { String formalGroupName = this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE).toString(); String shortGroupName = this.compactSerializer .serialize(this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE)); q.bindValue(i * 2 + 1, formalGroupName); q.bindValue(i * 2 + 2, shortGroupName); } nestedGroups = q.execute(); nestedGroups.removeAll(groups); groups.addAll(nestedGroups); } q = this.qm.createQuery( "from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL); groups.retainAll(q.execute()); for (Object groupName : groups) { result.add(getGroup(String.valueOf(groupName))); } } catch (QueryException ex) { this.logger.warn("Failed to search for user's groups: {}", ex.getMessage()); } return Collections.unmodifiableSet(result); } }
DefaultGroupManager implements GroupManager { @Override public Set<Group> getGroupsForUser(User user) { if (user == null || user.getProfileDocument() == null) { return Collections.emptySet(); } DocumentReference profile = user.getProfileDocument(); Set<Group> result = new LinkedHashSet<>(); try { Query q = this.qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL); q.bindValue("u", profile.toString()); q.bindValue("su", this.compactSerializer.serialize(profile)); List<Object> groups = q.execute(); List<Object> nestedGroups = new ArrayList<>(groups); while (!nestedGroups.isEmpty()) { StringBuilder qs = new StringBuilder("from doc.object(XWiki.XWikiGroups) grp where grp.member in ("); for (int i = 0; i < nestedGroups.size(); ++i) { if (i > 0) { qs.append(','); } qs.append('?').append(i * 2 + 1).append(",?").append(i * 2 + 2); } qs.append(')'); q = this.qm.createQuery(qs.toString(), Query.XWQL); for (int i = 0; i < nestedGroups.size(); ++i) { String formalGroupName = this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE).toString(); String shortGroupName = this.compactSerializer .serialize(this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE)); q.bindValue(i * 2 + 1, formalGroupName); q.bindValue(i * 2 + 2, shortGroupName); } nestedGroups = q.execute(); nestedGroups.removeAll(groups); groups.addAll(nestedGroups); } q = this.qm.createQuery( "from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL); groups.retainAll(q.execute()); for (Object groupName : groups) { result.add(getGroup(String.valueOf(groupName))); } } catch (QueryException ex) { this.logger.warn("Failed to search for user's groups: {}", ex.getMessage()); } return Collections.unmodifiableSet(result); }  }
DefaultGroupManager implements GroupManager { @Override public Set<Group> getGroupsForUser(User user) { if (user == null || user.getProfileDocument() == null) { return Collections.emptySet(); } DocumentReference profile = user.getProfileDocument(); Set<Group> result = new LinkedHashSet<>(); try { Query q = this.qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL); q.bindValue("u", profile.toString()); q.bindValue("su", this.compactSerializer.serialize(profile)); List<Object> groups = q.execute(); List<Object> nestedGroups = new ArrayList<>(groups); while (!nestedGroups.isEmpty()) { StringBuilder qs = new StringBuilder("from doc.object(XWiki.XWikiGroups) grp where grp.member in ("); for (int i = 0; i < nestedGroups.size(); ++i) { if (i > 0) { qs.append(','); } qs.append('?').append(i * 2 + 1).append(",?").append(i * 2 + 2); } qs.append(')'); q = this.qm.createQuery(qs.toString(), Query.XWQL); for (int i = 0; i < nestedGroups.size(); ++i) { String formalGroupName = this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE).toString(); String shortGroupName = this.compactSerializer .serialize(this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE)); q.bindValue(i * 2 + 1, formalGroupName); q.bindValue(i * 2 + 2, shortGroupName); } nestedGroups = q.execute(); nestedGroups.removeAll(groups); groups.addAll(nestedGroups); } q = this.qm.createQuery( "from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL); groups.retainAll(q.execute()); for (Object groupName : groups) { result.add(getGroup(String.valueOf(groupName))); } } catch (QueryException ex) { this.logger.warn("Failed to search for user's groups: {}", ex.getMessage()); } return Collections.unmodifiableSet(result); }  @Override Set<Group> getGroupsForUser(User user); @Override Group getGroup(String name); @Override Group getGroup(DocumentReference groupReference); @Override Set<Document> getAllMembersForGroup(String name); }
DefaultGroupManager implements GroupManager { @Override public Set<Group> getGroupsForUser(User user) { if (user == null || user.getProfileDocument() == null) { return Collections.emptySet(); } DocumentReference profile = user.getProfileDocument(); Set<Group> result = new LinkedHashSet<>(); try { Query q = this.qm.createQuery("from doc.object(XWiki.XWikiGroups) grp where grp.member in (:u, :su)", Query.XWQL); q.bindValue("u", profile.toString()); q.bindValue("su", this.compactSerializer.serialize(profile)); List<Object> groups = q.execute(); List<Object> nestedGroups = new ArrayList<>(groups); while (!nestedGroups.isEmpty()) { StringBuilder qs = new StringBuilder("from doc.object(XWiki.XWikiGroups) grp where grp.member in ("); for (int i = 0; i < nestedGroups.size(); ++i) { if (i > 0) { qs.append(','); } qs.append('?').append(i * 2 + 1).append(",?").append(i * 2 + 2); } qs.append(')'); q = this.qm.createQuery(qs.toString(), Query.XWQL); for (int i = 0; i < nestedGroups.size(); ++i) { String formalGroupName = this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE).toString(); String shortGroupName = this.compactSerializer .serialize(this.resolver.resolve(String.valueOf(nestedGroups.get(i)), GROUP_SPACE)); q.bindValue(i * 2 + 1, formalGroupName); q.bindValue(i * 2 + 2, shortGroupName); } nestedGroups = q.execute(); nestedGroups.removeAll(groups); groups.addAll(nestedGroups); } q = this.qm.createQuery( "from doc.object(XWiki.XWikiGroups) grp, doc.object(PhenoTips.PhenoTipsGroupClass) phgrp", Query.XWQL); groups.retainAll(q.execute()); for (Object groupName : groups) { result.add(getGroup(String.valueOf(groupName))); } } catch (QueryException ex) { this.logger.warn("Failed to search for user's groups: {}", ex.getMessage()); } return Collections.unmodifiableSet(result); }  @Override Set<Group> getGroupsForUser(User user); @Override Group getGroup(String name); @Override Group getGroup(DocumentReference groupReference); @Override Set<Document> getAllMembersForGroup(String name);  }