Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Servlet JSON Response integration Tests

Avatar

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

Avatar

Correct answer by
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

Avatar

Correct answer by
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>