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