public class MoneyDeserializer extends JsonDeserializer<Money> { @Override public Money deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String currencyCode = "GBP"; ObjectMapper mapper = (ObjectMapper) jp.getCodec(); ObjectNode root = mapper.readTree(jp); DoubleNode amountNode = (DoubleNode) root.findValue("amount"); String amount = null; if (null != amountNode) { amount = amountNode.asText(); } JsonNode currencyUnitNode = root.get("currencyUnit"); JsonNode currencyCodeNode = currencyUnitNode.get("currencyCode"); currencyCode = currencyCodeNode.textValue(); if (StringUtils.isBlank(amount) || StringUtils.isBlank(currencyCode)) { throw new IOException("unable to parse json"); } return Money.parse(currencyCode + " " + amount); }}
@Override public AlienEvent deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = (ObjectMapper) jp.getCodec(); ObjectNode root = mapper.readTree(jp); Class<? extends AlienEvent> eventClass = null; String type = EventType.INSTANCE_STATE; if (root.hasNonNull(TYPE_FIELD)) { type = root.findValue(TYPE_FIELD).asText(); } eventClass = registry.get(type); if (eventClass == null) { return null; } return mapper.readValue(jp, eventClass); } }
public static OdeBsmData createOdeBsmData(String consumedData) throws XmlUtilsException { ObjectNode consumed = XmlUtils.toObjectNode(consumedData); JsonNode metadataNode = consumed.findValue(AppContext.METADATA_STRING); if (metadataNode instanceof ObjectNode) { ObjectNode object = (ObjectNode) metadataNode; object.remove(AppContext.ENCODINGS_STRING); } OdeBsmMetadata metadata = (OdeBsmMetadata) JsonUtils.fromJson( metadataNode.toString(), OdeBsmMetadata.class); /* * ODE-755 and ODE-765 Starting with schemaVersion=5 receivedMessageDetails * will be present in BSM metadata. None should be present in prior versions. */ if (metadata.getSchemaVersion() <= 4) { metadata.setReceivedMessageDetails(null); } OdeBsmPayload payload = new OdeBsmPayload( BsmBuilder.genericBsm(consumed.findValue("BasicSafetyMessage"))); return new OdeBsmData(metadata, payload ); } }
@Test public void processShouldWorkWhenKnownMethod() throws Exception { ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false)); parameters.put("id", "testId"); parameters.put("name", "testName"); JsonNode[] nodes = new JsonNode[] { new ObjectNode(new JsonNodeFactory(false)).textNode("getTestMethod"), parameters, new ObjectNode(new JsonNodeFactory(false)).textNode("#1")}; List<ProtocolResponse> responses = testee.handle(AuthenticatedProtocolRequest.decorate(ProtocolRequest.deserialize(nodes), mockHttpServletRequest)) .collect(Collectors.toList()); assertThat(responses).hasSize(1) .extracting( x -> x.getResults().findValue("id").asText(), x -> x.getResults().findValue("name").asText(), x -> x.getResults().findValue("message").asText()) .containsExactly(tuple("testId", "testName", "works")); } }
@Test public void testCopyExclude() 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( "/person/ssn", "/person/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 ).findPath( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).findValue( "children" ) ); }
@Test public void testCopyExclude() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .exclude( "/person/ssn", "/person/children" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (ObjectNode) value ).findPath( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).findValue( "children" ) ); }
@Test public void testCopyExclude() 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( "/person/ssn", "/person/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 ).findPath( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).findValue( "children" ) ); }
@Test public void testCopyExclude() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .exclude( "/person/ssn", "/person/children" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (ObjectNode) value ).findPath( "name" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); assertEquals( null, ( (ObjectNode) value ).findValue( "children" ) ); }
@Test public void testCopyIncludeDescent() 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() .include( "/person/firstName" ) .include( "/**/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeWild() 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() .include( "/person/firstName" ) .include( "/*/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyInclude() 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() .include( "/person/firstName" ) .include( "/person/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyInclude() 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() .include( "/person/firstName" ) .include( "/person/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeWild() 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() .include( "/person/firstName" ) .include( "/*/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeDescent() 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() .include( "/person/firstName" ) .include( "/**/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 ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeWild() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/*/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeWild() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/*/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyInclude() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/person/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeDescent() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/**/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyInclude() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/person/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }
@Test public void testCopyIncludeDescent() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); CopySpec copySpec = new CopySpec() .include( "/person/firstName" ) .include( "/**/age" ); JSONCopyAsFunction function = new JSONCopyAsFunction( new Fields( "result" ), copySpec ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John", ( (ObjectNode) value ).findPath( "firstName" ).textValue() ); assertEquals( 50, ( (ObjectNode) value ).findPath( "age" ).intValue() ); assertEquals( null, ( (ObjectNode) value ).findValue( "ssn" ) ); }