@Override public int hashCode() { return getCanonicalType().hashCode(); }
/** * Creates a new JSONGetFunction instance. * * @param coercibleType of JSONCoercibleType * @param pointerMap of Map */ @ConstructorProperties({"coercibleType", "pointerMap"}) public JSONGetFunction( JSONCoercibleType coercibleType, Map<Fields, String> pointerMap ) { this( coercibleType, asFields( pointerMap.keySet() ), asArray( pointerMap.values() ) ); }
@Test public void stringLiteralCoercions() { testCoercion( "\"Foo\"", JsonNodeType.STRING, "Foo", String.class ); testCoercion( "Foo", JsonNodeType.STRING, "Foo", String.class ); testCoercion( "100", JsonNodeType.NUMBER, 100, Integer.class ); testCoercion( 100, JsonNodeType.NUMBER, 100, Integer.class ); }
@Test public void testRegexFilter() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); assertTrue( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John D Doe" ) ), entry ) ); assertTrue( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "^John$" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "^John Doe$" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John Doe" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John[ ]Doe$" ) ), entry ) ); } }
@Test public void testGetMissing() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), "/person/foobar" ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNull( value ); }
@Test(expected = OperationException.class) public void testGetMissingFail() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), true, "/person/foobar" ); invokeFunction( function, entry, new Fields( "result" ) ); }
private void testCoercion( Object value, JsonNodeType nodeType, Object resultValue, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( resultValue, JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); }
@Test public void objectCoercions() { for( String value : JSONData.objects ) testContainerCoercion( value, JsonNodeType.OBJECT, String.class ); }
private String textOrWrite( JsonNode value ) { if( value != null && value.isTextual() ) return value.textValue(); try { return write( value ); } catch( JsonProcessingException exception ) { throw new CascadingException( "unable to write value as json", exception ); } }
@Test public void testRegexFilter() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); assertTrue( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John D Doe" ) ), entry ) ); assertTrue( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "^John$" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "^John Doe$" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John Doe" ) ), entry ) ); assertFalse( invokeFilter( new JSONRegexFilter( "/person/name", Pattern.compile( "John[ ]Doe$" ) ), entry ) ); } }
@Test(expected = OperationException.class) public void testGetMissingFail() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), true, "/person/foobar" ); invokeFunction( function, entry, new Fields( "result" ) ); }
private void testCoercion( Object value, JsonNodeType nodeType, Object resultValue, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( resultValue, JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); }
@Test public void arrayCoercions() { for( String value : JSONData.arrays ) testContainerCoercion( value, JsonNodeType.ARRAY, String.class ); }
/** * Creates a new JSONGetFunction instance. * * @param pointerMap of Map * @param failOnMissingNode of boolean */ @ConstructorProperties({"pointerMap", "failOnMissingNode"}) public JSONGetFunction( Map<Fields, String> pointerMap, boolean failOnMissingNode ) { this( asFields( pointerMap.keySet() ), failOnMissingNode, asArray( pointerMap.values() ) ); }
@Test public void stringLiteralCoercions() { testCoercion( "\"Foo\"", JsonNodeType.STRING, "Foo", String.class ); testCoercion( "Foo", JsonNodeType.STRING, "Foo", String.class ); testCoercion( "100", JsonNodeType.NUMBER, 100, Integer.class ); testCoercion( 100, JsonNodeType.NUMBER, 100, Integer.class ); }
@Override public boolean equals( Object object ) { if( this == object ) return true; if( !( object instanceof CoercibleType ) ) return false; return getCanonicalType().equals( ( (CoercibleType) object ).getCanonicalType() ); } }
@Test public void objectCoercions() { for( String value : JSONData.objects ) testContainerCoercion( value, JsonNodeType.OBJECT, String.class ); }
@Test public void arrayCoercions() { for( String value : JSONData.arrays ) testContainerCoercion( value, JsonNodeType.ARRAY, String.class ); }
/** * Creates a new JSONGetFunction instance. * * @param coercibleType of JSONCoercibleType * @param pointerMap of Map * @param failOnMissingNode of boolean */ @ConstructorProperties({"coercibleType", "pointerMap", "failOnMissingNode"}) public JSONGetFunction( JSONCoercibleType coercibleType, Map<Fields, String> pointerMap, boolean failOnMissingNode ) { this( coercibleType, asFields( pointerMap.keySet() ), failOnMissingNode, asArray( pointerMap.values() ) ); }