From 2894d3c13925e01ba80f3324d0aee7ae79c4fb1c Mon Sep 17 00:00:00 2001 From: Andy Damevin Date: Mon, 1 Jul 2024 11:30:43 +0200 Subject: [PATCH] Introduce resources/site support and dev mode test --- .../roq/deployment/RoqProjectProcessor.java | 21 ++++-- .../roq/deployment/config/RoqConfig.java | 12 ++- .../deployment/items/RoqProjectBuildItem.java | 20 ++++- .../ROOT/pages/includes/quarkus-roq.adoc | 19 ++++- roq-data/deployment/pom.xml | 22 +++++- .../roq/data/deployment/DataConverter.java | 6 ++ .../roq/data/deployment/JsonConverter.java | 10 +-- .../data/deployment/JsonObjectConverter.java | 12 +-- .../data/deployment/ReadRoqDataProcessor.java | 43 +++++++---- .../roq/data/deployment/RoqDataProcessor.java | 23 ++++-- .../roq/data/deployment/YamlConverter.java | 32 ++++++-- .../items/RoqDataJsonBuildItem.java | 23 +++--- .../roq/data/test/RoqDataDevModeTest.java | 45 +++++++++-- ...jectableBeanLookingCustomLocationTest.java | 58 ++++++--------- ...ectableBeanLookingDefaultLocationTest.java | 74 +++++++++---------- .../roq/data/test/RoqDataTest.java | 23 ------ .../deployment/src/test/resources/foo.json | 3 + .../src/test/resources/fromResource.json | 3 + .../deployment/src/test/site/data/list.yaml | 3 + .../roq/data/runtime/RoqDataRecorder.java | 5 +- 20 files changed, 276 insertions(+), 181 deletions(-) create mode 100644 roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/DataConverter.java delete mode 100644 roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataTest.java create mode 100644 roq-data/deployment/src/test/resources/foo.json create mode 100644 roq-data/deployment/src/test/resources/fromResource.json create mode 100644 roq-data/deployment/src/test/site/data/list.yaml diff --git a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/RoqProjectProcessor.java b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/RoqProjectProcessor.java index 95a07644..9f6efade 100644 --- a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/RoqProjectProcessor.java +++ b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/RoqProjectProcessor.java @@ -1,5 +1,6 @@ package io.quarkiverse.roq.deployment; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -20,10 +21,20 @@ public class RoqProjectProcessor { RoqProjectBuildItem findProject(RoqConfig config, OutputTargetBuildItem outputTarget, CurateOutcomeBuildItem curateOutcome) { final RoqProjectBuildItem.RoqProject project = resolveProjectDirs(config, curateOutcome, outputTarget); - if (project == null) { - return null; + + String resourceSiteDir; + try { + final boolean hasResourceSiteDir = Thread.currentThread().getContextClassLoader() + .getResources(config.resourceSiteDir()).hasMoreElements(); + resourceSiteDir = hasResourceSiteDir ? config.resourceSiteDir() : null; + } catch (IOException e) { + resourceSiteDir = null; } - return new RoqProjectBuildItem(project); + final RoqProjectBuildItem roqProject = new RoqProjectBuildItem(project, resourceSiteDir); + if (!roqProject.isActive()) { + LOG.warn("Not Roq site directory found. It is recommended to remove the quarkus-roq extension if not used."); + } + return roqProject; } /** @@ -40,7 +51,6 @@ private static RoqProjectBuildItem.RoqProject resolveProjectDirs(RoqConfig confi OutputTargetBuildItem outputTarget) { Path projectRoot = findProjectRoot(outputTarget.getOutputDirectory()); Path configuredSiteDirPath = Paths.get(config.siteDir().trim()); - if (projectRoot == null || !Files.isDirectory(projectRoot)) { if (configuredSiteDirPath.isAbsolute() && Files.isDirectory(configuredSiteDirPath)) { @@ -55,9 +65,6 @@ private static RoqProjectBuildItem.RoqProject resolveProjectDirs(RoqConfig confi final Path siteRoot = projectRoot.resolve(configuredSiteDirPath).normalize(); if (!Files.isDirectory(siteRoot)) { - LOG.warnf( - "Roq directory not found 'quarkus.roq.site-dir=%s' resolved to '%s'. It is recommended to remove the quarkus-roq extension if not used.", - config.siteDir(), siteRoot.toAbsolutePath()); return null; } diff --git a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/config/RoqConfig.java b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/config/RoqConfig.java index 89ce5386..fb73f60a 100644 --- a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/config/RoqConfig.java +++ b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/config/RoqConfig.java @@ -12,17 +12,27 @@ public interface RoqConfig { String DEFAULT_SITE_DIR = "src/main/site"; + String DEFAULT_RESOURCE_SITE_DIR = "site"; /** - * Path to the static root directory (relative to the project root). + * Path to the Roq site directory (relative to the project root). */ @WithDefault(DEFAULT_SITE_DIR) String siteDir(); + /** + * Path to the Roq site directory in the resources. + */ + @WithDefault(DEFAULT_RESOURCE_SITE_DIR) + String resourceSiteDir(); + static boolean isEqual(RoqConfig q1, RoqConfig q2) { if (!Objects.equals(q1.siteDir(), q2.siteDir())) { return false; } + if (!Objects.equals(q1.resourceSiteDir(), q2.resourceSiteDir())) { + return false; + } return true; } } \ No newline at end of file diff --git a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqProjectBuildItem.java b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqProjectBuildItem.java index 2a035997..6fc7d2d2 100644 --- a/common/deployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqProjectBuildItem.java +++ b/common/deployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqProjectBuildItem.java @@ -4,22 +4,34 @@ import java.nio.file.Path; import java.util.function.Consumer; +import io.quarkiverse.roq.util.PathUtils; import io.quarkus.builder.item.SimpleBuildItem; +import io.quarkus.runtime.util.ClassPathUtils; public final class RoqProjectBuildItem extends SimpleBuildItem { private final RoqProject project; + private final String resourceSiteDir; - public RoqProjectBuildItem(RoqProject project) { + public RoqProjectBuildItem(RoqProject project, String resourceSiteDir) { this.project = project; + this.resourceSiteDir = resourceSiteDir; } - public RoqProject dirs() { + public RoqProject project() { return project; } + public boolean isActive() { + return project != null || resourceSiteDir != null; + } + public void consumePathFromSite(String resource, Consumer consumer) throws IOException { - // TODO: in the future we might want to scan dependencies when configured - consumer.accept(project.siteDir().resolve(resource)); + if (resourceSiteDir != null) { + ClassPathUtils.consumeAsPaths(PathUtils.join(resourceSiteDir, resource), consumer); + } + if (project != null) { + consumer.accept(project.siteDir().resolve(resource)); + } } /** diff --git a/docs/modules/ROOT/pages/includes/quarkus-roq.adoc b/docs/modules/ROOT/pages/includes/quarkus-roq.adoc index f918b0dd..1fe9fcae 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-roq.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-roq.adoc @@ -15,7 +15,7 @@ a|icon:lock[title=Fixed at build time] [[quarkus-roq_quarkus-roq-site-dir]]`link [.description] -- -Path to the static root directory (relative to the project root). +Path to the Roq site directory (relative to the project root). ifdef::add-copy-button-to-env-var[] Environment variable: env_var_with_copy_button:+++QUARKUS_ROQ_SITE_DIR+++[] @@ -26,4 +26,21 @@ endif::add-copy-button-to-env-var[] --|string |`src/main/site` + +a|icon:lock[title=Fixed at build time] [[quarkus-roq_quarkus-roq-resource-site-dir]]`link:#quarkus-roq_quarkus-roq-resource-site-dir[quarkus.roq.resource-site-dir]` + + +[.description] +-- +Path to the Roq site directory in the resources. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++QUARKUS_ROQ_RESOURCE_SITE_DIR+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++QUARKUS_ROQ_RESOURCE_SITE_DIR+++` +endif::add-copy-button-to-env-var[] +--|string +|`site` + |=== \ No newline at end of file diff --git a/roq-data/deployment/pom.xml b/roq-data/deployment/pom.xml index b93a475e..1dd19b07 100644 --- a/roq-data/deployment/pom.xml +++ b/roq-data/deployment/pom.xml @@ -29,18 +29,32 @@ quarkus-roq-data ${project.version} + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + + + io.vertx + vertx-core + io.quarkus quarkus-junit5-internal test - org.yaml - snakeyaml + io.quarkus + quarkus-rest + test - io.vertx - vertx-core + io.rest-assured + rest-assured + test diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/DataConverter.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/DataConverter.java new file mode 100644 index 00000000..84169695 --- /dev/null +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/DataConverter.java @@ -0,0 +1,6 @@ +package io.quarkiverse.roq.data.deployment; + +public interface DataConverter { + + Object convert(String content); +} diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonConverter.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonConverter.java index 13df149d..7b58d358 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonConverter.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonConverter.java @@ -1,12 +1,10 @@ package io.quarkiverse.roq.data.deployment; -import java.util.function.Function; +import io.vertx.core.json.Json; -import io.vertx.core.json.JsonObject; - -public class JsonConverter implements Function { +public class JsonConverter implements DataConverter { @Override - public JsonObject apply(String content) { - return new JsonObject(content); + public Object convert(String content) { + return Json.decodeValue(content); } } diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonObjectConverter.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonObjectConverter.java index 15cc80ad..f737dd28 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonObjectConverter.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/JsonObjectConverter.java @@ -1,9 +1,5 @@ package io.quarkiverse.roq.data.deployment; -import java.util.function.Function; - -import io.vertx.core.json.JsonObject; - public class JsonObjectConverter { private static final YamlConverter YAML_CONVERTER = new YamlConverter(); @@ -15,9 +11,9 @@ public enum Extensions { YML(".yml", YAML_CONVERTER); private final String extension; - private final Function converter; + private final DataConverter converter; - Extensions(String extension, Function converter) { + Extensions(String extension, DataConverter converter) { this.extension = extension; this.converter = converter; } @@ -26,8 +22,8 @@ public String getExtension() { return extension; } - public JsonObject convert(String content) { - return this.converter.apply(content); + public Object convert(String content) { + return this.converter.convert(content); } } diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/ReadRoqDataProcessor.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/ReadRoqDataProcessor.java index cad5256b..2e0e6d5d 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/ReadRoqDataProcessor.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/ReadRoqDataProcessor.java @@ -1,10 +1,13 @@ package io.quarkiverse.roq.data.deployment; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashSet; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Stream; @@ -13,7 +16,7 @@ import io.quarkiverse.roq.deployment.items.RoqProjectBuildItem; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; -import io.vertx.core.json.JsonObject; +import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem; public class ReadRoqDataProcessor { @@ -22,10 +25,11 @@ public class ReadRoqDataProcessor { RoqDataConfig roqDataConfig; @BuildStep - void scanDataFiles(RoqProjectBuildItem roqProject, RoqDataConfig config, BuildProducer dataProducer) { - if (roqProject != null) { + void scanDataFiles(RoqProjectBuildItem roqProject, RoqDataConfig config, BuildProducer dataProducer, + BuildProducer watchedFilesProducer) { + if (roqProject.isActive()) { try { - Set items = scanDataFiles(roqProject, config); + Collection items = scanDataFiles(roqProject, watchedFilesProducer, config); for (RoqDataJsonBuildItem item : items) { dataProducer.produce(item); @@ -38,37 +42,46 @@ void scanDataFiles(RoqProjectBuildItem roqProject, RoqDataConfig config, BuildPr } - public Set scanDataFiles(RoqProjectBuildItem roqProject, RoqDataConfig config) throws IOException { + public Collection scanDataFiles(RoqProjectBuildItem roqProject, + BuildProducer watchedFilesProducer, RoqDataConfig config) + throws IOException { - HashSet items = new HashSet<>(); + Map items = new HashMap<>(); roqProject.consumePathFromSite(config.dir(), (path) -> { if (Files.isDirectory(path)) { try (Stream pathStream = Files.find(path, Integer.MAX_VALUE, (p, a) -> Files.isRegularFile(p) && isExtensionSupported(p))) { - pathStream.forEach(addRoqDataJsonBuildItem(path, items)); + pathStream.forEach(addRoqDataJsonBuildItem(watchedFilesProducer, path, items)); } catch (IOException e) { throw new RuntimeException("Error while scanning data files on location %s".formatted(path.toString()), e); } } }); - return items; + return items.values(); } - private static Consumer addRoqDataJsonBuildItem(Path rootDir, HashSet items) { + private static Consumer addRoqDataJsonBuildItem(BuildProducer watchedFilesProducer, + Path rootDir, Map items) { return file -> { - var name = rootDir.relativize(file).toString().replaceAll("\\..*", "").replaceAll("\\/", "_"); + var name = rootDir.relativize(file).toString().replaceAll("\\..*", "").replaceAll("/", "_"); + if (items.containsKey(name)) { + throw new RuntimeException("Multiple data files found for name: " + name); + } String filename = file.getFileName().toString(); - + if (Path.of("").getFileSystem().equals(file.getFileSystem())) { + // We don't need to watch file out of the local filesystem + watchedFilesProducer.produce(new HotDeploymentWatchedFileBuildItem(file.toAbsolutePath().toString(), true)); + } JsonObjectConverter.Extensions converter = JsonObjectConverter.findExtensionConverter(filename); if (converter != null) { try { - JsonObject jsonObject = converter.convert(Files.readString(file, StandardCharsets.UTF_8)); - items.add(new RoqDataJsonBuildItem(name, jsonObject)); + Object value = converter.convert(Files.readString(file, StandardCharsets.UTF_8)); + items.put(name, new RoqDataJsonBuildItem(name, value)); } catch (IOException e) { - throw new RuntimeException("Was not possible to read the file %s using the converter for %s extension" + throw new UncheckedIOException("Error while decoding using %s converter: %s " .formatted(filename, converter.getExtension()), e); } } diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/RoqDataProcessor.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/RoqDataProcessor.java index 0ae2b6b9..33095ad7 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/RoqDataProcessor.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/RoqDataProcessor.java @@ -14,6 +14,7 @@ import io.quarkus.deployment.annotations.ExecutionTime; import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; class RoqDataProcessor { @@ -34,12 +35,22 @@ void generateInjectable(BuildProducer beansProducer, for (RoqDataJsonBuildItem roqData : roqDataJsonBuildItems) { LOGGER.info("Creating synthetic bean with identifier " + roqData.getName()); - beansProducer.produce(SyntheticBeanBuildItem.configure(JsonObject.class) - .scope(ApplicationScoped.class) - .setRuntimeInit() - .addQualifier().annotation(Named.class).addValue("value", roqData.getName()).done() - .runtimeValue(recorder.createRoqDataJson(roqData.getJsonObject())) - .done()); + if (roqData.getData() instanceof JsonObject) { + beansProducer.produce(SyntheticBeanBuildItem.configure(JsonObject.class) + .scope(ApplicationScoped.class) + .setRuntimeInit() + .addQualifier().annotation(Named.class).addValue("value", roqData.getName()).done() + .runtimeValue(recorder.createRoqDataJson(roqData.getData())) + .done()); + } else if (roqData.getData() instanceof JsonArray) { + beansProducer.produce(SyntheticBeanBuildItem.configure(JsonArray.class) + .scope(ApplicationScoped.class) + .setRuntimeInit() + .addQualifier().annotation(Named.class).addValue("value", roqData.getName()).done() + .runtimeValue(recorder.createRoqDataJson(roqData.getData())) + .done()); + } + } } diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/YamlConverter.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/YamlConverter.java index 007e61ed..099060f6 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/YamlConverter.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/YamlConverter.java @@ -1,17 +1,35 @@ package io.quarkiverse.roq.data.deployment; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; import java.util.Map; -import java.util.function.Function; -import org.yaml.snakeyaml.Yaml; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; -public class YamlConverter implements Function { +public class YamlConverter implements DataConverter { + + private static final ObjectMapper YAML_READER = new ObjectMapper(new YAMLFactory()); + + @SuppressWarnings("unchecked") @Override - public JsonObject apply(String content) { - Yaml yaml = new Yaml(); - Map map = yaml.loadAs(content, Map.class); - return new JsonObject(map); + public Object convert(String content) { + try { + JsonNode rootNode = YAML_READER.readTree(content); + if (rootNode.isObject()) { + return new JsonObject(YAML_READER.convertValue(rootNode, Map.class)); + } else if (rootNode.isArray()) { + return new JsonArray(YAML_READER.convertValue(rootNode, List.class)); + } else { + throw new IOException("Unsupported YAML root element type: " + rootNode.getNodeType()); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } } } diff --git a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/items/RoqDataJsonBuildItem.java b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/items/RoqDataJsonBuildItem.java index 61ae24a5..1f0d636a 100644 --- a/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/items/RoqDataJsonBuildItem.java +++ b/roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/items/RoqDataJsonBuildItem.java @@ -3,7 +3,6 @@ import java.util.Objects; import io.quarkus.builder.item.MultiBuildItem; -import io.vertx.core.json.JsonObject; /** * A build item representing a Roq data file. @@ -18,33 +17,33 @@ public final class RoqDataJsonBuildItem extends MultiBuildItem { /** * The content of the Roq data file as a JSON string. */ - private final JsonObject jsonObject; + private final Object data; - public RoqDataJsonBuildItem(String name, JsonObject jsonObject) { + public RoqDataJsonBuildItem(String name, Object data) { this.name = name; - this.jsonObject = jsonObject; + this.data = data; } public String getName() { return name; } - public JsonObject getJsonObject() { - return jsonObject; + public Object getData() { + return data; } @Override - public boolean equals(Object o) { - if (this == o) + public boolean equals(Object object) { + if (this == object) return true; - if (o == null || getClass() != o.getClass()) + if (object == null || getClass() != object.getClass()) return false; - RoqDataJsonBuildItem that = (RoqDataJsonBuildItem) o; - return Objects.equals(name, that.name); + RoqDataJsonBuildItem that = (RoqDataJsonBuildItem) object; + return Objects.equals(name, that.name) && Objects.equals(data, that.data); } @Override public int hashCode() { - return Objects.hash(name); + return Objects.hash(name, data); } } diff --git a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataDevModeTest.java b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataDevModeTest.java index b0b8a519..037c1959 100644 --- a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataDevModeTest.java +++ b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataDevModeTest.java @@ -1,23 +1,54 @@ package io.quarkiverse.roq.data.test; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Assertions; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.inject.Named; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import io.quarkus.test.QuarkusDevModeTest; +import io.restassured.RestAssured; +import io.vertx.core.json.JsonObject; public class RoqDataDevModeTest { // Start hot reload (DevMode) test with your extension loaded @RegisterExtension static final QuarkusDevModeTest devModeTest = new QuarkusDevModeTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); + .withApplicationRoot((jar) -> jar + .addClass(Hello.class) + .addAsResource("foo.json", "site/data/foo.json")); @Test - public void writeYourOwnDevModeTest() { - // Write your dev mode tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-hot-reload for more information - Assertions.assertTrue(true, "Add dev mode assertions to " + getClass().getName()); + public void changeData() { + RestAssured.given() + .get("/hello") + .then() + .statusCode(200) + .body(Matchers.equalTo("Super Heroes from Json")); + devModeTest.modifyResourceFile("site/data/foo.json", (content) -> content.replace("Super", "Mega")); + RestAssured.given() + .get("/hello") + .then() + .statusCode(200) + .body(Matchers.equalTo("Mega Heroes from Json")); + } + + @ApplicationScoped + @Path("/hello") + public static class Hello { + + @Inject + @Named("foo") + JsonObject foo; + + @GET + public String foo() { + return foo.getString("name"); + } } } diff --git a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingCustomLocationTest.java b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingCustomLocationTest.java index a5443bbe..1bba5cc5 100644 --- a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingCustomLocationTest.java +++ b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingCustomLocationTest.java @@ -1,9 +1,7 @@ package io.quarkiverse.roq.data.test; -import jakarta.enterprise.inject.Instance; import jakarta.inject.Inject; import jakarta.inject.Named; -import jakarta.inject.Singleton; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.junit.jupiter.api.Assertions; @@ -18,53 +16,39 @@ public class RoqDataInjectableBeanLookingCustomLocationTest { @RegisterExtension final static QuarkusUnitTest devMode = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClass(SimpleBean.class) .add(new StringAsset("quarkus.roq.site-dir=./\nquarkus.roq.data.dir=test-data\n"), "application.properties")); @Inject - Instance simpleBean; + @Named("foo") + JsonObject foo; - @Test - public void whenUseFooYamlShouldGetSuperHeroesYaml() { - String fromYaml = simpleBean.get().getNameFromBar(); + @Inject + @Named("bar") + JsonObject bar; - Assertions.assertEquals("Super Heroes from Yaml custom", fromYaml); - } + @Inject + @Named("baz") + JsonObject baz; @Test - public void whenUseFooYmlShouldGetSuperHeroesYml() { - String fromYml = simpleBean.get().getNameFromBaz(); + public void foo() { + String fromYaml = foo.getString("name"); - Assertions.assertEquals("Super Heroes from Yml custom", fromYml); + Assertions.assertEquals("Super Heroes from Json custom", fromYaml); } - @Singleton - static class SimpleBean { - - @Inject - @Named("foo") - JsonObject foo; - - @Inject - @Named("bar") - JsonObject bar; - - @Inject - @Named("baz") - JsonObject baz; - - public String getNameFromFoo() { - return foo.getString("name"); - } - - public String getNameFromBar() { - return bar.getString("name"); - } + @Test + public void bar() { + String fromYml = bar.getString("name"); - public String getNameFromBaz() { - return baz.getString("name"); - } + Assertions.assertEquals("Super Heroes from Yaml custom", fromYml); + } + @Test + public void baz() { + String fromYml = baz.getString("name"); + Assertions.assertEquals("Super Heroes from Yml custom", fromYml); } + } diff --git a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingDefaultLocationTest.java b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingDefaultLocationTest.java index 44f71627..6af653e5 100644 --- a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingDefaultLocationTest.java +++ b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataInjectableBeanLookingDefaultLocationTest.java @@ -1,9 +1,7 @@ package io.quarkiverse.roq.data.test; -import jakarta.enterprise.inject.Instance; import jakarta.inject.Inject; import jakarta.inject.Named; -import jakarta.inject.Singleton; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.junit.jupiter.api.Assertions; @@ -11,68 +9,64 @@ import org.junit.jupiter.api.extension.RegisterExtension; import io.quarkus.test.QuarkusUnitTest; +import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; public class RoqDataInjectableBeanLookingDefaultLocationTest { @RegisterExtension final static QuarkusUnitTest devMode = new QuarkusUnitTest() - .withApplicationRoot((jar) -> jar - .addClass(SimpleBean.class) + .addAsResource("fromResource.json", "site/data/fromResource.json") .add(new StringAsset("quarkus.roq.site-dir=src/test/site"), "application.properties")); @Inject - Instance simpleBean; + @Named("foo") + JsonObject foo; - @Test - public void whenUseFooJsonShouldGetSuperHeroesJson() { - String fromJson = simpleBean.get().getNameFromFoo(); + @Inject + @Named("bar") + JsonObject bar; + @Inject + @Named("baz") + JsonObject baz; + + @Inject + @Named("list") + JsonArray list; + + @Inject + @Named("fromResource") + JsonObject fromResource; + + @Test + public void foo() { + String fromJson = foo.getString("name"); Assertions.assertEquals("Super Heroes from Json", fromJson); } @Test - public void whenUseFooYamlShouldGetSuperHeroesYaml() { - String fromYaml = simpleBean.get().getNameFromBar(); - + public void bar() { + String fromYaml = bar.getString("name"); Assertions.assertEquals("Super Heroes from Yaml", fromYaml); } @Test - public void whenUseFooYmlShouldGetSuperHeroesYml() { - String fromYml = simpleBean.get().getNameFromBaz(); - + public void baz() { + String fromYml = baz.getString("name"); Assertions.assertEquals("Super Heroes from Yml", fromYml); } - @Singleton - static class SimpleBean { - - @Inject - @Named("foo") - JsonObject foo; - - @Inject - @Named("bar") - JsonObject bar; - - @Inject - @Named("baz") - JsonObject baz; - - public String getNameFromFoo() { - return foo.getString("name"); - } - - public String getNameFromBar() { - return bar.getString("name"); - } - - public String getNameFromBaz() { - return baz.getString("name"); - } + @Test + public void list() { + Assertions.assertEquals(3, list.size()); + } + @Test + public void fromResource() { + Assertions.assertEquals("Hello from resource", fromResource.getString("name")); } + } diff --git a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataTest.java b/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataTest.java deleted file mode 100644 index 1ab4f97e..00000000 --- a/roq-data/deployment/src/test/java/io/quarkiverse/roq/data/test/RoqDataTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.quarkiverse.roq.data.test; - -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.test.QuarkusUnitTest; - -public class RoqDataTest { - - // Start unit test with your extension loaded - @RegisterExtension - static final QuarkusUnitTest unitTest = new QuarkusUnitTest() - .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); - - @Test - public void writeYourOwnUnitTest() { - // Write your unit tests here - see the testing extension guide https://quarkus.io/guides/writing-extensions#testing-extensions for more information - Assertions.assertTrue(true, "Add some assertions to " + getClass().getName()); - } -} diff --git a/roq-data/deployment/src/test/resources/foo.json b/roq-data/deployment/src/test/resources/foo.json new file mode 100644 index 00000000..b0df5f61 --- /dev/null +++ b/roq-data/deployment/src/test/resources/foo.json @@ -0,0 +1,3 @@ +{ + "name": "Super Heroes from Json" +} \ No newline at end of file diff --git a/roq-data/deployment/src/test/resources/fromResource.json b/roq-data/deployment/src/test/resources/fromResource.json new file mode 100644 index 00000000..85a617e3 --- /dev/null +++ b/roq-data/deployment/src/test/resources/fromResource.json @@ -0,0 +1,3 @@ +{ + "name": "Hello from resource" +} \ No newline at end of file diff --git a/roq-data/deployment/src/test/site/data/list.yaml b/roq-data/deployment/src/test/site/data/list.yaml new file mode 100644 index 00000000..fb4989f8 --- /dev/null +++ b/roq-data/deployment/src/test/site/data/list.yaml @@ -0,0 +1,3 @@ +- name: Super Heroes 1 from Yaml +- name: Super Heroes 2 from Yaml +- name: Super Heroes 3 from Yaml \ No newline at end of file diff --git a/roq-data/runtime/src/main/java/io/quarkiverse/roq/data/runtime/RoqDataRecorder.java b/roq-data/runtime/src/main/java/io/quarkiverse/roq/data/runtime/RoqDataRecorder.java index a9dec89b..a3e9b0c2 100644 --- a/roq-data/runtime/src/main/java/io/quarkiverse/roq/data/runtime/RoqDataRecorder.java +++ b/roq-data/runtime/src/main/java/io/quarkiverse/roq/data/runtime/RoqDataRecorder.java @@ -2,12 +2,11 @@ import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; -import io.vertx.core.json.JsonObject; @Recorder public class RoqDataRecorder { - public RuntimeValue createRoqDataJson(JsonObject jsonObject) { - return new RuntimeValue<>(jsonObject); + public RuntimeValue createRoqDataJson(Object data) { + return new RuntimeValue<>(data); } }