Previously we used the XML and Xpath Hoverfly matchers.
On this blog we shall focus on rules that assist us with the data exchanged using Json.
The default Json matcher will compare the Json submitted with the Json expected. This means that the submitted Json shall be validated for all the elements and their value. New lines or any extra spaces as long as they don’t change the information that the JSON carries, will not prevent the request from being a success.
Let’s put our initial configuration that will make the Json match.
@BeforeEach
void setUp() {
var simulation = SimulationSource.dsl(service("http://localhost:8085")
.post("/json")
.body(RequestFieldMatcher.newJsonMatcher("{\"document\":\"document-a\"}"))
.willReturn(success(SUCCESS_RESPONSE, "application/json"))
.post("/json/partial")
.body(RequestFieldMatcher.newJsonPartialMatcher("{\"document\":\"document-a\"}"))
.willReturn(success(SUCCESS_RESPONSE, "application/json"))
.post("/jsonpath")
.body(RequestFieldMatcher.newJsonPathMatch("$.document[1].description"))
.willReturn(success(SUCCESS_RESPONSE, "application/json"))
);
var localConfig = HoverflyConfig.localConfigs().disableTlsVerification().asWebServer().proxyPort(8085);
hoverfly = new Hoverfly(localConfig, SIMULATE);
hoverfly.start();
hoverfly.simulate(simulation);
}
@AfterEach
void tearDown() {
hoverfly.close();
}
In our first example we will try to match the Json of our request with the Json expected.
@Test
void testJsonExactMatch() {
var client = HttpClient.newHttpClient();
var exactRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8085/json"))
.POST(HttpRequest.BodyPublishers.ofString(" {\"document\": \"document-a\"}"))
.build();
var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.join();
Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);
}
Also let’s make sure there is going to be a failure on an extra element.
@Test
void testJsonNoMatch() {
var client = HttpClient.newHttpClient();
var exactRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8085/json"))
.POST(HttpRequest.BodyPublishers.ofString("{\"doc2\":\"value\", \"document\":\"document-a\"}"))
.build();
var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
.join();
Assertions.assertEquals(502, exactResponse.statusCode());
}
Now let’s see the non exact matcher.
@Test
void testJsonPartialMatch() {
var client = HttpClient.newHttpClient();
var exactRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8085/json/partial"))
.POST(HttpRequest.BodyPublishers.ofString("{\"doc2\":\"value\", \"document\":\"document-a\"}"))
.build();
var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.join();
Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);
}
So far we checked matching the whole payload. Let’s try the Jsonpath approach. The example below does match.@Test
void testJsonPathMatch() {
var client = HttpClient.newHttpClient();
var exactRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8085/jsonpath"))
.POST(HttpRequest.BodyPublishers.ofString("{\"document\":[{\"description\":\"description-1\"},{\"description\":\"description-2\"}]}"))
.build();
var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.join();
Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);
}
But the example below won’t match
@Test
void testJsonPathNoMatch() {
var client = HttpClient.newHttpClient();
var exactRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8085/jsonpath"))
.POST(HttpRequest.BodyPublishers.ofString("{\"document\":[{\"description\":\"description-1\"}]}"))
.build();
var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
.join();
Assertions.assertEquals(502, exactResponse.statusCode());
}
That’s it we did use the Json and JsonPath matchers for the Json based data!
0 comments:
Post a Comment