@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); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param id the connection ID. * @param connectionType the connection type. * @param connectionStatus the connection status. * @param uri the URI. * @return new instance of {@code ConnectionBuilder}. * @throws NullPointerException if any argument is {@code null}. */ public static ConnectionBuilder getBuilder(final String id, final ConnectionType connectionType, final ConnectionStatus connectionStatus, final String uri) { return new Builder(connectionType) .id(id) .connectionStatus(connectionStatus) .uri(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); }
/** * 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(); }
private void openConnection(final OpenConnection command) { checkConnectionNotNull(); final ConnectionOpened connectionOpened = ConnectionOpened.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionOpened, persistedEvent -> { restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.OPEN).build()); askClientActor(command, response -> { final ConnectivityCommandResponse commandResponse = OpenConnectionResponse.of(connectionId, command.getDittoHeaders()); final PerformTask performTask = new PerformTask("open connection", subscribeForEventsAndScheduleResponse(commandResponse, origin)); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("open-connection", origin, error) ); }); }
@Test public void createMinimalConnectionConfigurationInstance() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .sources(SOURCES) .targets(TARGETS) .build(); assertThat(connection.getId()).isEqualTo(ID); assertThat((Object) connection.getConnectionType()).isEqualTo(TYPE); assertThat(connection.getUri()).isEqualTo(URI); assertThat(connection.getSources()).isEqualTo(SOURCES); }
@Test public void toStringDoesNotContainPassword() { final String password = "thePassword"; final String uri = "amqps://foo:" + password + "@host.com:5671"; final Connection connection = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, uri) .sources(Collections.singletonList(SOURCE1)) .build(); assertThat(connection.toString()).doesNotContain(password); }
/** * Sets the connection credentials in JSON representation. * * @param jsonObject credentials in JSON representation. * @return this builder. */ default ConnectionBuilder credentialsFromJson(final JsonObject jsonObject) { return credentials(Credentials.fromJson(jsonObject)); }
@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 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(); }
/** * 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(); }
@Test public void toJsonReturnsExpected() { final Connection underTest = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .credentials(CREDENTIALS) .name(NAME) .sources(Arrays.asList(SOURCE2, SOURCE1)) // use different order to test sorting .targets(TARGETS) .clientCount(2) .mappingContext(KNOWN_MAPPING_CONTEXT) .tags(KNOWN_TAGS) .build(); final JsonObject actual = underTest.toJson(); assertThat(actual).isEqualTo(KNOWN_JSON); }
private void openConnection(final OpenConnection command) { checkConnectionNotNull(); final ConnectionOpened connectionOpened = ConnectionOpened.of(command.getConnectionId(), command.getDittoHeaders()); final ActorRef origin = getSender(); final ActorRef self = getSelf(); persistEvent(connectionOpened, persistedEvent -> { restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.OPEN).build()); askClientActor(command, response -> { final ConnectivityCommandResponse commandResponse = OpenConnectionResponse.of(connectionId, command.getDittoHeaders()); final PerformTask performTask = new PerformTask("open connection", subscribeForEventsAndScheduleResponse(commandResponse, origin)); self.tell(performTask, ActorRef.noSender()); }, error -> handleException("open-connection", origin, error) ); }); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param id the connection ID. * @param connectionType the connection type. * @param connectionStatus the connection status. * @param uri the URI. * @return new instance of {@code ConnectionBuilder}. * @throws NullPointerException if any argument is {@code null}. */ public static ConnectionBuilder getBuilder(final String id, final ConnectionType connectionType, final ConnectionStatus connectionStatus, final String uri) { return new Builder(connectionType) .id(id) .connectionStatus(connectionStatus) .uri(uri); }
/** * Sets the connection credentials in JSON representation. * * @param jsonObject credentials in JSON representation. * @return this builder. */ default ConnectionBuilder credentialsFromJson(final JsonObject jsonObject) { return credentials(Credentials.fromJson(jsonObject)); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }
restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.CLOSED).build());
restoreConnection(connection.toBuilder().connectionStatus(ConnectionStatus.CLOSED).build());
.match(ConnectionModified.class, event -> restoreConnection(event.getConnection())) .match(ConnectionOpened.class, event -> restoreConnection(connection != null ? connection.toBuilder() .connectionStatus(ConnectionStatus.OPEN).build() : null)) .match(ConnectionClosed.class, event -> restoreConnection(connection != null ? connection.toBuilder() .connectionStatus(ConnectionStatus.CLOSED).build() : null)) .match(ConnectionDeleted.class, event -> restoreConnection(null)) .match(RecoveryCompleted.class, rc -> {