Skip to content

Commit 9586540

Browse files
committed
[#231] Extend maven plugin to intake preferred language as a parameter during open api and json schema generation
Signed-off-by: Kartheeswaran Kalidass <[email protected]>
1 parent fd73a3f commit 9586540

File tree

7 files changed

+168
-9
lines changed

7 files changed

+168
-9
lines changed

tools/sds-aspect-model-maven-plugin/src/main/java/io/openmanufacturing/sds/aspectmodel/GenerateJsonSchema.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import java.io.IOException;
1717
import java.io.OutputStream;
1818
import java.util.Locale;
19+
import java.util.Optional;
1920
import java.util.Set;
2021

2122
import org.apache.maven.plugin.MojoExecutionException;
2223
import org.apache.maven.plugin.MojoFailureException;
2324
import org.apache.maven.plugins.annotations.LifecyclePhase;
2425
import org.apache.maven.plugins.annotations.Mojo;
26+
import org.apache.maven.plugins.annotations.Parameter;
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
2729

@@ -39,16 +41,19 @@ public class GenerateJsonSchema extends AspectModelMojo {
3941
private final Logger logger = LoggerFactory.getLogger( GenerateJsonSchema.class );
4042
private final AspectModelJsonSchemaGenerator generator = new AspectModelJsonSchemaGenerator();
4143

42-
@Override
44+
@Parameter( defaultValue = "en" )
45+
private String language;
46+
47+
@Override
4348
public void execute() throws MojoExecutionException, MojoFailureException {
4449
validateParameters();
4550

4651
final Set<VersionedModel> aspectModels = loadModelsOrFail();
52+
final Locale locale = Optional.ofNullable( language ).map( Locale::forLanguageTag ).orElse( Locale.ENGLISH );
4753
try {
4854
for ( final VersionedModel aspectModel : aspectModels ) {
4955
final Aspect aspect = AspectModelLoader.fromVersionedModelUnchecked( aspectModel );
50-
//TODO to extend so that language can be selected as in bamm-cli
51-
final JsonNode schema = generator.apply( aspect, Locale.ENGLISH );
56+
final JsonNode schema = generator.apply( aspect, locale );
5257

5358
final OutputStream out = getStreamForFile( aspect.getName() + ".schema.json", outputDirectory );
5459
final ObjectMapper objectMapper = new ObjectMapper();

tools/sds-aspect-model-maven-plugin/src/main/java/io/openmanufacturing/sds/aspectmodel/GenerateOpenApiSpec.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.InputStream;
2020
import java.io.OutputStream;
2121
import java.nio.charset.StandardCharsets;
22+
import java.util.Locale;
2223
import java.util.Optional;
2324
import java.util.Set;
2425

@@ -80,18 +81,22 @@ public class GenerateOpenApiSpec extends AspectModelMojo {
8081
@Parameter( required = true )
8182
private String outputFormat = "";
8283

84+
@Parameter( defaultValue = "en" )
85+
private String language;
86+
8387
@Override
8488
public void execute() throws MojoExecutionException, MojoFailureException {
8589
validateParameters();
8690

8791
final Set<VersionedModel> aspectModels = loadModelsOrFail();
92+
final Locale locale = Optional.ofNullable( language ).map( Locale::forLanguageTag ).orElse( Locale.ENGLISH );
8893
try {
8994
final OpenApiFormat format = OpenApiFormat.valueOf( outputFormat.toUpperCase() );
9095
if ( format.equals( OpenApiFormat.JSON ) ) {
91-
generateOpenApiSpecJson( aspectModels );
96+
generateOpenApiSpecJson( aspectModels, locale );
9297
return;
9398
}
94-
generateOpenApiSpecYaml( aspectModels );
99+
generateOpenApiSpecYaml( aspectModels, locale );
95100
logger.info( "Successfully generated OpenAPI specification for Aspect Models." );
96101
} catch ( final IllegalArgumentException exception ) {
97102
throw new MojoExecutionException( "Invalid output format.", exception );
@@ -109,7 +114,7 @@ protected void validateParameters() throws MojoExecutionException {
109114
super.validateParameters();
110115
}
111116

112-
private void generateOpenApiSpecJson( final Set<VersionedModel> aspectModels ) throws MojoExecutionException {
117+
private void generateOpenApiSpecJson( final Set<VersionedModel> aspectModels, final Locale locale ) throws MojoExecutionException {
113118
for ( final VersionedModel aspectModel : aspectModels ) {
114119
final Aspect aspect = AspectModelLoader.fromVersionedModelUnchecked( aspectModel );
115120
JsonNode result = JsonNodeFactory.instance.objectNode();
@@ -122,7 +127,7 @@ private void generateOpenApiSpecJson( final Set<VersionedModel> aspectModels ) t
122127
}
123128
}
124129
final JsonNode jsonSpec = generator.applyForJson( aspect, useSemanticApiVersion, aspectApiBaseUrl, Optional.ofNullable( aspectResourcePath ),
125-
Optional.of( result ), includeQueryApi, getPagingFromArgs() );
130+
Optional.of( result ), includeQueryApi, getPagingFromArgs(), locale );
126131

127132
final OutputStream out = getStreamForFile( aspect.getName() + ".oai.json", outputDirectory );
128133
try {
@@ -134,12 +139,12 @@ private void generateOpenApiSpecJson( final Set<VersionedModel> aspectModels ) t
134139
}
135140
}
136141

137-
private void generateOpenApiSpecYaml( final Set<VersionedModel> aspectModels ) throws MojoExecutionException {
142+
private void generateOpenApiSpecYaml( final Set<VersionedModel> aspectModels, final Locale locale ) throws MojoExecutionException {
138143
for ( final VersionedModel aspectModel : aspectModels ) {
139144
final Aspect aspect = AspectModelLoader.fromVersionedModelUnchecked( aspectModel );
140145
try {
141146
final String yamlSpec = generator.applyForYaml( aspect, useSemanticApiVersion, aspectApiBaseUrl, Optional.ofNullable( aspectResourcePath ),
142-
getFileAsString( aspectParameterFile ), includeQueryApi, getPagingFromArgs() );
147+
getFileAsString( aspectParameterFile ), includeQueryApi, getPagingFromArgs(), locale );
143148
final OutputStream out = getStreamForFile( aspect.getName() + ".oai.yaml", outputDirectory );
144149
out.write( yamlSpec.getBytes() );
145150
out.flush();

tools/sds-aspect-model-maven-plugin/src/test/java/io/openmanufacturing/sds/aspectmodel/GenerateJsonSchemaTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ public void testGenerateJsonSchemaTest() throws Exception {
3232
deleteGeneratedFile( "Aspect.schema.json" );
3333
}
3434

35+
/**
36+
* Verify that a preferred language can be chosen during json schema generation from the given aspect model.
37+
* @throws Exception in case of any error during execution of the test.
38+
*/
39+
@Test
40+
public void testGenerateOpenApiSpecJsonValidAspectModelWithLanguageParameter() throws Exception {
41+
final File testPom = getTestFile( "src/test/resources/generate-schema-json-pom-valid-aspect-model-language.xml" );
42+
final Mojo generateOpenApiSpec = lookupMojo( "generateJsonSchema", testPom );
43+
assertThatCode( generateOpenApiSpec::execute ).doesNotThrowAnyException();
44+
45+
assertGeneratedFileExists( "AspectWithEnglishAndGermanDescription.schema.json" );
46+
deleteGeneratedFile( "AspectWithEnglishAndGermanDescription.schema.json" );
47+
}
3548
}

tools/sds-aspect-model-maven-plugin/src/test/java/io/openmanufacturing/sds/aspectmodel/GenerateOpenApiSpecTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ public void testGenerateOpenApiSpecJsonValidAspectModel() throws Exception {
3333
deleteGeneratedFile( "Aspect.oai.json" );
3434
}
3535

36+
/**
37+
* Verify that a preferred language can be chosen during open api specification generation from the given aspect model.
38+
* @throws Exception in case of any error during execution of the test.
39+
*/
40+
@Test
41+
public void testGenerateOpenApiSpecJsonValidAspectModelWithLanguageParameter() throws Exception {
42+
final File testPom = getTestFile( "src/test/resources/generate-openapi-spec-json-pom-valid-aspect-model-language.xml" );
43+
final Mojo generateOpenApiSpec = lookupMojo( "generateOpenApiSpec", testPom );
44+
assertThatCode( generateOpenApiSpec::execute ).doesNotThrowAnyException();
45+
46+
assertGeneratedFileExists( "AspectWithEnglishAndGermanDescription.oai.json" );
47+
deleteGeneratedFile( "AspectWithEnglishAndGermanDescription.oai.json" );
48+
}
49+
3650
@Test
3751
public void testGenerateOpenApiSpecYamlValidAspectModel() throws Exception {
3852
final File testPom = getTestFile( "src/test/resources/generate-openapi-spec-yaml-pom-valid-aspect-model.xml" );
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
4+
~
5+
~ See the AUTHORS file(s) distributed with this work for additional
6+
~ information regarding authorship.
7+
~
8+
~ This Source Code Form is subject to the terms of the Mozilla Public
9+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
10+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
~
12+
~ SPDX-License-Identifier: MPL-2.0
13+
-->
14+
15+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>io.openmanufacturing</groupId>
20+
<artifactId>test-generate-openapi-spec-mojo</artifactId>
21+
<version>1.0</version>
22+
<packaging>jar</packaging>
23+
<name>Test Generate OpenApi Spec Mojo</name>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>sds-aspect-model-maven-plugin</artifactId>
29+
<configuration>
30+
<modelsRootDirectory>${basedir}/../../core/sds-test-aspect-models/src/main/resources/valid/bamm_2_0_0</modelsRootDirectory>
31+
<includes>
32+
<include>urn:bamm:io.openmanufacturing.test:1.0.0#AspectWithEnglishAndGermanDescription</include>
33+
</includes>
34+
<aspectApiBaseUrl>http://example.com</aspectApiBaseUrl>
35+
<outputDirectory>${basedir}/target/test-artifacts</outputDirectory>
36+
<outputFormat>json</outputFormat>
37+
<language>de</language>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
4+
~
5+
~ See the AUTHORS file(s) distributed with this work for additional
6+
~ information regarding authorship.
7+
~
8+
~ This Source Code Form is subject to the terms of the Mozilla Public
9+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
10+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
~
12+
~ SPDX-License-Identifier: MPL-2.0
13+
-->
14+
15+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>io.openmanufacturing</groupId>
20+
<artifactId>test-mojos</artifactId>
21+
<version>1.0</version>
22+
<packaging>jar</packaging>
23+
<name>Test Mojos</name>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>sds-aspect-model-maven-plugin</artifactId>
29+
<configuration>
30+
<modelsRootDirectory>${basedir}/../../core/sds-test-aspect-models/src/main/resources/valid/bamm_2_0_0</modelsRootDirectory>
31+
<includes>
32+
<include>urn:bamm:io.openmanufacturing.test:1.0.0#AspectWithEnglishAndGermanDescription</include>
33+
</includes>
34+
<outputDirectory>${basedir}/target/test-artifacts</outputDirectory>
35+
<language>de</language>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
4+
~
5+
~ See the AUTHORS file(s) distributed with this work for additional
6+
~ information regarding authorship.
7+
~
8+
~ This Source Code Form is subject to the terms of the Mozilla Public
9+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
10+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
~
12+
~ SPDX-License-Identifier: MPL-2.0
13+
-->
14+
15+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>io.openmanufacturing</groupId>
20+
<artifactId>test-mojos</artifactId>
21+
<version>1.0</version>
22+
<packaging>jar</packaging>
23+
<name>Test Mojos</name>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>sds-aspect-model-maven-plugin</artifactId>
29+
<configuration>
30+
<modelsRootDirectory>${basedir}/../../core/sds-test-aspect-models/src/main/resources/valid/bamm_2_0_0</modelsRootDirectory>
31+
<includes>
32+
<include>urn:bamm:io.openmanufacturing.test:1.0.0#AspectWithEnglishAndGermanDescription</include>
33+
</includes>
34+
<outputDirectory>${basedir}/target/test-artifacts</outputDirectory>
35+
<language>de</language>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>

0 commit comments

Comments
 (0)