You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2020/02/12 22:09:31 UTC
[isis] 01/03: ISIS-2180: adds SwaggerExporter.
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
commit a0068ad18b24e0a3657d2cd30a7db9c33c0795e8
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Feb 12 18:18:34 2020 +0000
ISIS-2180: adds SwaggerExporter.
NB: some earlier commits tagged to ISIS-2285 (domain model validator) should have been tagged with ISIS-2180 also.
---
.../pages/metadata-api/SwaggerService.adoc | 1 -
.../adoc/modules/integtestsupport/pages/about.adoc | 20 ++++
.../pages/domain-model-validator.adoc | 34 +++++++
.../integtestsupport/pages/swagger-exporter.adoc | 39 ++++++++
.../integtestsupport/partials/module-nav.adoc | 2 +
.../applib/swagger/SwaggerExporter.java | 104 +++++++++++++++++++++
6 files changed, 199 insertions(+), 1 deletion(-)
diff --git a/api/applib/src/main/adoc/modules/applib-svc/pages/metadata-api/SwaggerService.adoc b/api/applib/src/main/adoc/modules/applib-svc/pages/metadata-api/SwaggerService.adoc
index 5fe87f4..07fa258 100644
--- a/api/applib/src/main/adoc/modules/applib-svc/pages/metadata-api/SwaggerService.adoc
+++ b/api/applib/src/main/adoc/modules/applib-svc/pages/metadata-api/SwaggerService.adoc
@@ -1,4 +1,3 @@
-[[SwaggerService]]
= `SwaggerService`
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or ag [...]
:page-partial:
diff --git a/testing/integtestsupport/adoc/modules/integtestsupport/pages/about.adoc b/testing/integtestsupport/adoc/modules/integtestsupport/pages/about.adoc
index 3c45c55..5d23cc6 100644
--- a/testing/integtestsupport/adoc/modules/integtestsupport/pages/about.adoc
+++ b/testing/integtestsupport/adoc/modules/integtestsupport/pages/about.adoc
@@ -580,6 +580,26 @@ To see this in action in the xref:docs:starters:simpleapp.adoc[simpleapp] starte
* change `isis.applib.annotation.action.explicit` to `false` in `application.yml`
* introduce an error, for example by renaming `default0UpdateName` to `default0UpdateFoo`.
+
+== Swagger Exporter
+
+@SpringBootTest(
+classes = {
+ApplicationIntegTestAbstract.AppManifest.class,
+IsisModuleViewerRestfulObjectsJaxrsResteasy4.class
+}
+)
+class SwaggerExport_IntegTest extends ApplicationIntegTestAbstract {
+
+ @Inject ServiceRegistry serviceRegistry;
+
+ @Test
+ void export() throws IOException {
+ new SwaggerExporter(serviceRegistry)
+ .export(SwaggerService.Visibility.PRIVATE, SwaggerService.Format.JSON);
+ }
+}
+
== Maven Configuration
Apache Isis' integ test support is most easily configured through a dependency on the `isis-mavendeps-integtests` module:
diff --git a/testing/integtestsupport/adoc/modules/integtestsupport/pages/domain-model-validator.adoc b/testing/integtestsupport/adoc/modules/integtestsupport/pages/domain-model-validator.adoc
new file mode 100644
index 0000000..fefa5d8
--- /dev/null
+++ b/testing/integtestsupport/adoc/modules/integtestsupport/pages/domain-model-validator.adoc
@@ -0,0 +1,34 @@
+= Domain Model Validator
+:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or ag [...]
+
+Domain model validation checks for a number of semantic errors with the domain model.
+For example, if a supporting method (such as `default0UpdateName()` in `SimpleObject`) is misspelt, then this would be flagged.
+
+Running up the application will flag any metamodel validation issues, as will running an integration test.
+However, depending upon configuration, the metamodel may only be built lazily, meaning that issues will only be detected while the application is running, rather than at bootstrap.
+
+The `DomainModelValidator` is a simple utility class that will fully rebuild the metamodel if required, and then verify that there are no issues.
+
+The example below is taken from the xref:docs:starters:simpleapp.adoc[simpleapp] starter app:
+
+[source,java]
+.ValidateDomainModel_IntegTest.java
+----
+class ValidateDomainModel_IntegTest
+ extends ApplicationIntegTestAbstract {
+
+ @Inject ServiceRegistry serviceRegistry;
+
+ @Test
+ void validate() {
+ new DomainModelValidator(serviceRegistry).assertValid();
+ }
+}
+----
+
+To see this in action:
+
+* change `isis.applib.annotation.action.explicit` to `false` in `application.yml`
+* introduce an error, for example by renaming `default0UpdateName` to `default0UpdateFoo`.
+
+
diff --git a/testing/integtestsupport/adoc/modules/integtestsupport/pages/swagger-exporter.adoc b/testing/integtestsupport/adoc/modules/integtestsupport/pages/swagger-exporter.adoc
new file mode 100644
index 0000000..572839a
--- /dev/null
+++ b/testing/integtestsupport/adoc/modules/integtestsupport/pages/swagger-exporter.adoc
@@ -0,0 +1,39 @@
+= Swagger Exporter
+:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or ag [...]
+
+
+The xref:refguide:applib-svc:metadata-api/SwaggerService.adoc[SwaggerService] serializes the domain's meta model as a Swagger 2.0 specification, either as JSON or YAML.
+The `SwaggerExporter` in turn is a utility class that simply writes this serialization out to a file from within an integration test.
+The idea is that subsequent jobs in the build process could then pick up this file, typically to code-generation REST clients in other languages.
+
+The example below is taken from the xref:docs:starters:simpleapp.adoc[simpleapp] starter app:
+
+[source,java]
+.SwaggerExport_IntegTest.java
+----
+@SpringBootTest(
+ classes = {
+ ApplicationIntegTestAbstract.AppManifest.class, //<1>
+ IsisModuleViewerRestfulObjectsJaxrsResteasy4.class //<2>
+ }
+)
+class SwaggerExport_IntegTest extends ApplicationIntegTestAbstract {
+
+ @Inject ServiceRegistry serviceRegistry;
+
+ @Test
+ void export() throws IOException {
+ val swaggerExporter = new SwaggerExporter(serviceRegistry); // <3>
+ swaggerExporter.export(
+ SwaggerService.Visibility.PRIVATE, // <4>
+ SwaggerService.Format.JSON); // <4>
+ }
+}
+----
+<1> bootstrap using the usual `AppManifest` for integration tests ...
+<2> \... and _also_ include the xref:vro:ROOT:about.adoc[Restful Objects] viewer because this contains the implementation of xref:refguide:applib-svc:metadata-api/SwaggerService.adoc[SwaggerService].
+<3> instantiate the `SwagerExporter` utility class
+<4> Export specifies the required visibility and format.
+There are also overloads of this method to specify the output directory and filename.
+
+By default the file will be written to `target/generated-resources/swagger-export`.
diff --git a/testing/integtestsupport/adoc/modules/integtestsupport/partials/module-nav.adoc b/testing/integtestsupport/adoc/modules/integtestsupport/partials/module-nav.adoc
index 29354fd..3c1b76c 100644
--- a/testing/integtestsupport/adoc/modules/integtestsupport/partials/module-nav.adoc
+++ b/testing/integtestsupport/adoc/modules/integtestsupport/partials/module-nav.adoc
@@ -1 +1,3 @@
* xref:testing:integtestsupport:about.adoc[Integ Test Support]
+* xref:testing:integtestsupport:domain-model-validator.adoc[Domain Model Validator]
+* xref:testing:integtestsupport:swagger-exporter.adoc[Swagger Exporter]
diff --git a/testing/integtestsupport/applib/src/main/java/org/apache/isis/testing/integtestsupport/applib/swagger/SwaggerExporter.java b/testing/integtestsupport/applib/src/main/java/org/apache/isis/testing/integtestsupport/applib/swagger/SwaggerExporter.java
new file mode 100644
index 0000000..09ba76a
--- /dev/null
+++ b/testing/integtestsupport/applib/src/main/java/org/apache/isis/testing/integtestsupport/applib/swagger/SwaggerExporter.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.testing.integtestsupport.applib.swagger;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Collections;
+
+import javax.inject.Inject;
+
+import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.applib.services.swagger.SwaggerService;
+
+import lombok.extern.log4j.Log4j2;
+
+/**
+ *
+ * @since 2.0
+ *
+ */
+@Log4j2
+public class SwaggerExporter {
+
+ private final SwaggerService swaggerService;
+
+ @Inject
+ public SwaggerExporter(final ServiceRegistry registry) {
+ this(registry.lookupServiceElseFail(SwaggerService.class));
+ }
+
+ public SwaggerExporter(final SwaggerService swaggerService) {
+ this.swaggerService = swaggerService;
+ }
+
+ public void export(SwaggerService.Visibility visibility, SwaggerService.Format format) throws IOException {
+ export(visibility, format, defaultOutputDir());
+ }
+
+ public void export(SwaggerService.Visibility visibility, SwaggerService.Format format, File outputDir) throws IOException {
+ export(visibility, format, outputDir, defaultFileNamePrefix());
+ }
+
+ public void export(SwaggerService.Visibility visibility, SwaggerService.Format format, File outputDir, String fileNamePrefix) throws IOException {
+ final File swaggerSpecFile = buildSwaggerSpecFile(outputDir, fileNamePrefix, visibility, format);
+ writeSwaggerSpec(visibility, format, swaggerSpecFile);
+ swaggerService.generateSwaggerSpec(visibility, format);
+ }
+
+
+ private File buildSwaggerSpecFile(
+ final File outputDir, final String fileNamePrefix,
+ final SwaggerService.Visibility visibility,
+ final SwaggerService.Format format) {
+ final String swaggerSpecName = fileNamePrefix + "-" + visibility + "." + format.name().toLowerCase();
+ return new File(outputDir, swaggerSpecName);
+ }
+
+ private void writeSwaggerSpec(
+ final SwaggerService.Visibility visibility,
+ final SwaggerService.Format format,
+ final File swaggerSpecFile) throws IOException {
+ final String swaggerSpec = swaggerService.generateSwaggerSpec(visibility, format);
+
+ createParentDirs(swaggerSpecFile);
+ Files.write(swaggerSpecFile.toPath(), Collections.singletonList(swaggerSpec));
+ }
+
+ private static void createParentDirs(File file) throws IOException {
+ File parent = file.getCanonicalFile().getParentFile();
+ if (parent != null) {
+ parent.mkdirs();
+ if (!parent.isDirectory()) {
+ throw new IOException("Unable to create parent directories of " + file);
+ }
+ }
+ }
+
+
+ private File defaultOutputDir() {
+ return new File("target/generated-resources/swagger-export");
+ }
+
+ private String defaultFileNamePrefix() {
+ return "swagger";
+ }
+
+}
\ No newline at end of file