Skip to content

Commit e09257d

Browse files
committed
Add moqu and wiremock module
1 parent 1230fb8 commit e09257d

File tree

53 files changed

+2361
-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.

53 files changed

+2361
-1
lines changed

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]

docs/modules/ROOT/pages/moqu.adoc

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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/main/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 executes:
76+
77+
[source,shell]
78+
----
79+
curl http://localhost:8888/frameworks/1
80+
----
81+
82+
== Request matching
83+
84+
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.
85+
86+
Example:
87+
88+
[source,yaml]
89+
----
90+
openapi: 3.0.3
91+
info:
92+
title: "Users API"
93+
version: 1.0.0-alpha
94+
servers:
95+
- url: http://localhost:8888
96+
paths:
97+
/users/{id}:
98+
get:
99+
description: Get user by ID
100+
parameters:
101+
- name: id
102+
in: path
103+
required: true
104+
schema:
105+
type: number
106+
examples:
107+
john: <1>
108+
value: 1 <2>
109+
responses:
110+
"200":
111+
description: Ok
112+
content:
113+
"application/json":
114+
examples:
115+
john: <3>
116+
value:
117+
'{"id": 1, "name": "John Doe"}'
118+
----
119+
120+
<1> Defines an example named `john` for request
121+
<2> Maps the request for path `/users/1` should use the response named as `john`
122+
<3> Defines an example named `john` for response
123+
124+
125+
In other words, if the user accesses `/users/1`, the response will be the one mapped for the `john` example in response.
126+
127+
The Wiremock definition using the OpenAPI specification above, looks something like this:
128+
129+
[source,json]
130+
----
131+
{
132+
"mappings": [
133+
{
134+
"request": {
135+
"method": "GET",
136+
"url": "/users/1"
137+
},
138+
"response": {
139+
"status": 200,
140+
"body": "{\"name\":\"John\",\"age\": 80}",
141+
"headers": {}
142+
}
143+
}
144+
]
145+
}
146+
----
147+
148+
=== Response as Schema
149+
150+
You can use the `$ref` to reference a schema for mapping a response:
151+
152+
[source,yaml]
153+
----
154+
paths:
155+
"/users/{id}":
156+
get:
157+
parameters:
158+
- name: id
159+
in: path
160+
examples:
161+
alice:
162+
value: 1
163+
responses:
164+
200:
165+
content:
166+
"application/json":
167+
examples:
168+
alice:
169+
$ref: "#/components/schemas/User"
170+
description: Ok
171+
components:
172+
schemas:
173+
User:
174+
type: object
175+
properties:
176+
name:
177+
type: string
178+
example: "Alice"
179+
age:
180+
type: number
181+
example: 80
182+
----
183+
184+
The Wiremock definition using the OpenAPI specification above, looks something like this:
185+
186+
[source,json]
187+
----
188+
{
189+
"mappings": [
190+
{
191+
"request": {
192+
"method": "GET",
193+
"url": "/users/1"
194+
},
195+
"response": {
196+
"status": 200,
197+
"body": "{\"name\":\"Alice\",\"age\":80}",
198+
"headers": {}
199+
}
200+
}
201+
]
202+
}
203+
----

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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.quarkiverse.openapi.moqu;
2+
3+
import java.util.List;
4+
5+
/**
6+
* A generic interface for mapping a {@link Moqu} instance to a list of objects of type {@code T}.
7+
*
8+
* @param <T> the type of objects to which the {@link Moqu} instance will be mapped.
9+
*/
10+
public interface MoquMapper<T> {
11+
12+
/**
13+
* Maps the given {@link Moqu} instance to a list of objects of type {@code T}.
14+
*
15+
* @param moqu the {@link Moqu} instance to be mapped.
16+
* @return a list of mapped objects of type {@code T}.
17+
*/
18+
List<T> map(Moqu moqu);
19+
}

0 commit comments

Comments
 (0)