Servlet JSON Response integration Tests | Community
Skip to main content
July 28, 2020
Solved

Servlet JSON Response integration Tests

  • July 28, 2020
  • 1 reply
  • 1135 views

Hello All,

There's requirement to expose a large sized JSON from an AEM servlet. The work is all complete. The Servlet is exposing the correct data. We are using unit tests to validate and ensure each and every property and type exists in the JSON response.

 

The next requirement is to write testes to validate against a swagger contract.

 

How can we set this up? Is this an integration test? How to get started?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ChitraMadan

Hi @17378453,

 

If you want to validate the swagger contract, you can try to validate the Yaml file generated by Swagger.

 

Please see below code for the same

 

import io.github.robwin.swagger.test.SwaggerAssertions;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerParser;


private SwaggerParser swaggerParser = new SwaggerParser();

@Test
public void validateImplementationMatchesDocumentation() {
AuthorizationValue authorizationValue = new AuthorizationValue("Authorization", swaggerApiToken, "header");
Swagger remoteSwagger = swaggerParser.read(swaggerApiPath, Collections.singletonList(authorizationValue), false);
SwaggerAssertions.assertThat(remoteSwagger).isEqualTo("MOCKED VALUE");
}

You need to pass Swagger API Token and API Path like - 

https://api.swaggerhub.com/apis/organization/service-name/${swagger.api.version}/swagger.yaml

 

<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>assertj-swagger</artifactId>
<version>${assertj-swagger.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.models.version}</version>
<scope>provided</scope>
</dependency>

 

1 reply

ChitraMadan
Community Advisor
ChitraMadanCommunity AdvisorAccepted solution
Community Advisor
July 28, 2020

Hi @17378453,

 

If you want to validate the swagger contract, you can try to validate the Yaml file generated by Swagger.

 

Please see below code for the same

 

import io.github.robwin.swagger.test.SwaggerAssertions;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerParser;


private SwaggerParser swaggerParser = new SwaggerParser();

@Test
public void validateImplementationMatchesDocumentation() {
AuthorizationValue authorizationValue = new AuthorizationValue("Authorization", swaggerApiToken, "header");
Swagger remoteSwagger = swaggerParser.read(swaggerApiPath, Collections.singletonList(authorizationValue), false);
SwaggerAssertions.assertThat(remoteSwagger).isEqualTo("MOCKED VALUE");
}

You need to pass Swagger API Token and API Path like - 

https://api.swaggerhub.com/apis/organization/service-name/${swagger.api.version}/swagger.yaml

 

<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>assertj-swagger</artifactId>
<version>${assertj-swagger.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.models.version}</version>
<scope>provided</scope>
</dependency>