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

Matching broken after updating from 4.1.* to 4.2.* #1347

Closed
pawelryznar opened this issue Apr 20, 2021 · 4 comments
Closed

Matching broken after updating from 4.1.* to 4.2.* #1347

pawelryznar opened this issue Apr 20, 2021 · 4 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@pawelryznar
Copy link

pawelryznar commented Apr 20, 2021

After upgrading from pact jvm 4.1.3 to 4.2.4 and regenerating files on the consumer side, provider tests are not working anymore for me.
Complete stack Spring Boot 2.4.5, Kotlin 1.4.32 - for both consumer and producer

I'm getting errors like

1) Verifying a pact between my-consumer and my-provider - testing pact has a matching body

    1.1) body: $.field3.nested1.0.value1 Expected "1st test value" (String) to equal 102 (Integer)

    1.2) body: $.field3.nested1.0.value2 Expected 99 (Integer) to equal 102 (Integer)

    1.3) body: $.field3.nested1.2.value1 Expected "2nd test value" (String) to equal 102 (Integer)

    1.4) body: $.field3.nested1.2.value2 Expected 98 (Integer) to equal 102 (Integer)

Tests were working for this pact:

{
    "provider": {
        "name": "my-provider"
    },
    "consumer": {
        "name": "my-consumer"
    },
    "interactions": [
        {
            "description": "testing pact",
            "request": {
                "method": "GET",
                "path": "/myapp/test"
            },
            "response": {
                "status": 200,
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": {
                    "field1": "test string",
                    "field2": false,
                    "field3": {
                        "nested1": {
                            "0": {
                                "value3": 102
                            }
                        }
                    },
                    "field4": 50
                },
                "matchingRules": {
                    "body": {
                        "$.field4": {
                            "matchers": [
                                {
                                    "match": "number"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$.field3.nested1.*": {
                            "matchers": [
                                {
                                    "match": "type"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$.field3.nested1.*.value3": {
                            "matchers": [
                                {
                                    "match": "number"
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }
            },
            "providerStates": [
                {
                    "name": "my test"
                }
            ]
        }
    ],
    "metadata": {
        "pactSpecification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "4.1.3"
        }
    }
}

After the update pact changed slightly (for $.field3.nested1 matcher from type to values, and also wildcard got removed)

{
    "provider": {
        "name": "my-provider"
    },
    "consumer": {
        "name": "my-consumer"
    },
    "interactions": [
        {
            "description": "testing pact",
            "request": {
                "method": "GET",
                "path": "/myapp/test"
            },
            "response": {
                "status": 200,
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": {
                    "field1": "test string",
                    "field2": false,
                    "field3": {
                        "nested1": {
                            "0": {
                                "value3": 102
                            }
                        }
                    },
                    "field4": 50
                },
                "matchingRules": {
                    "body": {
                        "$.field4": {
                            "matchers": [
                                {
                                    "match": "number"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$.field3.nested1": {
                            "matchers": [
                                {
                                    "match": "values"
                                }
                            ],
                            "combine": "AND"
                        },
                        "$.field3.nested1.*.value3": {
                            "matchers": [
                                {
                                    "match": "number"
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }
            },
            "providerStates": [
                {
                    "name": "my test"
                }
            ]
        }
    ],
    "metadata": {
        "pactSpecification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "4.2.4"
        }
    }
}

Model:

data class TestValues(
    val value1: String,
    val value2: Int,
    val value3: BigDecimal
)

data class TestCustom(
    val nested1: Map<String, TestValues>
)

data class TestModel(
    val field1: String,
    val field2: Boolean,
    val field3: TestCustom,
    val field4: Int,
)

Controller:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Provider("my-provider")
@PactFolder("pacts")
@VerificationReports(value = ["console", "markdown"], reportDir = "build/pact/reports")
class MyPactTest {

    @MockkBean
    private lateinit var myService: myService

    @LocalServerPort
    private var randomServerPort = 0

    @WithMockCustomPrincipal
    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider::class)
    internal fun testTemplate(context: PactVerificationContext) {
        context.verifyInteraction()
    }

    @BeforeEach
    @Throws(MalformedURLException::class)
    internal fun before(context: PactVerificationContext) {
        context.target = HttpTestTarget(port = randomServerPort)
    }
	
    @State("my test")
    fun `my test`() {
        every {
            myService.getTestModel()
        } returns TestModel(
            field1 = "test string",
            field2 = false,
            field3 = TestCustom(
                nested1 = mapOf(
                    "0" to TestValues(
                        value1 = "1st test value",
                        value2 = 99,
                        value3 = BigDecimal.valueOf(100)
                    ),
                    "2" to TestValues(
                        value1 = "2nd test value",
                        value2 = 98,
                        value3 = BigDecimal.valueOf(102)
                    )
                )
            ),
            field4 = 50
        )
    }
}
@uglyog
Copy link
Member

uglyog commented May 1, 2021

Can you provide the consumer test?

4.2.x introduced a matcher for matching the values in the map (i.e. the values matcher in your second pact) and the previous logic was removed. Looks like a defect with the values matcher, as it is comparing all the values, not just the ones for value3.

@uglyog uglyog added the bug Indicates an unexpected problem or unintended behavior label May 1, 2021
@uglyog
Copy link
Member

uglyog commented May 1, 2021

Actually, don't worry about the consumer test, I have reproduced it with what you have given.

uglyog pushed a commit that referenced this issue May 1, 2021
@uglyog
Copy link
Member

uglyog commented May 9, 2021

4.2.5 has been released

@pawelryznar
Copy link
Author

Great, it works now. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants