Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
JsonPath transformation service comply with the contract
Browse files Browse the repository at this point in the history
...as it was returning null in case the path pointed to an element which did not exist.

fixes #4376
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
  • Loading branch information
sjsf committed Oct 5, 2017
1 parent e4bcb6b commit 5cd0969
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Expand Up @@ -8,4 +8,5 @@ Bundle-Version: 0.9.0.qualifier
Fragment-Host: org.eclipse.smarthome.transform.jsonpath
Import-Package:
org.eclipse.jdt.annotation;resolution:=optional,
org.hamcrest,
org.junit;version="4.0.0"
Expand Up @@ -7,8 +7,9 @@
*/
package org.eclipse.smarthome.transform.jsonpath.internal;

import static org.junit.Assert.assertEquals;

import org.eclipse.smarthome.core.transform.TransformationException;
import org.eclipse.smarthome.transform.jsonpath.internal.JSonPathTransformationService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,47 +28,45 @@ public void init() {

@Test
public void testTransformByJSon() throws TransformationException {
String json = "{'store':{'book':[{'category':'reference','author':'Nigel Rees','title': 'Sayings of the Century', 'price': 8.95 } ], 'bicycle': { 'color': 'red', 'price': 19.95} }}";

String json = "{'store':{'book':[{'category':'reference','author':'Nigel Rees','title': 'Sayings of the Century', 'price': 8.95 } ], 'bicycle': { 'color': 'red', 'price': 19.95} }}";
// method under test
String transformedResponse = processor.transform("$.store.book[0].author",json);
String transformedResponse = processor.transform("$.store.book[0].author", json);

// Asserts
Assert.assertEquals("Nigel Rees", transformedResponse);
}
private static final String jsonArray = "["
+ "{ \"id\":1, \"name\":\"bob\" },"
+ "{ \"id\":2, \"name\":\"alice\" }"
+ "]";

private static final String jsonArray = "[" + //
"{ \"id\":1, \"name\":\"bob\" }," + //
"{ \"id\":2, \"name\":\"alice\" }" + //
"]";

@Test
public void testValidPath1() throws TransformationException {
String transformedResponse = processor.transform("$[0].name", jsonArray);
assert(transformedResponse == "bob");
assertEquals("bob", transformedResponse);
}

@Test
public void testValidPath2() throws TransformationException {
String transformedResponse = processor.transform("$[1].id", jsonArray);
assert(transformedResponse == "2");
assertEquals("2", transformedResponse);
}

@Test(expected = TransformationException.class)
public void testInvalidPathThrowsException() throws TransformationException {
processor.transform("$$", jsonArray);
}
@Test

@Test(expected = TransformationException.class)
public void testPathMismatchReturnNull() throws TransformationException {
String transformedResponse = processor.transform("$[5].id", jsonArray);
assert(transformedResponse == null);
processor.transform("$[5].id", jsonArray);
}
@Test

@Test(expected = TransformationException.class)
public void testInvalidJsonReturnNull() throws TransformationException {
String transformedResponse = processor.transform("$", jsonArray.substring(1));
assert(transformedResponse == null);
processor.transform("$", "{id:");
}

}
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
Expand Down Expand Up @@ -48,10 +49,10 @@ public String transform(String jsonPathExpression, String source) throws Transfo
try {
Object transformationResult = JsonPath.read(source, jsonPathExpression);
logger.debug("transformation resulted in '{}'", transformationResult);
return (transformationResult != null) ? transformationResult.toString() : null;
return (transformationResult != null) ? transformationResult.toString() : source;
} catch (PathNotFoundException e) {
return null;
} catch (InvalidPathException e) {
throw new TransformationException("Invalid path '" + jsonPathExpression + "' in '" + source + "'");
} catch (InvalidPathException | InvalidJsonException e) {
throw new TransformationException("An error occurred while transforming JSON expression.", e);
}

Expand Down

0 comments on commit 5cd0969

Please sign in to comment.