Skip to content

Commit 885af21

Browse files
author
Matheus Cruz
committed
Allow get resources from network
1 parent a05ae33 commit 885af21

File tree

10 files changed

+261
-6
lines changed

10 files changed

+261
-6
lines changed

moqu/deployment/pom.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<parent>
45
<artifactId>quarkus-openapi-generator-moqu-parent</artifactId>
56
<groupId>io.quarkiverse.openapi.generator</groupId>
@@ -22,6 +23,27 @@
2223
<artifactId>quarkus-openapi-generator-moqu</artifactId>
2324
<version>${project.version}</version>
2425
</dependency>
26+
<dependency>
27+
<groupId>io.quarkiverse.openapi.generator</groupId>
28+
<artifactId>quarkus-openapi-generator-moqu-core</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>io.quarkus</groupId>
34+
<artifactId>quarkus-junit5-internal</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>io.quarkus</groupId>
39+
<artifactId>quarkus-vertx-http-deployment</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>io.rest-assured</groupId>
44+
<artifactId>rest-assured</artifactId>
45+
<scope>test</scope>
46+
</dependency>
2547
</dependencies>
2648

2749
<build>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import io.quarkus.deployment.annotations.BuildStep;
44
import io.quarkus.deployment.builditem.FeatureBuildItem;
55

6-
public class MoquProcessor {
6+
public class MoquFeatureProcessor {
77

88
@BuildStep
99
FeatureBuildItem feature() {
1010
return new FeatureBuildItem("moqu");
1111
}
12+
1213
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.quarkiverse.openapi.generator;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
import java.util.stream.Stream;
10+
11+
import org.jboss.logging.Logger;
12+
13+
import io.quarkiverse.openapi.generator.items.MoquProjectBuildItem;
14+
import io.quarkiverse.openapi.generator.moqu.MoquConfig;
15+
import io.quarkiverse.openapi.generator.moqu.recorder.MoquRoutesRecorder;
16+
import io.quarkus.deployment.IsDevelopment;
17+
import io.quarkus.deployment.annotations.BuildProducer;
18+
import io.quarkus.deployment.annotations.BuildStep;
19+
import io.quarkus.deployment.annotations.ExecutionTime;
20+
import io.quarkus.deployment.annotations.Record;
21+
import io.quarkus.runtime.util.ClassPathUtils;
22+
import io.quarkus.vertx.http.deployment.RouteBuildItem;
23+
24+
public class MoquProjectProcessor {
25+
26+
private static final Logger LOGGER = Logger.getLogger(MoquProjectProcessor.class);
27+
28+
@BuildStep
29+
MoquProjectBuildItem generate(MoquConfig config) {
30+
try {
31+
32+
HashMap<String, String> filesMap = new HashMap<>();
33+
ClassPathUtils.consumeAsPaths(config.resourceDir(), path -> {
34+
try {
35+
boolean directory = Files.isDirectory(path);
36+
if (directory) {
37+
try (Stream<Path> pathStream = Files.find(path, Integer.MAX_VALUE,
38+
(p, a) -> Files.isRegularFile(p) && p.getFileName().toString().endsWith(".yaml"))) {
39+
pathStream.forEach(p -> {
40+
try {
41+
filesMap.put(p.getFileName().toString(), Files.readString(p));
42+
} catch (IOException e) {
43+
throw new RuntimeException(e);
44+
}
45+
});
46+
}
47+
}
48+
} catch (IOException e) {
49+
throw new RuntimeException(e);
50+
}
51+
});
52+
return new MoquProjectBuildItem(filesMap);
53+
54+
} catch (IOException e) {
55+
LOGGER.error("Was not possible to scan Moqu project files.", e);
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
60+
@BuildStep(onlyIf = { IsDevelopment.class })
61+
@Record(ExecutionTime.RUNTIME_INIT)
62+
void consume(Optional<MoquProjectBuildItem> moquProject,
63+
MoquConfig config,
64+
BuildProducer<RouteBuildItem> routes,
65+
MoquRoutesRecorder recorder) {
66+
67+
moquProject.ifPresent(project -> {
68+
for (Map.Entry<String, String> spec : project.specs().entrySet()) {
69+
routes.produce(RouteBuildItem.builder()
70+
.routeFunction(config.moquBase() + spec.getKey(),
71+
recorder.handleFile(spec.getValue()))
72+
.build());
73+
}
74+
});
75+
}
76+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.quarkiverse.openapi.generator.items;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
6+
import io.quarkus.builder.item.SimpleBuildItem;
7+
8+
public final class MoquProjectBuildItem extends SimpleBuildItem {
9+
10+
private final Map<String, String> specs;
11+
12+
public MoquProjectBuildItem(Map<String, String> specs) {
13+
this.specs = specs;
14+
}
15+
16+
public Map<String, String> specs() {
17+
return Collections.unmodifiableMap(specs);
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.quarkiverse.openapi.generator;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.RegisterExtension;
6+
7+
import io.quarkus.test.QuarkusDevModeTest;
8+
import io.restassured.RestAssured;
9+
10+
public class MoquProjectProcessorTest {
11+
12+
@RegisterExtension
13+
static final QuarkusDevModeTest unitTest = new QuarkusDevModeTest()
14+
.withApplicationRoot(javaArchive -> javaArchive.addAsResource(
15+
"api.yaml", "src/openapi/openapi.yaml"));
16+
17+
@Test
18+
void test() {
19+
RestAssured.given()
20+
.when().get("/q/moqu/f/openapi.yaml")
21+
.then()
22+
.statusCode(200)
23+
.body(Matchers.containsString("version: 999-SNAPSHOT"));
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
openapi: 3.0.3
2+
servers:
3+
- url: http://localhost:8888
4+
info:
5+
version: 999-SNAPSHOT
6+
title: Method GET one path param
7+
paths:
8+
"/frameworks/{id}":
9+
get:
10+
parameters:
11+
- name: id
12+
in: path
13+
examples:
14+
quarkus:
15+
value: 1
16+
responses:
17+
200:
18+
content:
19+
"application/json":
20+
examples:
21+
quarkus:
22+
$ref: "#/components/schemas/Framework"
23+
description: Ok
24+
components:
25+
schemas:
26+
Framework:
27+
type: object
28+
properties:
29+
name:
30+
type: string
31+
example: "Quarkus"
32+
versions:
33+
type: array
34+
example: ["999-SNAPSHOT", "3.15.1"]
35+
supportsJava:
36+
type: boolean
37+
example: true
38+
contributors:
39+
type: integer
40+
example: 1000
41+
rules:
42+
type: object
43+
example:
44+
hello: world
45+

moqu/integration-tests/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<name>Quarkus - Openapi Generator - Moqu - Integration Tests</name>
1313

1414
<dependencies>
15-
<dependency>
16-
<groupId>io.quarkus</groupId>
17-
<artifactId>quarkus-arc</artifactId>
18-
</dependency>
15+
<!-- <dependency>-->
16+
<!-- <groupId>io.quarkus</groupId>-->
17+
<!-- <artifactId>quarkus-arc</artifactId>-->
18+
<!-- </dependency>-->
1919
<dependency>
2020
<groupId>io.quarkus</groupId>
2121
<artifactId>quarkus-rest</artifactId>

moqu/runtime/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
<groupId>io.quarkus</groupId>
2323
<artifactId>quarkus-core</artifactId>
2424
</dependency>
25+
26+
<dependency>
27+
<groupId>io.quarkus</groupId>
28+
<artifactId>quarkus-vertx-http</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>io.quarkus</groupId>
33+
<artifactId>quarkus-junit5</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>io.rest-assured</groupId>
39+
<artifactId>rest-assured</artifactId>
40+
<scope>test</scope>
41+
</dependency>
2542
</dependencies>
2643
<build>
2744
<plugins>

moqu/runtime/src/main/java/io/quarkiverse/openapi/generator/moqu/MoquConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,28 @@
33
import io.quarkus.runtime.annotations.ConfigPhase;
44
import io.quarkus.runtime.annotations.ConfigRoot;
55
import io.smallrye.config.ConfigMapping;
6+
import io.smallrye.config.WithDefault;
67

78
@ConfigMapping(prefix = "quarkus.openapi-generator.moqu")
89
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
910
public interface MoquConfig {
11+
12+
String DEFAULT_RESOURCE_DIR = "src/openapi";
13+
String DEFAULT_MOQU_BASE_PREFIX = "/q/moqu/f/";
14+
15+
/**
16+
* Path to the Moqu (relative to the project).
17+
*
18+
* @return
19+
*/
20+
@WithDefault(DEFAULT_RESOURCE_DIR)
21+
String resourceDir();
22+
23+
/**
24+
* The base path to access Moqu resources on dev mode.
25+
*
26+
* @return
27+
*/
28+
@WithDefault(DEFAULT_MOQU_BASE_PREFIX)
29+
String moquBase();
1030
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.quarkiverse.openapi.generator.moqu.recorder;
2+
3+
import java.util.function.Consumer;
4+
5+
import io.quarkus.runtime.annotations.Recorder;
6+
import io.vertx.core.Handler;
7+
import io.vertx.core.http.HttpMethod;
8+
import io.vertx.ext.web.Route;
9+
import io.vertx.ext.web.RoutingContext;
10+
11+
@Recorder
12+
public class MoquRoutesRecorder {
13+
14+
public Consumer<Route> handleFile(String spec) {
15+
return new Consumer<Route>() {
16+
@Override
17+
public void accept(Route route) {
18+
route.method(HttpMethod.GET);
19+
route.handler(new Handler<RoutingContext>() {
20+
@Override
21+
public void handle(RoutingContext routingContext) {
22+
routingContext.response().headers()
23+
.add("Content-Type", "plain/text");
24+
routingContext.response().send(spec);
25+
}
26+
});
27+
}
28+
};
29+
}
30+
}

0 commit comments

Comments
 (0)