]> git.basschouten.com Git - openhab-addons.git/blob
ecf78c971682550681d50238aa969c76391bdc48
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.jinja.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.core.transform.TransformationException;
20
21 /**
22  * @author Jochen Klein - Initial contribution
23  */
24 public class JinjaTransformationServiceTest {
25
26     private JinjaTransformationService processor;
27
28     @BeforeEach
29     public void init() {
30         processor = new JinjaTransformationService();
31     }
32
33     @Test
34     public void testTransformByJSon() throws TransformationException {
35         String json = "{\"Time\":\"2019-01-05T22:45:12\",\"AM2301\":{\"Temperature\":4.7,\"Humidity\":99.9},\"TempUnit\":\"C\"}";
36         // method under test
37         String transformedResponse = processor.transform("{{value_json['AM2301'].Temperature}}", json);
38
39         // Asserts
40         assertEquals("4.7", transformedResponse);
41     }
42
43     @Test
44     public void testStringOnly() throws TransformationException {
45         String value = "world";
46         // method under test
47         String transformedResponse = processor.transform("Hello {{ value }}!", value);
48
49         // Asserts
50         assertEquals("Hello world!", transformedResponse);
51     }
52
53     @Test
54     public void testQuotedStringOnly() throws TransformationException {
55         String value = "\"world\"";
56         // method under test
57         String transformedResponse = processor.transform("Hello {{ value_json }}!", value);
58
59         // Asserts
60         assertEquals("Hello world!", transformedResponse);
61     }
62
63     @Test
64     public void testJsonParsingError() throws TransformationException {
65         // when JSON binding parsing failed
66         String transformedResponse = processor.transform("Hello {{ value }}!", "{\"string\"{: \"world\"}");
67
68         // then template should be rendered
69         assertEquals("Hello {\"string\"{: \"world\"}!", transformedResponse);
70     }
71
72     @Test
73     public void testTemplateError() {
74         assertThrows(TransformationException.class,
75                 () -> processor.transform("Hello {{{ value_json.string }}!", "{\"string\": \"world\"}"));
76     }
77
78     @Test
79     public void testMissingVariableError() {
80         assertThrows(TransformationException.class,
81                 () -> processor.transform("Hello {{ missing }}!", "{\"string\": \"world\"}"));
82     }
83
84     @Test
85     public void testMissingMapKeyError() {
86         assertThrows(TransformationException.class,
87                 () -> processor.transform("Hello {{ value_json.missing }}!", "{\"string\": \"world\"}"));
88     }
89
90     @Test
91     public void testMissingVariableIsDefined() throws TransformationException {
92         // when checking missing variable
93         String transformedResponse = processor.transform("{{ missing is defined }}", "{\"string\": \"world\"}");
94
95         // then missing variable is not defined
96         assertEquals("false", transformedResponse);
97     }
98
99     @Test
100     public void testMissingMapKeyIsDefined() throws TransformationException {
101         // when checking missing map key
102         String transformedResponse = processor.transform("{{ value_json.missing is defined }}",
103                 "{\"string\": \"world\"}");
104
105         // then missing map key is not defined
106         assertEquals("false", transformedResponse);
107     }
108
109     @Test
110     public void testIsDefined() throws TransformationException {
111         // when checking map key
112         String transformedResponse = processor.transform("{{ value_json.string is defined }}",
113                 "{\"string\": \"world\"}");
114
115         // then map key is defined
116         assertEquals("true", transformedResponse);
117     }
118 }