我正在学习如何在我的 Java 项目中使用 PACT,并且我想对预期输出定义一些值限制。例如,在一个请求/hello-world
中,我希望在 id 属性中接收一个数字,该数字应该始终大于零。
package com.thiagomata.pact.hello.consumer.consumer;
import au.com.dius.pact.consumer.ConsumerPactBuilder;
import au.com.dius.pact.consumer.PactVerificationResult;
import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.model.MockProviderConfig;
import au.com.dius.pact.model.RequestResponsePact;
import com.thiagomata.pact.hello.consumer.models.Greeting;
import io.pactfoundation.consumer.dsl.LambdaDslJsonBody;
import org.junit.Assert;
import org.junit.Test;
import scala.tools.jline_embedded.internal.Log;
import static au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest;
import static org.junit.Assert.assertEquals;
public class NameApplicationPactTest {
@Test
public void testNamePact() throws Throwable {
Log.debug("inside the test");
/**
* Creating the mock server
*
* Define the expected input
* Using relative address
* The provider address will be automatically created
* The provider port will be automatically created
* Define the expected output
* Keep the id as a undefined integer
* Set the content to the test
*/
RequestResponsePact pact = ConsumerPactBuilder
.consumer("hello_world_consumer")
.hasPactWith("hello_world_provider")
.uponReceiving("a request of hello world")
.path("/hello-world")
.matchQuery("name","johny")
.method("GET")
.willRespondWith()
.body(
newJsonBody( (LambdaDslJsonBody o) -> o.
numberType("id"). // <====================================
stringType("content", "Hello johny")
).build()
)
.toPact();
/**
* Let the Pact define the mock server address and port
*/
MockProviderConfig config = MockProviderConfig.createDefault();
/**
* Create the mock server into the defined config and with the
* pact result prepared
*/
PactVerificationResult result = runConsumerTest(
pact,
config,
mockServer -> {
Log.debug("inside mock server");
/**
* Pass the mock server configuration to the consumer classes
*/
DummyConsumer consumer = new DummyConsumer(
mockServer.getUrl(),
mockServer.getPort(),
"johny"
);
/**
* Now, when the code internally fires to the
* mockServer we should get the expected answer
*/
Greeting greeting = consumer.getGreeting();
Log.debug(greeting);
Assert.assertNotNull(
"Greeting id should not be null",
greeting.getId()
);
/**
* Currently I am not able to define a rule into the
* DSL Matching methods to assure that the value should
* be bigger than 0
*/
Assert.assertTrue( greeting.getId() > 0 ); // <=================================================
assertEquals(
"Validate expected default greeting content",
"Hello johny",
greeting.getContent()
);
Log.debug("status code = " + consumer.getStatusCode() );
Assert.assertTrue(
"test consumer status code",
consumer.getStatusCode().equals(
200
)
);
}
);
/**
* If some Assert inside of the anonymous functions fails
* it will not automatically throw a failure.
*
* We need to capture the error from the result
*/
if (result instanceof PactVerificationResult.Error) {
throw ((PactVerificationResult.Error) result).getError();
}
assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
}
有人可能会说 PACT 它无法应用这样的限制。但是,查看生成的 PACT,看起来为生成器创建最小值和最大值应该是 PACT 的一种可能:
{
"provider": {
"name": "hello_world_provider"
},
"consumer": {
"name": "hello_world_consumer"
},
"interactions": [
{
"description": "Test User Service",
"request": {
"method": "GET",
"path": "/hello-world"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json",
"Content-Type": "application/json; charset\u003dUTF-8"
},
"body": {
"id": 100,
"content": "string"
},
"matchingRules": {
"body": {
"$.id": {
"matchers": [
{
"match": "integer"
}
],
"combine": "AND"
},
"$.content": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
}
},
"generators": {
"body": {
"$.id": {
"type": "RandomInt",
"min": 0, /* <======================================== */
"max": 2147483647
},
"$.content": {
"type": "RandomString",
"size": 20
}
}
}
},
"providerStates": [
{
"name": "default"
}
]
}
],
"metadata": {
"pact-specification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "3.5.10"
}
}
}
我试图找到一些方法来做到这一点,查看 PACT 代码。因此,按照numberType
方法的轨迹, LambdaDsl
:
/* ... */
public LambdaDslObject numberType(final String... names) {
object.numberType(names);
return this;
}
/* ... */
该方法使用以下可能的方法调用object.numberTypes
它:LambdaDslJsonBody
/**
* Attribute that can be any number
* @param name attribute name
*/
public PactDslJsonBody numberType(String name) {
generators.addGenerator(
Category.BODY,
matcherKey(name),
new RandomIntGenerator(0, Integer.MAX_VALUE) // <========================
);
return numberType(name, 100);
}
/**
* Attributes that can be any number
* @param names attribute names
*/
public PactDslJsonBody numberType(String... names) {
for (String name: names) {
numberType(name);
}
return this;
}
/**
* Attribute that can be any number
* @param name attribute name
* @param number example number to use for generated bodies
*/
public PactDslJsonBody numberType(String name, Number number) {
body.put(name, number);
matchers.addRule(matcherKey(name), new NumberTypeMatcher(NumberTypeMatcher.NumberType.NUMBER));
return this;
}
只有一个生成器,它总是从零开始。
那么,有一些可能的方法可以为 PACT 创建这种随机生成器,以确保生成的随机数的值大于零或小于 100?