Skip to content

Commit bc58e6f

Browse files
committed
Add mock and wiremock modules
Signed-off-by: Matheus Cruz <[email protected]>
1 parent f2ef46a commit bc58e6f

File tree

57 files changed

+2480
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2480
-1
lines changed
39.8 KB
Loading
36.6 KB
Loading

docs/modules/ROOT/nav.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* xref:client.adoc[Client Generator]
2-
* xref:server.adoc[Server Generator]
2+
* xref:server.adoc[Server Generator]
3+
* xref:moqu.adoc[Moqu Wiremock Generator]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[.configuration-legend]
2+
icon:lock[title=Fixed at build time] Configuration property fixed at build time - All other configuration properties are overridable at runtime
3+
[.configuration-reference.searchable, cols="80,.^10,.^10"]
4+
|===
5+
6+
h|[.header-title]##Configuration property##
7+
h|Type
8+
h|Default
9+
10+
a|icon:lock[title=Fixed at build time] [[quarkus-openapi-generator-moqu-wiremock_quarkus-openapi-generator-moqu-resource-dir]] [.property-path]##link:#quarkus-openapi-generator-moqu-wiremock_quarkus-openapi-generator-moqu-resource-dir[`quarkus.openapi-generator.moqu.resource-dir`]##
11+
ifdef::add-copy-button-to-config-props[]
12+
config_property_copy_button:+++quarkus.openapi-generator.moqu.resource-dir+++[]
13+
endif::add-copy-button-to-config-props[]
14+
15+
16+
[.description]
17+
--
18+
Path to the Moqu (relative to the project).
19+
20+
21+
ifdef::add-copy-button-to-env-var[]
22+
Environment variable: env_var_with_copy_button:+++QUARKUS_OPENAPI_GENERATOR_MOQU_RESOURCE_DIR+++[]
23+
endif::add-copy-button-to-env-var[]
24+
ifndef::add-copy-button-to-env-var[]
25+
Environment variable: `+++QUARKUS_OPENAPI_GENERATOR_MOQU_RESOURCE_DIR+++`
26+
endif::add-copy-button-to-env-var[]
27+
--
28+
|string
29+
|`openapi`
30+
31+
|===
32+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[.configuration-legend]
2+
icon:lock[title=Fixed at build time] Configuration property fixed at build time - All other configuration properties are overridable at runtime
3+
[.configuration-reference.searchable, cols="80,.^10,.^10"]
4+
|===
5+
6+
h|[.header-title]##Configuration property##
7+
h|Type
8+
h|Default
9+
10+
a|icon:lock[title=Fixed at build time] [[quarkus-openapi-generator-moqu-wiremock_quarkus-openapi-generator-moqu-resource-dir]] [.property-path]##link:#quarkus-openapi-generator-moqu-wiremock_quarkus-openapi-generator-moqu-resource-dir[`quarkus.openapi-generator.moqu.resource-dir`]##
11+
ifdef::add-copy-button-to-config-props[]
12+
config_property_copy_button:+++quarkus.openapi-generator.moqu.resource-dir+++[]
13+
endif::add-copy-button-to-config-props[]
14+
15+
16+
[.description]
17+
--
18+
Path to the Moqu (relative to the project).
19+
20+
21+
ifdef::add-copy-button-to-env-var[]
22+
Environment variable: env_var_with_copy_button:+++QUARKUS_OPENAPI_GENERATOR_MOQU_RESOURCE_DIR+++[]
23+
endif::add-copy-button-to-env-var[]
24+
ifndef::add-copy-button-to-env-var[]
25+
Environment variable: `+++QUARKUS_OPENAPI_GENERATOR_MOQU_RESOURCE_DIR+++`
26+
endif::add-copy-button-to-env-var[]
27+
--
28+
|string
29+
|`openapi`
30+
31+
|===
32+

docs/modules/ROOT/pages/moqu.adoc

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
= Quarkus - Open API Generator - Moqu
2+
3+
include::./includes/attributes.adoc[]
4+
5+
The **OpenAPI Generator Moqu extension** converts an OpenAPI specification into a mock representation. This mock can then be mapped to the link:https://wiremock.org/[WireMock] for further use, providing a way to simulate APIs for testing and development purposes.
6+
7+
[NOTE]
8+
====
9+
Currently, this extension supports only link:https://wiremock.org/[WireMock] definitions.
10+
====
11+
12+
[[getting-started]]
13+
== Getting Started
14+
15+
[source,xml]
16+
----
17+
<dependency>
18+
<groupId>io.quarkiverse.openapi.generator</groupId>
19+
<artifactId>quarkus-openapi-generator-moqu</artifactId>
20+
<version>{project-version}</version>
21+
</dependency>
22+
----
23+
24+
Now, create the following OpenAPI specification file under your `src/resources/openapi` directory:
25+
26+
[source,yaml]
27+
.src/main/openapi/hello.yaml
28+
----
29+
openapi: 3.0.3
30+
servers:
31+
- url: http://localhost:8888
32+
info:
33+
version: 999-SNAPSHOT
34+
title: Get framework by ID
35+
paths:
36+
"/frameworks/{id}":
37+
get:
38+
parameters:
39+
- name: id
40+
in: path
41+
examples:
42+
quarkus:
43+
value: 1
44+
responses:
45+
200:
46+
content:
47+
"application/json":
48+
examples:
49+
quarkus:
50+
$ref: "#/components/schemas/Framework"
51+
description: Ok
52+
components:
53+
schemas:
54+
Framework:
55+
type: object
56+
properties:
57+
name:
58+
type: string
59+
example: "Quarkus"
60+
versions:
61+
type: array
62+
example: ["999-SNAPSHOT", "3.15.1"]
63+
supportsJava:
64+
type: boolean
65+
example: true
66+
contributors:
67+
type: integer
68+
example: 1000
69+
rules:
70+
type: object
71+
example:
72+
hello: world
73+
----
74+
75+
Execute now your application on Dev mode, and access the Dev UI for getting you wiremock stubbing:
76+
77+
image::moqu-devui-card-framework.png[]
78+
79+
Click on `Moqu Wiremock`, you will se a table containing all wiremock definitions:
80+
81+
image::table-wiremock.png[]
82+
83+
Now, you can `see` or `download` the Wiremock stubbing.
84+
85+
== Request matching
86+
87+
The Moqu extension uses the request and response examples defined in the OpenAPI Specification to determine the appropriate response for a specific request, creating a corresponding request/response pair.
88+
89+
Example:
90+
91+
[source,yaml]
92+
----
93+
openapi: 3.0.3
94+
info:
95+
title: "Users API"
96+
version: 1.0.0-alpha
97+
servers:
98+
- url: http://localhost:8888
99+
paths:
100+
/users/{id}:
101+
get:
102+
description: Get user by ID
103+
parameters:
104+
- name: id
105+
in: path
106+
required: true
107+
schema:
108+
type: number
109+
examples:
110+
john: <1>
111+
value: 1 <2>
112+
responses:
113+
"200":
114+
description: Ok
115+
content:
116+
"application/json":
117+
examples:
118+
john: <3>
119+
value:
120+
'{"id": 1, "name": "John Doe"}'
121+
----
122+
123+
<1> Defines an example named `john` for request
124+
<2> Maps the request for path `/users/1` should use the response named as `john`
125+
<3> Defines an example named `john` for response
126+
127+
128+
In other words, if the user accesses `/users/1`, the response will be the one mapped for the `john` example in response.
129+
130+
The Wiremock definition using the OpenAPI specification above, looks something like this:
131+
132+
[source,json]
133+
----
134+
{
135+
"mappings": [
136+
{
137+
"request": {
138+
"method": "GET",
139+
"url": "/users/1"
140+
},
141+
"response": {
142+
"status": 200,
143+
"body": "{\"name\":\"John\",\"age\": 80}",
144+
"headers": {}
145+
}
146+
}
147+
]
148+
}
149+
----
150+
151+
=== Response as Schema
152+
153+
You can use the `$ref` to reference a schema for mapping a response:
154+
155+
[source,yaml]
156+
----
157+
paths:
158+
"/users/{id}":
159+
get:
160+
parameters:
161+
- name: id
162+
in: path
163+
examples:
164+
alice:
165+
value: 1
166+
responses:
167+
200:
168+
content:
169+
"application/json":
170+
examples:
171+
alice:
172+
$ref: "#/components/schemas/User"
173+
description: Ok
174+
components:
175+
schemas:
176+
User:
177+
type: object
178+
properties:
179+
name:
180+
type: string
181+
example: "Alice"
182+
age:
183+
type: number
184+
example: 80
185+
----
186+
187+
The Wiremock definition using the OpenAPI specification above, looks something like this:
188+
189+
[source,json]
190+
----
191+
{
192+
"mappings": [
193+
{
194+
"request": {
195+
"method": "GET",
196+
"url": "/users/1"
197+
},
198+
"response": {
199+
"status": 200,
200+
"body": "{\"name\":\"Alice\",\"age\":80}",
201+
"headers": {}
202+
}
203+
}
204+
]
205+
}
206+
----

moqu/core/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.quarkiverse.openapi.generator</groupId>
8+
<artifactId>quarkus-openapi-generator-moqu-parent</artifactId>
9+
<version>3.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>quarkus-openapi-generator-moqu-core</artifactId>
13+
<name>Quarkus :: Openapi Generator :: Moqu :: Core</name>
14+
15+
<properties>
16+
<commons.io.version>2.16.1</commons.io.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.swagger.parser.v3</groupId>
22+
<artifactId>swagger-parser</artifactId>
23+
<version>${version.io.swagger.parser}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.assertj</groupId>
27+
<artifactId>assertj-core</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-params</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter-engine</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>commons-io</groupId>
46+
<artifactId>commons-io</artifactId>
47+
<version>${commons.io.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.jboss.logmanager</groupId>
51+
<artifactId>jboss-logmanager</artifactId>
52+
</dependency>
53+
</dependencies>
54+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.quarkiverse.openapi.moqu;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
import io.quarkiverse.openapi.moqu.model.RequestResponsePair;
9+
10+
/**
11+
* Represents a collection of request-response pairs, providing methods to access
12+
* these pairs in an immutable list.
13+
*/
14+
public class Moqu {
15+
16+
private List<RequestResponsePair> requestResponsePairs = new ArrayList<>();
17+
18+
/**
19+
* Constructs a {@code Moqu} instance with the provided list of request-response pairs.
20+
*
21+
* @param requestResponsePairs the list of {@link RequestResponsePair} objects to initialize
22+
* the collection. Must not be {@code null}.
23+
* @throws NullPointerException if {@code requestResponsePairs} is null.
24+
*/
25+
public Moqu(List<RequestResponsePair> requestResponsePairs) {
26+
this.requestResponsePairs = Objects.requireNonNull(requestResponsePairs);
27+
}
28+
29+
/**
30+
* Returns an unmodifiable list of request-response pairs.
31+
*
32+
* @return an immutable list of {@link RequestResponsePair}.
33+
*/
34+
public List<RequestResponsePair> getRequestResponsePairs() {
35+
return Collections.unmodifiableList(requestResponsePairs);
36+
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.quarkiverse.openapi.moqu;
2+
3+
/**
4+
* {@link MoquImporter} aims to convert a specification into a {@link Moqu} model.
5+
* It provides a method to parse the content, typically from an OpenAPI specification,
6+
* and generate a corresponding {@link Moqu} instance.
7+
*/
8+
public interface MoquImporter {
9+
10+
/**
11+
* Parses the provided OpenAPI content and generates a new {@link Moqu} instance.
12+
*
13+
* @param content the OpenAPI content as a string, which will be parsed into a {@link Moqu} model.
14+
* @return a new {@link Moqu} instance based on the provided content.
15+
*/
16+
Moqu parse(String content);
17+
}

0 commit comments

Comments
 (0)