diff --git a/mustache/pom.xml b/mustache/pom.xml new file mode 100644 index 0000000000..8916b196f0 --- /dev/null +++ b/mustache/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + com.baeldung + mustache + 0.0.1-SNAPSHOT + jar + mustache + + + + com.github.spullara.mustache.java + compiler + ${mustache.compiler.api.version} + + + + org.assertj + assertj-core + ${assertj.version} + + + + + log4j + log4j + ${log4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + 0.9.2 + 3.7.0 + 1.2.16 + 4.12 + + UTF-8 + + + 3.6.0 + 2.19.1 + + + \ No newline at end of file diff --git a/mustache/src/main/java/com/baeldung/mustache/MustacheUtil.java b/mustache/src/main/java/com/baeldung/mustache/MustacheUtil.java new file mode 100644 index 0000000000..a30bb23adb --- /dev/null +++ b/mustache/src/main/java/com/baeldung/mustache/MustacheUtil.java @@ -0,0 +1,15 @@ +package com.baeldung.mustache; + +import com.github.mustachejava.DefaultMustacheFactory; +import com.github.mustachejava.MustacheFactory; + +public class MustacheUtil { + + private static final MustacheFactory mf = new DefaultMustacheFactory(); + + public static MustacheFactory getMustacheFactory(){ + return mf; + } + + +} diff --git a/mustache/src/main/java/com/baeldung/mustache/model/Todo.java b/mustache/src/main/java/com/baeldung/mustache/model/Todo.java new file mode 100644 index 0000000000..c329045876 --- /dev/null +++ b/mustache/src/main/java/com/baeldung/mustache/model/Todo.java @@ -0,0 +1,84 @@ +package com.baeldung.mustache.model; + +import java.time.Duration; +import java.time.temporal.Temporal; +import java.util.Date; +import java.util.function.Function; + +public class Todo { + + public Todo(){} + + public Todo(String title, String text){ + this.title = title; + this.text = text; + createdOn = new Date(); + } + + private String title; + private String text; + private boolean done = false; + + private Date createdOn; + private Date completedOn; + + public void markAsDone(){ + done = true; + completedOn = new Date(); + } + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean getDone() { + return done; + } + + public Date getCreatedOn() { + return createdOn; + } + + public Date getCompletedOn() { + return completedOn; + } + + public void setDone(boolean done) { + this.done = done; + } + + public void setCompletedOn(Date completedOn) { + this.completedOn = completedOn; + } + + public long doneSince(){ + if ( done ){ + return Duration.between(createdOn.toInstant(), completedOn.toInstant()).toMinutes(); + } + return 0; + } + + public Function handleDone(){ + return (obj) -> { + if ( done ){ + return String.format("Done %s minutes ago", obj); + }else{ + return ""; + } + + }; + + } + +} diff --git a/mustache/src/main/resources/todo-section.mustache b/mustache/src/main/resources/todo-section.mustache new file mode 100644 index 0000000000..f05be6f553 --- /dev/null +++ b/mustache/src/main/resources/todo-section.mustache @@ -0,0 +1,5 @@ +{{#todo}} +

{{title}}

+Created on {{createdOn}} +

{{text}}

+{{/todo}} diff --git a/mustache/src/main/resources/todo.mustache b/mustache/src/main/resources/todo.mustache new file mode 100644 index 0000000000..5cd7a99ff5 --- /dev/null +++ b/mustache/src/main/resources/todo.mustache @@ -0,0 +1,3 @@ +

{{title}}

+Created on {{createdOn}} +

{{text}}

\ No newline at end of file diff --git a/mustache/src/main/resources/todos-inverted-section.mustache b/mustache/src/main/resources/todos-inverted-section.mustache new file mode 100644 index 0000000000..5d3cd16c73 --- /dev/null +++ b/mustache/src/main/resources/todos-inverted-section.mustache @@ -0,0 +1,6 @@ +{{#todos}} +

{{title}}

+{{/todos}} +{{^todos}} +

No todos!

+{{/todos}} \ No newline at end of file diff --git a/mustache/src/main/resources/todos-lambda.mustache b/mustache/src/main/resources/todos-lambda.mustache new file mode 100644 index 0000000000..c89a7bc4c5 --- /dev/null +++ b/mustache/src/main/resources/todos-lambda.mustache @@ -0,0 +1,3 @@ +{{#todos}} +

{{title}}{{#handleDone}}{{doneSince}}{{/handleDone}}

+{{/todos}} \ No newline at end of file diff --git a/mustache/src/main/resources/todos.mustache b/mustache/src/main/resources/todos.mustache new file mode 100644 index 0000000000..90722e74bb --- /dev/null +++ b/mustache/src/main/resources/todos.mustache @@ -0,0 +1,3 @@ +{{#todos}} +

{{title}}

+{{/todos}} \ No newline at end of file diff --git a/mustache/src/test/java/com/baeldung/mustache/TodoMustacheServiceTest.java b/mustache/src/test/java/com/baeldung/mustache/TodoMustacheServiceTest.java new file mode 100644 index 0000000000..17a59aa6a1 --- /dev/null +++ b/mustache/src/test/java/com/baeldung/mustache/TodoMustacheServiceTest.java @@ -0,0 +1,102 @@ +package com.baeldung.mustache; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.io.StringWriter; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.temporal.TemporalUnit; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +import org.junit.Test; + +import com.baeldung.mustache.model.Todo; +import com.github.mustachejava.Mustache; +public class TodoMustacheServiceTest { + + private String executeTemplate(Mustache m, Map context) throws IOException{ + StringWriter writer = new StringWriter(); + m.execute(writer, context).flush(); + return writer.toString(); + } + + private String executeTemplate(Mustache m, Todo todo) throws IOException{ + StringWriter writer = new StringWriter(); + m.execute(writer, todo).flush(); + return writer.toString(); + } + + @Test + public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException{ + Todo todo = new Todo("Todo 1", "Todo description"); + Mustache m = MustacheUtil.getMustacheFactory().compile("todo.mustache"); + Map context = new HashMap<>(); + context.put("todo", todo); + + String expected = "

Todo 1

"; + assertThat(executeTemplate(m, todo)).contains(expected); + } + + @Test + public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException{ + Mustache m = MustacheUtil.getMustacheFactory().compile("todo-section.mustache"); + Map context = new HashMap<>(); + assertThat(executeTemplate(m, context)).isEmpty(); + } + + @Test + public void givenTodoList_whenGetHtml_thenSuccess() throws IOException{ + Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); + + List todos = Arrays.asList( + new Todo("Todo 1", "Todo description"), + new Todo("Todo 2", "Todo description another"), + new Todo("Todo 3", "Todo description another") + ); + Map context = new HashMap<>(); + context.put("todos", todos); + + assertThat(executeTemplate(m, context)) + .contains("

Todo 1

") + .contains("

Todo 2

") + .contains("

Todo 3

"); + } + + @Test + public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException{ + Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); + + Map context = new HashMap<>(); + assertThat(executeTemplate(m, context)).isEmpty();; + } + + @Test + public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException{ + Mustache m = MustacheUtil.getMustacheFactory().compile("todos-inverted-section.mustache"); + + Map context = new HashMap<>(); + assertThat(executeTemplate(m, context).trim()).isEqualTo("

No todos!

"); + } + + @Test + public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException{ + Mustache m = MustacheUtil.getMustacheFactory().compile("todos-lambda.mustache"); + List todos = Arrays.asList( + new Todo("Todo 1", "Todo description"), + new Todo("Todo 2", "Todo description another"), + new Todo("Todo 3", "Todo description another") + ); + todos.get(2).setDone(true); + todos.get(2).setCompletedOn(Date.from(Instant.now().plusSeconds(300))); + + Map context = new HashMap<>(); + context.put("todos", todos); + assertThat(executeTemplate(m, context).trim()).contains("Done 5 minutes ago"); + } +} diff --git a/pom.xml b/pom.xml index 609d69c3ea..3663d1308a 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,7 @@ mockito mockito2 mocks + mustache orika