2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.jsonpath.internal;
15 import static org.junit.Assert.assertEquals;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.openhab.core.transform.TransformationException;
23 * @author Gaƫl L'hopital
25 public class JSonPathTransformationServiceTest {
27 private JSonPathTransformationService processor;
31 processor = new JSonPathTransformationService();
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} }}";
38 String transformedResponse = processor.transform("$.store.book[0].author", json);
41 Assert.assertEquals("Nigel Rees", transformedResponse);
44 private static final String jsonArray = "[" + //
45 "{ \"id\":1, \"name\":\"bob\", \"empty\":null }," + //
46 "{ \"id\":2, \"name\":\"alice\" }" + //
50 public void testValidPath1() throws TransformationException {
51 String transformedResponse = processor.transform("$[0].name", jsonArray);
52 assertEquals("bob", transformedResponse);
56 public void testValidPath2() throws TransformationException {
57 String transformedResponse = processor.transform("$[1].id", jsonArray);
58 assertEquals("2", transformedResponse);
61 @Test(expected = TransformationException.class)
62 public void testInvalidPathThrowsException() throws TransformationException {
63 processor.transform("$$", jsonArray);
66 @Test(expected = TransformationException.class)
67 public void testPathMismatchReturnNull() throws TransformationException {
68 processor.transform("$[5].id", jsonArray);
71 @Test(expected = TransformationException.class)
72 public void testInvalidJsonReturnNull() throws TransformationException {
73 processor.transform("$", "{id:");
77 public void testNullValue() throws TransformationException {
78 String transformedResponse = processor.transform("$[0].empty", jsonArray);
79 assertEquals(null, transformedResponse);
83 public void testIndefinite_filteredToSingle() throws TransformationException {
84 String transformedResponse = processor.transform("$.*[?(@.name=='bob')].id", jsonArray);
85 assertEquals("1", transformedResponse);
89 public void testIndefinite_notFiltered() throws TransformationException {
90 String transformedResponse = processor.transform("$.*.id", jsonArray);
91 assertEquals("[1, 2]", transformedResponse);
95 public void testIndefinite_noMatch() throws TransformationException {
96 String transformedResponse = processor.transform("$.*[?(@.name=='unknown')].id", jsonArray);
97 assertEquals("NULL", transformedResponse);
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);
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);
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);
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);