From 0da32ec3a9cbf3a70bd5e42cfe4bdd10fa37691b Mon Sep 17 00:00:00 2001 From: "ilya.aliaksandrovich" Date: Fri, 24 Feb 2023 17:32:22 +0100 Subject: [PATCH] fix merging message pacts - old messages taking precedence --- .../pact/core/model/messaging/MessagePact.kt | 2 +- .../model/messaging/MessagePactSpec.groovy | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt index 16bb72358f..18e0b876d3 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/messaging/MessagePact.kt @@ -70,7 +70,7 @@ class MessagePact @JvmOverloads constructor ( override fun mergeInteractions(interactions: List): Pact { interactions as List - messages = (messages + interactions).distinctBy { it.uniqueKey() }.toMutableList() + messages = (interactions + messages).distinctBy { it.uniqueKey() }.toMutableList() sortInteractions() return this } diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/messaging/MessagePactSpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/messaging/MessagePactSpec.groovy index e3d59e1134..b3725888ff 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/messaging/MessagePactSpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/messaging/MessagePactSpec.groovy @@ -85,4 +85,31 @@ class MessagePactSpec extends Specification { pact2 = new MessagePact(provider, consumer, [ message, message2 ]) } + def 'merge message pact'() { + when: + pact.mergeInteractions(pact2.interactions) + + then: + pact.interactions == [ newMessage1, message2 ] + + where: + newMessage1 = new Message('message', [], OptionalBody.body('0 9 8 7'.bytes)) + message2 = new Message('message2', [], OptionalBody.body('A B C'.bytes)) + pact = new MessagePact(provider, consumer, [ message ]) + pact2 = new MessagePact(provider, consumer, [ newMessage1, message2 ]) + } + + + def 'merge message pact - same'() { + when: + pact.mergeInteractions(pact2.interactions) + + then: + pact.interactions == [ message ] + + where: + pact = new MessagePact(provider, consumer, [ message ]) + pact2 = new MessagePact(provider, consumer, [ message ]) + } + }