Search in sources :

Example 66 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NHINDAgentTest method testEndToEndMessageBase64AttachmentOnly.

public void testEndToEndMessageBase64AttachmentOnly() throws Exception {
    DefaultNHINDAgent agent = TestUtils.getStockAgent(Arrays.asList(new String[] { "messaging.cernerdemos.com" }));
    String testMessage = TestUtils.readResource("raw2.txt");
    MimeMessage originalMsg = new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII")));
    OutgoingMessage SMIMEenvMessage = agent.processOutgoing(testMessage);
    assertNotNull(SMIMEenvMessage);
    assertTrue(SMIMEenvMessage.getMessage().toString().length() > 0);
    // verify the message
    // need a new agent because this is a different domain
    agent = TestUtils.getStockAgent(Arrays.asList(new String[] { "securehealthemail.com" }));
    IncomingMessage strippedAndVerifiesMessage = agent.processIncoming(SMIMEenvMessage);
    assertNotNull(strippedAndVerifiesMessage);
    assertTrue(strippedAndVerifiesMessage.getMessage().toString().length() > 0);
    MimeMessage processedMsg = new MimeMessage(null, new ByteArrayInputStream(strippedAndVerifiesMessage.getMessage().toString().getBytes("ASCII")));
    assertNotNull(processedMsg);
    // can't do a direct compare on headers because the processing may strip some of the recipients
    assertTrue(processedMsg.getContentType().compareTo(originalMsg.getContentType()) == 0);
    assertTrue(originalMsg.getSubject().compareTo(processedMsg.getSubject()) == 0);
    // get the message data and compare
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count = 0;
    InputStream inStream = originalMsg.getInputStream();
    while ((count = inStream.read(buffer)) > -1) oStream.write(buffer, 0, count);
    oStream = new ByteArrayOutputStream();
    count = 0;
    inStream = processedMsg.getInputStream();
    while ((count = inStream.read(buffer)) > -1) oStream.write(buffer, 0, count);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultNHINDAgent(org.nhindirect.stagent.DefaultNHINDAgent)

Example 67 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NotificationTest method testParseFieldsFromMimeMessage_NonMDNMessage_AssertExecption.

public void testParseFieldsFromMimeMessage_NonMDNMessage_AssertExecption() throws Exception {
    String testMessage = TestUtils.readResource("MessageWithAttachment.txt");
    MimeMessage msg = new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII")));
    boolean exceptionOccured = false;
    try {
        Notification.getNotificationFieldsAsHeaders(msg);
    } catch (IllegalArgumentException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 68 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NotificationTest method testParseFieldsFromMimeMessage.

public void testParseFieldsFromMimeMessage() throws Exception {
    String testMessage = TestUtils.readResource("MDNMessage.txt");
    MimeMessage msg = new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII")));
    InternetHeaders headers = Notification.getNotificationFieldsAsHeaders(msg);
    assertNotNull(headers.getHeader(MDNStandard.Headers.Disposition));
    assertEquals("automatic-action/MDN-sent-automatically;processed", headers.getHeader(MDNStandard.Headers.Disposition, ","));
    assertNotNull(headers.getHeader(MDNStandard.Headers.ReportingAgent));
    assertEquals("starugh-stateline.com;NHIN Direct Security Agent", headers.getHeader(MDNStandard.Headers.ReportingAgent, ","));
    assertNotNull(headers.getHeader(MDNStandard.Headers.FinalRecipient));
    assertEquals("externUser1@starugh-stateline.com", headers.getHeader(MDNStandard.Headers.FinalRecipient, ","));
    assertNotNull(headers.getHeader(MDNStandard.Headers.OriginalMessageID));
    assertEquals("<9501051053.aa04167@IETF.CNR I.Reston.VA.US>", headers.getHeader(MDNStandard.Headers.OriginalMessageID, ","));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 69 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class DSNMessageGenerator_generateDSNFailureMessageTest method testGenerateDSNFailureMessage_fromJson_incomingAndCompleteRecips.

@Test
public void testGenerateDSNFailureMessage_fromJson_incomingAndCompleteRecips() throws Exception {
    Exchange exchange = new DefaultExchange(mock(CamelContext.class));
    final String json = FileUtils.readFileToString(new File("./src/test/resources/json/multiRecipEmailSingleDNSAndMDN.json"));
    final ObjectMapper jsonMapper = new ObjectMapper();
    jsonMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    List<Tx> txs = jsonMapper.readValue(json, TypeFactory.collectionType(ArrayList.class, Tx.class));
    DSNMessageGenerator generator = createGenerator();
    generator.generateDSNFailureMessage(txs, exchange);
    MimeMessage dsnMessage = (MimeMessage) exchange.getIn().getBody();
    assertNotNull(dsnMessage);
    ByteArrayOutputStream oStr = new ByteArrayOutputStream();
    dsnMessage.writeTo(oStr);
    final String dsnStr = new String(oStr.toByteArray());
    assertTrue(dsnStr.contains("gm2552@direct.securehealthemail.com"));
    assertFalse(dsnStr.contains("test@aol.com"));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) CamelContext(org.apache.camel.CamelContext) Tx(org.nhindirect.common.tx.model.Tx) MimeMessage(javax.mail.internet.MimeMessage) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 70 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class TestTimeoutToDupStateManager method testTimeoutReliableMessage_conditionNotComplete_assertDupAdded.

@Test
public void testTimeoutReliableMessage_conditionNotComplete_assertDupAdded() throws Exception {
    NotificationDuplicationDAO dao = context.getRegistry().lookup("notificationDuplicationDAO", NotificationDuplicationDAO.class);
    assertNotNull(dao);
    purgeNotifDAO(dao);
    MockEndpoint mock = getMockEndpoint("mock:result");
    // send original message
    final String originalMessageId = UUID.randomUUID().toString();
    Tx originalMessage = TestUtils.makeReliableMessage(TxMessageType.IMF, originalMessageId, "", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com", "", "", "");
    template.sendBody("direct:start", originalMessage);
    // no MDN sent... messages should timeout after 2 seconds
    // sleep 3 seconds to make sure it completes
    Thread.sleep(3000);
    List<Exchange> exchanges = mock.getReceivedExchanges();
    assertEquals(1, exchanges.size());
    Exchange exchange = exchanges.iterator().next();
    // make sure there is only 1 message in the exchange
    MimeMessage message = exchange.getIn().getBody(MimeMessage.class);
    assertNotNull(message);
    assertEquals("timeout", exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY));
    Set<String> addresses = dao.getReceivedAddresses(originalMessageId + "\t" + message.getMessageID(), Arrays.asList("gm2552@direct.securehealthemail.com"));
    assertEquals(1, addresses.size());
    assertTrue(addresses.contains("gm2552@direct.securehealthemail.com"));
    addresses = dao.getReceivedAddresses(originalMessageId, Arrays.asList("gm2552@direct.securehealthemail.com"));
    assertEquals(1, addresses.size());
    assertTrue(addresses.contains("gm2552@direct.securehealthemail.com"));
}
Also used : Exchange(org.apache.camel.Exchange) Tx(org.nhindirect.common.tx.model.Tx) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MimeMessage(javax.mail.internet.MimeMessage) NotificationDuplicationDAO(org.nhindirect.monitor.dao.NotificationDuplicationDAO) Test(org.junit.Test)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70