2 * Copyright (c) 2010-2023 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.jupiter.api.Assertions.*;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.core.transform.TransformationException;
22 * @author Gaƫl L'hopital
24 public class JSonPathTransformationServiceTest {
26 private JSonPathTransformationService processor;
30 processor = new JSonPathTransformationService();
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} }}";
37 String transformedResponse = processor.transform("$.store.book[0].author", json);
40 assertEquals("Nigel Rees", transformedResponse);
43 private static final String jsonArray = "[" + //
44 "{ \"id\":1, \"name\":\"bob\", \"empty\":null }," + //
45 "{ \"id\":2, \"name\":\"alice\" }" + //
49 public void testValidPath1() throws TransformationException {
50 String transformedResponse = processor.transform("$[0].name", jsonArray);
51 assertEquals("bob", transformedResponse);
55 public void testValidPath2() throws TransformationException {
56 String transformedResponse = processor.transform("$[1].id", jsonArray);
57 assertEquals("2", transformedResponse);
61 public void testInvalidPathThrowsException() {
62 assertThrows(TransformationException.class, () -> processor.transform("$$", jsonArray));
66 public void testPathMismatchReturnNull() {
67 assertThrows(TransformationException.class, () -> processor.transform("$[5].id", jsonArray));
71 public void testInvalidJsonReturnNull() throws TransformationException {
72 assertThrows(TransformationException.class, () -> processor.transform("$", "{id:"));
76 public void testNullValue() throws TransformationException {
77 String transformedResponse = processor.transform("$[0].empty", jsonArray);
78 assertEquals(null, transformedResponse);
82 public void testIndefinite_filteredToSingle() throws TransformationException {
83 String transformedResponse = processor.transform("$.*[?(@.name=='bob')].id", jsonArray);
84 assertEquals("1", transformedResponse);
88 public void testIndefinite_notFiltered() throws TransformationException {
89 String transformedResponse = processor.transform("$.*.id", jsonArray);
90 assertEquals("[1, 2]", transformedResponse);
94 public void testIndefinite_noMatch() throws TransformationException {
95 String transformedResponse = processor.transform("$.*[?(@.name=='unknown')].id", jsonArray);
96 assertEquals("NULL", transformedResponse);
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);
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);
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);
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);