Search in sources :

Example 6 with PropertyValues

use of org.xwiki.rest.model.jaxb.PropertyValues in project xwiki-platform by xwiki.

the class DBListClassPropertyValuesProviderTest method getValuesMixedWithoutUsed.

@Test
public void getValuesMixedWithoutUsed() throws Exception {
    ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
    DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
    PropertyValues values = new PropertyValues();
    values.getPropertyValues().add(new PropertyValue());
    when(this.authorExecutor.call(any(), eq(authorReference))).thenReturn(values);
    assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 1, "foo"));
    assertEquals(1, values.getPropertyValues().size());
    verify(this.usedValuesQueryBuilder, never()).build(any());
}
Also used : PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) PropertyValue(org.xwiki.rest.model.jaxb.PropertyValue) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with PropertyValues

use of org.xwiki.rest.model.jaxb.PropertyValues in project xwiki-platform by xwiki.

the class GroupsClassPropertyValuesProviderTest method getValuesLocal.

@Test
public void getValuesLocal() throws Exception {
    when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_ONLY);
    DocumentReference devsReference = new DocumentReference("wiki", "Groups", "Devs");
    XWikiDocument devsProfile = mock(XWikiDocument.class, "devs");
    when(this.xcontext.getWiki().getDocument(devsReference, this.xcontext)).thenReturn(devsProfile);
    when(devsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Developers");
    DocumentReference adminsReference = new DocumentReference("wiki", "Groups", "Admins");
    XWikiDocument adminsProfile = mock(XWikiDocument.class, "admins");
    XWikiAttachment notAnImageAttachment = mock(XWikiAttachment.class, "noAnImage");
    XWikiAttachment imageAttachment = mock(XWikiAttachment.class, "image");
    AttachmentReference imageAttachmentReference = new AttachmentReference("admins.png", adminsReference);
    when(this.xcontext.getWiki().getDocument(adminsReference, this.xcontext)).thenReturn(adminsProfile);
    when(adminsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Administrators");
    when(adminsProfile.getAttachmentList()).thenReturn(Arrays.asList(notAnImageAttachment, imageAttachment));
    when(imageAttachment.isImage(this.xcontext)).thenReturn(true);
    when(imageAttachment.getReference()).thenReturn(imageAttachmentReference);
    when(this.xcontext.getWiki().getURL(imageAttachmentReference, "download", "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/admins/image");
    when(this.allowedValuesQuery.execute()).thenReturn(Arrays.asList(devsReference, adminsReference));
    PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
    assertEquals(2, values.getPropertyValues().size());
    assertEquals("Developers", values.getPropertyValues().get(0).getMetaData().get("label"));
    assertEquals("url/to/noavatar.png", values.getPropertyValues().get(0).getMetaData().get("icon"));
    assertEquals("Administrators", values.getPropertyValues().get(1).getMetaData().get("label"));
    assertEquals("url/to/admins/image", values.getPropertyValues().get(1).getMetaData().get("icon"));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with PropertyValues

use of org.xwiki.rest.model.jaxb.PropertyValues in project xwiki-platform by xwiki.

the class UsersClassPropertyValuesProviderTest method getValuesLocalAndGlobal.

@Test
public void getValuesLocalAndGlobal() throws Exception {
    when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_AND_GLOBAL);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getMainWikiId()).thenReturn("chess");
    DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
    when(this.xcontext.getWiki().getDocument(aliceReference, this.xcontext)).thenReturn(mock(XWikiDocument.class, "alice"));
    DocumentReference bobReference = new DocumentReference("chess", "Users", "Bob");
    when(this.xcontext.getWiki().getDocument(bobReference, this.xcontext)).thenReturn(mock(XWikiDocument.class, "bob"));
    when(this.allowedValuesQuery.execute()).thenReturn(Collections.singletonList(bobReference), Collections.singletonList(aliceReference));
    PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
    assertEquals(2, values.getPropertyValues().size());
    assertEquals("Alice", values.getPropertyValues().get(0).getMetaData().get("label"));
    assertEquals("Bob", values.getPropertyValues().get(1).getMetaData().get("label"));
    verify(this.allowedValuesQuery).setWiki("chess");
    verify(this.allowedValuesQuery, times(2)).execute();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with PropertyValues

use of org.xwiki.rest.model.jaxb.PropertyValues in project xwiki-platform by xwiki.

the class AbstractUsersAndGroupsClassPropertyValuesProvider method getLocalAndGlobalAllowedValues.

protected PropertyValues getLocalAndGlobalAllowedValues(T propertyDefinition, int limit, String filter) throws Exception {
    PropertyValues localUsers = getLocalAllowedValues(propertyDefinition, limit, filter);
    PropertyValues globalUsers = getGlobalAllowedValues(propertyDefinition, limit, filter);
    Iterator<PropertyValue> localUsersIterator = localUsers.getPropertyValues().iterator();
    Iterator<PropertyValue> globalUsersIterator = globalUsers.getPropertyValues().iterator();
    PropertyValues users = new PropertyValues();
    int oldSize;
    do {
        oldSize = users.getPropertyValues().size();
        if (localUsersIterator.hasNext() && (limit <= 0 || users.getPropertyValues().size() < limit)) {
            users.getPropertyValues().add(localUsersIterator.next());
        }
        if (globalUsersIterator.hasNext() && (limit <= 0 || users.getPropertyValues().size() < limit)) {
            users.getPropertyValues().add(globalUsersIterator.next());
        }
    } while (oldSize < users.getPropertyValues().size());
    users.getPropertyValues().sort((alice, bob) -> {
        String aliceName = alice.getMetaData().getOrDefault(META_DATA_LABEL, alice.getValue()).toString();
        String bobName = bob.getMetaData().getOrDefault(META_DATA_LABEL, alice.getValue()).toString();
        return aliceName.compareToIgnoreCase(bobName);
    });
    return users;
}
Also used : PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) PropertyValue(org.xwiki.rest.model.jaxb.PropertyValue)

Example 10 with PropertyValues

use of org.xwiki.rest.model.jaxb.PropertyValues in project xwiki-platform by xwiki.

the class ClassPropertyValuesResourceImpl method getClassPropertyValues.

@Override
public PropertyValues getClassPropertyValues(String wikiName, String className, String propertyName, Integer limit, List<String> filterParameters) throws XWikiRestException {
    DocumentReference classReference = this.resolver.resolve(className, new WikiReference(wikiName));
    ClassPropertyReference classPropertyReference = new ClassPropertyReference(propertyName, classReference);
    if (!this.authorization.hasAccess(Right.VIEW, classPropertyReference)) {
        throw new WebApplicationException(Status.UNAUTHORIZED);
    }
    if (!exists(classPropertyReference)) {
        throw new WebApplicationException(Status.NOT_FOUND);
    }
    URI propertyURI = Utils.createURI(this.uriInfo.getBaseUri(), ClassPropertyResource.class, wikiName, className, propertyName);
    Link propertyLink = this.objectFactory.createLink();
    propertyLink.setHref(propertyURI.toString());
    propertyLink.setRel(Relations.PROPERTY);
    PropertyValues propertyValues = this.propertyValuesProvider.getValues(classPropertyReference, limit, filterParameters.toArray());
    propertyValues.getLinks().add(propertyLink);
    return propertyValues;
}
Also used : PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) WebApplicationException(javax.ws.rs.WebApplicationException) WikiReference(org.xwiki.model.reference.WikiReference) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) URI(java.net.URI) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Aggregations

PropertyValues (org.xwiki.rest.model.jaxb.PropertyValues)10 Test (org.junit.Test)8 DocumentReference (org.xwiki.model.reference.DocumentReference)7 ClassPropertyReference (org.xwiki.model.reference.ClassPropertyReference)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 PropertyValue (org.xwiki.rest.model.jaxb.PropertyValue)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 QueryFilter (org.xwiki.query.QueryFilter)2 Link (org.xwiki.rest.model.jaxb.Link)2 ClassPropertyValuesProvider (org.xwiki.rest.resources.classes.ClassPropertyValuesProvider)2 DBListClass (com.xpn.xwiki.objects.classes.DBListClass)1 URI (java.net.URI)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 WikiReference (org.xwiki.model.reference.WikiReference)1 Query (org.xwiki.query.Query)1 QueryParameter (org.xwiki.query.QueryParameter)1 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)1