use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.
the class DSNMailSender_sendMailTest method testSendMail_nullWriter_assertException.
@Test
public void testSendMail_nullWriter_assertException() throws Exception {
MimeMessage msg = TestUtils.readMimeMessageFromFile("MessageWithAttachment.txt");
final SMTPClient client = mock(SMTPClient.class);
when(client.setSender((String) any())).thenReturn(true);
when(client.addRecipient((String) any())).thenReturn(true);
when(client.sendMessageData()).thenReturn(null);
when(client.getReplyCode()).thenReturn(250);
final SMTPClientFactory factory = createFactory(client);
DSNMailSender sender = new DSNMailSender("smtp://localhost", factory);
Exchange exchange = new DefaultExchange(mock(CamelContext.class));
exchange.getIn().setBody(msg);
boolean exceptionOccurred = false;
try {
sender.sendMail(exchange);
} catch (IOException e) {
exceptionOccurred = true;
}
assertTrue(exceptionOccurred);
verify(client, times(1)).sendMessageData();
verify(client, never()).completePendingCommand();
}
use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.
the class DSNMessageGenerator_generateDSNFailureMessageTest method testGenerateDSNFailureMessage_assertMessageCreated_noCopiedSubject.
@Test
public void testGenerateDSNFailureMessage_assertMessageCreated_noCopiedSubject() throws Exception {
Exchange exchange = new DefaultExchange(mock(CamelContext.class));
String message = TestUtils.readMessageFromFile("MessageWithAttachment.txt");
DefaultTxDetailParser parser = new DefaultTxDetailParser();
InputStream str = IOUtils.toInputStream(message);
try {
// send original message
final MimeMessage mimeMessage = new MimeMessage(null, str);
mimeMessage.setHeader(MailStandard.Headers.Subject, "");
// change the message id
mimeMessage.saveChanges();
Map<String, TxDetail> details = parser.getMessageDetails(mimeMessage);
Tx originalMessage = new Tx(TxMessageType.IMF, details);
Collection<Tx> txs = new ArrayList<Tx>();
txs.add(originalMessage);
DSNMessageGenerator generator = createGenerator();
generator.generateDSNFailureMessage(txs, exchange);
MimeMessage dsnMessage = (MimeMessage) exchange.getIn().getBody();
assertNotNull(dsnMessage);
// check the subject
String newSubject = MailStandard.getHeader(dsnMessage, MailStandard.Headers.Subject);
assertEquals("Not Delivered:", newSubject);
} finally {
IOUtils.closeQuietly(str);
}
}
use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.
the class TestNonCompletedToDSNGeneratorMonitorRoute method testNonCompleted_multipleRecipeints_singleDSNSingleProcessAndOneIncomplete_assertDSNGeneratedAndValidTimedout.
@Test
public void testNonCompleted_multipleRecipeints_singleDSNSingleProcessAndOneIncomplete_assertDSNGeneratedAndValidTimedout() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
// send original message
final String originalMessageId = UUID.randomUUID().toString();
Tx originalMessage = TestUtils.makeMessage(TxMessageType.IMF, originalMessageId, "", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com,ah4626@direct.securehealthemail.com,gm2552@test.com", "");
template.sendBody("direct:start", originalMessage);
// now send a DSN failure for only recipient
Tx dsntxMessage = TestUtils.makeMessage(TxMessageType.DSN, UUID.randomUUID().toString(), originalMessageId, "gm2552@direct.securehealthemail.com", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com", DSNStandard.DSNAction.FAILED.toString(), "");
template.sendBody("direct:start", dsntxMessage);
// now send an MDN success for only recipient
Tx mdnMessage = TestUtils.makeMessage(TxMessageType.MDN, UUID.randomUUID().toString(), originalMessageId, "ah4626@direct.securehealthemail.com", "gm2552@cerner.com", "ah4626@direct.securehealthemail.com", "", MDNStandard.Disposition_Processed);
template.sendBody("direct:start", mdnMessage);
// single 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());
MimeMessage dsnMessage = (MimeMessage) exchanges.get(0).getIn().getBody();
ByteArrayOutputStream oStr = new ByteArrayOutputStream();
dsnMessage.writeTo(oStr);
String str = new String(oStr.toByteArray());
assertFalse(str.contains("ah4626@direct.securehealthemail.com"));
assertFalse(str.contains("gm2552@direct.securehealthemail.com"));
assertTrue(str.contains("gm2552@test.com"));
}
use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.
the class TestNonCompletedToDSNGeneratorMonitorRoute method testNonCompleted_multipleRecipeints_singleDSNAndOneIncomplete_assertDSNGeneratedAndValidTimedout.
@Test
public void testNonCompleted_multipleRecipeints_singleDSNAndOneIncomplete_assertDSNGeneratedAndValidTimedout() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
// send original message
final String originalMessageId = UUID.randomUUID().toString();
Tx originalMessage = TestUtils.makeMessage(TxMessageType.IMF, originalMessageId, "", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com,ah4626@direct.securehealthemail.com", "");
template.sendBody("direct:start", originalMessage);
// now send a DSN failure for only recipient
Tx dsntxMessage = TestUtils.makeMessage(TxMessageType.DSN, UUID.randomUUID().toString(), originalMessageId, "gm2552@direct.securehealthemail.com", "gm2552@cerner.com", "gm2552@direct.securehealthemail.com", DSNStandard.DSNAction.FAILED.toString(), "");
template.sendBody("direct:start", dsntxMessage);
// single 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());
MimeMessage dsnMessage = (MimeMessage) exchanges.get(0).getIn().getBody();
ByteArrayOutputStream oStr = new ByteArrayOutputStream();
dsnMessage.writeTo(oStr);
String str = new String(oStr.toByteArray());
assertTrue(str.contains("ah4626@direct.securehealthemail.com"));
assertFalse(str.contains("gm2552@direct.securehealthemail.com"));
}
use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.
the class DSNMessageGenerator method generateDSNFailureMessage.
/**
* Generates the DSN message a replacing the existing exchange in body with the DSN message as a MimeMessage object.
* @param txs Collection of correlated Tx objects.
* @param ex The message exchange.
* @throws Exception
*/
@Handler
public void generateDSNFailureMessage(Collection<Tx> txs, Exchange ex) throws Exception {
// change the inbound message body to null
ex.getIn().setBody(null);
// get the message that is being tracked so we can generate an error message for it
Tx messageToTrack = AbstractCompletionCondition.getMessageToTrack(txs);
if (messageToTrack != null) {
// make sure we have incomplete recipients
final Collection<String> incompleteRecips = conditionChecker.getIncompleteRecipients(txs);
if (incompleteRecips != null && !incompleteRecips.isEmpty()) {
InternetAddress originalSender = null;
String originalSubject = "";
InternetAddress postmaster = null;
String originalMessageId = "";
Enumeration<Header> fullMessageHeaders = null;
final List<DSNRecipientHeaders> recipientDSNHeaders = new ArrayList<DSNRecipientHeaders>();
final List<Address> failedRecipAddresses = new ArrayList<Address>();
final TxDetail sender = messageToTrack.getDetail(TxDetailType.FROM);
if (sender != null) {
originalSender = new InternetAddress(sender.getDetailValue());
postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
}
final TxDetail subject = messageToTrack.getDetail(TxDetailType.SUBJECT);
if (subject != null)
originalSubject = subject.getDetailValue();
for (String incompleteRecip : incompleteRecips) {
final Address failedRecipAddress = new InternetAddress(incompleteRecip);
DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS), failedRecipAddress);
recipientDSNHeaders.add(dsnRecipHeaders);
failedRecipAddresses.add(failedRecipAddress);
}
///CLOVER:OFF
final TxDetail origMessId = messageToTrack.getDetail(TxDetailType.MSG_ID);
if (origMessId != null)
originalMessageId = origMessId.getDetailValue();
///CLOVER:ON
final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
final TxDetail fullHeaders = messageToTrack.getDetail(TxDetailType.MSG_FULL_HEADERS);
if (fullHeaders != null)
fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
final MimeMessage dnsMessage = generator.createDSNMessage(originalSender, originalSubject, postmaster, recipientDSNHeaders, messageDSNHeaders, textBodyPart);
ex.getIn().setBody(dnsMessage);
}
}
}
Aggregations