@Test public void encodeAllData() { final int id = 17; final int typeID = 42; final Calendar calendar = Calendar.getInstance(); calendar.set( Calendar.YEAR, 2001 ); calendar.set( Calendar.MONTH, Calendar.JULY ); calendar.set( Calendar.DAY_OF_MONTH, 5 ); calendar.set( Calendar.AM_PM, Calendar.AM ); calendar.set( Calendar.HOUR_OF_DAY, 5 ); calendar.set( Calendar.MINUTE, 8 ); calendar.set( Calendar.SECOND, 56 ); calendar.set( Calendar.MILLISECOND, 0 ); final Date date = calendar.getTime(); final EntityMessage message = MessageTestUtil.createMessage( id, typeID, 0, "r1", "r2", "a1", "a2" ); final Map<String, Serializable> values = message.getAttributeValues(); assertNotNull( values ); values.put( "key3", date ); final int requestId = 1; final String etag = "#1"; final JsonObject filter = Json.createBuilderFactory( null ).createObjectBuilder().add( "a", "b" ).build(); final Change change = new Change( message ); change.getChannels().put( 1, null ); change.getChannels().put( 2, 42 ); change.getChannels().put( 3, 73 ); final ChangeSet cs = new ChangeSet(); cs.merge( change ); cs.mergeAction( new ChannelAction( new ChannelAddress( 45, 77 ), Action.UPDATE, filter ) ); final String encoded = JsonEncoder.encodeChangeSet( requestId, etag, cs ); final JsonObject changeSet = toJsonObject( encoded ); assertNotNull( changeSet ); assertEquals( changeSet.getInt( TransportConstants.REQUEST_ID ), requestId ); assertEquals( changeSet.getString( TransportConstants.ETAG ), etag ); final JsonObject action = changeSet.getJsonArray( TransportConstants.FILTERED_CHANNEL_ACTIONS ).getJsonObject( 0 ); assertEquals( action.getString( TransportConstants.CHANNEL ), "=45.77" ); assertEquals( action.getJsonObject( TransportConstants.CHANNEL_FILTER ).toString(), filter.toString() ); final JsonObject object = changeSet.getJsonArray( TransportConstants.CHANGES ).getJsonObject( 0 ); assertEquals( object.getString( TransportConstants.ENTITY_ID ), "42.17" ); final JsonObject data = object.getJsonObject( TransportConstants.DATA ); assertNotNull( data ); assertEquals( data.getString( MessageTestUtil.ATTR_KEY1 ), "a1" ); assertEquals( data.getString( MessageTestUtil.ATTR_KEY2 ), "a2" ); assertTrue( data.getString( "key3" ).startsWith( "2001-07-05T05:08:56.000" ) ); final JsonArray channels = object.getJsonArray( TransportConstants.CHANNELS ); assertNotNull( channels ); assertEquals( channels.size(), 3 ); final String channel1 = channels.getString( 0 ); assertNotNull( channel1 ); final String channel2 = channels.getString( 1 ); assertNotNull( channel2 ); final String channel3 = channels.getString( 2 ); assertNotNull( channel3 ); assertEquals( channel1, "1" ); assertEquals( channel2, "2.42" ); assertEquals( channel3, "3.73" ); }
@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 );  }