@Test public void encodeLong() { final int id = ValueUtil.randomInt(); final int typeID = 42; final HashMap<String, Serializable> routingKeys = new HashMap<>(); final HashMap<String, Serializable> attributeData = new HashMap<>(); attributeData.put( "X", 1392061102056L ); final EntityMessage message = new EntityMessage( id, typeID, 0, routingKeys, attributeData, null ); final ChangeSet cs = new ChangeSet(); cs.merge( new Change( message ) ); final String encoded = JsonEncoder.encodeChangeSet( null, null, cs ); final String value = toJsonObject( encoded ). getJsonArray( TransportConstants.CHANGES ). getJsonObject( 0 ). getJsonObject( TransportConstants.DATA ). getString( "X" ); assertNotNull( value ); assertEquals( value, "1392061102056" ); }
@Nonnull public static String encodeChangeSet( @Nullable final Integer requestId, @Nullable final String etag, @Nonnull final ChangeSet changeSet ) { final StringWriter writer = new StringWriter(); final JsonGenerator generator = FACTORY.createGenerator( writer ); final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); generator.writeStartObject(); generator.write( TransportConstants.TYPE, TransportConstants.UPDATE_TYPE ); if ( null != requestId ) { generator.write( TransportConstants.REQUEST_ID, requestId ); } if ( null != etag ) { generator.write( TransportConstants.ETAG, etag ); } final List<ChannelAction> actions = changeSet.getChannelActions().stream().filter( c -> null == c.getFilter() ).collect( Collectors.toList() ); if ( !actions.isEmpty() ) { generator.writeStartArray( TransportConstants.CHANNEL_ACTIONS ); actions.stream().map( JsonEncoder::toDescriptor ).forEach( generator::write ); generator.writeEnd(); } final List<ChannelAction> filteredActions = changeSet.getChannelActions().stream().filter( c -> null != c.getFilter() ).collect( Collectors.toList() ); if ( !filteredActions.isEmpty() ) { generator.writeStartArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ); filteredActions.forEach( a -> { generator.writeStartObject(); generator.write( TransportConstants.CHANNEL, toDescriptor( a ) ); generator.write( TransportConstants.CHANNEL_FILTER, a.getFilter() ); generator.writeEnd(); } ); generator.writeEnd(); } final Collection<Change> changes = changeSet.getChanges(); if ( 0 != changes.size() ) { generator.writeStartArray( TransportConstants.CHANGES ); for ( final Change change : changes ) { final EntityMessage entityMessage = change.getEntityMessage(); generator.writeStartObject(); generator.write( TransportConstants.ENTITY_ID, entityMessage.getTypeId() + "." + entityMessage.getId() ); final Map<Integer, Integer> channels = change.getChannels(); if ( channels.size() > 0 ) { generator.writeStartArray( TransportConstants.CHANNELS ); for ( final Entry<Integer, Integer> entry : channels.entrySet() ) { final Integer cid = entry.getKey(); final Integer scid = entry.getValue(); generator.write( cid + ( null == scid ? "" : "." + scid ) ); } generator.writeEnd(); } if ( entityMessage.isUpdate() ) { generator.writeStartObject( TransportConstants.DATA ); final Map<String, Serializable> values = entityMessage.getAttributeValues(); assert null != values; for ( final Entry<String, Serializable> entry : values.entrySet() ) { writeField( generator, entry.getKey(), entry.getValue(), dateFormat ); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); generator.close(); return writer.toString(); }
JsonEncoder { @Nonnull public static String encodeChangeSet( @Nullable final Integer requestId, @Nullable final String etag, @Nonnull final ChangeSet changeSet ) { final StringWriter writer = new StringWriter(); final JsonGenerator generator = FACTORY.createGenerator( writer ); final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); generator.writeStartObject(); generator.write( TransportConstants.TYPE, TransportConstants.UPDATE_TYPE ); if ( null != requestId ) { generator.write( TransportConstants.REQUEST_ID, requestId ); } if ( null != etag ) { generator.write( TransportConstants.ETAG, etag ); } final List<ChannelAction> actions = changeSet.getChannelActions().stream().filter( c -> null == c.getFilter() ).collect( Collectors.toList() ); if ( !actions.isEmpty() ) { generator.writeStartArray( TransportConstants.CHANNEL_ACTIONS ); actions.stream().map( JsonEncoder::toDescriptor ).forEach( generator::write ); generator.writeEnd(); } final List<ChannelAction> filteredActions = changeSet.getChannelActions().stream().filter( c -> null != c.getFilter() ).collect( Collectors.toList() ); if ( !filteredActions.isEmpty() ) { generator.writeStartArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ); filteredActions.forEach( a -> { generator.writeStartObject(); generator.write( TransportConstants.CHANNEL, toDescriptor( a ) ); generator.write( TransportConstants.CHANNEL_FILTER, a.getFilter() ); generator.writeEnd(); } ); generator.writeEnd(); } final Collection<Change> changes = changeSet.getChanges(); if ( 0 != changes.size() ) { generator.writeStartArray( TransportConstants.CHANGES ); for ( final Change change : changes ) { final EntityMessage entityMessage = change.getEntityMessage(); generator.writeStartObject(); generator.write( TransportConstants.ENTITY_ID, entityMessage.getTypeId() + "." + entityMessage.getId() ); final Map<Integer, Integer> channels = change.getChannels(); if ( channels.size() > 0 ) { generator.writeStartArray( TransportConstants.CHANNELS ); for ( final Entry<Integer, Integer> entry : channels.entrySet() ) { final Integer cid = entry.getKey(); final Integer scid = entry.getValue(); generator.write( cid + ( null == scid ? "" : "." + scid ) ); } generator.writeEnd(); } if ( entityMessage.isUpdate() ) { generator.writeStartObject( TransportConstants.DATA ); final Map<String, Serializable> values = entityMessage.getAttributeValues(); assert null != values; for ( final Entry<String, Serializable> entry : values.entrySet() ) { writeField( generator, entry.getKey(), entry.getValue(), dateFormat ); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); generator.close(); return writer.toString(); } }
JsonEncoder { @Nonnull public static String encodeChangeSet( @Nullable final Integer requestId, @Nullable final String etag, @Nonnull final ChangeSet changeSet ) { final StringWriter writer = new StringWriter(); final JsonGenerator generator = FACTORY.createGenerator( writer ); final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); generator.writeStartObject(); generator.write( TransportConstants.TYPE, TransportConstants.UPDATE_TYPE ); if ( null != requestId ) { generator.write( TransportConstants.REQUEST_ID, requestId ); } if ( null != etag ) { generator.write( TransportConstants.ETAG, etag ); } final List<ChannelAction> actions = changeSet.getChannelActions().stream().filter( c -> null == c.getFilter() ).collect( Collectors.toList() ); if ( !actions.isEmpty() ) { generator.writeStartArray( TransportConstants.CHANNEL_ACTIONS ); actions.stream().map( JsonEncoder::toDescriptor ).forEach( generator::write ); generator.writeEnd(); } final List<ChannelAction> filteredActions = changeSet.getChannelActions().stream().filter( c -> null != c.getFilter() ).collect( Collectors.toList() ); if ( !filteredActions.isEmpty() ) { generator.writeStartArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ); filteredActions.forEach( a -> { generator.writeStartObject(); generator.write( TransportConstants.CHANNEL, toDescriptor( a ) ); generator.write( TransportConstants.CHANNEL_FILTER, a.getFilter() ); generator.writeEnd(); } ); generator.writeEnd(); } final Collection<Change> changes = changeSet.getChanges(); if ( 0 != changes.size() ) { generator.writeStartArray( TransportConstants.CHANGES ); for ( final Change change : changes ) { final EntityMessage entityMessage = change.getEntityMessage(); generator.writeStartObject(); generator.write( TransportConstants.ENTITY_ID, entityMessage.getTypeId() + "." + entityMessage.getId() ); final Map<Integer, Integer> channels = change.getChannels(); if ( channels.size() > 0 ) { generator.writeStartArray( TransportConstants.CHANNELS ); for ( final Entry<Integer, Integer> entry : channels.entrySet() ) { final Integer cid = entry.getKey(); final Integer scid = entry.getValue(); generator.write( cid + ( null == scid ? "" : "." + scid ) ); } generator.writeEnd(); } if ( entityMessage.isUpdate() ) { generator.writeStartObject( TransportConstants.DATA ); final Map<String, Serializable> values = entityMessage.getAttributeValues(); assert null != values; for ( final Entry<String, Serializable> entry : values.entrySet() ) { writeField( generator, entry.getKey(), entry.getValue(), dateFormat ); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); generator.close(); return writer.toString(); } private  JsonEncoder(); }
JsonEncoder { @Nonnull public static String encodeChangeSet( @Nullable final Integer requestId, @Nullable final String etag, @Nonnull final ChangeSet changeSet ) { final StringWriter writer = new StringWriter(); final JsonGenerator generator = FACTORY.createGenerator( writer ); final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); generator.writeStartObject(); generator.write( TransportConstants.TYPE, TransportConstants.UPDATE_TYPE ); if ( null != requestId ) { generator.write( TransportConstants.REQUEST_ID, requestId ); } if ( null != etag ) { generator.write( TransportConstants.ETAG, etag ); } final List<ChannelAction> actions = changeSet.getChannelActions().stream().filter( c -> null == c.getFilter() ).collect( Collectors.toList() ); if ( !actions.isEmpty() ) { generator.writeStartArray( TransportConstants.CHANNEL_ACTIONS ); actions.stream().map( JsonEncoder::toDescriptor ).forEach( generator::write ); generator.writeEnd(); } final List<ChannelAction> filteredActions = changeSet.getChannelActions().stream().filter( c -> null != c.getFilter() ).collect( Collectors.toList() ); if ( !filteredActions.isEmpty() ) { generator.writeStartArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ); filteredActions.forEach( a -> { generator.writeStartObject(); generator.write( TransportConstants.CHANNEL, toDescriptor( a ) ); generator.write( TransportConstants.CHANNEL_FILTER, a.getFilter() ); generator.writeEnd(); } ); generator.writeEnd(); } final Collection<Change> changes = changeSet.getChanges(); if ( 0 != changes.size() ) { generator.writeStartArray( TransportConstants.CHANGES ); for ( final Change change : changes ) { final EntityMessage entityMessage = change.getEntityMessage(); generator.writeStartObject(); generator.write( TransportConstants.ENTITY_ID, entityMessage.getTypeId() + "." + entityMessage.getId() ); final Map<Integer, Integer> channels = change.getChannels(); if ( channels.size() > 0 ) { generator.writeStartArray( TransportConstants.CHANNELS ); for ( final Entry<Integer, Integer> entry : channels.entrySet() ) { final Integer cid = entry.getKey(); final Integer scid = entry.getValue(); generator.write( cid + ( null == scid ? "" : "." + scid ) ); } generator.writeEnd(); } if ( entityMessage.isUpdate() ) { generator.writeStartObject( TransportConstants.DATA ); final Map<String, Serializable> values = entityMessage.getAttributeValues(); assert null != values; for ( final Entry<String, Serializable> entry : values.entrySet() ) { writeField( generator, entry.getKey(), entry.getValue(), dateFormat ); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); generator.close(); return writer.toString(); } private  JsonEncoder(); @Nonnull static String encodeChangeSet( @Nullable final Integer requestId,
                                        @Nullable final String etag,
                                        @Nonnull final ChangeSet changeSet ); @Nonnull static String asString( @Nonnull final JsonObject message ); }
JsonEncoder { @Nonnull public static String encodeChangeSet( @Nullable final Integer requestId, @Nullable final String etag, @Nonnull final ChangeSet changeSet ) { final StringWriter writer = new StringWriter(); final JsonGenerator generator = FACTORY.createGenerator( writer ); final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ); generator.writeStartObject(); generator.write( TransportConstants.TYPE, TransportConstants.UPDATE_TYPE ); if ( null != requestId ) { generator.write( TransportConstants.REQUEST_ID, requestId ); } if ( null != etag ) { generator.write( TransportConstants.ETAG, etag ); } final List<ChannelAction> actions = changeSet.getChannelActions().stream().filter( c -> null == c.getFilter() ).collect( Collectors.toList() ); if ( !actions.isEmpty() ) { generator.writeStartArray( TransportConstants.CHANNEL_ACTIONS ); actions.stream().map( JsonEncoder::toDescriptor ).forEach( generator::write ); generator.writeEnd(); } final List<ChannelAction> filteredActions = changeSet.getChannelActions().stream().filter( c -> null != c.getFilter() ).collect( Collectors.toList() ); if ( !filteredActions.isEmpty() ) { generator.writeStartArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ); filteredActions.forEach( a -> { generator.writeStartObject(); generator.write( TransportConstants.CHANNEL, toDescriptor( a ) ); generator.write( TransportConstants.CHANNEL_FILTER, a.getFilter() ); generator.writeEnd(); } ); generator.writeEnd(); } final Collection<Change> changes = changeSet.getChanges(); if ( 0 != changes.size() ) { generator.writeStartArray( TransportConstants.CHANGES ); for ( final Change change : changes ) { final EntityMessage entityMessage = change.getEntityMessage(); generator.writeStartObject(); generator.write( TransportConstants.ENTITY_ID, entityMessage.getTypeId() + "." + entityMessage.getId() ); final Map<Integer, Integer> channels = change.getChannels(); if ( channels.size() > 0 ) { generator.writeStartArray( TransportConstants.CHANNELS ); for ( final Entry<Integer, Integer> entry : channels.entrySet() ) { final Integer cid = entry.getKey(); final Integer scid = entry.getValue(); generator.write( cid + ( null == scid ? "" : "." + scid ) ); } generator.writeEnd(); } if ( entityMessage.isUpdate() ) { generator.writeStartObject( TransportConstants.DATA ); final Map<String, Serializable> values = entityMessage.getAttributeValues(); assert null != values; for ( final Entry<String, Serializable> entry : values.entrySet() ) { writeField( generator, entry.getKey(), entry.getValue(), dateFormat ); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); } generator.writeEnd(); generator.close(); return writer.toString(); } private  JsonEncoder(); @Nonnull static String encodeChangeSet( @Nullable final Integer requestId,
                                        @Nullable final String etag,
                                        @Nonnull final ChangeSet changeSet ); @Nonnull static String asString( @Nonnull final JsonObject message );  }