ConnectionValidator { void validate(final Connection connection, final DittoHeaders dittoHeaders, final ActorSystem actorSystem) { final AbstractProtocolValidator spec = specMap.get(connection.getConnectionType()); validateSourceAndTargetAddressesAreNonempty(connection, dittoHeaders); checkMappingNumberOfSourcesAndTargets(dittoHeaders, connection); validateFormatOfCertificates(connection, dittoHeaders); hostValidator.validateHostname(connection.getHostname(), dittoHeaders); if (spec != null) { spec.validate(connection, dittoHeaders, actorSystem); } else { throw new IllegalStateException("Unknown connection type: " + connection); } } private  ConnectionValidator(
            final ConnectivityConfig connectivityConfig,
            LoggingAdapter loggingAdapter, final AbstractProtocolValidator... connectionSpecs); static ConnectionValidator of(final ConnectivityConfig connectivityConfig,
            LoggingAdapter loggingAdapter, final AbstractProtocolValidator... connectionSpecs);  }
@Test public void acceptValidConnection() { final Connection connection = createConnection(CONNECTION_ID); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }
@Test public void rejectConnectionWithSourceWithoutAddresses() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, "amqp: .sources(singletonList( ConnectivityModelFactory.newSourceBuilder() .authorizationContext(Authorization.AUTHORIZATION_CONTEXT) .consumerCount(0) .index(1) .build())) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void rejectConnectionWithEmptySourceAddress() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, "amqp: .sources(singletonList( ConnectivityModelFactory.newSourceBuilder() .authorizationContext(Authorization.AUTHORIZATION_CONTEXT) .address("") .consumerCount(1) .index(0) .build())) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void rejectConnectionWithEmptyTargetAddress() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, "amqp: .targets(Collections.singletonList(ConnectivityModelFactory.newTargetBuilder() .address("") .authorizationContext(Authorization.AUTHORIZATION_CONTEXT) .topics(Topic.LIVE_MESSAGES) .build())) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void rejectConnectionWithInvalidNormalizerMapperJsonFieldSelector() { final Map<String, String> normalizerMessageMapperOptions = new HashMap<>(); normalizerMessageMapperOptions.put(NormalizedMessageMapper.FIELDS, "foo(bar"); final Connection connection = ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, "amqp: .targets(TestConstants.Targets.TARGETS) .mappingContext(ConnectivityModelFactory.newMappingContext( NormalizedMessageMapper.class.getName(), normalizerMessageMapperOptions )) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void rejectConnectionWithIllFormedTrustedCertificates() { final Connection connection = createConnection(CONNECTION_ID).toBuilder() .trustedCertificates("Wurst") .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void acceptConnectionWithTrustedCertificates() { final String trustedCertificates = String.join("\n", Certificates.CA_CRT, Certificates.SERVER_CRT, Certificates.CLIENT_CRT, Certificates.CLIENT_SELF_SIGNED_CRT); final Connection connection = createConnection(CONNECTION_ID).toBuilder() .trustedCertificates(trustedCertificates) .build(); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }
@Test public void rejectIllFormedClientCertificate() { final Connection connection = createConnection(CONNECTION_ID).toBuilder() .credentials(ClientCertificateCredentials.newBuilder() .clientKey(Certificates.CLIENT_KEY) .clientCertificate("Wurst") .build()) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void rejectIllFormedClientKey() { final Connection connection = createConnection(CONNECTION_ID).toBuilder() .credentials(ClientCertificateCredentials.newBuilder() .clientKey("-----BEGIN RSA PRIVATE KEY-----\nWurst\n-----END RSA PRIVATE KEY-----") .clientCertificate(Certificates.CLIENT_CRT) .build()) .build(); final ConnectionValidator underTest = getConnectionValidator(); assertThatExceptionOfType(ConnectionConfigurationInvalidException.class) .isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem)); }
@Test public void acceptClientCertificate() { final Connection connection = createConnection(CONNECTION_ID).toBuilder() .credentials(ClientCertificateCredentials.newBuilder() .clientKey(Certificates.CLIENT_KEY) .clientCertificate(Certificates.CLIENT_CRT) .build()) .build(); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }
@Test public void acceptValidConnectionWithValidNumberPayloadMapping() { final Connection connection = createConnection(CONNECTION_ID) .toBuilder() .setSources(TestConstants.Sources.SOURCES_WITH_VALID_MAPPING_NUMBER) .setTargets(TestConstants.Targets.TARGET_WITH_VALID_MAPPING_NUMBER) .build(); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }
@Test public void rejectValidConnectionWithInvalidNumberSourcePayloadMapping() { exception.expect(ConnectionConfigurationInvalidException.class); final Connection connection = createConnection(CONNECTION_ID) .toBuilder() .setSources(TestConstants.Sources.SOURCES_WITH_INVALID_MAPPING_NUMBER) .build(); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }
@Test public void rejectValidConnectionWithInvalidNumberTargetPayloadMapping() { exception.expect(ConnectionConfigurationInvalidException.class); final Connection connection = createConnection(CONNECTION_ID) .toBuilder() .setTargets(TestConstants.Targets.TARGET_WITH_INVALID_MAPPING_NUMBER) .build(); final ConnectionValidator underTest = getConnectionValidator(); underTest.validate(connection, DittoHeaders.empty(), actorSystem); }