Search in sources :

Example 16 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class DefaultMailTemplateManagerTest method evaluateWithObjectNotFoundWithDefaultLanguage.

@Test
public void evaluateWithObjectNotFoundWithDefaultLanguage() throws Exception {
    DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    // First call with the passed language, return -1 (No XWiki.Mail xobject found)
    when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
    // Second call with the default language, return -1 (No XWiki.Mail xobject found)
    when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(-1);
    when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(0), eq("html"))).thenReturn("Salut <b>${name}</b> <br />${email}");
    VelocityEngine velocityEngine = mock(VelocityEngine.class);
    VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
    when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
    when(velocityEvaluator.evaluateVelocity(eq("Salut <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Salut <b>John Doe</b> <br />john@doe.com");
    String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH);
    verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
    verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("en"));
    assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 17 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachments.

@Test
public void createWithAttachmentAndTemplateAttachments() throws Exception {
    MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
    Attachment attachment1 = mock(Attachment.class, "attachment1");
    Map<String, Object> bodyPartParameters = new HashMap<>();
    bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
    bodyPartParameters.put("attachments", Collections.singletonList(attachment1));
    bodyPartParameters.put("includeTemplateAttachments", true);
    // Mock the retrieval and conversion of attachments from the Template document
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    XWikiDocument xwikiDocument = mock(XWikiDocument.class);
    when(dab.getDocumentInstance(this.documentReference)).thenReturn(xwikiDocument);
    XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
    when(xwikiDocument.getAttachmentList()).thenReturn(Collections.singletonList(xwikiAttachment));
    AttachmentConverter attachmentConverter = this.mocker.getInstance(AttachmentConverter.class);
    Attachment attachment2 = mock(Attachment.class, "attachment2");
    when(attachmentConverter.convert(Collections.singletonList(xwikiAttachment))).thenReturn(Collections.singletonList(attachment2));
    this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
    Map<String, Object> htmlParameters = new HashMap<>();
    htmlParameters.put("alternate", "Hello John Doe, john@doe.com");
    htmlParameters.put("attachments", Arrays.asList(attachment1, attachment2));
    verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters);
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 18 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class MessageStreamTest method testPostDirectMessageWithNonExistingTarget.

@Test(expected = IllegalArgumentException.class)
public void testPostDirectMessageWithNonExistingTarget() throws Exception {
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final DocumentReference targetUser = new DocumentReference("xwiki", "XWiki", "Nobody");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockBridge).exists(targetUser);
            will(returnValue(false));
        }
    });
    this.stream.postDirectMessageToUser("Hello Nobody!", targetUser);
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 19 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class MessageStreamTest method setupForDirectMessage.

private Event setupForDirectMessage() throws ComponentLookupException, Exception {
    final Event e = setupForNewMessage();
    final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockBridge).exists(MessageStreamTest.this.targetUser);
            will(returnValue(true));
            exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.targetUser);
            will(returnValue("wiki:XWiki.JaneBuck"));
        }
    });
    return e;
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Event(org.xwiki.eventstream.Event) DefaultEvent(org.xwiki.eventstream.internal.DefaultEvent)

Example 20 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class MessageStreamTest method testGetRecentPersonalMessagesWhenQueryFails.

@Test
public void testGetRecentPersonalMessagesWhenQueryFails() throws Exception {
    final Query mockQuery = getMockQuery();
    final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
    final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    getMockery().checking(new Expectations() {

        {
            allowing(mockBridge).getCurrentUserReference();
            will(returnValue(MessageStreamTest.this.currentUser));
            allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
            will(returnValue("wiki:XWiki.JohnDoe"));
            exactly(1).of(mockQuery).setLimit(30);
            will(returnValue(mockQuery));
            exactly(1).of(mockQuery).setOffset(0);
            will(returnValue(mockQuery));
            allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
            allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
            will(returnValue(mockQuery));
            exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
            will(throwException(new QueryException("", null, null)));
        }
    });
    List<Event> result = this.stream.getRecentPersonalMessages();
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) EventStream(org.xwiki.eventstream.EventStream) QueryManager(org.xwiki.query.QueryManager) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Event(org.xwiki.eventstream.Event) DefaultEvent(org.xwiki.eventstream.internal.DefaultEvent) Test(org.junit.Test)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5