ImmutableSource implements Source { @Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder(); jsonObjectBuilder.set(JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate); jsonObjectBuilder.set(JsonFields.ADDRESSES, addresses.stream() .map(JsonFactory::newValue) .collect(JsonCollectors.valuesToArray()), predicate.and(Objects::nonNull)); jsonObjectBuilder.set(JsonFields.CONSUMER_COUNT, consumerCount, predicate); if (qos != null) { jsonObjectBuilder.set(JsonFields.QOS, qos); } if (!authorizationContext.isEmpty()) { jsonObjectBuilder.set(JsonFields.AUTHORIZATION_CONTEXT, authorizationContext.stream() .map(AuthorizationSubject::getId) .map(JsonFactory::newValue) .collect(JsonCollectors.valuesToArray()), predicate); } if (enforcement != null) { jsonObjectBuilder.set(JsonFields.ENFORCEMENT, enforcement.toJson(schemaVersion, thePredicate), predicate); } if (acknowledgementRequests != null) { jsonObjectBuilder.set(JsonFields.ACKNOWLEDGEMENT_REQUESTS, acknowledgementRequests.toJson(schemaVersion, thePredicate), predicate); } if (headerMapping != null) { jsonObjectBuilder.set(JsonFields.HEADER_MAPPING, headerMapping.toJson(schemaVersion, thePredicate), predicate); } if (!payloadMapping.isEmpty()) { jsonObjectBuilder.set(JsonFields.PAYLOAD_MAPPING, payloadMapping.toJson(), predicate); } if (replyTarget != null) { jsonObjectBuilder.set(JsonFields.REPLY_TARGET, replyTarget.toJson(schemaVersion, thePredicate), predicate); } if (!(replyTargetEnabled && replyTarget == null)) { jsonObjectBuilder.set(JsonFields.REPLY_TARGET_ENABLED, replyTargetEnabled, predicate); } return jsonObjectBuilder.build(); } private  ImmutableSource(final Builder builder); @Override Set<String> getAddresses(); @Override int getConsumerCount(); @Override AuthorizationContext getAuthorizationContext(); @Override int getIndex(); @Override Optional<Integer> getQos(); @Override Optional<Enforcement> getEnforcement(); @Override Optional<FilteredAcknowledgementRequest> getAcknowledgementRequests(); @Override Optional<HeaderMapping> getHeaderMapping(); @Override PayloadMapping getPayloadMapping(); @Override Optional<ReplyTarget> getReplyTarget(); @Override boolean isReplyTargetEnabled(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); static Source fromJson(final JsonObject jsonObject, final int index); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void toJsonReturnsExpected() { final JsonObject actual = SOURCE_WITH_AUTH_CONTEXT.toJson(); assertThat(actual).isEqualTo(SOURCE_JSON_WITH_AUTH_CONTEXT); }
@Test public void mqttToJsonReturnsExpected() { final JsonObject actual = MQTT_SOURCE.toJson(); assertThat(actual).isEqualTo(MQTT_SOURCE_JSON); }