From e88805ffd78b60a928f33d38c5a0e86b150ce13d Mon Sep 17 00:00:00 2001 From: Guy Davenport Date: Fri, 15 Nov 2024 19:42:44 +1300 Subject: [PATCH] updated String Utils, metadata and tests --- ...brapi.schema-tools.java-conventions.gradle | 2 +- .../schematools/cli/GenerateSubCommand.java | 18 +++--- java/core/build.gradle | 1 + .../metadata/GraphQLGeneratorMetadata.java | 41 ++++++++++++++ .../core/openapi/OpenAPIGenerator.java | 3 +- .../metadata/OpenAPIGeneratorMetadata.java | 55 ++++++++++++++++--- .../schematools/core/utils/StringUtils.java | 29 +++------- .../src/main/resources/graphql-metadata.yaml | 2 + .../src/main/resources/openapi-metadata.yaml | 2 + .../GraphQLGeneratorMetadataTest.java | 29 ++++++++++ .../OntModelGeneratorMetadataTest.java | 4 -- .../OpenAPIGeneratorMetadataTest.java | 29 ++++++++++ .../core/utils/StringUtilsTest.java | 36 +++++++++++- 13 files changed, 210 insertions(+), 41 deletions(-) create mode 100644 java/core/src/main/resources/graphql-metadata.yaml create mode 100644 java/core/src/main/resources/openapi-metadata.yaml create mode 100644 java/core/src/test/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadataTest.java create mode 100644 java/core/src/test/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadataTest.java diff --git a/java/buildSrc/src/main/groovy/brapi.schema-tools.java-conventions.gradle b/java/buildSrc/src/main/groovy/brapi.schema-tools.java-conventions.gradle index 653bc89..d727b8a 100644 --- a/java/buildSrc/src/main/groovy/brapi.schema-tools.java-conventions.gradle +++ b/java/buildSrc/src/main/groovy/brapi.schema-tools.java-conventions.gradle @@ -3,7 +3,7 @@ plugins { } group = 'org.brapi' -version = '0.12.0-SNAPSHOT' +version = '0.13.0-SNAPSHOT' sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 diff --git a/java/cli/src/main/java/org/brapi/schematools/cli/GenerateSubCommand.java b/java/cli/src/main/java/org/brapi/schematools/cli/GenerateSubCommand.java index 3a1ec23..662d6c8 100644 --- a/java/cli/src/main/java/org/brapi/schematools/cli/GenerateSubCommand.java +++ b/java/cli/src/main/java/org/brapi/schematools/cli/GenerateSubCommand.java @@ -17,6 +17,7 @@ import org.brapi.schematools.core.ontmodel.metadata.OntModelGeneratorMetadata; import org.brapi.schematools.core.ontmodel.options.OntModelGeneratorOptions; import org.brapi.schematools.core.openapi.OpenAPIGenerator; +import org.brapi.schematools.core.openapi.metadata.OpenAPIGeneratorMetadata; import org.brapi.schematools.core.openapi.options.OpenAPIGeneratorOptions; import org.brapi.schematools.core.response.Response; import org.brapi.schematools.core.xlsx.XSSFWorkbookGenerator; @@ -79,13 +80,16 @@ public void run() { case OPEN_API -> { OpenAPIGeneratorOptions options = optionsPath != null ? OpenAPIGeneratorOptions.load(optionsPath) : OpenAPIGeneratorOptions.load() ; - generateOpenAPISpecification(options); + OpenAPIGeneratorMetadata metadata = metadataPath != null ? + OpenAPIGeneratorMetadata.load(metadataPath) : OpenAPIGeneratorMetadata.load() ; + generateOpenAPISpecification(options, metadata); } case GRAPHQL -> { GraphQLGeneratorOptions options = optionsPath != null ? GraphQLGeneratorOptions.load(optionsPath) : GraphQLGeneratorOptions.load(); - GraphQLGeneratorMetadata metadata ; - generateGraphQLSchema(options); + GraphQLGeneratorMetadata metadata = metadataPath != null ? + GraphQLGeneratorMetadata.load(metadataPath) : GraphQLGeneratorMetadata.load() ; + generateGraphQLSchema(options, metadata); } case OWL -> { OntModelGeneratorOptions options = optionsPath != null ? @@ -119,10 +123,10 @@ public void run() { } } - private void generateGraphQLSchema(GraphQLGeneratorOptions options) { + private void generateGraphQLSchema(GraphQLGeneratorOptions options, GraphQLGeneratorMetadata metadata) { GraphQLGenerator graphQLGenerator = new GraphQLGenerator(options); - Response response = graphQLGenerator.generate(schemaDirectory); + Response response = graphQLGenerator.generate(schemaDirectory, metadata); response.onSuccessDoWithResult(this::outputIDLSchema).onFailDoWithResponse(this::printGraphQLSchemaErrors); } @@ -169,10 +173,10 @@ private void printGraphQLSchemaErrors(Response response) { } } - private void generateOpenAPISpecification(OpenAPIGeneratorOptions options) { + private void generateOpenAPISpecification(OpenAPIGeneratorOptions options, OpenAPIGeneratorMetadata metadata) { OpenAPIGenerator openAPIGenerator = new OpenAPIGenerator(options); - Response> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory); + Response> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory, metadata); response.onSuccessDoWithResult(this::outputOpenAPISpecification).onFailDoWithResponse(this::printOpenAPISpecificationErrors); } diff --git a/java/core/build.gradle b/java/core/build.gradle index b6eba35..39eb35d 100644 --- a/java/core/build.gradle +++ b/java/core/build.gradle @@ -15,6 +15,7 @@ dependencies { implementation 'org.apache.poi:poi:5.2.5' implementation 'org.apache.poi:poi-ooxml:5.2.5' implementation 'commons-beanutils:commons-beanutils:1.9.4' + implementation 'org.atteo:evo-inflector:1.3' } publishing { diff --git a/java/core/src/main/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadata.java b/java/core/src/main/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadata.java index 1f8306d..f7278fd 100644 --- a/java/core/src/main/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadata.java +++ b/java/core/src/main/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadata.java @@ -3,6 +3,11 @@ import lombok.Getter; import lombok.Setter; import org.brapi.schematools.core.metadata.Metadata; +import org.brapi.schematools.core.utils.ConfigurationUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; /** * Provides metadata for the GraphQL generation @@ -10,4 +15,40 @@ @Getter @Setter public class GraphQLGeneratorMetadata implements Metadata { + private String title ; + private String version ; + + /** + * Load the metadata from a metadata file in YAML or Json. The metadata file may have missing + * (defined) values, in these cases the default values are loaded. See {@link #load()} + * @param metadataFile The path to the metadata file in YAML or Json. + * @return The metadata loaded from the YAML or Json file. + * @throws IOException if the metadata file can not be found or is incorrectly formatted. + */ + public static GraphQLGeneratorMetadata load(Path metadataFile) throws IOException { + return ConfigurationUtils.load(metadataFile, GraphQLGeneratorMetadata.class) ; + } + + /** + * Load the default metadata + * @return The default metadata + */ + public static GraphQLGeneratorMetadata load() { + try { + return ConfigurationUtils.load("graphql-metadata.yaml", GraphQLGeneratorMetadata.class) ; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Load the metadata from an metadata input stream in YAML or Json. The metadata file may have missing + * (defined) values, in these cases the default values are loaded. See {@link #load()} + * @param inputStream The input stream in YAML or Json. + * @return The metadata loaded from input stream. + * @throws IOException if the input stream is not valid or the content is incorrectly formatted. + */ + public static GraphQLGeneratorMetadata load(InputStream inputStream) throws IOException { + return ConfigurationUtils.load(inputStream, GraphQLGeneratorMetadata.class) ; + } } diff --git a/java/core/src/main/java/org/brapi/schematools/core/openapi/OpenAPIGenerator.java b/java/core/src/main/java/org/brapi/schematools/core/openapi/OpenAPIGenerator.java index a0f04d0..99fbb88 100644 --- a/java/core/src/main/java/org/brapi/schematools/core/openapi/OpenAPIGenerator.java +++ b/java/core/src/main/java/org/brapi/schematools/core/openapi/OpenAPIGenerator.java @@ -132,7 +132,8 @@ private Response generate(String title, Collection types) { Info info = new Info(); info.setTitle(title); - info.setVersion(metadata.getVersion() != null ?metadata.getVersion() : "0.0.0"); + info.setTitle(metadata.getTitle() != null ? metadata.getTitle() : title); + info.setVersion(metadata.getVersion() != null ? metadata.getVersion() : "0.0.0"); openAPI.setInfo(info); diff --git a/java/core/src/main/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadata.java b/java/core/src/main/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadata.java index ac38c95..e5d05c0 100644 --- a/java/core/src/main/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadata.java +++ b/java/core/src/main/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadata.java @@ -3,6 +3,11 @@ import lombok.Getter; import lombok.Setter; import org.brapi.schematools.core.metadata.Metadata; +import org.brapi.schematools.core.utils.ConfigurationUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; /** * Provides metadata for the OpenAPI generation @@ -10,11 +15,47 @@ @Getter @Setter public class OpenAPIGeneratorMetadata implements Metadata { - String version ; - SingleGetMetadata singleGet = new SingleGetMetadata() ; - ListGetMetadata listGet = new ListGetMetadata() ; - PostMetadata post = new PostMetadata() ; - PutMetadata put = new PutMetadata() ; - DeleteMetadata delete = new DeleteMetadata() ; - SearchMetadata search = new SearchMetadata() ; + private String title ; + private String version ; + + private SingleGetMetadata singleGet = new SingleGetMetadata() ; + private ListGetMetadata listGet = new ListGetMetadata() ; + private PostMetadata post = new PostMetadata() ; + private PutMetadata put = new PutMetadata() ; + private DeleteMetadata delete = new DeleteMetadata() ; + private SearchMetadata search = new SearchMetadata() ; + + /** + * Load the metadata from a metadata file in YAML or Json. The metadata file may have missing + * (defined) values, in these cases the default values are loaded. See {@link #load()} + * @param metadataFile The path to the metadata file in YAML or Json. + * @return The metadata loaded from the YAML or Json file. + * @throws IOException if the metadata file can not be found or is incorrectly formatted. + */ + public static OpenAPIGeneratorMetadata load(Path metadataFile) throws IOException { + return ConfigurationUtils.load(metadataFile, OpenAPIGeneratorMetadata.class) ; + } + + /** + * Load the default metadata + * @return The default metadata + */ + public static OpenAPIGeneratorMetadata load() { + try { + return ConfigurationUtils.load("openapi-metadata.yaml", OpenAPIGeneratorMetadata.class) ; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Load the metadata from an metadata input stream in YAML or Json. The metadata file may have missing + * (defined) values, in these cases the default values are loaded. See {@link #load()} + * @param inputStream The input stream in YAML or Json. + * @return The metadata loaded from input stream. + * @throws IOException if the input stream is not valid or the content is incorrectly formatted. + */ + public static OpenAPIGeneratorMetadata load(InputStream inputStream) throws IOException { + return ConfigurationUtils.load(inputStream, OpenAPIGeneratorMetadata.class) ; + } } diff --git a/java/core/src/main/java/org/brapi/schematools/core/utils/StringUtils.java b/java/core/src/main/java/org/brapi/schematools/core/utils/StringUtils.java index 4409f19..a8f3730 100644 --- a/java/core/src/main/java/org/brapi/schematools/core/utils/StringUtils.java +++ b/java/core/src/main/java/org/brapi/schematools/core/utils/StringUtils.java @@ -2,6 +2,7 @@ import graphql.com.google.common.collect.ImmutableList; import graphql.com.google.common.collect.ImmutableSet; +import org.atteo.evo.inflector.English; import java.util.List; import java.util.Set; @@ -16,12 +17,11 @@ public class StringUtils { public static String capitalise(String value) { return value.substring(0, 1).toUpperCase() + value.substring(1); } - private static final Set unpluralisables = ImmutableSet.of( - "equipment", "information", "rice", "money", "species", "series", - "fish", "sheep", "deer"); + private static final Set unpluralisables = ImmutableSet.of("germplasm"); private static final List singularisations = ImmutableList.of( replace("(.*)people$").with("$1person"), + replace("(.*)People$").with("$1Person"), replace("oxen$").with("ox"), replace("children$").with("child"), replace("feet$").with("foot"), @@ -46,22 +46,10 @@ public static String capitalise(String value) { ); private static final List pluralisations = ImmutableList.of( + replace("(.*)matrix$").with("$1matrices"), + replace("(.*)Matrix$").with("$1Matrices"), replace("(.*)person$").with("$1people"), - replace("ox$").with("oxen"), - replace("child$").with("children"), - replace("foot$").with("feet"), - replace("tooth$").with("teeth"), - replace("goose$").with("geese"), - replace("(.*)fe?$").with("$1ves"), - replace("(.*)man$").with("$1men"), - replace("(.+[aeiou]y)$").with("$1s"), - replace("(.+[^aeiou])y$").with("$1ies"), - replace("(.+z)$").with("$1zes"), - replace("([m|l])ouse$").with("$1ice"), - replace("(.+)(e|i)x$").with("$1ices"), - replace("(octop|vir)us$").with("$1i"), - replace("(.+(s|x|sh|ch))$").with("$1es"), - replace("(.+)").with("$1s") + replace("(.*)Person$").with("$1People") ); /** @@ -71,6 +59,7 @@ public static String capitalise(String value) { * @return singular form of a plural noun */ public static String toSingular(String value) { + if (unpluralisables.contains(value.toLowerCase())) { return value; } @@ -81,7 +70,7 @@ public static String toSingular(String value) { } } - return value; + return value ; } /** @@ -101,7 +90,7 @@ public static String toPlural(String value) { } } - return value; + return English.plural(value); } /** diff --git a/java/core/src/main/resources/graphql-metadata.yaml b/java/core/src/main/resources/graphql-metadata.yaml new file mode 100644 index 0000000..186740c --- /dev/null +++ b/java/core/src/main/resources/graphql-metadata.yaml @@ -0,0 +1,2 @@ +title: BrAPI +version: 0.0.0 \ No newline at end of file diff --git a/java/core/src/main/resources/openapi-metadata.yaml b/java/core/src/main/resources/openapi-metadata.yaml new file mode 100644 index 0000000..186740c --- /dev/null +++ b/java/core/src/main/resources/openapi-metadata.yaml @@ -0,0 +1,2 @@ +title: BrAPI +version: 0.0.0 \ No newline at end of file diff --git a/java/core/src/test/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadataTest.java b/java/core/src/test/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadataTest.java new file mode 100644 index 0000000..cb6df65 --- /dev/null +++ b/java/core/src/test/java/org/brapi/schematools/core/graphql/metadata/GraphQLGeneratorMetadataTest.java @@ -0,0 +1,29 @@ +package org.brapi.schematools.core.graphql.metadata; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GraphQLGeneratorMetadataTest { + + void load() { + GraphQLGeneratorMetadata metadata = GraphQLGeneratorMetadata.load(); + + checkMetadata(metadata); + } + + @Test + void loadJson() { + } + + @Test + void loadYaml() { + } + + private void checkMetadata(GraphQLGeneratorMetadata metadata) { + assertNotNull(metadata); + + assertEquals("BrAPI", metadata.getTitle()); + assertEquals("0.0.0", metadata.getVersion()); + } +} \ No newline at end of file diff --git a/java/core/src/test/java/org/brapi/schematools/core/ontmodel/metadata/OntModelGeneratorMetadataTest.java b/java/core/src/test/java/org/brapi/schematools/core/ontmodel/metadata/OntModelGeneratorMetadataTest.java index 8ddb4ec..c513f9d 100644 --- a/java/core/src/test/java/org/brapi/schematools/core/ontmodel/metadata/OntModelGeneratorMetadataTest.java +++ b/java/core/src/test/java/org/brapi/schematools/core/ontmodel/metadata/OntModelGeneratorMetadataTest.java @@ -8,10 +8,6 @@ class OntModelGeneratorMetadataTest { - @BeforeEach - void setUp() { - } - @Test void load() { OntModelGeneratorMetadata metadata = OntModelGeneratorMetadata.load(); diff --git a/java/core/src/test/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadataTest.java b/java/core/src/test/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadataTest.java new file mode 100644 index 0000000..0f1e3d8 --- /dev/null +++ b/java/core/src/test/java/org/brapi/schematools/core/openapi/metadata/OpenAPIGeneratorMetadataTest.java @@ -0,0 +1,29 @@ +package org.brapi.schematools.core.openapi.metadata; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class OpenAPIGeneratorMetadataTest { + + void load() { + OpenAPIGeneratorMetadata metadata = OpenAPIGeneratorMetadata.load(); + + checkMetadata(metadata); + } + + @Test + void loadJson() { + } + + @Test + void loadYaml() { + } + + private void checkMetadata(OpenAPIGeneratorMetadata metadata) { + assertNotNull(metadata); + + assertEquals("BrAPI", metadata.getTitle()); + assertEquals("0.0.0", metadata.getVersion()); + } +} \ No newline at end of file diff --git a/java/core/src/test/java/org/brapi/schematools/core/utils/StringUtilsTest.java b/java/core/src/test/java/org/brapi/schematools/core/utils/StringUtilsTest.java index ed5c500..68b696a 100644 --- a/java/core/src/test/java/org/brapi/schematools/core/utils/StringUtilsTest.java +++ b/java/core/src/test/java/org/brapi/schematools/core/utils/StringUtilsTest.java @@ -3,40 +3,74 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; class StringUtilsTest { @Test void toSingular() { assertEquals("DataMatrix", StringUtils.toSingular("DataMatrices")) ; + assertEquals("Person", StringUtils.toSingular("People")) ; + assertEquals("Germplasm", StringUtils.toSingular("Germplasm")) ; + assertEquals("Study", StringUtils.toSingular("Studies")) ; + assertEquals("dataMatrix", StringUtils.toSingular("dataMatrices")) ; + assertEquals("person", StringUtils.toSingular("people")) ; + assertEquals("germplasm", StringUtils.toSingular("germplasm")) ; + assertEquals("study", StringUtils.toSingular("studies")) ; + assertEquals("trial", StringUtils.toSingular("trials")) ; } @Test void toPlural() { assertEquals("DataMatrices", StringUtils.toPlural("DataMatrix")) ; + assertEquals("People", StringUtils.toPlural("Person")) ; + assertEquals("Germplasm", StringUtils.toPlural("Germplasm")) ; + assertEquals("Studies", StringUtils.toPlural("Study")) ; + assertEquals("dataMatrices", StringUtils.toPlural("dataMatrix")) ; + assertEquals("people", StringUtils.toPlural("person")) ; + assertEquals("germplasm", StringUtils.toPlural("germplasm")) ; + assertEquals("studies", StringUtils.toPlural("study")) ; + assertEquals("trials", StringUtils.toPlural("trial")) ; } @Test void makeValidName() { + assertEquals("null", StringUtils.makeValidName(null)) ; + assertEquals("blank", StringUtils.makeValidName("")) ; + assertEquals("N1", StringUtils.makeValidName("1")) ; + assertEquals("this_is_a_test", StringUtils.makeValidName("this/is/a/test")) ; + assertEquals("this_is_a_test", StringUtils.makeValidName("this.is.a.test")) ; + assertEquals("this_is_a_test", StringUtils.makeValidName("this-is-a-test")) ; } @Test void toSentenceCase() { + assertEquals("SentenceCase", StringUtils.toSentenceCase("SentenceCase")) ; + assertEquals("ParameterCase", StringUtils.toSentenceCase("parameterCase")) ; } @Test void toParameterCase() { + assertEquals("sentenceCase", StringUtils.toParameterCase("SentenceCase")) ; + assertEquals("parameterCase", StringUtils.toParameterCase("parameterCase")) ; } @Test void startsWithLowerCase() { + assertTrue(StringUtils.startsWithLowerCase("lowerCase")) ; + assertFalse(StringUtils.startsWithLowerCase("UpperCase")) ; } @Test void startsWithUpperCase() { + assertFalse(StringUtils.startsWithUpperCase("lowerCase")) ; + assertTrue(StringUtils.startsWithUpperCase("UpperCase")) ; } @Test - void replace() { + void toLabel() { + assertEquals("Sentence Case", StringUtils.toLabel("SentenceCase")) ; + assertEquals("Parameter Case", StringUtils.toLabel("parameterCase")) ; } } \ No newline at end of file