@Test public void testComposeMailEntityInternalFullIntegration() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); mockery.checking(new Expectations() {{ allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(with(any(String.class))); will(returnValue(null)); allowing(cache).put(with(any(String.class)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); final MailTemplateResourcesProvider mailTemplateResourcesProvider = mockery.mock(MailTemplateResourcesProvider.class); mockery.checking(new Expectations() {{ oneOf(mailTemplateResourcesProvider).getTemplate(Collections.singletonList("default/mail/"), "SHOP10", "en", "imtest", ".txt"); will(returnValue(IOUtils.toString(new FileInputStream(new File("src/test/resources/mailtemplates/default/imtest/imtest.txt")), "UTF-8"))); oneOf(mailTemplateResourcesProvider).getTemplate(Collections.singletonList("default/mail/"), "SHOP10", "en", "imtest", ".html"); will(returnValue(IOUtils.toString(new FileInputStream(new File("src/test/resources/mailtemplates/default/imtest/imtest.html")), "UTF-8"))); oneOf(mailTemplateResourcesProvider).getTemplate(Collections.singletonList("default/mail/"), "SHOP10", "en", "imtest", ".properties"); will(returnValue(IOUtils.toString(new FileInputStream(new File("src/test/resources/mailtemplates/default/imtest/imtest.properties")), "UTF-8"))); oneOf(mailTemplateResourcesProvider).getResource(Collections.singletonList("default/mail/"), "SHOP10", "en", "imtest", "test.gif"); will(returnValue(IOUtils.toByteArray(new FileInputStream(new File("src/test/resources/mailtemplates/default/imtest/resources/test.gif"))))); oneOf(mailTemplateResourcesProvider).getResource(Collections.singletonList("default/mail/"), "SHOP10", "en", "imtest", "test.jpeg"); will(returnValue(IOUtils.toByteArray(new FileInputStream(new File("src/test/resources/mailtemplates/default/imtest/resources/test.jpeg"))))); }}); final Mail mail = new MailEntity(); MailComposerImpl mailComposer = new MailComposerImpl(mailTemplateResourcesProvider, templates); mailComposer.composeMessage( mail, "SHOP10", "en", Collections.singletonList("default/mail/"), "imtest", "test@localhost.lo", "to@somedomain.com", "cc@somedomain.com", "bcc@somedomain.com", createModel()); assertTrue(mail.getTextVersion().contains("Bender lives in theme park with blackjack poetess")); assertTrue(mail.getHtmlVersion().contains("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>")); assertEquals("test@localhost.lo", mail.getFrom()); assertEquals("to@somedomain.com", mail.getRecipients()); assertEquals("cc@somedomain.com", mail.getCc()); assertEquals("bcc@somedomain.com", mail.getBcc()); assertEquals("Тема письма", mail.getSubject()); assertEquals("SHOP10", mail.getShopCode()); mockery.assertIsSatisfied(); }
void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } MailComposerImpl(final MailTemplateResourcesProvider mailTemplateResourcesProvider,
                            final TemplateProcessor templateSupport); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } MailComposerImpl(final MailTemplateResourcesProvider mailTemplateResourcesProvider,
                            final TemplateProcessor templateSupport); String getResourceExpression(); void setResourceExpression(final String resourceExpression); @Override void composeMessage(final Mail mail,
                               final String shopCode,
                               final String locale,
                               final List<String> mailTemplateChain,
                               final String templateName,
                               final String from,
                               final String toEmail,
                               final String ccEmail,
                               final String bccEmail,
                               final Map<String, Object> model); @Override void convertMessage(final Mail mail, final MimeMessage mimeMessage); @Override String convertMessageToTXT(final Mail mail); @Override String convertMessageToHTML(final Mail mail); }
MailComposerImpl implements MailComposer { void composeMessage(final MimeMessage message, final String shopCode, final String locale, final List<String> mailTemplateChain, final String templateName, final String from, final String toEmail, final String ccEmail, final String bccEmail, final Map<String, Object> model) throws MessagingException, IOException, ClassNotFoundException { final MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(toEmail); helper.setSentDate(new Date()); if (ccEmail != null) { helper.setCc(ccEmail); } if (bccEmail != null) { helper.setBcc(bccEmail); } final String textContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".txt", model); final String htmlContent = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".html", model); final String propString = processTemplate(mailTemplateChain, shopCode, locale, templateName, ".properties", model); final Properties prop = new Properties(); if (propString != null) { prop.load(new StringReader(propString)); } helper.setSubject(prop.getProperty("subject")); if (from == null) { helper.setFrom(prop.getProperty("from")); } else { helper.setFrom(from); } final Map<String, byte[]> attachments = collectAttachments(model); composeMessage(helper, textContent, htmlContent, attachments, mailTemplateChain, shopCode, locale, templateName); } MailComposerImpl(final MailTemplateResourcesProvider mailTemplateResourcesProvider,
                            final TemplateProcessor templateSupport); String getResourceExpression(); void setResourceExpression(final String resourceExpression); @Override void composeMessage(final Mail mail,
                               final String shopCode,
                               final String locale,
                               final List<String> mailTemplateChain,
                               final String templateName,
                               final String from,
                               final String toEmail,
                               final String ccEmail,
                               final String bccEmail,
                               final Map<String, Object> model); @Override void convertMessage(final Mail mail, final MimeMessage mimeMessage); @Override String convertMessageToTXT(final Mail mail); @Override String convertMessageToHTML(final Mail mail);  }