Search in sources :

Example 51 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultModelBridgeTest method getDocumentReferences.

@Test
public void getDocumentReferences() throws Exception {
    SpaceReference spaceReference = new SpaceReference("wiki", "Space");
    Query query = mock(Query.class);
    QueryManager queryManager = this.mocker.getInstance(QueryManager.class);
    when(queryManager.createQuery(any(), any())).thenReturn(query);
    EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localEntityReferenceSerializer.serialize(spaceReference)).thenReturn("Space");
    when(query.execute()).thenReturn(Arrays.<Object>asList("Page"));
    DocumentReferenceResolver<String> explicitDocumentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "explicit");
    DocumentReference documentReference = new DocumentReference("Page", spaceReference);
    when(explicitDocumentReferenceResolver.resolve("Page", spaceReference)).thenReturn(documentReference);
    assertEquals(Arrays.asList(documentReference), this.mocker.getComponentUnderTest().getDocumentReferences(spaceReference));
    verify(query).setWiki(spaceReference.getWikiReference().getName());
    verify(query).bindValue("space", "Space");
    verify(query).bindValue("spacePrefix", "Space.%");
}
Also used : Query(org.xwiki.query.Query) SpaceReference(org.xwiki.model.reference.SpaceReference) QueryManager(org.xwiki.query.QueryManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 52 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class PageTest method loadPage.

/**
 * @param documentReference the reference of the Document to load from the ClassLoader
 * @return the loaded document
 * @throws Exception in case of errors
 */
protected XWikiDocument loadPage(DocumentReference documentReference) throws Exception {
    List<String> path = new ArrayList<>();
    for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
        path.add(spaceReference.getName());
    }
    path.add(documentReference.getName() + ".xml");
    XWikiDocument document = new XWikiDocument(documentReference);
    document.fromXML(getClass().getClassLoader().getResourceAsStream(StringUtils.join(path, '/')));
    this.xwiki.saveDocument(document, "registering document", true, this.context);
    return document;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) ArrayList(java.util.ArrayList)

Example 53 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class InstanceInputFilterStream method writeSpace.

private void writeSpace(EntityReferenceTreeNode node, Object filter, InstanceFilter proxyFilter) throws FilterException {
    SpaceReference spaceReference = (SpaceReference) node.getReference();
    FilterEventParameters parameters = new FilterEventParameters();
    // Get begin/end space parameters
    for (InstanceInputEventGenerator generator : this.eventGenerators) {
        generator.setWikiSpaceParameters(spaceReference.getName(), parameters);
    }
    // Begin space
    proxyFilter.beginWikiSpace(spaceReference.getName(), parameters);
    // Extend begin space
    for (InstanceInputEventGenerator generator : this.eventGenerators) {
        generator.beginWikiSpace(spaceReference.getName(), parameters);
    }
    // Write documents
    for (DocumentReference documentReference : this.instanceModel.getDocumentReferences(spaceReference)) {
        if (isDocumentEnabled(documentReference)) {
            writeDocument(documentReference, filter, proxyFilter);
        } else {
            if (this.properties.isVerbose()) {
                this.logger.info(LOG_DOCUMENT_SKIPPED, "Skipped document [{}]", documentReference);
            }
        }
    }
    // Write nested spaces
    for (EntityReferenceTreeNode child : node.getChildren()) {
        writeSpace(child, filter, proxyFilter);
    }
    // Extend end space
    for (InstanceInputEventGenerator generator : this.eventGenerators) {
        generator.endWikiSpace(spaceReference.getName(), parameters);
    }
    // End space
    proxyFilter.endWikiSpace(spaceReference.getName(), parameters);
}
Also used : FilterEventParameters(org.xwiki.filter.FilterEventParameters) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReferenceTreeNode(org.xwiki.model.reference.EntityReferenceTreeNode) InstanceInputEventGenerator(org.xwiki.filter.instance.input.InstanceInputEventGenerator) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 54 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class ScopeNotificationFilterTest method complexCase1.

@Test
public void complexCase1() throws Exception {
    // Preferences:
    // 
    // α: "update" event type enabled for format ALERT
    // 
    // β: Exclusive filter on "wikiA".
    // γ: Inclusive filter on "wikiA:SpaceB"
    // δ: Exclusive filter on "wikiA:SpaceB.SpaceC"
    // ε: Exclusive filter on "wikiA:SpaceB.SpaceC.SpaceD"
    // Mock α
    NotificationPreference preference = mock(NotificationPreference.class);
    when(preference.getFormat()).thenReturn(NotificationFormat.ALERT);
    Map<NotificationPreferenceProperty, Object> properties = new HashMap<>();
    properties.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
    when(preference.getProperties()).thenReturn(properties);
    // Mock β
    WikiReference wikiReference = new WikiReference("wikiA");
    NotificationFilterPreference prefβ = mockNotificationFilterPreference("wikiA", wikiReference, NotificationFilterType.EXCLUSIVE, null);
    // Mock γ
    SpaceReference spaceReferenceB = new SpaceReference("SpaceB", wikiReference);
    NotificationFilterPreference prefγ = mockNotificationFilterPreference("wikiA:SpaceB", spaceReferenceB, NotificationFilterType.INCLUSIVE, "update");
    // Mock δ
    SpaceReference spaceReferenceC = new SpaceReference("SpaceC", spaceReferenceB);
    NotificationFilterPreference prefδ = mockNotificationFilterPreference("wikiA:SpaceB.SpaceC", spaceReferenceC, NotificationFilterType.EXCLUSIVE, null);
    // Mock ε
    SpaceReference spaceReferenceD = new SpaceReference("SpaceD", spaceReferenceC);
    NotificationFilterPreference prefε = mockNotificationFilterPreference("wikiA:SpaceB.SpaceC.SpaceD", spaceReferenceD, NotificationFilterType.INCLUSIVE, null);
    when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(prefβ, prefγ, prefδ, prefε));
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Test 1
    String result = mocker.getComponentUnderTest().filterExpression(user, preference).toString();
    assertEquals("(((NOT (WIKI = \"wikiA\") OR (WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB\"))" + " OR (WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC.SpaceD\")) AND (NOT ((WIKI = \"wikiA\" " + "AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC\")) OR (WIKI = \"wikiA\" " + "AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC.SpaceD\")))", result);
    // Test with wikiA:SpaceE (filtered by β)
    Event event1 = mock(Event.class);
    when(event1.getSpace()).thenReturn(new SpaceReference("SpaceE", wikiReference));
    assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event1, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.DocumentE (kept by γ)
    Event event2 = mock(Event.class);
    when(event2.getDocument()).thenReturn(new DocumentReference("DocumentE", spaceReferenceB));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event2, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.SpaceC.DocumentF (filtered by δ)
    Event event3 = mock(Event.class);
    when(event3.getDocument()).thenReturn(new DocumentReference("DocumentF", spaceReferenceC));
    assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event3, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.SpaceC.SpaceD.DocumentG (kept by ε)
    Event event4 = mock(Event.class);
    when(event4.getDocument()).thenReturn(new DocumentReference("DocumentG", spaceReferenceD));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event4, user, NotificationFormat.ALERT));
    // Test with wikiB:SpaceH.DocumentI - kept because nothing match and there is no top level inclusive filter
    Event event5 = mock(Event.class);
    when(event5.getDocument()).thenReturn(new DocumentReference("wikiB", "SpaceH", "DocumentI"));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event5, user, NotificationFormat.ALERT));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) SpaceReference(org.xwiki.model.reference.SpaceReference) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Event(org.xwiki.eventstream.Event) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 55 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class ScopeNotificationFilterTest method testWithTopLevelInclusiveFilters.

@Test
public void testWithTopLevelInclusiveFilters() throws Exception {
    // Preferences:
    // 
    // α: "update" event type enabled for format ALERT
    // 
    // γ: Inclusive filter on "wikiA:SpaceB"
    // ζ: Inclusive filter on "wikiA:SpaceM.DocumentN"
    // Mock α
    NotificationPreference preference = mock(NotificationPreference.class);
    when(preference.getFormat()).thenReturn(NotificationFormat.ALERT);
    Map<NotificationPreferenceProperty, Object> properties = new HashMap<>();
    properties.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
    when(preference.getProperties()).thenReturn(properties);
    // Mock γ
    WikiReference wikiReference = new WikiReference("wikiA");
    SpaceReference spaceReferenceB = new SpaceReference("SpaceB", new WikiReference(wikiReference));
    NotificationFilterPreference prefγ = mockNotificationFilterPreference("wikiA:SpaceB", spaceReferenceB, NotificationFilterType.INCLUSIVE, null);
    // Mock ζ
    DocumentReference documentReference = new DocumentReference("wikiA", "SpaceM", "DocumentN");
    NotificationFilterPreference prefζ = mockNotificationFilterPreference("wikiA:SpaceM.DocumentN", documentReference, NotificationFilterType.INCLUSIVE, null);
    when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(prefγ, prefζ));
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Test 1
    String result = mocker.getComponentUnderTest().filterExpression(user, preference).toString();
    assertEquals("((WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB\") " + "OR (WIKI = \"wikiA\" AND PAGE = \"wikiA:SpaceM.DocumentN\"))", result);
    // Test with wikiA:SpaceE (filtered by γ & ζ)
    Event event1 = mock(Event.class);
    when(event1.getSpace()).thenReturn(new SpaceReference("SpaceE", wikiReference));
    assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event1, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.DocumentJ (kept by γ)
    Event event2 = mock(Event.class);
    when(event2.getDocument()).thenReturn(new DocumentReference("wikiA", "SpaceB", "DocumentJ"));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event2, user, NotificationFormat.ALERT));
    // Test with wikiB:SpaceK.DocumentL (filtered by γ & ζ)
    Event event3 = mock(Event.class);
    when(event3.getDocument()).thenReturn(new DocumentReference("wikiB", "SpaceK", "DocumentL"));
    assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event3, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceM.DocumentN (kept by ζ)
    Event event4 = mock(Event.class);
    when(event4.getDocument()).thenReturn(new DocumentReference("wikiA", "SpaceM", "DocumentN"));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event4, user, NotificationFormat.ALERT));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) SpaceReference(org.xwiki.model.reference.SpaceReference) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Event(org.xwiki.eventstream.Event) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

SpaceReference (org.xwiki.model.reference.SpaceReference)142 DocumentReference (org.xwiki.model.reference.DocumentReference)96 Test (org.junit.Test)83 WikiReference (org.xwiki.model.reference.WikiReference)58 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)21 ArrayList (java.util.ArrayList)11 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)11 XWikiException (com.xpn.xwiki.XWikiException)9 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)8 Expectations (org.jmock.Expectations)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)7 ComponentManager (org.xwiki.component.manager.ComponentManager)7 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)7