ImmutableConnection implements Connection { public static Connection fromJson(final JsonObject jsonObject) { final ConnectionType type = getConnectionTypeOrThrow(jsonObject); final MappingContext mappingContext = jsonObject.getValue(JsonFields.MAPPING_CONTEXT) .map(ConnectivityModelFactory::mappingContextFromJson) .orElse(null); final PayloadMappingDefinition payloadMappingDefinition = jsonObject.getValue(JsonFields.MAPPING_DEFINITIONS) .map(ImmutablePayloadMappingDefinition::fromJson) .orElse(ConnectivityModelFactory.emptyPayloadMappingDefinition()); final ConnectionBuilder builder = new Builder(type) .id(ConnectionId.of(jsonObject.getValueOrThrow(JsonFields.ID))) .connectionStatus(getConnectionStatusOrThrow(jsonObject)) .uri(jsonObject.getValueOrThrow(JsonFields.URI)) .sources(getSources(jsonObject)) .targets(getTargets(jsonObject)) .name(jsonObject.getValue(JsonFields.NAME).orElse(null)) .mappingContext(mappingContext) .payloadMappingDefinition(payloadMappingDefinition) .specificConfig(getSpecificConfiguration(jsonObject)) .tags(getTags(jsonObject)); jsonObject.getValue(Connection.JsonFields.LIFECYCLE) .flatMap(ConnectionLifecycle::forName).ifPresent(builder::lifecycle); jsonObject.getValue(JsonFields.CREDENTIALS).ifPresent(builder::credentialsFromJson); jsonObject.getValue(JsonFields.CLIENT_COUNT).ifPresent(builder::clientCount); jsonObject.getValue(JsonFields.FAILOVER_ENABLED).ifPresent(builder::failoverEnabled); jsonObject.getValue(JsonFields.VALIDATE_CERTIFICATES).ifPresent(builder::validateCertificate); jsonObject.getValue(JsonFields.PROCESSOR_POOL_SIZE).ifPresent(builder::processorPoolSize); jsonObject.getValue(JsonFields.TRUSTED_CERTIFICATES).ifPresent(builder::trustedCertificates); return builder.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 fromJsonReturnsExpected() { final Connection expected = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .credentials(CREDENTIALS) .name(NAME) .setSources(addSourceMapping(SOURCES, JAVA_SCRIPT_MAPPING)) .setTargets(addTargetMapping(TARGETS, STATUS_MAPPING)) .clientCount(2) .payloadMappingDefinition(KNOWN_MAPPING_DEFINITIONS) .tags(KNOWN_TAGS) .build(); final Connection actual = ImmutableConnection.fromJson(KNOWN_JSON); assertThat(actual).isEqualTo(expected); }
@Test public void fromJsonWithLegacyMappingContextReturnsExpected() { final Map<String, MappingContext> definitions = new HashMap<>(KNOWN_MAPPING_DEFINITIONS.getDefinitions()); definitions.putAll(LEGACY_MAPPINGS.getDefinitions()); final Connection expected = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .credentials(CREDENTIALS) .name(NAME) .setSources(addSourceMapping(SOURCES, JAVA_SCRIPT_MAPPING, "javascript")) .setTargets(addTargetMapping(TARGETS, STATUS_MAPPING, "javascript")) .clientCount(2) .payloadMappingDefinition(ConnectivityModelFactory.newPayloadMappingDefinition(definitions)) .tags(KNOWN_TAGS) .build(); final Connection actual = ImmutableConnection.fromJson(KNOWN_LEGACY_JSON); assertThat(actual).isEqualTo(expected); }
@Test public void fromInvalidJsonFails() { final JsonObject INVALID_JSON = KNOWN_JSON.remove(Connection.JsonFields.SOURCES.getPointer()) .remove(Connection.JsonFields.TARGETS.getPointer()); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> ImmutableConnection.fromJson(INVALID_JSON)) .withMessageContaining("source") .withMessageContaining("target") .withNoCause(); }