@Test public void testCoerceArray() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromTransform( "/person", "/measures/*/value", JSONPrimitiveTransforms.TO_FLOAT ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( JsonNodeType.ARRAY, ( (ObjectNode) value ).get( "measures" ).getNodeType() ); assertEquals( FloatNode.class, ( (ObjectNode) value ).get( "measures" ).get( 0 ).get( "value" ).getClass() ); assertEquals( 1000.0F, ( (ObjectNode) value ).get( "measures" ).get( 0 ).get( "value" ).floatValue() ); assertEquals( FloatNode.class, ( (ObjectNode) value ).get( "measures" ).get( 1 ).get( "value" ).getClass() ); assertEquals( 2000.0F, ( (ObjectNode) value ).get( "measures" ).get( 1 ).get( "value" ).floatValue() ); }
@Test public void testCopyIntoArrayPredicate() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.people ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/people/*", new JSONStringPointerFilter( "/person/name", "John Doe" ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "person" ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "person" ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "person" ).get( "ssn" ).textValue() ); }
@Test public void testCopyIntoArrayPredicate() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.people ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/people/*", new JSONStringPointerFilter( "/person/name", "John Doe" ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "person" ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "person" ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "person" ).get( "ssn" ).textValue() ); }
@Test public void testCopyIntoArrayPredicateNegate() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.people ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/people/*", new JSONStringPointerFilter( "/person/name", "John Doe" ).negate() ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "Jane Doe", ( (ObjectNode) value ).get( "person" ).get( "name" ).textValue() ); assertEquals( 49, ( (ObjectNode) value ).get( "person" ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "person" ).get( "ssn" ).textValue() ); }
@Test public void testCopyIntoArrayPredicateNegate() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.people ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/people/*", new JSONStringPointerFilter( "/person/name", "John Doe" ).negate() ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "Jane Doe", ( (ObjectNode) value ).get( "person" ).get( "name" ).textValue() ); assertEquals( 49, ( (ObjectNode) value ).get( "person" ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "person" ).get( "ssn" ).textValue() ); }
@Test public void testCopyExcludeDescent() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .exclude( "/**/value" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertNotNull( ( (ObjectNode) value ).get( "person" ) ); value = ( (ObjectNode) value ).get( "person" ); assertNull( ( (ObjectNode) value ).get( "measure" ).get( "value" ) ); }
@Test public void testCopyInto() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCopyInto() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCopyIntoPredicate() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person", new JSONStringPointerFilter( "/name", "John Doe" ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCoerceZero() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromTransform( "/person/zero", "/zeroValue", JSONPrimitiveTransforms.TO_FLOAT ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( JsonNodeType.NUMBER, ( (ObjectNode) value ).get( "zeroValue" ).getNodeType() ); assertEquals( FloatNode.class, ( (ObjectNode) value ).get( "zeroValue" ).getClass() ); assertEquals( 0.0F, ( (ObjectNode) value ).get( "zeroValue" ).floatValue() ); assertEquals( "0.0", ( (ObjectNode) value ).get( "zeroValue" ).asText() ); }
@Test public void testCopyIntoPredicateBoolean() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person", new JSONBooleanPointerFilter( "/human", true ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( true, ( (ObjectNode) value ).get( "human" ).booleanValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCopyAsPredicateBoolean() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person", new JSONBooleanPointerFilter( "/human", true ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( true, ( (ObjectNode) value ).get( "human" ).booleanValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCopyExcludeDescent() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .exclude( "/**/value" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertNotNull( ( (ObjectNode) value ).get( "person" ) ); value = ( (ObjectNode) value ).get( "person" ); assertNull( ( (ObjectNode) value ).get( "measure" ).get( "value" ) ); }
@Test public void testCopyExcludeFrom() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromExclude( "/person", "/ssn", "/children" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).get( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).get( "children" ) ); }
@Test public void testCopyIncludeFrom2() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromInclude( "/person", "/firstName", "/age" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John", ( (ObjectNode) value ).get( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).get( "ssn" ) ); }
@Test public void testCopyAsPredicateBoolean() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person", new JSONBooleanPointerFilter( "/human", true ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( true, ( (ObjectNode) value ).get( "human" ).booleanValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }
@Test public void testCopyIncludeFrom2() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromInclude( "/person", "/firstName", "/age" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John", ( (ObjectNode) value ).get( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).get( "ssn" ) ); }
@Test public void testCopyExcludeFrom() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromExclude( "/person", "/ssn", "/children" ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).get( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).get( "children" ) ); }
@Test public void testCoerceZero() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .fromTransform( "/person/zero", "/zeroValue", JSONPrimitiveTransforms.TO_FLOAT ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( JsonNodeType.NUMBER, ( (ObjectNode) value ).get( "zeroValue" ).getNodeType() ); assertEquals( FloatNode.class, ( (ObjectNode) value ).get( "zeroValue" ).getClass() ); assertEquals( 0.0F, ( (ObjectNode) value ).get( "zeroValue" ).floatValue() ); assertEquals( "0.0", ( (ObjectNode) value ).get( "zeroValue" ).asText() ); }
@Test public void testCopyIntoPredicateBoolean() throws Exception { Fields fields = new Fields( "json", JSONCoercibleType.TYPE ).append( new Fields( "result", JSONCoercibleType.TYPE ) ); TupleEntry entry = new TupleEntry( fields, Tuple.size( 2 ) ); entry.setObject( 0, JSONData.nested ); entry.setObject( 1, JSONData.simple ); CopySpec copySpec = new CopySpec() .from( "/person", new JSONBooleanPointerFilter( "/human", true ) ); JSONCopyIntoFunction function = new JSONCopyIntoFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "value", ( (ObjectNode) value ).get( "existing" ).textValue() ); // confirm we put data into an existing object assertEquals( "John Doe", ( (ObjectNode) value ).get( "name" ).textValue() ); assertEquals( true, ( (ObjectNode) value ).get( "human" ).booleanValue() ); assertEquals( 50, ( (ObjectNode) value ).get( "age" ).intValue() ); assertEquals( "123-45-6789", ( (ObjectNode) value ).get( "ssn" ).textValue() ); }