Skip to content

Commit 3959998

Browse files
committed
Add documentation and devUI
1 parent ff65bac commit 3959998

File tree

19 files changed

+454
-71
lines changed

19 files changed

+454
-71
lines changed

docs/modules/ROOT/pages/mock.adoc

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
"/frameworks/{id}":
91+
get:
92+
parameters:
93+
- name: id
94+
in: path
95+
examples:
96+
quarkus: <1>
97+
value: 1 <2>
98+
responses:
99+
200:
100+
content:
101+
"application/json":
102+
examples:
103+
quarkus: <3>
104+
$ref: "#/components/schemas/Framework"
105+
description: Ok
106+
components:
107+
schemas:
108+
Framework:
109+
type: object
110+
properties:
111+
name:
112+
type: string
113+
example: "Quarkus"
114+
versions:
115+
type: array
116+
example: ["999-SNAPSHOT", "3.15.1"]
117+
supportsJava:
118+
type: boolean
119+
example: true
120+
contributors:
121+
type: integer
122+
example: 1000
123+
rules:
124+
type: object
125+
example:
126+
hello: world
127+
----
128+
129+
<1> Defines an example named `quarkus`.
130+
<2> Specifies that the request `/frameworks/1` will match the `quarkus` example.
131+
<3> Maps the response for the `quarkus` example.
132+
133+
In other words, if the user accesses `/frameworks/1` (using the `quarkus` example), the response will be the one mapped for the `quarkus` example, which in this case references the Framework schema defined in the OpenAPI specification.
134+
135+
The Wiremock created using the OpenAPI specification above looks like this one:
136+
137+
[source,json]
138+
----
139+
{
140+
"mappings": [
141+
{
142+
"request": {
143+
"method": "GET",
144+
"url": "/frameworks/1"
145+
},
146+
"response": {
147+
"status": 200,
148+
"body": "{\"supportsJava\":true,\"versions\":[\"999-SNAPSHOT\",\"3.15.1\"],\"name\":\"Quarkus\",\"rules\":{\"hello\":\"world\"},\"contributors\":1000}",
149+
"headers": {}
150+
}
151+
}
152+
]
153+
}
154+
----

moqu/core/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<properties>
1616
<commons.io.version>2.16.1</commons.io.version>
17+
<version.io.swagger.parser>2.1.22</version.io.swagger.parser>
1718
</properties>
1819

1920
<dependencies>

moqu/core/src/main/java/io/quarkiverse/openapi/moqu/wiremock/mapper/WiremockMapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import io.quarkiverse.openapi.moqu.model.Request;
1111
import io.quarkiverse.openapi.moqu.model.RequestResponsePair;
1212
import io.quarkiverse.openapi.moqu.model.Response;
13-
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockDefinition;
13+
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockMapping;
1414
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockRequest;
1515
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockResponse;
1616

17-
public class WiremockMapper implements MoquMapper<WiremockDefinition> {
17+
public class WiremockMapper implements MoquMapper<WiremockMapping> {
1818

1919
@Override
20-
public List<WiremockDefinition> map(Moqu moqu) {
21-
ArrayList<WiremockDefinition> definitions = new ArrayList<>();
20+
public List<WiremockMapping> map(Moqu moqu) {
21+
ArrayList<WiremockMapping> definitions = new ArrayList<>();
2222
for (RequestResponsePair pair : moqu.getRequestResponsePairs()) {
2323

2424
Request mockRequest = pair.request();
@@ -32,7 +32,7 @@ public List<WiremockDefinition> map(Moqu moqu) {
3232

3333
WiremockResponse response = new WiremockResponse(mockResponse.statusCode(), mockResponse.content(), headers);
3434

35-
definitions.add(new WiremockDefinition(
35+
definitions.add(new WiremockMapping(
3636
request, response));
3737
}
3838

moqu/core/src/main/java/io/quarkiverse/openapi/moqu/wiremock/model/WiremockDefinition.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.quarkiverse.openapi.moqu.wiremock.model;
2+
3+
public record WiremockMapping(WiremockRequest request, WiremockResponse response) {
4+
}

moqu/core/src/test/java/io/quarkiverse/openapi/moqu/wiremock/mapper/WiremockMapperTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import io.quarkiverse.openapi.moqu.Moqu;
1212
import io.quarkiverse.openapi.moqu.OpenAPIMoquImporter;
13-
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockDefinition;
13+
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockMapping;
1414

1515
class WiremockMapperTest {
1616

@@ -24,7 +24,7 @@ void shouldMapOneWiremockDefinition() {
2424

2525
Moqu moqu = importer.parse(content);
2626

27-
List<WiremockDefinition> definitions = sut.map(moqu);
27+
List<WiremockMapping> definitions = sut.map(moqu);
2828

2929
SoftAssertions.assertSoftly(softly -> {
3030
softly.assertThat(definitions).isNotEmpty();
@@ -44,7 +44,7 @@ void shouldMapTwoWiremockDefinitions() {
4444

4545
Moqu mock = importer.parse(content);
4646

47-
List<WiremockDefinition> definitions = sut.map(mock);
47+
List<WiremockMapping> definitions = sut.map(mock);
4848

4949
SoftAssertions.assertSoftly(softly -> {
5050
softly.assertThat(definitions).isNotEmpty();

moqu/core/src/test/java/io/quarkiverse/openapi/moqu/wiremock/mapper/WiremockPathParamTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io.quarkiverse.openapi.moqu.Moqu;
1111
import io.quarkiverse.openapi.moqu.OpenAPIMoquImporter;
1212
import io.quarkiverse.openapi.moqu.Testing;
13-
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockDefinition;
13+
import io.quarkiverse.openapi.moqu.wiremock.model.WiremockMapping;
1414

1515
public class WiremockPathParamTest {
1616

@@ -29,11 +29,11 @@ void shouldMapOneWiremockDefinition() {
2929

3030
WiremockMapper wiremockMapper = new WiremockMapper();
3131

32-
List<WiremockDefinition> definitions = wiremockMapper.map(mock);
32+
List<WiremockMapping> definitions = wiremockMapper.map(mock);
3333

3434
SoftAssertions.assertSoftly(softly -> {
3535
softly.assertThat(definitions).hasSize(1);
36-
WiremockDefinition definition = definitions.get(0);
36+
WiremockMapping definition = definitions.get(0);
3737

3838
softly.assertThat(definition).satisfies(wiremockDefinition -> {
3939
// request
@@ -61,7 +61,7 @@ void shouldMapTwoWiremockDefinitions() {
6161

6262
WiremockMapper wiremockMapper = new WiremockMapper();
6363

64-
List<WiremockDefinition> definitions = wiremockMapper.map(mock);
64+
List<WiremockMapping> definitions = wiremockMapper.map(mock);
6565

6666
SoftAssertions.assertSoftly(softly -> {
6767
softly.assertThat(definitions).hasSize(2);
@@ -102,7 +102,7 @@ void shouldConvertWithACombinationOfPathParam() {
102102

103103
WiremockMapper wiremockMapper = new WiremockMapper();
104104

105-
List<WiremockDefinition> definitions = wiremockMapper.map(mock);
105+
List<WiremockMapping> definitions = wiremockMapper.map(mock);
106106

107107
SoftAssertions.assertSoftly(softly -> {
108108
softly.assertThat(definitions).hasSize(2);
@@ -144,7 +144,7 @@ void shouldConvertPathParamCombinationOnlyOneWithExample() {
144144

145145
WiremockMapper wiremockMapper = new WiremockMapper();
146146

147-
List<WiremockDefinition> definitions = wiremockMapper.map(mock);
147+
List<WiremockMapping> definitions = wiremockMapper.map(mock);
148148

149149
SoftAssertions.assertSoftly(softly -> {
150150
softly.assertThat(definitions).hasSize(1);

moqu/deployment/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<artifactId>rest-assured</artifactId>
4545
<scope>test</scope>
4646
</dependency>
47+
4748
</dependencies>
4849

4950
<build>

moqu/deployment/src/main/java/io/quarkiverse/openapi/generator/MoquFeatureProcessor.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)