hello guys, i am using `valueFromProviderState` s...
# pact-jvm
c
hello guys, i am using
valueFromProviderState
syntax in a test and i was wondering if there is any trick to apply a function on the value return by the state provider i.e the state populates the map with
userId
in the test to use something like
Copy code
newJsonBody((payload) ->
                payload.valueFromProviderState("id", () -> Base64.encode("${userId}"), "MTIzCg=="))
                .build()
what i am trying to achieve is to avoid exposing same value from provider state with many variation (userId, userId in base64 and so on). please also suggest other way to achieve this
u
The provider state callback is the place to do it, in the consumer test you use an example value, and then you can replace it with whatever you need in the provider state callback
c
@uglyog thank you for your response but i am not sure i follow, i invoke the provider state callback (
valueFromProviderState("id", "${userId}", "whatever")
) but i would like to be able to manipulate/change the injected value of userId (for example, encode it)
can you give me an example on how to achieve that?
u
valueFromProviderState("id", "${userId}", "whatever")
does not invoke anything, it configures things so that when provider state callback is invoked on the provider side, it will use the values returned from that instead of the example one. On the consumer side, it uses the example value.
So, coming back to your question, if you want to base 64 encode things, in the consumer test, you base 64 encode the example value (
whatever
), and then, in the provider verification test, you base 64 encode the returned
userId
value in the provider state callback.
c
got it, thank you. the goal was to avoid exposing flavours for the same value (
userId
) in the provider state callback. i managed to do this by adding a new generator to
LambdaDslJsonBody
which works almost the same as
ProviderStateGenerator
but before returning the value, applies a function which is provided as param. so the provider state callback does not duplicate returned values (with different flavours). not sure if would be widely used but seems like a simple addition to
ProviderStateGenerator
and
valueFromProviderState
i.e. default no op function as param for
valueFromProviderState
and
ProviderStateGenerator
class attribute which would be applied on value returned by
Copy code
this.ep.containsExpressions(this.expression, true) 
? this.ep.parseExpression(this.expression, this.dataType, (ValueResolver)(new MapValueResolver(map)), true) 
: map.get(this.expression);
and on
example
value