3 changed files with 64 additions and 3 deletions
@ -0,0 +1,37 @@
|
||||
package com.jayway.jsonpath.internal.function; |
||||
|
||||
import com.jayway.jsonpath.Configuration; |
||||
import com.jayway.jsonpath.Configurations; |
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.io.InvalidClassException; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
|
||||
class CustomFunctionTest extends BaseFunctionTest { |
||||
|
||||
private Configuration conf = Configurations.JSON_SMART_CONFIGURATION; |
||||
|
||||
public class InvalidFunction { |
||||
|
||||
} |
||||
@Test |
||||
void testAddInvalidFunction(){ |
||||
assertThrows(InvalidClassException.class, () -> PathFunctionFactory.addCustomFunction("invalid", |
||||
InvalidFunction.class)); |
||||
} |
||||
|
||||
@Test |
||||
void testAddValidFunction(){ |
||||
assertDoesNotThrow(()->PathFunctionFactory.addCustomFunction("toUpperCase",ToUpperCase.class)); |
||||
verifyTextFunction(conf,"$['text'][0].toUpperCase()","A"); |
||||
} |
||||
|
||||
@Test |
||||
void testAddExistentFunction(){ |
||||
assertThrows(InvalidPathException.class, () -> PathFunctionFactory.addCustomFunction("avg", |
||||
ToUpperCase.class)); |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.jayway.jsonpath.internal.function; |
||||
|
||||
import com.jayway.jsonpath.internal.EvaluationContext; |
||||
import com.jayway.jsonpath.internal.PathRef; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class ToUpperCase implements PathFunction{ |
||||
@Override |
||||
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, |
||||
List<Parameter> parameters) { |
||||
return ((String) model).toUpperCase(); |
||||
} |
||||
} |
Loading…
Reference in new issue