PebbleTemplateService implements TemplateService { @Override public @NonNull String mergeTemplateIntoString(final @NonNull String templateReference, final @NonNull Map<String, Object> model) throws IOException, TemplateException { final String trimmedTemplateReference = templateReference.trim(); checkArgument(!isNullOrEmpty(trimmedTemplateReference), "The given templateName is null, empty or blank"); if (trimmedTemplateReference.contains(".")) checkArgument(Objects.equals(getFileExtension(trimmedTemplateReference), expectedTemplateExtension()), "Expected a Pebble template file with extension '%s', while '%s' was given. To check " + "the default extension look at 'pebble.suffix' in your application.properties file", expectedTemplateExtension(), getFileExtension(trimmedTemplateReference)); try { final PebbleTemplate template = pebbleEngine.getTemplate(normalizeTemplateReference(trimmedTemplateReference)); final Writer writer = new StringWriter(); template.evaluate(writer, model); return writer.toString(); } catch (Exception e) { throw new TemplateException(e); } }  @Override @NonNull String mergeTemplateIntoString(final @NonNull String templateReference,
                                   final @NonNull Map<String, Object> model); @Override String expectedTemplateExtension();  }
@Test public void shouldMergeTemplateIntoString() throws Exception { final String expectedBody = TemplatingTestUtils.getExpectedBody(); final String templateWithExtension = TemplatingTestUtils.TEMPLATE; assertions.assertThat(templateWithExtension).endsWith(".html"); final String body = templateService.mergeTemplateIntoString(templateWithExtension, TemplatingTestUtils.MODEL_OBJECT); assertions.assertThat(body).isEqualTo(expectedBody); }
@Test public void shouldMergeTemplateIntoStringGivenTemplateInSubfolder() throws Exception { final String expectedBody = TemplatingTestUtils.getExpectedBody(); final String templateInSubfolder = TemplatingTestUtils.TEMPLATE_IN_SUBFOLDER; assertions.assertThat(templateInSubfolder).contains("/"); final String body = templateService.mergeTemplateIntoString(templateInSubfolder, TemplatingTestUtils.MODEL_OBJECT); assertions.assertThat(body).isEqualTo(expectedBody); }
@Test public void shouldMergeTemplateIntoStringWhenNoDotsIsAvailable() throws Exception { final String expectedBody = TemplatingTestUtils.getExpectedBody(); final String templateWithNoExtension = TemplatingTestUtils.TEMPLATE.replace(".html", ""); assertions.assertThat(templateWithNoExtension).doesNotEndWith(".html"); final String body = templateService.mergeTemplateIntoString(templateWithNoExtension, TemplatingTestUtils.MODEL_OBJECT); assertions.assertThat(body).isEqualTo(expectedBody); }
@Test public void shouldMergeTemplateIntoStringThrowExceptionOnNullTemplateReference() throws Exception { final Map<String, Object> modelObject = new ImmutableMap.Builder<String, Object>().build(); assertions.assertThat(modelObject).isNotNull(); expectedException.expect(NullPointerException.class); templateService.mergeTemplateIntoString(null, modelObject); fail("NullPointerException expected"); }
@Test public void shouldMergeTemplateIntoStringThrowExceptionOnNullModel() throws Exception { String templateReference = "file." + UUID.randomUUID(); assertions.assertThat(templateReference).isNotNull(); expectedException.expect(NullPointerException.class); templateService.mergeTemplateIntoString(templateReference, null); fail("NullPointerException expected"); }
@Test public void shouldNotAcceptEmptyTemplateName() throws Exception { final Map<String, Object> modelObject = new ImmutableMap.Builder<String, Object>() .build(); expectedException.expect(IllegalArgumentException.class); templateService.mergeTemplateIntoString(" ", modelObject); fail("IllegalArgumentException expected"); }
@Test public void shouldNotAcceptTemplateNameWithoutSpecifiedExtension() throws Exception { final Map<String, Object> modelObject = new ImmutableMap.Builder<String, Object>() .build(); expectedException.expect(IllegalArgumentException.class); templateService.mergeTemplateIntoString("file." + UUID.randomUUID(), modelObject); fail("IllegalArgumentException expected"); }
@Test public void shouldThrowExceptionOnWrongTemplate() throws Exception { final Map<String, Object> modelObject = new ImmutableMap.Builder<String, Object>().build(); expectedException.expect(TemplateException.class); expectedException.expectCause(instanceOf(PebbleException.class)); templateService.mergeTemplateIntoString(TemplatingTestUtils.WRONG_TEMPLATE, modelObject); fail("TemplateException expected"); }