|
21 | 21 | * <http://www.gnu.org/licenses/gpl-3.0.html>. |
22 | 22 | * #L% |
23 | 23 | */ |
24 | | - |
| 24 | +import com.condation.cms.api.feature.features.MarkdownRendererFeature; |
| 25 | +import com.condation.cms.api.markdown.MarkdownRenderer; |
| 26 | +import com.condation.cms.api.request.RequestContext; |
| 27 | +import com.condation.cms.api.request.RequestContextScope; |
25 | 28 | import com.condation.cms.templates.filter.impl.DateFilter; |
| 29 | +import com.condation.cms.templates.filter.impl.MarkdownFilter; |
26 | 30 | import com.condation.cms.templates.filter.impl.RawFilter; |
27 | 31 | import java.text.SimpleDateFormat; |
28 | 32 | import java.util.Date; |
29 | 33 | import org.apache.commons.text.StringEscapeUtils; |
30 | 34 | import org.assertj.core.api.Assertions; |
31 | 35 | import org.junit.jupiter.api.BeforeEach; |
32 | 36 | import org.junit.jupiter.api.Test; |
| 37 | +import org.mockito.Mockito; |
33 | 38 |
|
34 | 39 | public class FilterTest { |
35 | 40 |
|
36 | | - FilterRegistry registry = new FilterRegistry(); |
37 | | - FilterPipeline pipeline = new FilterPipeline(registry); |
38 | | - |
39 | | - @BeforeEach |
40 | | - public void setup() { |
41 | | - // Register filters |
42 | | - registry.register("raw", (input, params) -> input); // Raw does nothing |
43 | | - registry.register("truncate", (input, params) -> { |
44 | | - if (input instanceof String stringValue) { |
45 | | - int length = params.length > 0 ? (Integer)params[0] : stringValue.length(); |
46 | | - return stringValue.length() > length ? stringValue.substring(0, length) + "..." : input; |
47 | | - } |
48 | | - return input; |
49 | | - }); |
50 | | - |
51 | | - pipeline.addStep("raw"); |
52 | | - pipeline.addStep("truncate", 20); |
53 | | - } |
54 | | - |
55 | | - @Test |
56 | | - void test() { |
57 | | - Object result = pipeline.execute("Dies ist ein langer Text, der abgeschnitten werden sollte."); |
58 | | - Assertions.assertThat(result).isEqualTo("Dies ist ein langer ..."); |
59 | | - } |
60 | | - |
| 41 | + FilterRegistry registry = new FilterRegistry(); |
| 42 | + FilterPipeline pipeline = new FilterPipeline(registry); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setup() { |
| 46 | + // Register filters |
| 47 | + registry.register("raw", (input, params) -> input); // Raw does nothing |
| 48 | + registry.register("truncate", (input, params) -> { |
| 49 | + if (input instanceof String stringValue) { |
| 50 | + int length = params.length > 0 ? (Integer) params[0] : stringValue.length(); |
| 51 | + return stringValue.length() > length ? stringValue.substring(0, length) + "..." : input; |
| 52 | + } |
| 53 | + return input; |
| 54 | + }); |
| 55 | + |
| 56 | + pipeline.addStep("raw"); |
| 57 | + pipeline.addStep("truncate", 20); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void test() { |
| 62 | + Object result = pipeline.execute("Dies ist ein langer Text, der abgeschnitten werden sollte."); |
| 63 | + Assertions.assertThat(result).isEqualTo("Dies ist ein langer ..."); |
| 64 | + } |
| 65 | + |
61 | 66 | @Test |
62 | | - void date() { |
63 | | - |
| 67 | + void date() { |
| 68 | + |
64 | 69 | FilterRegistry registry = new FilterRegistry(); |
65 | 70 | FilterPipeline pipeline = new FilterPipeline(registry); |
66 | 71 | registry.register(DateFilter.NAME, new DateFilter()); |
67 | | - |
| 72 | + |
68 | 73 | pipeline.addStep("date"); |
69 | | - |
70 | | - |
| 74 | + |
71 | 75 | var date = new Date(); |
72 | 76 | SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy"); |
73 | 77 | var formatted = format.format(date); |
74 | | - |
75 | | - Object result = pipeline.execute(date); |
76 | | - Assertions.assertThat(result).isEqualTo(formatted); |
77 | | - } |
78 | | - |
| 78 | + |
| 79 | + Object result = pipeline.execute(date); |
| 80 | + Assertions.assertThat(result).isEqualTo(formatted); |
| 81 | + } |
| 82 | + |
79 | 83 | @Test |
80 | | - void date_custom_format() { |
81 | | - |
| 84 | + void date_custom_format() { |
| 85 | + |
82 | 86 | FilterRegistry registry = new FilterRegistry(); |
83 | 87 | FilterPipeline pipeline = new FilterPipeline(registry); |
84 | 88 | registry.register(DateFilter.NAME, new DateFilter()); |
85 | | - |
| 89 | + |
86 | 90 | pipeline.addStep("date", "MM/yyyy"); |
87 | | - |
88 | | - |
| 91 | + |
89 | 92 | var date = new Date(); |
90 | 93 | SimpleDateFormat format = new SimpleDateFormat("MM/yyyy"); |
91 | 94 | var formatted = format.format(date); |
92 | | - |
93 | | - Object result = pipeline.execute(date); |
94 | | - Assertions.assertThat(result).isEqualTo(formatted); |
95 | | - } |
96 | | - |
| 95 | + |
| 96 | + Object result = pipeline.execute(date); |
| 97 | + Assertions.assertThat(result).isEqualTo(formatted); |
| 98 | + } |
| 99 | + |
97 | 100 | @Test |
98 | | - void raw() { |
99 | | - |
| 101 | + void raw() { |
100 | 102 | FilterRegistry registry = new FilterRegistry(); |
101 | 103 | FilterPipeline pipeline = new FilterPipeline(registry); |
102 | 104 | registry.register(RawFilter.NAME, new RawFilter()); |
103 | | - |
| 105 | + |
104 | 106 | pipeline.addStep("raw"); |
105 | | - |
| 107 | + |
106 | 108 | String input = """ |
107 | 109 | <p>"We believe that great content is the foundation for successful communication and lasting connections. With Condation, we aim to create the base where ideas can grow and thrive. Our goal is to provide a solid, reliable platform that enables everyone to easily create, manage, and share their content—helping to change the world, one idea at a time."</p><p></p> |
108 | 110 | """; |
109 | 111 |
|
110 | 112 | String escaped = StringEscapeUtils.ESCAPE_HTML4.translate(input); |
111 | 113 | Object result = pipeline.execute(escaped); |
112 | 114 | Assertions.assertThat(result).isEqualTo(input); |
113 | | - } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void markdown() { |
| 119 | + FilterRegistry registry = new FilterRegistry(); |
| 120 | + FilterPipeline pipeline = new FilterPipeline(registry); |
| 121 | + registry.register(MarkdownFilter.NAME, new MarkdownFilter()); |
| 122 | + pipeline.addStep("markdown"); |
| 123 | + |
| 124 | + var markdownRenderer = Mockito.mock(MarkdownRenderer.class); |
| 125 | + Mockito.when(markdownRenderer.render(Mockito.any())).thenReturn(""); |
| 126 | + |
| 127 | + RequestContext context = new RequestContext(); |
| 128 | + context.add(MarkdownRendererFeature.class, new MarkdownRendererFeature(markdownRenderer)); |
| 129 | + ScopedValue.where(RequestContextScope.REQUEST_CONTEXT, context).run(() -> { |
| 130 | + String input = "input string"; |
| 131 | + pipeline.execute(input); |
| 132 | + |
| 133 | + Mockito.verify(markdownRenderer, Mockito.times(1)).render(input); |
| 134 | + }); |
| 135 | + } |
114 | 136 | } |
0 commit comments