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

pact-jvm-consumer: Message should return metadata as Map<String, Object> #1006

Closed
msa-itsonix opened this issue Jan 24, 2020 · 3 comments
Closed
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@msa-itsonix
Copy link

I am using au.com.dius:pact-jvm-consumer-junit5 in version 4.0.4.

When defining a pact with the MessagePactBuilder dsl, you can provide metadata through withMetadata giving a Map<String, Object>, where Object can be a raw value, a Matcher or a Generator. But the resulting Message returns its metadata as Map<String, String>.

@Pact(consumer = "MyConsumer")
public MessagePact somethingHappend(final MessagePactBuilder builder) {

	final Map<String, Object> expectedMetadata = new HashMap<>();
	expectedMetadata.put("myLongProperty", 10L);

	return builder.given("something happend")
			.expectsToReceive("MyEvent")
			.withMetadata(expectedMetadata)
			.toPact();
}

@Test
public void myEventIsReceived(final List<Message> messages) {
	final Message message = Iterables.getOnlyElement(messages);
	final Map<String, String> metaData = message.getMetaData();
	// expected Map<String, Object> here, but this contains the String "10"
	// for key "myLongProperty" => no chance to convert it back
}

This is a problem when integrating with messaging frameworks that also provide metadata with raw Object values instead of String (like IMessage of azure servicebus in my case) because you can't translate the pact Message to such a message representation then (at least not with hard workarounds.)

Shouldn't Message just return the raw metadata. To keep the API compatible I would propose to add something like getRawMetadata that returns Map<String, Object>.

@uglyog uglyog added the bug Indicates an unexpected problem or unintended behavior label Jan 25, 2020
@uglyog
Copy link
Member

uglyog commented Jan 26, 2020

Version 4.0.5 has been released with this change

@msa-itsonix
Copy link
Author

Thanks for the fast reply.
The map's signature now changed to Map<String, Object> in 4.0.5 but the contained value is still a String. Running the following test (with au.com.dius:pact-jvm-consumer-junit5:4.0.5) will fail:

@ExtendWith(PactConsumerTestExt.class)
public class MyTest {

	@Nested
	@PactTestFor(providerName = "MyProvider", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V3)
	public class GivenSomethingHappend{

		@Pact(consumer = "MyConsumer")
		public MessagePact somethingHappend(final MessagePactBuilder builder) {

			final Map<String, Object> expectedMetadata = new HashMap<>();
			expectedMetadata.put("myLongProperty", 10L);

			return builder.given("something happend")
					.expectsToReceive("MyEvent")
					.withMetadata(expectedMetadata)
					.toPact();
		}

		@Test
		public void myEventIsReceived(final List<Message> messages) {
			final Message message = messages.get(0);
			final Map<String, Object> metaData = message.getMetaData();
			final Object exectedLongProperty = metaData.get("myLongProperty");
			assertTrue(Long.class.isAssignableFrom(exectedLongProperty.getClass()));
		}
	}
}

I also noticed that on the provider side the tests will also fail when an event is produced containing the expected metadata property as long with an error message indicating the consumer expected a string, which is not correct here. Looking at the generated json of the pact, one can see that the expected metadata is also contained as string although it should be a json number:

{
  "consumer": {
    "name": "MyConsumer"
  },
  "provider": {
    "name": "MyProvider"
  },
  "messages": [
    {
      "description": "MyEvent",
      "metaData": {
        "myLongProperty": "10"
      },
      "providerStates": [
        {
          "name": "something happend"
        }
      ]
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "4.0.5"
    }
  }
}

@uglyog
Copy link
Member

uglyog commented Feb 22, 2020

4.0.6 has been released with the updated fix

@uglyog uglyog closed this as completed May 7, 2020
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