Skip to content

Commit

Permalink
feat(core): #3 - Investigate RESTAssured integration (#22)
Browse files Browse the repository at this point in the history
- added and implemented openapi-extender-restassured module
- added quarkus sample with openapi-extender-restassured module
  • Loading branch information
bcsabi authored Nov 21, 2022
1 parent d525353 commit 2daf3fc
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 0 deletions.
33 changes: 33 additions & 0 deletions openapi-extender-restassured/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>openapi-extender-parent</artifactId>
<groupId>org.rodnansol</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>openapi-extender-restassured</artifactId>
<name>OpenAPI Extender - REST Assured</name>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.rodnansol</groupId>
<artifactId>openapi-extender-resource-generator</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.rodnalsol.openapi.extender.restassured.exception;

/**
* Exception for OpenAPI filters.
*
* @author csaba.balogh
* @since 0.3.0
*/
public class OpenApiFilterException extends RuntimeException {

/**
* Constructs a new {@link OpenApiFilterException} with the specified detail message.
*
* @param message the detail message.
*/
public OpenApiFilterException(String message) {
super(message);
}

/**
* Constructs a new {@link OpenApiFilterException} with the specified detail message and cause.
*
* @param message the detail message.
* @param cause the cause.
*/
public OpenApiFilterException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.rodnalsol.openapi.extender.restassured.filter;

import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.rodnalsol.openapi.extender.restassured.exception.OpenApiFilterException;
import org.rodnansol.generator.ReportParams;
import org.rodnansol.generator.RequestBodyExampleFileOutputResourceGenerator;

/**
* Rest assured filter class for generating OpenAPI request example.
*
* @author csaba.balogh
* @since 0.3.0
*/
public class OpenApiRequestFilter implements Filter {

/**
* The OpenAPI request example generator.
*/
private final RequestBodyExampleFileOutputResourceGenerator requestBodyExampleFileOutputResourceGenerator;

/**
* The operation's name.
*/
private final String operation;

/**
* The name of the OpenAPI request example.
*/
private final String name;

/**
* The description of the OpenAPI request example.
*/
private final String description;

/**
* Creates a new {@link OpenApiRequestFilter} with the specified operation and name.
* The description will be null and the output directory will be {@link RequestBodyExampleFileOutputResourceGenerator#DEFAULT_OUTPUT_DIRECTORY}.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI request example.
*/
public OpenApiRequestFilter(String operation, String name) {
this(operation, name, null, RequestBodyExampleFileOutputResourceGenerator.DEFAULT_OUTPUT_DIRECTORY);
}

/**
* Creates a new {@link OpenApiRequestFilter} with the specified operation, name and description.
* The output directory will be {@link RequestBodyExampleFileOutputResourceGenerator#DEFAULT_OUTPUT_DIRECTORY}.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI request example.
* @param description the description of the OpenAPI request example.
*/
public OpenApiRequestFilter(String operation, String name, String description) {
this(operation, name, description, RequestBodyExampleFileOutputResourceGenerator.DEFAULT_OUTPUT_DIRECTORY);
}

/**
* Creates a new {@link OpenApiRequestFilter} with the specified operation, name, description and output directory.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI request example.
* @param description the description of the OpenAPI request example.
* @param outputDirectory the output directory of the OpenAPI request example.
*/
public OpenApiRequestFilter(String operation, String name, String description, String outputDirectory) {
this.operation = operation;
this.name = name;
this.description = description;
this.requestBodyExampleFileOutputResourceGenerator = new RequestBodyExampleFileOutputResourceGenerator(outputDirectory);
}

@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
try {
Response response = ctx.next(requestSpec, responseSpec);
generateResources(requestSpec, response);
return response;
} catch (Exception e) {
throw new OpenApiFilterException("Error during documenting response", e);
}
}

private void generateResources(FilterableRequestSpecification requestSpec, Response response) throws IOException {
byte[] requestBodyAsByteArray = requestSpec.getBody().toString().getBytes(StandardCharsets.UTF_8);
ReportParams params = new ReportParams(operation, name, response.getStatusCode(), requestSpec.getContentType(), requestBodyAsByteArray);
params.setDescription(description);
requestBodyExampleFileOutputResourceGenerator.generateResources(params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.rodnalsol.openapi.extender.restassured.filter;

import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import java.io.IOException;

import org.rodnalsol.openapi.extender.restassured.exception.OpenApiFilterException;
import org.rodnansol.generator.ApiResponseExampleFileOutputResourceGenerator;
import org.rodnansol.generator.ReportParams;

/**
* Rest assured filter class for generating OpenAPI response example.
*
* @author csaba.balogh
* @since 0.3.0
*/
public class OpenApiResponseFilter implements Filter {

/**
* The OpenAPI response example generator.
*/
private final ApiResponseExampleFileOutputResourceGenerator apiResponseExampleFileOutputResourceGenerator;

/**
* The operation's name.
*/
private final String operation;

/**
* The name of the OpenAPI response example.
*/
private final String name;

/**
* The description of the OpenAPI response example.
*/
private final String description;

/**
* Creates a new {@link OpenApiResponseFilter} with the specified operation and name.
* The description will be null and the output directory will be {@link ApiResponseExampleFileOutputResourceGenerator#DEFAULT_OUTPUT_DIRECTORY}.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI response example.
*/
public OpenApiResponseFilter(String operation, String name) {
this(operation, name, null, ApiResponseExampleFileOutputResourceGenerator.DEFAULT_OUTPUT_DIRECTORY);
}

/**
* Creates a new {@link OpenApiResponseFilter} with the specified operation, name and description.
* The output directory will be {@link ApiResponseExampleFileOutputResourceGenerator#DEFAULT_OUTPUT_DIRECTORY}.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI response example.
* @param description the description of the OpenAPI response example.
*/
public OpenApiResponseFilter(String operation, String name, String description) {
this(operation, name, description, ApiResponseExampleFileOutputResourceGenerator.DEFAULT_OUTPUT_DIRECTORY);
}

/**
* Creates a new {@link OpenApiResponseFilter} with the specified operation, name, description and output directory.
*
* @param operation the operation's name.
* @param name the name of the OpenAPI response example.
* @param description the description of the OpenAPI response example.
* @param outputDirectory the output directory of the OpenAPI response example.
*/
public OpenApiResponseFilter(String operation, String name, String description, String outputDirectory) {
this.operation = operation;
this.name = name;
this.description = description;
this.apiResponseExampleFileOutputResourceGenerator = new ApiResponseExampleFileOutputResourceGenerator(outputDirectory);
}

@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
try {
Response response = ctx.next(requestSpec, responseSpec);
generateResources(response);
return response;
} catch (Exception e) {
throw new OpenApiFilterException("Error during documenting response", e);
}
}

private void generateResources(Response response) throws IOException {
ReportParams params = new ReportParams(operation, name, response.getStatusCode(), response.getContentType(), response.asByteArray());
params.setDescription(description);
apiResponseExampleFileOutputResourceGenerator.generateResources(params);
}
}
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<module>openapi-extender-resource-generator</module>
<module>openapi-extender-spring-test</module>
<module>openapi-extender-springdoc</module>
<module>openapi-extender-restassured</module>
</modules>

<properties>
Expand Down Expand Up @@ -43,6 +44,7 @@
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
<checkstyle.version>10.3.3</checkstyle.version>
<assertj-core.version>3.22.0</assertj-core.version>
<rest-assured.version>5.2.0</rest-assured.version>
</properties>

<!--Release Stuff-->
Expand Down Expand Up @@ -179,6 +181,12 @@
<version>${junit-jupiter-params.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
84 changes: 84 additions & 0 deletions samples/quarkus-with-restassured/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>openapi-extender-parent</artifactId>
<groupId>org.rodnansol</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-with-restassured</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<quarkus.platform.version>2.13.0.Final</quarkus.platform.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus.platform</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.rodnansol</groupId>
<artifactId>openapi-extender-restassured</artifactId>
<version>999-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jaxb</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jaxb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus.platform</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 2daf3fc

Please sign in to comment.