Skip to content

Commit

Permalink
updated String Utils, metadata and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Nov 15, 2024
1 parent fe47c1a commit e88805f
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ?
Expand Down Expand Up @@ -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<GraphQLSchema> response = graphQLGenerator.generate(schemaDirectory);
Response<GraphQLSchema> response = graphQLGenerator.generate(schemaDirectory, metadata);

response.onSuccessDoWithResult(this::outputIDLSchema).onFailDoWithResponse(this::printGraphQLSchemaErrors);
}
Expand Down Expand Up @@ -169,10 +173,10 @@ private void printGraphQLSchemaErrors(Response<GraphQLSchema> response) {
}
}

private void generateOpenAPISpecification(OpenAPIGeneratorOptions options) {
private void generateOpenAPISpecification(OpenAPIGeneratorOptions options, OpenAPIGeneratorMetadata metadata) {
OpenAPIGenerator openAPIGenerator = new OpenAPIGenerator(options);

Response<List<OpenAPI>> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory);
Response<List<OpenAPI>> response = openAPIGenerator.generate(schemaDirectory, componentsDirectory, metadata);

response.onSuccessDoWithResult(this::outputOpenAPISpecification).onFailDoWithResponse(this::printOpenAPISpecificationErrors);
}
Expand Down
1 change: 1 addition & 0 deletions java/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,52 @@
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
*/
@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) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private Response<OpenAPI> generate(String title, Collection<BrAPIClass> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@
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
*/
@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) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> unpluralisables = ImmutableSet.of(
"equipment", "information", "rice", "money", "species", "series",
"fish", "sheep", "deer");
private static final Set<String> unpluralisables = ImmutableSet.of("germplasm");

private static final List<Replacer> singularisations = ImmutableList.of(
replace("(.*)people$").with("$1person"),
replace("(.*)People$").with("$1Person"),
replace("oxen$").with("ox"),
replace("children$").with("child"),
replace("feet$").with("foot"),
Expand All @@ -46,22 +46,10 @@ public static String capitalise(String value) {
);

private static final List<Replacer> 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")
);

/**
Expand All @@ -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;
}
Expand All @@ -81,7 +70,7 @@ public static String toSingular(String value) {
}
}

return value;
return value ;
}

/**
Expand All @@ -101,7 +90,7 @@ public static String toPlural(String value) {
}
}

return value;
return English.plural(value);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions java/core/src/main/resources/graphql-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: BrAPI
version: 0.0.0
2 changes: 2 additions & 0 deletions java/core/src/main/resources/openapi-metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: BrAPI
version: 0.0.0
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

class OntModelGeneratorMetadataTest {

@BeforeEach
void setUp() {
}

@Test
void load() {
OntModelGeneratorMetadata metadata = OntModelGeneratorMetadata.load();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading

0 comments on commit e88805f

Please sign in to comment.