Search in sources :

Example 66 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class AuthenticatingIntegrationTest method sendTextMail.

@Test
public void sendTextMail() throws Exception {
    // Set the EC
    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    when(execution.getContext()).thenReturn(executionContext);
    Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    // Just return the same execution context
    when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
    // Step 1: Create a JavaMail Session
    Properties properties = this.configuration.getAllProperties();
    assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
    Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);
    // Step 4: Send the mail
    this.sender.sendAsynchronously(Arrays.asList(message), session, null);
    // Verify that the mail has been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals("john@doe.com", messages[0].getHeader("To", null));
    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
}
Also used : BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Copier(org.xwiki.mail.internal.thread.context.Copier) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 67 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class ScriptingIntegrationTest method initialize.

@Before
public void initialize() throws Exception {
    this.scriptService = this.componentManager.getInstance(ScriptService.class, "mailsender");
    // Set the EC
    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    execution.setContext(executionContext);
    Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    // Just return the same execution context
    when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
    // Simulate receiving the Application Ready Event to start the mail threads
    MailSenderInitializerListener listener = this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME);
    listener.onEvent(new ApplicationReadyEvent(), null, null);
}
Also used : ScriptService(org.xwiki.script.service.ScriptService) MailSenderScriptService(org.xwiki.mail.script.MailSenderScriptService) DefaultExecution(org.xwiki.context.internal.DefaultExecution) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) Copier(org.xwiki.mail.internal.thread.context.Copier) ApplicationReadyEvent(org.xwiki.bridge.event.ApplicationReadyEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailSenderInitializerListener(org.xwiki.mail.internal.thread.MailSenderInitializerListener) Before(org.junit.Before)

Example 68 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class DatabaseMailListenerTest method setUp.

@Before
public void setUp() throws Exception {
    this.message = new ExtendedMimeMessage();
    this.message.setType("type");
    this.message.saveChanges();
    this.message.setHeader("Message-ID", mimeMessageId);
    this.messageId = message.getUniqueMessageId();
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = Mockito.mock(ExecutionContext.class);
    XWikiContext xcontext = Mockito.mock(XWikiContext.class);
    when(xcontext.getWikiId()).thenReturn("mywiki");
    when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xcontext);
    when(execution.getContext()).thenReturn(executionContext);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) Before(org.junit.Before)

Example 69 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class UsersAndGroupsMimeMessageFactoryTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
    Map<String, Object> source = Collections.<String, Object>singletonMap("users", Collections.singletonList(userReference));
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hint", "template");
    parameters.put("source", new DocumentReference("templatewiki", "templatespace", "templatepage"));
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    this.mocker.registerMockComponent(new DefaultParameterizedType(null, MimeMessageFactory.class, MimeMessage.class), "template");
    // Setup XWikiContext since this is required internally by the iterator constructor
    Execution execution = this.mocker.registerMockComponent(Execution.class);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xwikiContext);
    when(execution.getContext()).thenReturn(executionContext);
    Iterator<MimeMessage> iterator = this.mocker.getComponentUnderTest().createMessage(source, parameters);
    assertNotNull(iterator);
}
Also used : HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) Provider(javax.inject.Provider) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 70 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class PrepareMailRunnableTest method prepareMailWhenIteratorFails.

@Test
public void prepareMailWhenIteratorFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    final MimeMessage message1 = new MimeMessage(session);
    message1.setText("Content1");
    MimeMessage message2 = new MimeMessage(session);
    message2.setText("Content2");
    String batchId1 = UUID.randomUUID().toString();
    String batchId2 = UUID.randomUUID().toString();
    ExecutionContext context1 = new ExecutionContext();
    XWikiContext xContext1 = new XWikiContext();
    xContext1.setWikiId("wiki1");
    context1.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext1);
    ExecutionContext context2 = new ExecutionContext();
    XWikiContext xContext2 = new XWikiContext();
    xContext2.setWikiId("wiki2");
    context2.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext2);
    MemoryMailListener listener1 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item1 = new PrepareMailQueueItem(new Iterable<MimeMessage>() {

        @Override
        public Iterator<MimeMessage> iterator() {
            return new Iterator<MimeMessage>() {

                int index = 0;

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public MimeMessage next() {
                    if (index++ == 0) {
                        return message1;
                    }
                    throw new RuntimeException("Iterator failure");
                }

                @Override
                public void remove() {
                }
            };
        }
    }, session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager prepareMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    MailQueueManager sendMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            MimeMessage message = (MimeMessage) args[1];
            message.saveChanges();
            return null;
        }
    }).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            SendMailQueueItem item = (SendMailQueueItem) args[0];
            ((UpdateableMailStatusResult) item.getListener().getMailStatusResult()).incrementCurrentSize();
            return null;
        }
    }).when(sendMailQueueManager).addToQueue(any(SendMailQueueItem.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    prepareMailQueueManager.addToQueue(item1);
    prepareMailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener1.getMailStatusResult().waitTillProcessed(10000L);
        listener2.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    MailStatusResult result1 = listener1.getMailStatusResult();
    MailStatusResult result2 = listener2.getMailStatusResult();
    // Despite the errors, both process should be ended with known total number of mails
    assertTrue(result1.isProcessed());
    assertTrue(result2.isProcessed());
    // This is the real test: we verify that there's been an error while sending each email.
    assertNotNull(listener1.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertNotNull(listener2.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertFalse(listener1.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertFalse(listener2.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertEquals("Failure during preparation phase of thread [" + batchId1 + "]", logRule.getMessage(0));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Iterator(java.util.Iterator) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContext(org.xwiki.context.ExecutionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

ExecutionContext (org.xwiki.context.ExecutionContext)114 Execution (org.xwiki.context.Execution)62 XWikiContext (com.xpn.xwiki.XWikiContext)47 Before (org.junit.Before)31 Test (org.junit.Test)25 XWiki (com.xpn.xwiki.XWiki)19 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)14 ComponentManager (org.xwiki.component.manager.ComponentManager)9 ExecutionContextException (org.xwiki.context.ExecutionContextException)9 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)8 List (java.util.List)6 Map (java.util.Map)6 Properties (java.util.Properties)6 Session (javax.mail.Session)6 MimeMessage (javax.mail.internet.MimeMessage)6 XWikiException (com.xpn.xwiki.XWikiException)5