VOOZH about

URL: https://www.javacodegeeks.com/2021/02/testing-with-hoverfly-and-java-part-6-json-and-jsonpath-matchers.html

⇱ Testing with Hoverfly and Java Part 6: JSON and JsonPath matchers - Java Code Geeks


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!

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Testing with Hoverfly and Java Part 6: JSON and JsonPath matchers

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Emmanouil Gkatziouras
Emmanouil Gkatziouras
February 10th, 2021Last Updated: February 9th, 2021
0 116 2 minutes read

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz