Skip to content

Commit

Permalink
Merge pull request #143 from th2-net/th2-2677
Browse files Browse the repository at this point in the history
[TH2-2677] Fix not working filtering by `message_type` for group batch routers
  • Loading branch information
OptimumCode authored Nov 16, 2021
2 parents 9f81079 + 10d55b0 commit 54c3e93
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (3.29.0)
# th2 common library (Java) (3.29.1)

## Usage

Expand Down Expand Up @@ -274,6 +274,10 @@ EVENTS METRICS:

## Release notes

### 3.29.1

+ Fix problem with filtering by `message_type` in MessageGroupBatch router

### 3.29.0

+ Update Cradle version from `2.13.0` to `2.20.0`
Expand Down
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ targetCompatibility = 11

ext {
cradleVersion = '2.20.0'
junitVersion = '5.4.2'
junitVersion = '5.7.2'
sharedDir = file("${project.rootDir}/shared")
}

Expand Down Expand Up @@ -201,9 +201,7 @@ dependencies {

implementation 'io.fabric8:kubernetes-client:4.13.0'

testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter:${junitVersion}"
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

release_version=3.29.0
release_version=3.29.1

description = 'th2 common library (Java)'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AnyMessageFilterStrategy : AbstractFilterStrategy<Message>() {

val metadata = message.message.metadata
result[AbstractTh2MsgFilterStrategy.SESSION_ALIAS_KEY] = metadata.id.connectionId.sessionAlias
result[AbstractTh2MsgFilterStrategy.MESSAGE_TYPE_KEY] = message.message.descriptorForType.name
result[AbstractTh2MsgFilterStrategy.MESSAGE_TYPE_KEY] = metadata.messageType
result[AbstractTh2MsgFilterStrategy.DIRECTION_KEY] = metadata.id.direction.name
}
message.hasRawMessage() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2021 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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.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("parsedMessages")
fun `matches the parsed message by message type with single filter`(anyMessage: AnyMessage, expectMatch: Boolean) {
val match = strategy.verify(
anyMessage,
MqRouterFilterConfiguration(
metadata = MultiMapUtils.newListValuedHashMap<String, FieldFilterConfiguration>().apply {
put("message_type", FieldFilterConfiguration(
fieldName = "message_type",
operation = FieldFilterOperation.EQUAL,
expectedValue = "test"
))
}
))

assertEquals(expectMatch, match) { "The message ${anyMessage.toJson()} was${if (expectMatch) "" else " not"} matched" }
}

@ParameterizedTest
@MethodSource("messages")
fun `matches the parsed message by direction with single filter`(message: AnyMessage, expectMatch: Boolean) {
val match = strategy.verify(
message,
MqRouterFilterConfiguration(
metadata = MultiMapUtils.newListValuedHashMap<String, FieldFilterConfiguration>().apply {
put("direction", FieldFilterConfiguration(
fieldName = "direction",
operation = FieldFilterOperation.EQUAL,
expectedValue = "FIRST"
))
}
))

assertEquals(expectMatch, match) { "The message ${message.toJson()} was${if (expectMatch) "" else " not"} matched" }
}

@ParameterizedTest
@MethodSource("messages")
fun `matches the parsed message by alias with single filter`(message: AnyMessage, expectMatch: Boolean) {
val match = strategy.verify(
message,
MqRouterFilterConfiguration(
metadata = MultiMapUtils.newListValuedHashMap<String, FieldFilterConfiguration>().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 val PARSED_MESSAGE_MATCH = AnyMessage.newBuilder().setMessage(
message("test", Direction.FIRST, "test-alias")
).build()

private val RAW_MESSAGE_MATCH = AnyMessage.newBuilder().setRawMessage(
RawMessage.newBuilder().apply {
metadataBuilder.idBuilder.apply {
connectionIdBuilder.sessionAlias = "test-alias"
direction = Direction.FIRST
}
}
).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()

@JvmStatic
fun parsedMessages(): List<Arguments> = listOf(
arguments(PARSED_MESSAGE_MATCH, true),
arguments(PARSED_MESSAGE_MISS_MATCH, false)
)

@JvmStatic
fun messages(): List<Arguments> = listOf(
arguments(RAW_MESSAGE_MATCH, true),
arguments(RAW_MESSAGE_MISS_MATCH, false)
) + parsedMessages()
}
}

0 comments on commit 54c3e93

Please sign in to comment.