ConnectionMigrationUtil { static Connection connectionFromJsonWithMigration(@Nonnull final JsonObject connectionJsonObject) { final UnaryOperator<JsonObject> migrateSourceFilters = new MigrateSourceFilters(); final UnaryOperator<JsonObject> migrateTopicActionSubjectFilters = new MigrateTopicActionSubjectFilters(); final UnaryOperator<JsonObject> migrateAuthorizationContexts = new MigrateAuthorizationContexts(); return ConnectivityModelFactory.connectionFromJson( migrateAuthorizationContexts .andThen(migrateSourceFilters) .andThen(migrateTopicActionSubjectFilters) .apply(connectionJsonObject)); } private  ConnectionMigrationUtil();   }
@Test public void migrateConnectionWithGlobalAuthorizationContext() { final Connection migratedConnection = ConnectionMigrationUtil.connectionFromJsonWithMigration(KNOWN_CONNECTION_JSON); assertThat((CharSequence) migratedConnection.getId()).isEqualTo(ID); assertThat(migratedConnection.getName()).contains(NAME); assertThat((Object) migratedConnection.getConnectionType()).isEqualTo(TYPE); assertThat(migratedConnection.getUri()).isEqualTo(URI); migratedConnection.getSources().forEach(source -> assertThat(source.getAuthorizationContext()).isEqualTo(AUTHORIZATION_CONTEXT)); migratedConnection.getTargets().forEach(target -> assertThat(target.getAuthorizationContext()).isEqualTo(AUTHORIZATION_CONTEXT)); }
@Test public void migrateConnectionContainingSourcesAndTargetsWithOwnAuthContextWithGlobalAuthContext() { final JsonObjectBuilder builder = KNOWN_CONNECTION_JSON.toBuilder(); builder.set(Connection.JsonFields.SOURCES, MigrateAuthorizationContexts.migrateSources(KNOWN_CONNECTION_JSON, ALT_AUTHORIZATION_CONTEXT .getAuthorizationSubjectIds() .stream() .map(JsonValue::of) .collect(JsonCollectors.valuesToArray())).get()); builder.set(Connection.JsonFields.TARGETS, MigrateAuthorizationContexts.migrateTargets(KNOWN_CONNECTION_JSON, ALT_AUTHORIZATION_CONTEXT .getAuthorizationSubjectIds() .stream() .map(JsonValue::of) .collect(JsonCollectors.valuesToArray())).get()); final Connection migratedConnection = ConnectionMigrationUtil.connectionFromJsonWithMigration(builder.build()); assertThat((CharSequence) migratedConnection.getId()).isEqualTo(ID); assertThat(migratedConnection.getName()).contains(NAME); assertThat((Object) migratedConnection.getConnectionType()).isEqualTo(TYPE); assertThat(migratedConnection.getUri()).isEqualTo(URI); migratedConnection.getSources().forEach(source -> assertThat(source.getAuthorizationContext()).isEqualTo(ALT_AUTHORIZATION_CONTEXT)); migratedConnection.getTargets().forEach(target -> assertThat(target.getAuthorizationContext()).isEqualTo(ALT_AUTHORIZATION_CONTEXT)); }
@Test public void migrateSourceWithFilters() { final Connection migratedConnection = ConnectionMigrationUtil.connectionFromJsonWithMigration(KNOWN_CONNECTION_JSON); assertThat(migratedConnection.getSources()).hasSize(2); assertThat(migratedConnection.getSources().get(0).getEnforcement()).isPresent(); assertThat(migratedConnection.getSources().get(1).getEnforcement()).isEmpty(); final Enforcement enforcement = migratedConnection.getSources().get(0).getEnforcement().get(); assertThat(enforcement.getInput()).isEqualTo("{{ source:address }}"); assertThat(enforcement.getFilters()).hasSize(2); assertThat(enforcement.getFilters()).contains("{{thing:id}}", "{{thing:name}}"); }
@Test public void migratePlaceholderTopicActionSubject() { final Connection migratedConnection = ConnectionMigrationUtil.connectionFromJsonWithMigration(KNOWN_CONNECTION_JSON); assertThat(migratedConnection.getSources().get(1).getHeaderMapping()).isPresent(); final HeaderMapping sourceHeaderMapping = migratedConnection.getSources().get(1).getHeaderMapping().get(); assertThat(sourceHeaderMapping.getMapping().get("source-action")) .isEqualTo("source/{{ topic:action }}"); assertThat(sourceHeaderMapping.getMapping().get("source-subject")) .isEqualTo("source/{{topic:action-subject }}"); assertThat(sourceHeaderMapping.getMapping().get("source-subject-next-gen")) .isEqualTo("source/{{ topic:action-subject }}"); assertThat(sourceHeaderMapping.getMapping().get("source-some-header")) .isEqualTo("source/{{ topic:full | fn:substring-before('/') }}"); assertThat(migratedConnection.getTargets().get(1).getHeaderMapping()).isPresent(); final HeaderMapping targetHeaderMapping = migratedConnection.getTargets().get(1).getHeaderMapping().get(); assertThat(targetHeaderMapping.getMapping().get("target-action")) .isEqualTo("target/{{ topic:action }}"); assertThat(targetHeaderMapping.getMapping().get("target-subject")) .isEqualTo("target/{{topic:action-subject }}"); assertThat(targetHeaderMapping.getMapping().get("target-subject-next-gen")) .isEqualTo("target/{{ topic:action-subject }}"); assertThat(targetHeaderMapping.getMapping().get("target-some-header")) .isEqualTo("target/{{ topic:full | fn:substring-before('/') }}"); assertThat(migratedConnection.getTargets().get(2).getAddress()) .isEqualTo("amqp/target3/{{topic:action-subject}}"); }