|
1 | 1 | package com.baeldung.mustache; |
2 | 2 |
|
3 | | -import static org.assertj.core.api.Assertions.assertThat; |
| 3 | +import com.baeldung.mustache.model.Todo; |
| 4 | +import com.github.mustachejava.Mustache; |
| 5 | +import org.junit.Test; |
4 | 6 |
|
5 | 7 | import java.io.IOException; |
6 | 8 | import java.io.StringWriter; |
7 | 9 | import java.time.Instant; |
8 | | -import java.time.LocalDateTime; |
9 | | -import java.time.temporal.TemporalUnit; |
10 | 10 | import java.util.Arrays; |
11 | 11 | import java.util.Date; |
12 | 12 | import java.util.HashMap; |
13 | 13 | import java.util.List; |
14 | 14 | import java.util.Map; |
15 | | -import java.util.function.Function; |
16 | 15 |
|
17 | | -import org.junit.Test; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
18 | 17 |
|
19 | | -import com.baeldung.mustache.model.Todo; |
20 | | -import com.github.mustachejava.Mustache; |
21 | 18 | public class TodoMustacheServiceTest { |
22 | 19 |
|
23 | | - private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException{ |
| 20 | + private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException { |
24 | 21 | StringWriter writer = new StringWriter(); |
25 | 22 | m.execute(writer, context).flush(); |
26 | 23 | return writer.toString(); |
27 | 24 | } |
28 | | - |
29 | | - private String executeTemplate(Mustache m, Todo todo) throws IOException{ |
| 25 | + |
| 26 | + private String executeTemplate(Mustache m, Todo todo) throws IOException { |
30 | 27 | StringWriter writer = new StringWriter(); |
31 | 28 | m.execute(writer, todo).flush(); |
32 | 29 | return writer.toString(); |
33 | 30 | } |
34 | | - |
| 31 | + |
35 | 32 | @Test |
36 | | - public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException{ |
| 33 | + public void givenTodoObject_whenGetHtml_thenSuccess() throws IOException { |
37 | 34 | Todo todo = new Todo("Todo 1", "Todo description"); |
38 | 35 | Mustache m = MustacheUtil.getMustacheFactory().compile("todo.mustache"); |
39 | 36 | Map<String, Object> context = new HashMap<>(); |
40 | 37 | context.put("todo", todo); |
41 | | - |
| 38 | + |
42 | 39 | String expected = "<h2>Todo 1</h2>"; |
43 | 40 | assertThat(executeTemplate(m, todo)).contains(expected); |
44 | 41 | } |
45 | | - |
| 42 | + |
46 | 43 | @Test |
47 | | - public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException{ |
| 44 | + public void givenNullTodoObject_whenGetHtml_thenEmptyHtml() throws IOException { |
48 | 45 | Mustache m = MustacheUtil.getMustacheFactory().compile("todo-section.mustache"); |
49 | 46 | Map<String, Object> context = new HashMap<>(); |
50 | 47 | assertThat(executeTemplate(m, context)).isEmpty(); |
51 | 48 | } |
52 | | - |
| 49 | + |
53 | 50 | @Test |
54 | | - public void givenTodoList_whenGetHtml_thenSuccess() throws IOException{ |
| 51 | + public void givenTodoList_whenGetHtml_thenSuccess() throws IOException { |
55 | 52 | Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); |
56 | | - |
| 53 | + |
57 | 54 | List<Todo> todos = Arrays.asList( |
58 | | - new Todo("Todo 1", "Todo description"), |
59 | | - new Todo("Todo 2", "Todo description another"), |
60 | | - new Todo("Todo 3", "Todo description another") |
61 | | - ); |
| 55 | + new Todo("Todo 1", "Todo description"), |
| 56 | + new Todo("Todo 2", "Todo description another"), |
| 57 | + new Todo("Todo 3", "Todo description another") |
| 58 | + ); |
62 | 59 | Map<String, Object> context = new HashMap<>(); |
63 | 60 | context.put("todos", todos); |
64 | | - |
| 61 | + |
65 | 62 | assertThat(executeTemplate(m, context)) |
66 | 63 | .contains("<h2>Todo 1</h2>") |
67 | 64 | .contains("<h2>Todo 2</h2>") |
68 | 65 | .contains("<h2>Todo 3</h2>"); |
69 | 66 | } |
70 | | - |
| 67 | + |
71 | 68 | @Test |
72 | | - public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException{ |
| 69 | + public void givenEmptyList_whenGetHtml_thenEmptyHtml() throws IOException { |
73 | 70 | Mustache m = MustacheUtil.getMustacheFactory().compile("todos.mustache"); |
74 | | - |
| 71 | + |
75 | 72 | Map<String, Object> context = new HashMap<>(); |
76 | | - assertThat(executeTemplate(m, context)).isEmpty();; |
| 73 | + assertThat(executeTemplate(m, context)).isEmpty(); |
| 74 | + ; |
77 | 75 | } |
78 | | - |
| 76 | + |
79 | 77 | @Test |
80 | | - public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException{ |
| 78 | + public void givenEmptyList_whenGetHtmlUsingInvertedSection_thenHtml() throws IOException { |
81 | 79 | Mustache m = MustacheUtil.getMustacheFactory().compile("todos-inverted-section.mustache"); |
82 | | - |
| 80 | + |
83 | 81 | Map<String, Object> context = new HashMap<>(); |
84 | 82 | assertThat(executeTemplate(m, context).trim()).isEqualTo("<p>No todos!</p>"); |
85 | 83 | } |
86 | | - |
| 84 | + |
87 | 85 | @Test |
88 | | - public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException{ |
| 86 | + public void givenTodoList_whenGetHtmlUsingLamdba_thenHtml() throws IOException { |
89 | 87 | Mustache m = MustacheUtil.getMustacheFactory().compile("todos-lambda.mustache"); |
90 | 88 | List<Todo> todos = Arrays.asList( |
91 | | - new Todo("Todo 1", "Todo description"), |
92 | | - new Todo("Todo 2", "Todo description another"), |
93 | | - new Todo("Todo 3", "Todo description another") |
94 | | - ); |
| 89 | + new Todo("Todo 1", "Todo description"), |
| 90 | + new Todo("Todo 2", "Todo description another"), |
| 91 | + new Todo("Todo 3", "Todo description another") |
| 92 | + ); |
95 | 93 | todos.get(2).setDone(true); |
96 | 94 | todos.get(2).setCompletedOn(Date.from(Instant.now().plusSeconds(300))); |
97 | | - |
| 95 | + |
98 | 96 | Map<String, Object> context = new HashMap<>(); |
99 | 97 | context.put("todos", todos); |
100 | 98 | assertThat(executeTemplate(m, context).trim()).contains("Done 5 minutes ago"); |
|
0 commit comments