/** * Creates a new {@code Connection} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Connection to be created. * @return a new Connection which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static Connection connectionFromJson(final JsonObject jsonObject) { return ImmutableConnection.fromJson(jsonObject); }
/** * Returns a mutable builder with a fluent API for an immutable {@link Connection}. The builder is initialised with * the values of the given Connection. * * @param connection the Connection which provides the initial values of the builder. * @return the new builder. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder newConnectionBuilder(final Connection connection) { return ImmutableConnection.getBuilder(connection); }
/** * Creates a new {@code Connection} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Connection to be created. * @return a new Connection which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static Connection fromJson(final JsonObject jsonObject) { final ConnectionType type = getConnectionTypeOrThrow(jsonObject); final ConnectionBuilder builder = new Builder(type) .id(jsonObject.getValueOrThrow(JsonFields.ID)) .connectionStatus(getConnectionStatusOrThrow(jsonObject)) .uri(jsonObject.getValueOrThrow(JsonFields.URI)) .sources(getSources(jsonObject, type)) .targets(getTargets(jsonObject, type)) .name(jsonObject.getValue(JsonFields.NAME).orElse(null)) .mappingContext(jsonObject.getValue(JsonFields.MAPPING_CONTEXT) .map(ConnectivityModelFactory::mappingContextFromJson) .orElse(null)) .specificConfig(getSpecificConfiguration(jsonObject)) .tags(getTags(jsonObject)); 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(); }
@Override public Connection build() { checkSourceAndTargetAreValid(); checkAuthorizationContextsAreValid(); return new ImmutableConnection(this); }
/** * Creates a new {@code Connection} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Connection to be created. * @return a new Connection which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static Connection fromJson(final JsonObject jsonObject) { final ConnectionType type = getConnectionTypeOrThrow(jsonObject); final ConnectionBuilder builder = new Builder(type) .id(jsonObject.getValueOrThrow(JsonFields.ID)) .connectionStatus(getConnectionStatusOrThrow(jsonObject)) .uri(jsonObject.getValueOrThrow(JsonFields.URI)) .sources(getSources(jsonObject, type)) .targets(getTargets(jsonObject, type)) .name(jsonObject.getValue(JsonFields.NAME).orElse(null)) .mappingContext(jsonObject.getValue(JsonFields.MAPPING_CONTEXT) .map(ConnectivityModelFactory::mappingContextFromJson) .orElse(null)) .specificConfig(getSpecificConfiguration(jsonObject)) .tags(getTags(jsonObject)); 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(); }
@Override public Connection build() { checkSourceAndTargetAreValid(); checkAuthorizationContextsAreValid(); return new ImmutableConnection(this); }
/** * Returns a mutable builder with a fluent API for an immutable {@link Connection}. The builder is initialised with * the values of the given Connection. * * @param connection the Connection which provides the initial values of the builder. * @return the new builder. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder newConnectionBuilder(final Connection connection) { return ImmutableConnection.getBuilder(connection); }
/** * Creates a new {@code Connection} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Connection to be created. * @return a new Connection which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static Connection connectionFromJson(final JsonObject jsonObject) { return ImmutableConnection.fromJson(jsonObject); }
/** * Returns a new {@code ConnectionBuilder} with the required fields set. * * @param id the connection identifier. * @param connectionType the connection type. * @param connectionStatus the connection status. * @param uri the connection URI. * @return the ConnectionBuilder. * @throws NullPointerException if any argument is {@code null}. */ public static ConnectionBuilder newConnectionBuilder(final String id, final ConnectionType connectionType, final ConnectionStatus connectionStatus, final String uri) { return ImmutableConnection.getBuilder(id, connectionType, connectionStatus, uri); }
@Test public void fromJsonReturnsExpected() { final Connection expected = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .credentials(CREDENTIALS) .name(NAME) .sources(SOURCES) .targets(TARGETS) .clientCount(2) .mappingContext(KNOWN_MAPPING_CONTEXT) .tags(KNOWN_TAGS) .build(); final Connection actual = ImmutableConnection.fromJson(KNOWN_JSON); assertThat(actual).isEqualTo(expected); }
/** * Returns a new {@code ConnectionBuilder} with the required fields set. * * @param id the connection identifier. * @param connectionType the connection type. * @param connectionStatus the connection status. * @param uri the connection URI. * @return the ConnectionBuilder. * @throws NullPointerException if any argument is {@code null}. */ public static ConnectionBuilder newConnectionBuilder(final String id, final ConnectionType connectionType, final ConnectionStatus connectionStatus, final String uri) { return ImmutableConnection.getBuilder(id, connectionType, connectionStatus, uri); }
@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(); }
@Test public void createInstanceWithNullEventTarget() { final ConnectionBuilder builder = ImmutableConnection.getBuilder(ID, TYPE, STATUS, URI); assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> builder.targets(null)) .withMessage("The %s must not be null!", "targets") .withNoCause(); }
@Test public void createInstanceWithoutSourceAndEventTarget() { final ConnectionBuilder builder = ImmutableConnection.getBuilder(ID, TYPE, STATUS, URI); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(builder::build) .withMessageContaining("source") .withMessageContaining("target") .withNoCause(); }
@Test public void createInstanceWithNullSources() { final ConnectionBuilder builder = ImmutableConnection.getBuilder(ID, TYPE, STATUS, URI); assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> builder.sources(null)) .withMessage("The %s must not be null!", "sources") .withNoCause(); }
@Test public void getBuilderFromConnectionCoversAllFields() { final Connection connection = ImmutableConnection.getBuilder(ID, TYPE, STATUS, URI) .sources(SOURCES) .targets(TARGETS) .connectionStatus(ConnectionStatus.OPEN) .name("connection") .clientCount(5) .tag("AAA") .trustedCertificates("certs") .processorPoolSize(8) .credentials(ClientCertificateCredentials.newBuilder() .clientKey("clientkey") .clientCertificate("certificate") .build()) .validateCertificate(true) .uri("amqps://some.amqp.org:5672") .id("id") .build(); assertThat(ImmutableConnection.getBuilder(connection).build()).isEqualTo(connection); }