MailComposerImpl implements MailComposer { String processTemplate(final List<String> mailTemplateChain, final String shopCode, final String locale, final String fileName, final String ext, final Map<String, Object> model) { return processTemplate(mailTemplateChain, shopCode, locale, fileName, ext, model, false); } 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);  }
@Test public void testMerge$style() throws ClassNotFoundException, IOException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String template = "$name lives...somewhere in time."; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(template)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + template); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + template)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); Map<String, Object> model = new HashMap<>(); model.put("name", "Bender"); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, model); assertEquals("Bender lives...somewhere in time.", result); mockery.assertIsSatisfied(); }
@Test public void testMergeJspStyle() throws ClassNotFoundException, IOException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String template = "$name lives in theme park with <% with.each{ out.print(it + ' ');}%>"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(template)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + template); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + template)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, createModel()); assertEquals("Bender lives in theme park with blackjack poetess ", result); mockery.assertIsSatisfied(); }
@Test public void testMergeGroovyStyle() throws ClassNotFoundException, IOException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String template = "<% def deliverySum = 1.23456789; deliverySum = deliverySum.setScale(2, BigDecimal.ROUND_HALF_UP); out.print(deliverySum); %>"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(template)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + template); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + template)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, createModel()); assertEquals("1.23", result); mockery.assertIsSatisfied(); }
@Test public void complexObjectNavigation() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String template = "$root.first $root.second"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(template)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + template); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + template)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); Map<String, Object> model = new HashMap<>(); model.put("root", new Pair("hi", "there")); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, model); assertEquals("hi there", result); mockery.assertIsSatisfied(); }
@Test public void testComposeMailEntityInternalTextAndHtmlVersion() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String txt = "txt"; final String html = "html"; final String textTemplate = "$name lives in theme park with <% with.each{ out.print(it + ' ');}%>"; final String htmlTemplate = "<h2>$name</h2> lives in theme park with:<br> <% with.each{ out.print(it + '<br>');}%>"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, txt); will(returnValue(textTemplate)); allowing(provider).getTemplate(chain, shopCode, locale, fileName, html); will(returnValue(htmlTemplate)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + textTemplate); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + textTemplate)), with(any(Object.class))); allowing(cache).get(includeFunc + htmlTemplate); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + htmlTemplate)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String resultHtml = mailComposer.processTemplate(chain, shopCode, locale, fileName, html, createModel()); String resultTxt = mailComposer.processTemplate(chain, shopCode, locale, fileName, txt, createModel()); assertEquals("Bender lives in theme park with blackjack poetess ", resultTxt); assertEquals("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>", resultHtml); mockery.assertIsSatisfied(); }
@Test public void testComposeMailEntityInternalTextVersionOnly() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String textTemplate = "$name lives in theme park with <% with.each{ out.print(it + ' ');}%>"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(textTemplate)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + textTemplate); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + textTemplate)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, createModel()); assertEquals("Bender lives in theme park with blackjack poetess ", result); mockery.assertIsSatisfied(); }
@Test public void testComposeMailEntityInternalHtmlVersionOnly() throws MessagingException, IOException, ClassNotFoundException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String htmlTemplate = "<h2>$name</h2> lives in theme park with:<br> <% with.each{ out.print(it + '<br>');}%>"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(htmlTemplate)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + htmlTemplate); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + htmlTemplate)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, createModel()); assertEquals("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>", result); mockery.assertIsSatisfied(); }
@Test public void testMergeWithIncludes() throws ClassNotFoundException, IOException { final CacheManager cacheManager = mockery.mock(CacheManager.class); final Cache cache = mockery.mock(Cache.class); final MailTemplateResourcesProvider provider = mockery.mock(MailTemplateResourcesProvider.class); final List<String> chain = Collections.EMPTY_LIST; final String shopCode = "S001"; final String locale = "en"; final String fileName = "tmp"; final String ext = "txt"; final String template = "${include('header')}$name lives in theme park with <% with.each{ out.print(it + ' ');}%>${include('footer')}"; final String templateHeader = "Email header and a logo\n\n"; final String templateFooter = "\n\nYours truly, team"; mockery.checking(new Expectations() {{ allowing(provider).getTemplate(chain, shopCode, locale, fileName, ext); will(returnValue(template)); allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache)); allowing(cache).get(includeFunc + template); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + template)), with(any(Object.class))); allowing(provider).getTemplate(chain, shopCode, locale, "header", ext); will(returnValue(templateHeader)); allowing(cache).get(includeFunc + templateHeader); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + templateHeader)), with(any(Object.class))); allowing(provider).getTemplate(chain, shopCode, locale, "footer", ext); will(returnValue(templateFooter)); allowing(cache).get(includeFunc + templateFooter); will(returnValue(null)); allowing(cache).put(with(equal(includeFunc + templateFooter)), with(any(Object.class))); }}); final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager)); MailComposerImpl mailComposer = new MailComposerImpl(provider, templates); String result = mailComposer.processTemplate(chain, shopCode, locale, fileName, ext, createModel()); assertEquals("Email header and a logo\n\nBender lives in theme park with blackjack poetess \n\nYours truly, team", result); mockery.assertIsSatisfied(); }