From 35f6fadcc88c117fd06c987b670db0cb7f4bf51c Mon Sep 17 00:00:00 2001 From: Rekish Date: Fri, 13 May 2022 16:15:32 +0400 Subject: [PATCH 01/16] [TH2-3673] Fixed filters logic --- .../schema/filter/strategy/impl/AbstractFilterStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java index fee14236e..15528ff88 100644 --- a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java +++ b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java @@ -52,7 +52,7 @@ public boolean verify(T message, List routerFilters) { protected abstract Map getFields(T message); private boolean checkValues(Map messageFields, MultiValuedMap fieldFilters) { - return fieldFilters.isEmpty() || fieldFilters.keys().stream().anyMatch(fieldName -> { + return fieldFilters.isEmpty() || fieldFilters.keys().stream().allMatch(fieldName -> { String messageValue = messageFields.get(fieldName); Collection filters = fieldFilters.get(fieldName); return !filters.isEmpty() && filters.stream().allMatch(filter -> FieldValueChecker.checkFieldValue(filter, messageValue)); From 952232b1bdd3f20377aba80df3a575fb495d757c Mon Sep 17 00:00:00 2001 From: Rekish Date: Fri, 13 May 2022 16:16:39 +0400 Subject: [PATCH 02/16] [TH2-3673] Added tests for new filtering logic --- .../impl/TestAnyMessageFilterStrategy.kt | 240 +++++++++++++++++- 1 file changed, 237 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index be2cb2a9a..b1f9edbda 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -19,24 +19,132 @@ package com.exactpro.th2.common.schema.filter.strategy.impl import com.exactpro.th2.common.grpc.AnyMessage import com.exactpro.th2.common.grpc.Direction import com.exactpro.th2.common.grpc.RawMessage +import com.exactpro.th2.common.message.addField import com.exactpro.th2.common.message.message import com.exactpro.th2.common.message.toJson import com.exactpro.th2.common.schema.message.configuration.FieldFilterConfiguration import com.exactpro.th2.common.schema.message.configuration.FieldFilterOperation import com.exactpro.th2.common.schema.message.configuration.MqRouterFilterConfiguration import org.apache.commons.collections4.MultiMapUtils -import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.Arguments.arguments import org.junit.jupiter.params.provider.MethodSource -import org.junit.jupiter.params.provider.ValueSource class TestAnyMessageFilterStrategy { private val strategy = AnyMessageFilterStrategy() + @ParameterizedTest + @MethodSource("multipleFiltersMatch") + fun `matches any filter`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + listOf( + MqRouterFilterConfiguration( + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("session_alias", FieldFilterConfiguration( + fieldName = "session_alias", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-alias" + )) + } + ), + MqRouterFilterConfiguration( + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("direction", FieldFilterConfiguration( + fieldName = "direction", + operation = FieldFilterOperation.EQUAL, + expectedValue = "FIRST" + )) + } + ), + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("rawMessagesBothFilters") + fun `matches with multiple metadata filters`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("session_alias", FieldFilterConfiguration( + fieldName = "session_alias", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-alias" + )) + put("direction", FieldFilterConfiguration( + fieldName = "direction", + operation = FieldFilterOperation.EQUAL, + expectedValue = "FIRST" + )) + } + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("parsedMessagesBothFilters") + fun `matches with multiple message filters`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put("test-field1", FieldFilterConfiguration( + fieldName = "test-field1", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value1" + )) + put("test-field2", FieldFilterConfiguration( + fieldName = "test-field2", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value2" + )) + } + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("messagesWithMessageAndMetadataFilters") + fun `matches with multiple message and metadata filters`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put("test-field1", FieldFilterConfiguration( + fieldName = "test-field1", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value1" + )) + put("test-field2", FieldFilterConfiguration( + fieldName = "test-field2", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value2" + )) + }, + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("session_alias", FieldFilterConfiguration( + fieldName = "session_alias", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-alias" + )) + put("direction", FieldFilterConfiguration( + fieldName = "direction", + operation = FieldFilterOperation.EQUAL, + expectedValue = "FIRST" + )) + } + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + @ParameterizedTest @MethodSource("parsedMessages") fun `matches the parsed message by message type with single filter`(anyMessage: AnyMessage, expectMatch: Boolean) { @@ -129,5 +237,131 @@ class TestAnyMessageFilterStrategy { arguments(RAW_MESSAGE_MATCH, true), arguments(RAW_MESSAGE_MISS_MATCH, false) ) + parsedMessages() + + ///////////////// + + private val MULTIPLE_FILTERS_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias") + ).build() + + private val MULTIPLE_FILTERS_MESSAGE_ONE_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.SECOND, "test-alias") + ).build() + + private val MULTIPLE_FILTERS_MESSAGE_NOT_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.SECOND, "test-alias-wrong") + ).build() + + @JvmStatic + fun multipleFiltersMatch(): List = listOf( + arguments(MULTIPLE_FILTERS_MESSAGE_FULL_MATCH, true), + arguments(MULTIPLE_FILTERS_MESSAGE_ONE_MATCH, true), + arguments(MULTIPLE_FILTERS_MESSAGE_NOT_MATCH, false), + ) + + ///////////////// + + private val PARSED_MESSAGE_BOTH_FILTERS_MESSAGES_AND_METADATA_FULL_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value2") + } + ).build() + + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.SECOND, "test-alias").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value2") + } + ).build() + + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value-wrong") + addField("test-field2", "test-value2") + } + ).build() + + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.SECOND, "test-alias").apply { + addField("test-field1", "test-value-wrong") + addField("test-field2", "test-value2") + } + ).build() + + @JvmStatic + fun messagesWithMessageAndMetadataFilters() : List = listOf( + arguments(PARSED_MESSAGE_BOTH_FILTERS_MESSAGES_AND_METADATA_FULL_MATCH, true), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH, false) + ) + + //////////// + + private val PARSED_MESSAGE_BOTH_FILTERS_FULL_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value2") + } + ).build() + + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value-wrong") + addField("test-field2", "test-value2") + } + ).build() + + private val PARSED_MESSAGE_BOTH_FILTERS_NO_MATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value-wrong") + addField("test-field2", "test-value-wrong") + } + ).build() + + @JvmStatic + fun parsedMessagesBothFilters() : List = listOf( + arguments(PARSED_MESSAGE_BOTH_FILTERS_FULL_MATCH, true), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MATCH, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_NO_MATCH, false), + ) + + ///////////// + + private val RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.idBuilder.apply { + connectionIdBuilder.sessionAlias = "test-alias" + direction = Direction.FIRST + } + } + ).build() + + private val RAW_MESSAGE_BOTH_FILTERS_ONE_MATCH = AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.idBuilder.apply { + connectionIdBuilder.sessionAlias = "test-alias" + direction = Direction.SECOND + } + } + ).build() + + private val RAW_MESSAGE_BOTH_FILTERS_NO_MATCH = AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.idBuilder.apply { + connectionIdBuilder.sessionAlias = "test-alias-wrong-value" + direction = Direction.SECOND + } + } + ).build() + + @JvmStatic + fun rawMessagesBothFilters() : List = listOf( + arguments(RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH, true), + arguments(RAW_MESSAGE_BOTH_FILTERS_ONE_MATCH, false), + arguments(RAW_MESSAGE_BOTH_FILTERS_NO_MATCH, false) + ) + } } \ No newline at end of file From 997dbbbfa304a64f5fd3051933673aa3414612c6 Mon Sep 17 00:00:00 2001 From: Rekish Date: Fri, 13 May 2022 16:35:36 +0400 Subject: [PATCH 03/16] [TH2-3673] Added tests for new filtering logic --- .../schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index b1f9edbda..af1db7a65 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -355,7 +355,7 @@ class TestAnyMessageFilterStrategy { } } ).build() - + @JvmStatic fun rawMessagesBothFilters() : List = listOf( arguments(RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH, true), From b3a2a182680656ac13a1acddc035ef8c93ac6d2b Mon Sep 17 00:00:00 2001 From: Rekish Date: Fri, 13 May 2022 16:37:27 +0400 Subject: [PATCH 04/16] [TH2-3673] Minor optimization for verifying against multiple filters --- .../strategy/impl/AbstractFilterStrategy.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java index 15528ff88..4aa0df4c8 100644 --- a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java +++ b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java @@ -21,6 +21,7 @@ import com.google.protobuf.Message; import org.apache.commons.collections4.MultiMapUtils; import org.apache.commons.collections4.MultiValuedMap; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; @@ -30,18 +31,15 @@ public abstract class AbstractFilterStrategy implements Filte @Override public boolean verify(T message, RouterFilter routerFilter) { - - MultiValuedMap msgFieldFilters = MultiMapUtils.newListValuedHashMap(); - msgFieldFilters.putAll(routerFilter.getMessage()); - msgFieldFilters.putAll(routerFilter.getMetadata()); - + MultiValuedMap msgFieldFilters = extractConfigMapFromRouterFilter(routerFilter); return checkValues(getFields(message), msgFieldFilters); } @Override public boolean verify(T message, List routerFilters) { + Map fields = getFields(message); for (var fieldsFilter : routerFilters) { - if (verify(message, fieldsFilter)) { + if (verify(fields, fieldsFilter)) { return true; } } @@ -49,6 +47,19 @@ public boolean verify(T message, List routerFilters) { return false; } + private boolean verify(Map fields, RouterFilter routerFilter) { + MultiValuedMap msgFieldFilters = extractConfigMapFromRouterFilter(routerFilter); + return checkValues(fields, msgFieldFilters); + } + + @NotNull + private MultiValuedMap extractConfigMapFromRouterFilter(RouterFilter routerFilter) { + MultiValuedMap msgFieldFilters = MultiMapUtils.newListValuedHashMap(); + msgFieldFilters.putAll(routerFilter.getMessage()); + msgFieldFilters.putAll(routerFilter.getMetadata()); + return msgFieldFilters; + } + protected abstract Map getFields(T message); private boolean checkValues(Map messageFields, MultiValuedMap fieldFilters) { From abd9642a77c76393f4d1978b94d47659e61677dd Mon Sep 17 00:00:00 2001 From: Rekish Date: Sat, 14 May 2022 20:46:56 +0400 Subject: [PATCH 05/16] [TH2-3673] Tests for multiple same values --- .../impl/TestAnyMessageFilterStrategy.kt | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index af1db7a65..904c5fb50 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -199,6 +199,106 @@ class TestAnyMessageFilterStrategy { assertEquals(expectMatch, match) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } } + @ParameterizedTest + @MethodSource("messagesWithSameFilterFields") + fun `miss matches with the same filter fields`(message: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + message, + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value1" + ) + ) + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value2" + ) + ) + }, + ) + ) + + assertEquals( + expectMatch, + match + ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("messagesWithMultipleFiltersWithSameFilterField") + fun `matches with multiple filters with the same filter fields`(message: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + message, + listOf( + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value1" + ) + ) + }, + ), + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value2" + ) + ) + }, + ), + + ) + ) + + assertEquals( + expectMatch, + match + ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("messagesWithMultipleSameFields") + fun `matches message with multiple fields with same name`(message: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + message, + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.NOT_EQUAL, + expectedValue = "test-value1" + ) + ) + put( + "test-field", FieldFilterConfiguration( + fieldName = "test-field", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-value2" + ) + ) + }, + ) + ) + + assertEquals( + expectMatch, + match + ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + companion object { private val PARSED_MESSAGE_MATCH = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias") @@ -240,6 +340,38 @@ class TestAnyMessageFilterStrategy { ///////////////// + private val MATCHES_WITH_SAME_FIELDS = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field", "test-value1") + addField("test-field", "test-value2") + } + ).build() + + @JvmStatic + fun messagesWithMultipleSameFields(): List = listOf( + arguments(MATCHES_WITH_SAME_FIELDS, true), + ) + + //////////////// + + private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field", "test-value1") + } + ).build() + + @JvmStatic + fun messagesWithSameFilterFields(): List = listOf( + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS, false), + ) + + @JvmStatic + fun messagesWithMultipleFiltersWithSameFilterField(): List = listOf( + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS, true), + ) + + /////////////// + private val MULTIPLE_FILTERS_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias") ).build() From ae7e315a6b3fa5646981ce5e0afba2dba1810b49 Mon Sep 17 00:00:00 2001 From: Rekish Date: Tue, 17 May 2022 12:19:12 +0400 Subject: [PATCH 06/16] [TH2-3673] minor test code style improvements --- .../impl/TestAnyMessageFilterStrategy.kt | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index 904c5fb50..588874fc7 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -300,6 +300,13 @@ class TestAnyMessageFilterStrategy { } companion object { + + private fun simpleMessageBuilder(messageType: String, direction: Direction, sessionAlias: String): AnyMessage { + return AnyMessage.newBuilder().setMessage( + message(messageType, direction, sessionAlias) + ).build() + } + private val PARSED_MESSAGE_MATCH = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias") ).build() @@ -372,23 +379,11 @@ class TestAnyMessageFilterStrategy { /////////////// - private val MULTIPLE_FILTERS_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias") - ).build() - - private val MULTIPLE_FILTERS_MESSAGE_ONE_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.SECOND, "test-alias") - ).build() - - private val MULTIPLE_FILTERS_MESSAGE_NOT_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.SECOND, "test-alias-wrong") - ).build() - @JvmStatic fun multipleFiltersMatch(): List = listOf( - arguments(MULTIPLE_FILTERS_MESSAGE_FULL_MATCH, true), - arguments(MULTIPLE_FILTERS_MESSAGE_ONE_MATCH, true), - arguments(MULTIPLE_FILTERS_MESSAGE_NOT_MATCH, false), + arguments(simpleMessageBuilder("test", Direction.FIRST, "test-alias"), true), + arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias"), true), + arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias-wrong"), false), ) ///////////////// From 76b908c53007ff24d3800803b02bbf163e2f5870 Mon Sep 17 00:00:00 2001 From: Rekish Date: Wed, 18 May 2022 15:23:37 +0400 Subject: [PATCH 07/16] additional tests --- .../impl/TestAnyMessageFilterStrategy.kt | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index 588874fc7..c50893511 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -361,20 +361,27 @@ class TestAnyMessageFilterStrategy { //////////////// - private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field", "test-value1") } ).build() + private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2 = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field", "test-value2") + } + ).build() @JvmStatic fun messagesWithSameFilterFields(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS, false), + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1, false), + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2, false), ) @JvmStatic fun messagesWithMultipleFiltersWithSameFilterField(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS, true), + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1, true), + arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2, true), ) /////////////// @@ -383,6 +390,7 @@ class TestAnyMessageFilterStrategy { fun multipleFiltersMatch(): List = listOf( arguments(simpleMessageBuilder("test", Direction.FIRST, "test-alias"), true), arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias"), true), + arguments(simpleMessageBuilder("test", Direction.FIRST, "test-wrong"), true), arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias-wrong"), false), ) @@ -395,33 +403,54 @@ class TestAnyMessageFilterStrategy { } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH = AnyMessage.newBuilder().setMessage( + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.SECOND, "test-alias").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value2") } ).build() + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias-wrong").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value2") + } + ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH = AnyMessage.newBuilder().setMessage( + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field1", "test-value-wrong") addField("test-field2", "test-value2") } ).build() + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value-wrong") + } + ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH = AnyMessage.newBuilder().setMessage( + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.SECOND, "test-alias").apply { addField("test-field1", "test-value-wrong") addField("test-field2", "test-value2") } ).build() + private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias-wrong").apply { + addField("test-field1", "test-value1") + addField("test-field2", "test-value-wrong") + } + ).build() @JvmStatic fun messagesWithMessageAndMetadataFilters() : List = listOf( arguments(PARSED_MESSAGE_BOTH_FILTERS_MESSAGES_AND_METADATA_FULL_MATCH, true), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH, false) + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_1, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_2, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_1, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_2, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_1, false), + arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_2, false), ) //////////// From e3a5c0b354af9046a1daa64015cbfe39d0c00884 Mon Sep 17 00:00:00 2001 From: Rekish Date: Wed, 18 May 2022 16:52:17 +0400 Subject: [PATCH 08/16] changed logic for filtering message batches --- .../impl/rabbitmq/group/RabbitMessageGroupBatchRouter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/RabbitMessageGroupBatchRouter.kt b/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/RabbitMessageGroupBatchRouter.kt index d1baf2502..468d26068 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/RabbitMessageGroupBatchRouter.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/RabbitMessageGroupBatchRouter.kt @@ -51,7 +51,7 @@ class RabbitMessageGroupBatchRouter : AbstractRabbitRouter() val builder = MessageGroupBatch.newBuilder() message.groupsList.forEach { group -> - if (group.messagesList.all { filterMessage(it, pinConfiguration.filters) }) { + if (group.messagesList.any { filterMessage(it, pinConfiguration.filters) }) { builder.addGroups(group) } else { incrementDroppedMetrics( From c88af558f04a15e16f2e40dfb86c41b75c76cca5 Mon Sep 17 00:00:00 2001 From: Rekish Date: Wed, 18 May 2022 16:52:32 +0400 Subject: [PATCH 09/16] tests for filtering message batches --- .../TestRabbitMessageGroupBatchRouter.kt | 77 +++++++++++++++++-- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt index 4b48e4510..210bd4311 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt @@ -35,13 +35,7 @@ import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.function.Executable -import org.mockito.kotlin.any -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.argumentCaptor -import org.mockito.kotlin.eq -import org.mockito.kotlin.mock -import org.mockito.kotlin.never -import org.mockito.kotlin.verify +import org.mockito.kotlin.* class TestRabbitMessageGroupBatchRouter { private val connectionConfiguration = ConnectionManagerConfiguration() @@ -263,6 +257,75 @@ class TestRabbitMessageGroupBatchRouter { } } + @Nested + inner class BatchPublishing { + + private val router = createRouter(mapOf( + "test-pine" to QueueConfiguration( + routingKey = "", + queue = "subscribe", + exchange = "test-exchange", + attributes = listOf("subscribe") + ), + "test-pin1" to QueueConfiguration( + routingKey = "test", + queue = "", + exchange = "test-exchange", + attributes = listOf("publish"), + filters = listOf( + MqRouterFilterConfiguration( + metadata = listOf( + FieldFilterConfiguration( + fieldName = "message_type", + expectedValue = "test-message", + operation = FieldFilterOperation.EQUAL + ) + ) + ) + ) + ), + )) + + @Test + fun `publish batch if all messages passed`() { + router.send( + MessageGroupBatch.newBuilder() + .addGroups(MessageGroup.newBuilder() + .apply { this += message("test-message", Direction.FIRST, "test-alias1") } + .apply { this += message("test-message", Direction.FIRST, "test-alias2") } + .apply { this += message("test-message", Direction.FIRST, "test-alias3") } + ).build() + ) + verify(connectionManager, times(1)).basicPublish(any(), any(), anyOrNull(), any()) + } + + @Test + fun `dont publish batch if all messages not passed`() { + router.send( + MessageGroupBatch.newBuilder() + .addGroups(MessageGroup.newBuilder() + .apply { this += message("test-message1", Direction.FIRST, "test-alias1") } + .apply { this += message("test-message2", Direction.FIRST, "test-alias2") } + .apply { this += message("test-message3", Direction.FIRST, "test-alias3") } + ).build() + ) + verify(connectionManager, never()).basicPublish(any(), any(), anyOrNull(), any()) + } + + @Test + fun `publish full batch if one message is passed`() { + router.send( + MessageGroupBatch.newBuilder() + .addGroups(MessageGroup.newBuilder() + .apply { this += message("test-message1", Direction.FIRST, "test-alias1") } + .apply { this += message("test-message", Direction.FIRST, "test-alias2") } + .apply { this += message("test-message3", Direction.FIRST, "test-alias3") } + ).build() + ) + verify(connectionManager, times(1)).basicPublish(any(), any(), anyOrNull(), any()) + } + } + private fun createRouter(pins: Map): MessageRouter = RabbitMessageGroupBatchRouter().apply { init(DefaultMessageRouterContext( From b49399e90454a65d8e271a7a4e2d4ebeff14bbc4 Mon Sep 17 00:00:00 2001 From: Rekish Date: Thu, 19 May 2022 15:00:25 +0400 Subject: [PATCH 10/16] added properties field in filter; added related tests --- .../strategy/impl/AbstractFilterStrategy.java | 1 + .../impl/AbstractTh2MsgFilterStrategy.java | 1 + .../strategy/impl/AnyMessageFilterStrategy.kt | 4 +- .../configuration/GrpcRawFilterStrategy.kt | 6 +- .../MessageRouterConfiguration.kt | 14 ++++- .../message/configuration/RouterFilter.kt | 1 + .../common/schema/TestJsonConfiguration.kt | 13 +++- .../impl/TestAnyMessageFilterStrategy.kt | 61 +++++++++++++++++++ .../message_router.json | 18 ++++++ 9 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java index 4aa0df4c8..e82fa58e1 100644 --- a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java +++ b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractFilterStrategy.java @@ -57,6 +57,7 @@ private MultiValuedMap extractConfigMapFromRou MultiValuedMap msgFieldFilters = MultiMapUtils.newListValuedHashMap(); msgFieldFilters.putAll(routerFilter.getMessage()); msgFieldFilters.putAll(routerFilter.getMetadata()); + msgFieldFilters.putAll(routerFilter.getProperties()); return msgFieldFilters; } diff --git a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java index 2d0fe5964..b48b52789 100644 --- a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java +++ b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java @@ -47,6 +47,7 @@ public Map getFields(com.google.protobuf.Message message) { DIRECTION_KEY, messageID.getDirection().name() ); + messageFields.putAll(th2Msg.getMetadata().getPropertiesMap()); messageFields.putAll(metadataMsgFields); return messageFields; diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt index d39f229d1..911d026f9 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt @@ -25,19 +25,21 @@ class AnyMessageFilterStrategy : AbstractFilterStrategy() { override fun getFields(message: Message): MutableMap { check(message is AnyMessage) { "Message is not an ${AnyMessage::class.qualifiedName}: ${message.toJson()}" } - val result = HashMap(); + val result = HashMap() when { message.hasMessage() -> { result.putAll(message.message.fieldsMap.mapValues { it.value.simpleValue }) val metadata = message.message.metadata + result.putAll(message.message.metadata.propertiesMap) result[AbstractTh2MsgFilterStrategy.SESSION_ALIAS_KEY] = metadata.id.connectionId.sessionAlias result[AbstractTh2MsgFilterStrategy.MESSAGE_TYPE_KEY] = metadata.messageType result[AbstractTh2MsgFilterStrategy.DIRECTION_KEY] = metadata.id.direction.name } message.hasRawMessage() -> { val metadata = message.rawMessage.metadata + result.putAll(message.rawMessage.metadata.propertiesMap) result[AbstractTh2MsgFilterStrategy.SESSION_ALIAS_KEY] = metadata.id.connectionId.sessionAlias result[AbstractTh2MsgFilterStrategy.DIRECTION_KEY] = metadata.id.direction.name } diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/grpc/configuration/GrpcRawFilterStrategy.kt b/src/main/kotlin/com/exactpro/th2/common/schema/grpc/configuration/GrpcRawFilterStrategy.kt index 50dfc202c..efe922487 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/grpc/configuration/GrpcRawFilterStrategy.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/grpc/configuration/GrpcRawFilterStrategy.kt @@ -30,7 +30,8 @@ data class GrpcRawFilterStrategy(var filters: List = emptyMultiMap(), - @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var message: MultiValuedMap = emptyMultiMap() + @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var message: MultiValuedMap = emptyMultiMap(), + @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var properties: MultiValuedMap = emptyMultiMap() ) : RouterFilter, Configuration() { @JsonGetter("metadata") @@ -39,4 +40,7 @@ data class GrpcRouterFilterConfiguration( @JsonGetter("message") fun getJsonMessage(): Collection = message.values() + @JsonGetter("properties") + fun getJsonProperties(): Collection = properties.values() + } \ No newline at end of file diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt index ed63c3fc2..5c4307f09 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt @@ -47,12 +47,14 @@ data class QueueConfiguration( data class MqRouterFilterConfiguration( @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var metadata: MultiValuedMap = emptyMultiMap(), - @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var message: MultiValuedMap = emptyMultiMap() + @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var message: MultiValuedMap = emptyMultiMap(), + @JsonDeserialize(using = MultiMapFiltersDeserializer::class) override var properties: MultiValuedMap = emptyMultiMap() ) : Configuration(), RouterFilter { constructor( metadata: Collection = emptyList(), - message: Collection = emptyList() + message: Collection = emptyList(), + properties: Collection = emptyList() ) : this( MultiMapUtils.newListValuedHashMap().also { metadataMultiMap -> metadata.forEach { @@ -63,6 +65,11 @@ data class MqRouterFilterConfiguration( message.forEach { messageMultiMap.put(it.fieldName, it) } + }, + MultiMapUtils.newListValuedHashMap().also { propertiesMultiMap -> + properties.forEach { + propertiesMultiMap.put(it.fieldName, it) + } } ) @@ -71,6 +78,9 @@ data class MqRouterFilterConfiguration( @JsonGetter("message") fun getJsonMessage(): Collection = message.values() + + @JsonGetter("properties") + fun getJsonProperties(): Collection = properties.values() } data class FieldFilterConfigurationOld( diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/RouterFilter.kt b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/RouterFilter.kt index fcdb650b7..e6d845a6b 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/RouterFilter.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/RouterFilter.kt @@ -20,4 +20,5 @@ import org.apache.commons.collections4.MultiValuedMap interface RouterFilter { val metadata: MultiValuedMap val message: MultiValuedMap + val properties: MultiValuedMap } \ No newline at end of file diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/TestJsonConfiguration.kt b/src/test/kotlin/com/exactpro/th2/common/schema/TestJsonConfiguration.kt index 16a5857b4..89e906b5f 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/TestJsonConfiguration.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/TestJsonConfiguration.kt @@ -183,7 +183,14 @@ class TestJsonConfiguration { FieldFilterOperation.EQUAL ) ), - listOf(FieldFilterConfiguration("test_field", "test_value", FieldFilterOperation.EQUAL)) + listOf(FieldFilterConfiguration("test_field", "test_value", FieldFilterOperation.EQUAL)), + listOf( + FieldFilterConfiguration( + "method", + "POST", + FieldFilterOperation.EQUAL + ) + ), ), MqRouterFilterConfiguration( listOf( @@ -196,6 +203,10 @@ class TestJsonConfiguration { listOf( FieldFilterConfiguration("test_field", "test_value0", FieldFilterOperation.EQUAL), FieldFilterConfiguration("test_field", "test_value1", FieldFilterOperation.EQUAL) + ), + listOf( + FieldFilterConfiguration("method1", "POST", FieldFilterOperation.EQUAL), + FieldFilterConfiguration("method2", "GET", FieldFilterOperation.NOT_EQUAL) ) ) ) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index c50893511..5a7226ed6 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -18,6 +18,7 @@ package com.exactpro.th2.common.schema.filter.strategy.impl import com.exactpro.th2.common.grpc.AnyMessage import com.exactpro.th2.common.grpc.Direction +import com.exactpro.th2.common.grpc.Message import com.exactpro.th2.common.grpc.RawMessage import com.exactpro.th2.common.message.addField import com.exactpro.th2.common.message.message @@ -299,6 +300,30 @@ class TestAnyMessageFilterStrategy { ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } } + @ParameterizedTest + @MethodSource("messagesWithProperty") + fun `matches message with properties`(message: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + message, + MqRouterFilterConfiguration( + properties = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-property", FieldFilterConfiguration( + fieldName = "test-property", + operation = FieldFilterOperation.EQUAL, + expectedValue = "property-value" + ) + ) + }, + ) + ) + + assertEquals( + expectMatch, + match + ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + companion object { private fun simpleMessageBuilder(messageType: String, direction: Direction, sessionAlias: String): AnyMessage { @@ -485,6 +510,42 @@ class TestAnyMessageFilterStrategy { ///////////// + private val RAW_MESSAGE_WITH_PROPERTY = AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.putProperties("test-property", "property-value") + } + ).build() + + private val RAW_MESSAGE_WITH_PROPERTY_MISMATCH = AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.putProperties("test-property", "property-value-wrong") + } + ).build() + + private val MESSAGE_WITH_PROPERTY = AnyMessage.newBuilder().setMessage( + Message.newBuilder().apply { + metadataBuilder.putProperties("test-property", "property-value") + } + ).build() + + private val MESSAGE_WITH_PROPERTY_MISMATCH = AnyMessage.newBuilder().setMessage( + Message.newBuilder().apply { + metadataBuilder.putProperties("test-property", "property-value-wrong") + } + ).build() + + + + @JvmStatic + fun messagesWithProperty() : List = listOf( + arguments(MESSAGE_WITH_PROPERTY, true), + arguments(MESSAGE_WITH_PROPERTY_MISMATCH, false), + arguments(RAW_MESSAGE_WITH_PROPERTY, true), + arguments(RAW_MESSAGE_WITH_PROPERTY_MISMATCH, false), + ) + + ///////////// + private val RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( RawMessage.newBuilder().apply { metadataBuilder.idBuilder.apply { diff --git a/src/test/resources/test_json_configurations/message_router.json b/src/test/resources/test_json_configurations/message_router.json index dc8a69397..458f8fc06 100644 --- a/src/test/resources/test_json_configurations/message_router.json +++ b/src/test/resources/test_json_configurations/message_router.json @@ -13,6 +13,12 @@ "operation": "EQUAL" } }, + "properties": { + "method": { + "value": "POST", + "operation": "EQUAL" + } + }, "message": { "test_field": { "value": "test_value", @@ -28,6 +34,18 @@ "operation": "EQUAL" } ], + "properties": [ + { + "fieldName": "method1", + "value": "POST", + "operation": "EQUAL" + }, + { + "fieldName": "method2", + "value": "GET", + "operation": "NOT_EQUAL" + } + ], "message": [ { "fieldName": "test_field", From 11a848bcc37ef0c83d31cde9d37847e6ddc5a0b4 Mon Sep 17 00:00:00 2001 From: Rekish Date: Thu, 19 May 2022 15:17:14 +0400 Subject: [PATCH 11/16] tests refactor --- .../impl/TestAnyMessageFilterStrategy.kt | 92 ++++++++----------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index 5a7226ed6..9664e6dd4 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -301,7 +301,7 @@ class TestAnyMessageFilterStrategy { } @ParameterizedTest - @MethodSource("messagesWithProperty") + @MethodSource("messagesWithProperties") fun `matches message with properties`(message: AnyMessage, expectMatch: Boolean) { val match = strategy.verify( message, @@ -386,12 +386,12 @@ class TestAnyMessageFilterStrategy { //////////////// - private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_ONE_FIELD_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field", "test-value1") } ).build() - private val MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_ONE_FIELD_2 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field", "test-value2") } @@ -399,14 +399,14 @@ class TestAnyMessageFilterStrategy { @JvmStatic fun messagesWithSameFilterFields(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1, false), - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2, false), + arguments(MESSAGE_WITH_ONE_FIELD_1, false), + arguments(MESSAGE_WITH_ONE_FIELD_2, false), ) @JvmStatic fun messagesWithMultipleFiltersWithSameFilterField(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_1, true), - arguments(MESSAGE_WITH_ONE_FIELDS_FOR_FILTER_WITH_SAME_FIELDS_2, true), + arguments(MESSAGE_WITH_ONE_FIELD_1, true), + arguments(MESSAGE_WITH_ONE_FIELD_2, true), ) /////////////// @@ -421,91 +421,77 @@ class TestAnyMessageFilterStrategy { ///////////////// - private val PARSED_MESSAGE_BOTH_FILTERS_MESSAGES_AND_METADATA_FULL_MATCH = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_FULL_MATCH = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value2") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_METADATA_MISMATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.SECOND, "test-alias").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value2") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_METADATA_MISMATCH_2 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias-wrong").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value2") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field1", "test-value-wrong") addField("test-field2", "test-value2") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value-wrong") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_1 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_1 = AnyMessage.newBuilder().setMessage( message("test", Direction.SECOND, "test-alias").apply { addField("test-field1", "test-value-wrong") addField("test-field2", "test-value2") } ).build() - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_2 = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_2 = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias-wrong").apply { addField("test-field1", "test-value1") addField("test-field2", "test-value-wrong") } ).build() - @JvmStatic - fun messagesWithMessageAndMetadataFilters() : List = listOf( - arguments(PARSED_MESSAGE_BOTH_FILTERS_MESSAGES_AND_METADATA_FULL_MATCH, true), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_1, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_METADATA_NOT_MATCH_2, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_1, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_NOT_MATCH_2, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_1, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MESSAGE_AND_ONE_METADATA_NOT_MATCH_2, false), - ) - - //////////// - - private val PARSED_MESSAGE_BOTH_FILTERS_FULL_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value2") - } - ).build() - - private val PARSED_MESSAGE_BOTH_FILTERS_ONE_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value-wrong") - addField("test-field2", "test-value2") - } - ).build() - - private val PARSED_MESSAGE_BOTH_FILTERS_NO_MATCH = AnyMessage.newBuilder().setMessage( + private val MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH = AnyMessage.newBuilder().setMessage( message("test", Direction.FIRST, "test-alias").apply { addField("test-field1", "test-value-wrong") addField("test-field2", "test-value-wrong") } ).build() + @JvmStatic + fun messagesWithMessageAndMetadataFilters() : List = listOf( + arguments(MESSAGE_WITH_FIELDS_FULL_MATCH, true), + arguments(MESSAGE_WITH_FIELDS_METADATA_MISMATCH_1, false), + arguments(MESSAGE_WITH_FIELDS_METADATA_MISMATCH_2, false), + arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1, false), + arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2, false), + arguments(MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_1, false), + arguments(MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_2, false), + arguments(MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH, false), + ) + @JvmStatic fun parsedMessagesBothFilters() : List = listOf( - arguments(PARSED_MESSAGE_BOTH_FILTERS_FULL_MATCH, true), - arguments(PARSED_MESSAGE_BOTH_FILTERS_ONE_MATCH, false), - arguments(PARSED_MESSAGE_BOTH_FILTERS_NO_MATCH, false), + arguments(MESSAGE_WITH_FIELDS_FULL_MATCH, true), + arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1, false), + arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2, false), + arguments(MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH, false), ) ///////////// @@ -534,10 +520,8 @@ class TestAnyMessageFilterStrategy { } ).build() - - @JvmStatic - fun messagesWithProperty() : List = listOf( + fun messagesWithProperties() : List = listOf( arguments(MESSAGE_WITH_PROPERTY, true), arguments(MESSAGE_WITH_PROPERTY_MISMATCH, false), arguments(RAW_MESSAGE_WITH_PROPERTY, true), @@ -546,7 +530,7 @@ class TestAnyMessageFilterStrategy { ///////////// - private val RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( + private val RAW_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( RawMessage.newBuilder().apply { metadataBuilder.idBuilder.apply { connectionIdBuilder.sessionAlias = "test-alias" @@ -555,7 +539,7 @@ class TestAnyMessageFilterStrategy { } ).build() - private val RAW_MESSAGE_BOTH_FILTERS_ONE_MATCH = AnyMessage.newBuilder().setRawMessage( + private val RAW_MESSAGE_ONE_MISMATCH = AnyMessage.newBuilder().setRawMessage( RawMessage.newBuilder().apply { metadataBuilder.idBuilder.apply { connectionIdBuilder.sessionAlias = "test-alias" @@ -564,7 +548,7 @@ class TestAnyMessageFilterStrategy { } ).build() - private val RAW_MESSAGE_BOTH_FILTERS_NO_MATCH = AnyMessage.newBuilder().setRawMessage( + private val RAW_MESSAGE_NO_MATCH = AnyMessage.newBuilder().setRawMessage( RawMessage.newBuilder().apply { metadataBuilder.idBuilder.apply { connectionIdBuilder.sessionAlias = "test-alias-wrong-value" @@ -575,9 +559,9 @@ class TestAnyMessageFilterStrategy { @JvmStatic fun rawMessagesBothFilters() : List = listOf( - arguments(RAW_MESSAGE_BOTH_FILTERS_FULL_MATCH, true), - arguments(RAW_MESSAGE_BOTH_FILTERS_ONE_MATCH, false), - arguments(RAW_MESSAGE_BOTH_FILTERS_NO_MATCH, false) + arguments(RAW_MESSAGE_FULL_MATCH, true), + arguments(RAW_MESSAGE_ONE_MISMATCH, false), + arguments(RAW_MESSAGE_NO_MATCH, false) ) } From 259e0dec3031ab7e7fcfba1a20034fd8e5462be1 Mon Sep 17 00:00:00 2001 From: Rekish Date: Thu, 19 May 2022 15:24:53 +0400 Subject: [PATCH 12/16] added additional tests for properties filtering --- .../impl/TestAnyMessageFilterStrategy.kt | 57 ++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index 9664e6dd4..e21ee9976 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -18,7 +18,6 @@ package com.exactpro.th2.common.schema.filter.strategy.impl import com.exactpro.th2.common.grpc.AnyMessage import com.exactpro.th2.common.grpc.Direction -import com.exactpro.th2.common.grpc.Message import com.exactpro.th2.common.grpc.RawMessage import com.exactpro.th2.common.message.addField import com.exactpro.th2.common.message.message @@ -324,6 +323,37 @@ class TestAnyMessageFilterStrategy { ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } } + @ParameterizedTest + @MethodSource("messagesWithPropertiesAndMetadata") + fun `matches message with properties and metadata`(message: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + message, + MqRouterFilterConfiguration( + properties = MultiMapUtils.newListValuedHashMap().apply { + put( + "test-property", FieldFilterConfiguration( + fieldName = "test-property", + operation = FieldFilterOperation.EQUAL, + expectedValue = "property-value" + ) + ) + }, + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("session_alias", FieldFilterConfiguration( + fieldName = "session_alias", + operation = FieldFilterOperation.EQUAL, + expectedValue = "test-alias" + )) + } + ) + ) + + assertEquals( + expectMatch, + match + ) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + companion object { private fun simpleMessageBuilder(messageType: String, direction: Direction, sessionAlias: String): AnyMessage { @@ -508,26 +538,39 @@ class TestAnyMessageFilterStrategy { } ).build() - private val MESSAGE_WITH_PROPERTY = AnyMessage.newBuilder().setMessage( - Message.newBuilder().apply { + private val MESSAGE_WITH_PROPERTIES = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { metadataBuilder.putProperties("test-property", "property-value") } ).build() - private val MESSAGE_WITH_PROPERTY_MISMATCH = AnyMessage.newBuilder().setMessage( - Message.newBuilder().apply { + private val MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { metadataBuilder.putProperties("test-property", "property-value-wrong") } ).build() + private val MESSAGE_WITH_PROPERTIES_METADATA_MISMATCH = AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias-wrong").apply { + metadataBuilder.putProperties("test-property", "property-value") + } + ).build() + @JvmStatic fun messagesWithProperties() : List = listOf( - arguments(MESSAGE_WITH_PROPERTY, true), - arguments(MESSAGE_WITH_PROPERTY_MISMATCH, false), + arguments(MESSAGE_WITH_PROPERTIES, true), + arguments(MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH, false), arguments(RAW_MESSAGE_WITH_PROPERTY, true), arguments(RAW_MESSAGE_WITH_PROPERTY_MISMATCH, false), ) + @JvmStatic + fun messagesWithPropertiesAndMetadata() : List = listOf( + arguments(MESSAGE_WITH_PROPERTIES, true), + arguments(MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH, false), + arguments(MESSAGE_WITH_PROPERTIES_METADATA_MISMATCH, false), + ) + ///////////// private val RAW_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( From b6b2f2dc74ef5690980356494072838a20d03505 Mon Sep 17 00:00:00 2001 From: Rekish Date: Thu, 19 May 2022 17:27:06 +0400 Subject: [PATCH 13/16] tests refactoring with util methods --- .../impl/TestAnyMessageFilterStrategy.kt | 302 ++++++------------ .../TestRabbitMessageGroupBatchRouter.kt | 28 +- 2 files changed, 114 insertions(+), 216 deletions(-) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index e21ee9976..27c4593ec 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -43,10 +43,10 @@ class TestAnyMessageFilterStrategy { listOf( MqRouterFilterConfiguration( metadata = MultiMapUtils.newListValuedHashMap().apply { - put("session_alias", FieldFilterConfiguration( - fieldName = "session_alias", + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", operation = FieldFilterOperation.EQUAL, - expectedValue = "test-alias" + expectedValue = "test" )) } ), @@ -129,10 +129,10 @@ class TestAnyMessageFilterStrategy { )) }, metadata = MultiMapUtils.newListValuedHashMap().apply { - put("session_alias", FieldFilterConfiguration( - fieldName = "session_alias", + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", operation = FieldFilterOperation.EQUAL, - expectedValue = "test-alias" + expectedValue = "test" )) put("direction", FieldFilterConfiguration( fieldName = "direction", @@ -339,10 +339,10 @@ class TestAnyMessageFilterStrategy { ) }, metadata = MultiMapUtils.newListValuedHashMap().apply { - put("session_alias", FieldFilterConfiguration( - fieldName = "session_alias", + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", operation = FieldFilterOperation.EQUAL, - expectedValue = "test-alias" + expectedValue = "test" )) } ) @@ -362,249 +362,139 @@ class TestAnyMessageFilterStrategy { ).build() } - private val PARSED_MESSAGE_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias") - ).build() + private fun simpleRawMessageBuilder(sessionAlias: String, directionValue: Direction): AnyMessage { + return AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.idBuilder.apply { + connectionIdBuilder.sessionAlias = sessionAlias + direction = directionValue + } + } + ).build() + } - private val RAW_MESSAGE_MATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.idBuilder.apply { - connectionIdBuilder.sessionAlias = "test-alias" - direction = Direction.FIRST + private fun messageWithFields(messageType: String, direction: Direction, fields: List>): AnyMessage { + return AnyMessage.newBuilder().setMessage( + message(messageType, direction, "test-alias").apply { + fields.forEach { addField(it.first, it.second) } } - } - ).build() - - private val PARSED_MESSAGE_MISS_MATCH = AnyMessage.newBuilder().setMessage( - message("test1", Direction.SECOND, "test-alias1") - ).build() - - private val RAW_MESSAGE_MISS_MATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.idBuilder.apply { - connectionIdBuilder.sessionAlias = "test-alias1" - direction = Direction.SECOND + ).build() + } + + private fun rawMessageWithOneProperty(propertyKey: String, propertyValue: String): AnyMessage { + return AnyMessage.newBuilder().setRawMessage( + RawMessage.newBuilder().apply { + metadataBuilder.putProperties(propertyKey, propertyValue) } - } - ).build() + ).build() + } - @JvmStatic - fun parsedMessages(): List = listOf( - arguments(PARSED_MESSAGE_MATCH, true), - arguments(PARSED_MESSAGE_MISS_MATCH, false) - ) + private fun messageWithOneProperty(messageType: String, propertyKey: String, propertyValue: String): AnyMessage { + return AnyMessage.newBuilder().setMessage( + message(messageType, Direction.FIRST, "test-alias").apply { + metadataBuilder.putProperties(propertyKey, propertyValue) + } + ).build() + } @JvmStatic fun messages(): List = listOf( - arguments(RAW_MESSAGE_MATCH, true), - arguments(RAW_MESSAGE_MISS_MATCH, false) + arguments(simpleRawMessageBuilder("test-alias", Direction.FIRST), true), + arguments(simpleRawMessageBuilder("test-alias1", Direction.SECOND), false) ) + parsedMessages() - ///////////////// - - private val MATCHES_WITH_SAME_FIELDS = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field", "test-value1") - addField("test-field", "test-value2") - } - ).build() + @JvmStatic + fun parsedMessages(): List = listOf( + arguments(simpleMessageBuilder("test", Direction.FIRST, "test-alias"), true), + arguments(simpleMessageBuilder("test1", Direction.SECOND, "test-alias1"), false) + ) @JvmStatic fun messagesWithMultipleSameFields(): List = listOf( - arguments(MATCHES_WITH_SAME_FIELDS, true), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field", "test-value1"), Pair("test-field", "test-value2"))), true), ) - //////////////// - - private val MESSAGE_WITH_ONE_FIELD_1 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field", "test-value1") - } - ).build() - private val MESSAGE_WITH_ONE_FIELD_2 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field", "test-value2") - } - ).build() - @JvmStatic fun messagesWithSameFilterFields(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELD_1, false), - arguments(MESSAGE_WITH_ONE_FIELD_2, false), + arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), false), + arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), false), ) @JvmStatic fun messagesWithMultipleFiltersWithSameFilterField(): List = listOf( - arguments(MESSAGE_WITH_ONE_FIELD_1, true), - arguments(MESSAGE_WITH_ONE_FIELD_2, true), + arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), true), + arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), true), ) - /////////////// - @JvmStatic fun multipleFiltersMatch(): List = listOf( arguments(simpleMessageBuilder("test", Direction.FIRST, "test-alias"), true), arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias"), true), - arguments(simpleMessageBuilder("test", Direction.FIRST, "test-wrong"), true), - arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias-wrong"), false), + arguments(simpleMessageBuilder("test-wrong", Direction.FIRST, "test-alias"), true), + arguments(simpleMessageBuilder("test-wrong", Direction.SECOND, "test-alias"), false), ) - ///////////////// - - private val MESSAGE_WITH_FIELDS_FULL_MATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value2") - } - ).build() - - private val MESSAGE_WITH_FIELDS_METADATA_MISMATCH_1 = AnyMessage.newBuilder().setMessage( - message("test", Direction.SECOND, "test-alias").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value2") - } - ).build() - private val MESSAGE_WITH_FIELDS_METADATA_MISMATCH_2 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias-wrong").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value2") - } - ).build() - - private val MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value-wrong") - addField("test-field2", "test-value2") - } - ).build() - private val MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value-wrong") - } - ).build() - - private val MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_1 = AnyMessage.newBuilder().setMessage( - message("test", Direction.SECOND, "test-alias").apply { - addField("test-field1", "test-value-wrong") - addField("test-field2", "test-value2") - } - ).build() - private val MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_2 = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias-wrong").apply { - addField("test-field1", "test-value1") - addField("test-field2", "test-value-wrong") - } - ).build() - - private val MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - addField("test-field1", "test-value-wrong") - addField("test-field2", "test-value-wrong") - } - ).build() - @JvmStatic fun messagesWithMessageAndMetadataFilters() : List = listOf( - arguments(MESSAGE_WITH_FIELDS_FULL_MATCH, true), - arguments(MESSAGE_WITH_FIELDS_METADATA_MISMATCH_1, false), - arguments(MESSAGE_WITH_FIELDS_METADATA_MISMATCH_2, false), - arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1, false), - arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2, false), - arguments(MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_1, false), - arguments(MESSAGE_WITH_FIELDS_FIELD_AND_METADATA_MISMATCH_2, false), - arguments(MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH, false), + // fields full match + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), true), + + // metadata mismatch + arguments(messageWithFields("test", Direction.SECOND, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), false), + arguments(messageWithFields("test-wrong", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), false), + + // fields mismatch + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value-wrong"))), false), + + // one field and one metadata mismatch + arguments(messageWithFields("test", Direction.SECOND, + listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), + arguments(messageWithFields("test-wrong", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), + ) @JvmStatic fun parsedMessagesBothFilters() : List = listOf( - arguments(MESSAGE_WITH_FIELDS_FULL_MATCH, true), - arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_1, false), - arguments(MESSAGE_WITH_FIELDS_FIELD_MISMATCH_2, false), - arguments(MESSAGE_WITH_FIELDS_ALL_FIELDS_MISMATCH, false), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), true), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), + arguments(messageWithFields("test", Direction.FIRST, + listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value-wrong"))), false), ) - ///////////// - - private val RAW_MESSAGE_WITH_PROPERTY = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.putProperties("test-property", "property-value") - } - ).build() - - private val RAW_MESSAGE_WITH_PROPERTY_MISMATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.putProperties("test-property", "property-value-wrong") - } - ).build() - - private val MESSAGE_WITH_PROPERTIES = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - metadataBuilder.putProperties("test-property", "property-value") - } - ).build() - - private val MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias").apply { - metadataBuilder.putProperties("test-property", "property-value-wrong") - } - ).build() - - private val MESSAGE_WITH_PROPERTIES_METADATA_MISMATCH = AnyMessage.newBuilder().setMessage( - message("test", Direction.FIRST, "test-alias-wrong").apply { - metadataBuilder.putProperties("test-property", "property-value") - } - ).build() - @JvmStatic fun messagesWithProperties() : List = listOf( - arguments(MESSAGE_WITH_PROPERTIES, true), - arguments(MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH, false), - arguments(RAW_MESSAGE_WITH_PROPERTY, true), - arguments(RAW_MESSAGE_WITH_PROPERTY_MISMATCH, false), + arguments(messageWithOneProperty("test", "test-property", "property-value"), true), + arguments(messageWithOneProperty("test", "test-property", "property-value-wrong"), false), + arguments(rawMessageWithOneProperty("test-property", "property-value"), true), + arguments(rawMessageWithOneProperty("test-property", "property-value-wrong"), false) ) @JvmStatic fun messagesWithPropertiesAndMetadata() : List = listOf( - arguments(MESSAGE_WITH_PROPERTIES, true), - arguments(MESSAGE_WITH_PROPERTIES_PROPERTY_MISMATCH, false), - arguments(MESSAGE_WITH_PROPERTIES_METADATA_MISMATCH, false), + arguments(messageWithOneProperty("test", "test-property", "property-value"), true), + arguments(messageWithOneProperty("test", "test-property", "property-value-wrong"), false), + arguments(messageWithOneProperty("test-wrong", "test-property", "property-value"), false), ) - ///////////// - - private val RAW_MESSAGE_FULL_MATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.idBuilder.apply { - connectionIdBuilder.sessionAlias = "test-alias" - direction = Direction.FIRST - } - } - ).build() - - private val RAW_MESSAGE_ONE_MISMATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.idBuilder.apply { - connectionIdBuilder.sessionAlias = "test-alias" - direction = Direction.SECOND - } - } - ).build() - - private val RAW_MESSAGE_NO_MATCH = AnyMessage.newBuilder().setRawMessage( - RawMessage.newBuilder().apply { - metadataBuilder.idBuilder.apply { - connectionIdBuilder.sessionAlias = "test-alias-wrong-value" - direction = Direction.SECOND - } - } - ).build() - @JvmStatic fun rawMessagesBothFilters() : List = listOf( - arguments(RAW_MESSAGE_FULL_MATCH, true), - arguments(RAW_MESSAGE_ONE_MISMATCH, false), - arguments(RAW_MESSAGE_NO_MATCH, false) + arguments(simpleRawMessageBuilder("test-alias", Direction.FIRST), true), + arguments(simpleRawMessageBuilder("test-alias", Direction.SECOND), false), + arguments(simpleRawMessageBuilder("test-alias-wrong-value", Direction.SECOND), false), ) } diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt index 210bd4311..bb8ab8927 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/group/TestRabbitMessageGroupBatchRouter.kt @@ -35,7 +35,15 @@ import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.function.Executable -import org.mockito.kotlin.* +import org.mockito.kotlin.mock +import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull +import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.eq +import org.mockito.kotlin.never +import org.mockito.kotlin.times +import org.mockito.kotlin.verify + class TestRabbitMessageGroupBatchRouter { private val connectionConfiguration = ConnectionManagerConfiguration() @@ -291,9 +299,9 @@ class TestRabbitMessageGroupBatchRouter { router.send( MessageGroupBatch.newBuilder() .addGroups(MessageGroup.newBuilder() - .apply { this += message("test-message", Direction.FIRST, "test-alias1") } - .apply { this += message("test-message", Direction.FIRST, "test-alias2") } - .apply { this += message("test-message", Direction.FIRST, "test-alias3") } + .apply { this += message("test-message", Direction.FIRST, "test-alias") } + .apply { this += message("test-message", Direction.SECOND, "test-alias") } + .apply { this += message("test-message", Direction.FIRST, "test-alias") } ).build() ) verify(connectionManager, times(1)).basicPublish(any(), any(), anyOrNull(), any()) @@ -304,9 +312,9 @@ class TestRabbitMessageGroupBatchRouter { router.send( MessageGroupBatch.newBuilder() .addGroups(MessageGroup.newBuilder() - .apply { this += message("test-message1", Direction.FIRST, "test-alias1") } - .apply { this += message("test-message2", Direction.FIRST, "test-alias2") } - .apply { this += message("test-message3", Direction.FIRST, "test-alias3") } + .apply { this += message("test-message1", Direction.FIRST, "test-alias") } + .apply { this += message("test-message2", Direction.FIRST, "test-alias") } + .apply { this += message("test-message3", Direction.FIRST, "test-alias") } ).build() ) verify(connectionManager, never()).basicPublish(any(), any(), anyOrNull(), any()) @@ -317,9 +325,9 @@ class TestRabbitMessageGroupBatchRouter { router.send( MessageGroupBatch.newBuilder() .addGroups(MessageGroup.newBuilder() - .apply { this += message("test-message1", Direction.FIRST, "test-alias1") } - .apply { this += message("test-message", Direction.FIRST, "test-alias2") } - .apply { this += message("test-message3", Direction.FIRST, "test-alias3") } + .apply { this += message("test-message1", Direction.FIRST, "test-alias") } + .apply { this += message("test-message", Direction.FIRST, "test-alias") } + .apply { this += message("test-message3", Direction.FIRST, "test-alias") } ).build() ) verify(connectionManager, times(1)).basicPublish(any(), any(), anyOrNull(), any()) From 8f44ca47b6c7b6b542f3ba0514f8d1f60458a58c Mon Sep 17 00:00:00 2001 From: Rekish Date: Fri, 20 May 2022 16:06:31 +0400 Subject: [PATCH 14/16] added protocol field to metadata, added according tests --- .../impl/AbstractTh2MsgFilterStrategy.java | 4 +- .../strategy/impl/AnyMessageFilterStrategy.kt | 5 +- .../impl/TestAnyMessageFilterStrategy.kt | 184 +++++++++++++++--- 3 files changed, 162 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java index b48b52789..c954cd073 100644 --- a/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java +++ b/src/main/java/com/exactpro/th2/common/schema/filter/strategy/impl/AbstractTh2MsgFilterStrategy.java @@ -29,6 +29,7 @@ public abstract class AbstractTh2MsgFilterStrategy extends AbstractFilterStrateg public static final String SESSION_ALIAS_KEY = "session_alias"; public static final String MESSAGE_TYPE_KEY = "message_type"; public static final String DIRECTION_KEY = "direction"; + public static final String PROTOCOL_KEY = "protocol"; @Override public Map getFields(com.google.protobuf.Message message) { @@ -44,7 +45,8 @@ public Map getFields(com.google.protobuf.Message message) { var metadataMsgFields = Map.of( SESSION_ALIAS_KEY, messageID.getConnectionId().getSessionAlias(), MESSAGE_TYPE_KEY, metadata.getMessageType(), - DIRECTION_KEY, messageID.getDirection().name() + DIRECTION_KEY, messageID.getDirection().name(), + PROTOCOL_KEY, metadata.getProtocol() ); messageFields.putAll(th2Msg.getMetadata().getPropertiesMap()); diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt index 911d026f9..2a9611ae9 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt @@ -22,6 +22,7 @@ import com.google.protobuf.Message class AnyMessageFilterStrategy : AbstractFilterStrategy() { + // FIXME data in a fields with the same name rewrite each other, so data get lost override fun getFields(message: Message): MutableMap { check(message is AnyMessage) { "Message is not an ${AnyMessage::class.qualifiedName}: ${message.toJson()}" } @@ -32,16 +33,18 @@ class AnyMessageFilterStrategy : AbstractFilterStrategy() { result.putAll(message.message.fieldsMap.mapValues { it.value.simpleValue }) val metadata = message.message.metadata - result.putAll(message.message.metadata.propertiesMap) + result.putAll(metadata.propertiesMap) result[AbstractTh2MsgFilterStrategy.SESSION_ALIAS_KEY] = metadata.id.connectionId.sessionAlias result[AbstractTh2MsgFilterStrategy.MESSAGE_TYPE_KEY] = metadata.messageType result[AbstractTh2MsgFilterStrategy.DIRECTION_KEY] = metadata.id.direction.name + result[AbstractTh2MsgFilterStrategy.PROTOCOL_KEY] = metadata.protocol } message.hasRawMessage() -> { val metadata = message.rawMessage.metadata result.putAll(message.rawMessage.metadata.propertiesMap) result[AbstractTh2MsgFilterStrategy.SESSION_ALIAS_KEY] = metadata.id.connectionId.sessionAlias result[AbstractTh2MsgFilterStrategy.DIRECTION_KEY] = metadata.id.direction.name + result[AbstractTh2MsgFilterStrategy.PROTOCOL_KEY] = metadata.protocol } else -> throw IllegalStateException("Message has not messages: ${message.toJson()}") } diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt index 27c4593ec..13bf9e5a6 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/TestAnyMessageFilterStrategy.kt @@ -26,7 +26,9 @@ import com.exactpro.th2.common.schema.message.configuration.FieldFilterConfigura import com.exactpro.th2.common.schema.message.configuration.FieldFilterOperation import com.exactpro.th2.common.schema.message.configuration.MqRouterFilterConfiguration import org.apache.commons.collections4.MultiMapUtils +import org.junit.Ignore import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Disabled import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.Arguments.arguments @@ -110,6 +112,29 @@ class TestAnyMessageFilterStrategy { assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } } + @ParameterizedTest + @MethodSource("messagesWithProperties") + fun `matches with multiple properties filters`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + properties = MultiMapUtils.newListValuedHashMap().apply { + put("prop-field1", FieldFilterConfiguration( + fieldName = "prop-field1", + operation = FieldFilterOperation.EQUAL, + expectedValue = "prop-value1" + )) + put("prop-field2", FieldFilterConfiguration( + fieldName = "prop-field2", + operation = FieldFilterOperation.EQUAL, + expectedValue = "prop-value2" + )) + } + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + @ParameterizedTest @MethodSource("messagesWithMessageAndMetadataFilters") fun `matches with multiple message and metadata filters`(anyMessage: AnyMessage, expectMatch: Boolean) { @@ -145,6 +170,59 @@ class TestAnyMessageFilterStrategy { assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } } + @ParameterizedTest + @MethodSource("messageWithAllParts") + @Disabled + // FIXME data in a fields with the same name rewrite each other, so data get lost + fun `matches with message and metadata and property same field filters`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + message = MultiMapUtils.newListValuedHashMap().apply { + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", + operation = FieldFilterOperation.EQUAL, + expectedValue = "message_value" + )) + }, + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", + operation = FieldFilterOperation.EQUAL, + expectedValue = "metadata_value" + )) + }, + properties = MultiMapUtils.newListValuedHashMap().apply { + put("message_type", FieldFilterConfiguration( + fieldName = "message_type", + operation = FieldFilterOperation.EQUAL, + expectedValue = "properties_value" + )) + }, + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + } + + @ParameterizedTest + @MethodSource("messageWithProtocol") + fun `matches protocol metadata filter`(anyMessage: AnyMessage, expectMatch: Boolean) { + val match = strategy.verify( + anyMessage, + MqRouterFilterConfiguration( + metadata = MultiMapUtils.newListValuedHashMap().apply { + put("protocol", FieldFilterConfiguration( + fieldName = "protocol", + operation = FieldFilterOperation.EQUAL, + expectedValue = "HTTP" + )) + }, + ) + ) + assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" } + + } + @ParameterizedTest @MethodSource("parsedMessages") fun `matches the parsed message by message type with single filter`(anyMessage: AnyMessage, expectMatch: Boolean) { @@ -300,7 +378,7 @@ class TestAnyMessageFilterStrategy { } @ParameterizedTest - @MethodSource("messagesWithProperties") + @MethodSource("messagesWithOneProperty") fun `matches message with properties`(message: AnyMessage, expectMatch: Boolean) { val match = strategy.verify( message, @@ -373,7 +451,7 @@ class TestAnyMessageFilterStrategy { ).build() } - private fun messageWithFields(messageType: String, direction: Direction, fields: List>): AnyMessage { + private fun messageWithFieldsBuilder(messageType: String, direction: Direction, fields: List>): AnyMessage { return AnyMessage.newBuilder().setMessage( message(messageType, direction, "test-alias").apply { fields.forEach { addField(it.first, it.second) } @@ -381,7 +459,15 @@ class TestAnyMessageFilterStrategy { ).build() } - private fun rawMessageWithOneProperty(propertyKey: String, propertyValue: String): AnyMessage { + private fun messageWithPropertiesBuilder(messageType: String, direction: Direction, properties: List>): AnyMessage { + return AnyMessage.newBuilder().setMessage( + message(messageType, direction, "test-alias").apply { + properties.forEach { metadataBuilder.putProperties(it.first, it.second) } + } + ).build() + } + + private fun rawMessageWithOnePropertyBuilder(propertyKey: String, propertyValue: String): AnyMessage { return AnyMessage.newBuilder().setRawMessage( RawMessage.newBuilder().apply { metadataBuilder.putProperties(propertyKey, propertyValue) @@ -389,14 +475,33 @@ class TestAnyMessageFilterStrategy { ).build() } - private fun messageWithOneProperty(messageType: String, propertyKey: String, propertyValue: String): AnyMessage { + private fun messageWithOnePropertyBuilder(messageType: String, propertyKey: String, propertyValue: String): AnyMessage { + return messageWithPropertiesBuilder(messageType, Direction.FIRST, listOf(Pair(propertyKey, propertyValue))) + } + + private fun messageWithAllPartsBuilder(messageType: String, propertyKey: String, propertyValue: String, fieldKey: String, fieldValue: String): AnyMessage { return AnyMessage.newBuilder().setMessage( message(messageType, Direction.FIRST, "test-alias").apply { metadataBuilder.putProperties(propertyKey, propertyValue) + addField(fieldKey, fieldValue) + } + ).build() + } + + private fun messageWithProtocolBuilder(protocol: String): AnyMessage { + return AnyMessage.newBuilder().setMessage( + message("test", Direction.FIRST, "test-alias").apply { + metadataBuilder.protocol = protocol } ).build() } + @JvmStatic + fun messageWithProtocol(): List = listOf( + arguments(messageWithProtocolBuilder("HTTP"), true), + arguments(messageWithProtocolBuilder("FTP"), false), + ) + @JvmStatic fun messages(): List = listOf( arguments(simpleRawMessageBuilder("test-alias", Direction.FIRST), true), @@ -409,22 +514,34 @@ class TestAnyMessageFilterStrategy { arguments(simpleMessageBuilder("test1", Direction.SECOND, "test-alias1"), false) ) + @JvmStatic + fun messagesWithProperties(): List = listOf( + arguments(messageWithPropertiesBuilder("test", Direction.FIRST, listOf( + Pair("prop-field1", "prop-value1"), Pair("prop-field2", "prop-value2"))), true), + arguments(messageWithPropertiesBuilder("test", Direction.FIRST, listOf( + Pair("prop-field1", "prop-value-wrong"), Pair("prop-field2", "prop-value2"))), false), + arguments(messageWithPropertiesBuilder("test", Direction.FIRST, listOf( + Pair("prop-field1", "prop-value1"), Pair("prop-field2", "prop-value-wrong"))), false), + arguments(messageWithPropertiesBuilder("test", Direction.FIRST, listOf( + Pair("prop-field1", "prop-value-wrong"), Pair("prop-field2", "prop-value-wrong"))), false) + ) + @JvmStatic fun messagesWithMultipleSameFields(): List = listOf( - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"), Pair("test-field", "test-value2"))), true), ) @JvmStatic fun messagesWithSameFilterFields(): List = listOf( - arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), false), - arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), false), + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), false), + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), false), ) @JvmStatic fun messagesWithMultipleFiltersWithSameFilterField(): List = listOf( - arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), true), - arguments(messageWithFields("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), true), + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field", "test-value1"))), true), + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field", "test-value2"))), true), ) @JvmStatic @@ -438,56 +555,56 @@ class TestAnyMessageFilterStrategy { @JvmStatic fun messagesWithMessageAndMetadataFilters() : List = listOf( // fields full match - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), true), // metadata mismatch - arguments(messageWithFields("test", Direction.SECOND, + arguments(messageWithFieldsBuilder("test", Direction.SECOND, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), false), - arguments(messageWithFields("test-wrong", Direction.FIRST, + arguments(messageWithFieldsBuilder("test-wrong", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), false), // fields mismatch - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value-wrong"))), false), // one field and one metadata mismatch - arguments(messageWithFields("test", Direction.SECOND, + arguments(messageWithFieldsBuilder("test", Direction.SECOND, listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), - arguments(messageWithFields("test-wrong", Direction.FIRST, + arguments(messageWithFieldsBuilder("test-wrong", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), ) @JvmStatic fun parsedMessagesBothFilters() : List = listOf( - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value2"))), true), - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value2"))), false), - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value1"), Pair("test-field2", "test-value-wrong"))), false), - arguments(messageWithFields("test", Direction.FIRST, + arguments(messageWithFieldsBuilder("test", Direction.FIRST, listOf(Pair("test-field1", "test-value-wrong"), Pair("test-field2", "test-value-wrong"))), false), ) @JvmStatic - fun messagesWithProperties() : List = listOf( - arguments(messageWithOneProperty("test", "test-property", "property-value"), true), - arguments(messageWithOneProperty("test", "test-property", "property-value-wrong"), false), - arguments(rawMessageWithOneProperty("test-property", "property-value"), true), - arguments(rawMessageWithOneProperty("test-property", "property-value-wrong"), false) + fun messagesWithOneProperty() : List = listOf( + arguments(messageWithOnePropertyBuilder("test", "test-property", "property-value"), true), + arguments(messageWithOnePropertyBuilder("test", "test-property", "property-value-wrong"), false), + arguments(rawMessageWithOnePropertyBuilder("test-property", "property-value"), true), + arguments(rawMessageWithOnePropertyBuilder("test-property", "property-value-wrong"), false) ) @JvmStatic fun messagesWithPropertiesAndMetadata() : List = listOf( - arguments(messageWithOneProperty("test", "test-property", "property-value"), true), - arguments(messageWithOneProperty("test", "test-property", "property-value-wrong"), false), - arguments(messageWithOneProperty("test-wrong", "test-property", "property-value"), false), + arguments(messageWithOnePropertyBuilder("test", "test-property", "property-value"), true), + arguments(messageWithOnePropertyBuilder("test", "test-property", "property-value-wrong"), false), + arguments(messageWithOnePropertyBuilder("test-wrong", "test-property", "property-value"), false), ) @JvmStatic @@ -497,5 +614,14 @@ class TestAnyMessageFilterStrategy { arguments(simpleRawMessageBuilder("test-alias-wrong-value", Direction.SECOND), false), ) + @JvmStatic + fun messageWithAllParts() : List = listOf( + arguments(messageWithAllPartsBuilder("message_type", "message_type", "properties_value", "message_type", "message_value"), true), + arguments(simpleMessageBuilder("metadata_value", Direction.FIRST, "test-alias"), false), + arguments(simpleMessageBuilder("metadata_value1", Direction.FIRST, "test-alias"), false), + arguments(simpleMessageBuilder("message_value", Direction.FIRST, "test-alias"), false), + + ) + } } \ No newline at end of file From 04a6ff87b0a3efa5ecd48c5285cc9aa2bfe850f9 Mon Sep 17 00:00:00 2001 From: Rekish Date: Mon, 23 May 2022 16:11:43 +0400 Subject: [PATCH 15/16] [TH2-3673] run ci --- .../schema/filter/strategy/impl/AnyMessageFilterStrategy.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt index 2a9611ae9..70e958b95 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/filter/strategy/impl/AnyMessageFilterStrategy.kt @@ -22,7 +22,7 @@ import com.google.protobuf.Message class AnyMessageFilterStrategy : AbstractFilterStrategy() { - // FIXME data in a fields with the same name rewrite each other, so data get lost + // FIXME data in a fields with the same name rewrite each other, so data can get lost override fun getFields(message: Message): MutableMap { check(message is AnyMessage) { "Message is not an ${AnyMessage::class.qualifiedName}: ${message.toJson()}" } From 4d26cee166edfa8855f2cc9f731e4ff9344ffc2c Mon Sep 17 00:00:00 2001 From: Rekish Date: Mon, 25 Jul 2022 15:30:23 +0400 Subject: [PATCH 16/16] [TH2-3673] added aliases for message filter configuration --- .../message/configuration/MessageRouterConfiguration.kt | 4 ++-- .../resources/test_json_configurations/message_router.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt index 5c4307f09..62774fa56 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/message/configuration/MessageRouterConfiguration.kt @@ -89,8 +89,8 @@ data class FieldFilterConfigurationOld( ) : Configuration() data class FieldFilterConfiguration( - @JsonProperty(value = "fieldName", required = true) var fieldName: String, - @JsonProperty("expectedValue") @JsonAlias("value") var expectedValue: String?, + @JsonProperty(value = "fieldName", required = true) @JsonAlias("field-name") var fieldName: String, + @JsonProperty("expectedValue") @JsonAlias("value", "expected-value") var expectedValue: String?, @JsonProperty(required = true) var operation: FieldFilterOperation ) : Configuration() diff --git a/src/test/resources/test_json_configurations/message_router.json b/src/test/resources/test_json_configurations/message_router.json index 458f8fc06..4f70c1c3e 100644 --- a/src/test/resources/test_json_configurations/message_router.json +++ b/src/test/resources/test_json_configurations/message_router.json @@ -36,8 +36,8 @@ ], "properties": [ { - "fieldName": "method1", - "value": "POST", + "field-name": "method1", + "expected-value": "POST", "operation": "EQUAL" }, {