Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Th2 3673 common need to change the filter #201

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RabbitMessageGroupBatchRouter : AbstractRabbitRouter<MessageGroupBatch>()

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -354,41 +361,37 @@ 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<Arguments> = 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<Arguments> = 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),
)

///////////////

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<Arguments> = 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.FIRST, "test-wrong"), true),
arguments(simpleMessageBuilder("test", Direction.SECOND, "test-alias-wrong"), false),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this case, here we can see:

  • simpleMessageBuilder - the base structure of the target message
  • "test", SECOND, "test-alias-wrong" - arguments

Could you please write others methods in a similar style

)

/////////////////
Expand All @@ -400,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<Arguments> = 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),
)

////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid multiple imports. You can configure idea for that


class TestRabbitMessageGroupBatchRouter {
private val connectionConfiguration = ConnectionManagerConfiguration()
Expand Down Expand Up @@ -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") }
Nikita-Smirnov-Exactpro marked this conversation as resolved.
Show resolved Hide resolved
.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<String, QueueConfiguration>): MessageRouter<MessageGroupBatch> =
RabbitMessageGroupBatchRouter().apply {
init(DefaultMessageRouterContext(
Expand Down