]> git.basschouten.com Git - openhab-addons.git/blob
c864bec469fc788c791bac584d07600ccf7b1aa9
[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.jsonpath.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 GaĆ«l L'hopital - Initial contribution
23  */
24 public class JSonPathTransformationServiceTest {
25
26     private JSonPathTransformationService processor;
27
28     @BeforeEach
29     public void init() {
30         processor = new JSonPathTransformationService();
31     }
32
33     @Test
34     public void testTransformByJSon() throws TransformationException {
35         String json = "{'store':{'book':[{'category':'reference','author':'Nigel Rees','title': 'Sayings of the Century', 'price': 8.95  } ],  'bicycle': { 'color': 'red',  'price': 19.95} }}";
36         // method under test
37         String transformedResponse = processor.transform("$.store.book[0].author", json);
38
39         // Asserts
40         assertEquals("Nigel Rees", transformedResponse);
41     }
42
43     private static final String jsonArray = "[" + //
44             "{ \"id\":1, \"name\":\"bob\", \"empty\":null }," + //
45             "{ \"id\":2, \"name\":\"alice\" }" + //
46             "]";
47
48     @Test
49     public void testValidPath1() throws TransformationException {
50         String transformedResponse = processor.transform("$[0].name", jsonArray);
51         assertEquals("bob", transformedResponse);
52     }
53
54     @Test
55     public void testValidPath2() throws TransformationException {
56         String transformedResponse = processor.transform("$[1].id", jsonArray);
57         assertEquals("2", transformedResponse);
58     }
59
60     @Test
61     public void testInvalidPathThrowsException() {
62         assertThrows(TransformationException.class, () -> processor.transform("$$", jsonArray));
63     }
64
65     @Test
66     public void testPathMismatchReturnNull() {
67         assertThrows(TransformationException.class, () -> processor.transform("$[5].id", jsonArray));
68     }
69
70     @Test
71     public void testInvalidJsonReturnNull() throws TransformationException {
72         assertThrows(TransformationException.class, () -> processor.transform("$", "{id:"));
73     }
74
75     @Test
76     public void testNullValue() throws TransformationException {
77         String transformedResponse = processor.transform("$[0].empty", jsonArray);
78         assertEquals(null, transformedResponse);
79     }
80
81     @Test
82     public void testIndefinite_filteredToSingle() throws TransformationException {
83         String transformedResponse = processor.transform("$.*[?(@.name=='bob')].id", jsonArray);
84         assertEquals("1", transformedResponse);
85     }
86
87     @Test
88     public void testIndefinite_notFiltered() throws TransformationException {
89         String transformedResponse = processor.transform("$.*.id", jsonArray);
90         assertEquals("[1, 2]", transformedResponse);
91     }
92
93     @Test
94     public void testIndefinite_noMatch() throws TransformationException {
95         String transformedResponse = processor.transform("$.*[?(@.name=='unknown')].id", jsonArray);
96         assertEquals("NULL", transformedResponse);
97     }
98
99     @Test
100     public void testBooleanList() throws TransformationException {
101         final String list = "[true, false, true, true, false]";
102         final String json = "{\"data\":" + list + "}";
103         String transformedResponse = processor.transform("$.data", json);
104         assertEquals(list, transformedResponse);
105     }
106
107     @Test
108     public void testNumberList() throws TransformationException {
109         final String list = "[0, 160, 253, -9, 21]";
110         final String json = "{\"data\":" + list + "}";
111         String transformedResponse = processor.transform("$.data", json);
112         assertEquals(list, transformedResponse);
113     }
114
115     @Test
116     public void testDoubleList() throws TransformationException {
117         final String list = "[1.0, 2.16, 5.253, -3.9, 21.21]";
118         final String json = "{\"data\":" + list + "}";
119         String transformedResponse = processor.transform("$.data", json);
120         assertEquals(list, transformedResponse);
121     }
122
123     @Test
124     public void testStringList() throws TransformationException {
125         final String list = "[\"test1\", \"test2\", \"test3\", \"test4\"]";
126         final String json = "{\"data\":" + list + "}";
127         String transformedResponse = processor.transform("$.data", json);
128         assertEquals(list, transformedResponse);
129     }
130 }