ImmutableConnection implements Connection { @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); if (null != lifecycle) { jsonObjectBuilder.set(Connection.JsonFields.LIFECYCLE, lifecycle.name(), predicate); } jsonObjectBuilder.set(JsonFields.ID, String.valueOf(id), predicate); jsonObjectBuilder.set(JsonFields.NAME, name, predicate); jsonObjectBuilder.set(JsonFields.CONNECTION_TYPE, connectionType.getName(), predicate); jsonObjectBuilder.set(JsonFields.CONNECTION_STATUS, connectionStatus.getName(), predicate); jsonObjectBuilder.set(JsonFields.URI, uri.toString(), predicate); jsonObjectBuilder.set(JsonFields.SOURCES, sources.stream() .sorted(Comparator.comparingInt(Source::getIndex)) .map(source -> source.toJson(schemaVersion, thePredicate)) .collect(JsonCollectors.valuesToArray()), predicate.and(Objects::nonNull)); jsonObjectBuilder.set(JsonFields.TARGETS, targets.stream() .map(target -> target.toJson(schemaVersion, thePredicate)) .collect(JsonCollectors.valuesToArray()), predicate.and(Objects::nonNull)); jsonObjectBuilder.set(JsonFields.CLIENT_COUNT, clientCount, predicate); jsonObjectBuilder.set(JsonFields.FAILOVER_ENABLED, failOverEnabled, predicate); jsonObjectBuilder.set(JsonFields.VALIDATE_CERTIFICATES, validateCertificate, predicate); jsonObjectBuilder.set(JsonFields.PROCESSOR_POOL_SIZE, processorPoolSize, predicate); if (!specificConfig.isEmpty()) { jsonObjectBuilder.set(JsonFields.SPECIFIC_CONFIG, specificConfig.entrySet() .stream() .map(entry -> JsonField.newInstance(entry.getKey(), JsonValue.of(entry.getValue()))) .collect(JsonCollectors.fieldsToObject()), predicate); } if (!payloadMappingDefinition.isEmpty()) { jsonObjectBuilder.set(JsonFields.MAPPING_DEFINITIONS, payloadMappingDefinition.toJson(schemaVersion, thePredicate)); } if (credentials != null) { jsonObjectBuilder.set(JsonFields.CREDENTIALS, credentials.toJson()); } if (trustedCertificates != null) { jsonObjectBuilder.set(JsonFields.TRUSTED_CERTIFICATES, trustedCertificates, predicate); } jsonObjectBuilder.set(JsonFields.TAGS, tags.stream() .map(JsonFactory::newValue) .collect(JsonCollectors.valuesToArray()), predicate); return jsonObjectBuilder.build(); } private  ImmutableConnection(final Builder builder); static ConnectionBuilder getBuilder(final ConnectionId id,
            final ConnectionType connectionType,
            final ConnectivityStatus connectionStatus,
            final String uri); static ConnectionBuilder getBuilder(final Connection connection); static Connection fromJson(final JsonObject jsonObject); @Override ConnectionId getId(); @Override Optional<String> getName(); @Override ConnectionType getConnectionType(); @Override ConnectivityStatus getConnectionStatus(); @Override List<Source> getSources(); @Override List<Target> getTargets(); @Override int getClientCount(); @Override boolean isFailoverEnabled(); @Override Optional<Credentials> getCredentials(); @Override Optional<String> getTrustedCertificates(); @Override String getUri(); @Override String getProtocol(); @Override Optional<String> getUsername(); @Override Optional<String> getPassword(); @Override String getHostname(); @Override int getPort(); @Override Optional<String> getPath(); @Override boolean isValidateCertificates(); @Override int getProcessorPoolSize(); @Override Map<String, String> getSpecificConfig(); @Override PayloadMappingDefinition getPayloadMappingDefinition(); @Override Set<String> getTags(); @Override Optional<ConnectionLifecycle> getLifecycle(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); @SuppressWarnings("OverlyComplexMethod") @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void toJsonReturnsExpected() { final Connection underTest = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .credentials(CREDENTIALS) .name(NAME) .sources(addSourceMapping(Arrays.asList(SOURCE2, SOURCE1), JAVA_SCRIPT_MAPPING)) .targets(addTargetMapping(TARGETS, STATUS_MAPPING)) .clientCount(2) .payloadMappingDefinition(KNOWN_MAPPING_DEFINITIONS) .tags(KNOWN_TAGS) .build(); final JsonObject actual = underTest.toJson(); assertThat(actual).isEqualTo(KNOWN_JSON_WITH_REPLY_TARGET); }