Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Servlet JSON Response integration Tests

karthik4
Level 2
Level 2

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?

1 Accepted Solution
ChitraMadan
Correct answer by
Community Advisor
Community Advisor

Hi @karthik4,

 

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>

 

View solution in original post

1 Reply
ChitraMadan
Correct answer by
Community Advisor
Community Advisor

Hi @karthik4,

 

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>

 

View solution in original post