From db7f984c6b4a7a381a336b26bfea5a32691a00fb Mon Sep 17 00:00:00 2001 From: "Benjamin, Max" Date: Thu, 7 May 2020 10:13:24 -0400 Subject: uri params now available in info object uri params now available in info object make sure uri keys are retrieved via constants Issue-ID: SO-2908 Signed-off-by: Benjamin, Max (mb388a) Change-Id: Ieb701c2705ecdb2365d557bc51e4f604d92ec680 --- .../graphinventory/generate/FluentGenerator.java | 88 ++++++++++++++++------ .../org/onap/graphinventory/generate/Patterns.java | 11 +++ .../graphinventory/generate/SwaggerConverter.java | 13 +--- 3 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java (limited to 'graph-inventory/fluent-builder-maven-plugin/src/main/java/org') diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java index f9c664389b..23a1a812df 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/FluentGenerator.java @@ -8,6 +8,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.regex.Matcher; import java.util.stream.Collectors; import javax.lang.model.element.Modifier; import org.apache.commons.lang3.ArrayUtils; @@ -240,19 +241,17 @@ public class FluentGenerator { } protected TypeSpec createTypes() { - Pair path = splitClasspath(this.nameClass); - ClassName nameType = ClassName.get(path.getLeft(), path.getRight()); - - List params = - doc.values().stream().filter(item -> item.getType().equals("singular")) - .sorted(Comparator - .comparing(item -> item.getName())) - .map(item -> FieldSpec - .builder(nameType, - CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, item.getName()), - Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) - .initializer("new $T($S)", nameType, item.getName()).build()) - .collect(Collectors.toList()); + List params = doc.values().stream().filter(item -> item.getType().equals("singular")) + .sorted(Comparator.comparing(item -> item.getName())).map(item -> { + ClassName nameType = + ClassName.get(CLASSPATH, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, item.getName())) + .nestedClass("Info"); + FieldSpec field = FieldSpec + .builder(nameType, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, item.getName()), + Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) + .initializer("new $T()", nameType).build(); + return field; + }).collect(Collectors.toList()); TypeSpec type = TypeSpec.classBuilder("Types").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC) .addFields(params).build(); @@ -276,30 +275,71 @@ public class FluentGenerator { .build()); ClassName superInterface; + String name; if (oType.getType().equals("plural")) { - classFields.add(FieldSpec.builder(String.class, "name") - .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) - .initializer("$S", oType.getAdditionalName()).build()); - superInterface = ClassName.get(this.pluralBuilderClass, "Info"); + Pair path = splitClasspath(this.pluralBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getAdditionalName(); } else if (oType.getType().equals("singular")) { - classFields.add(FieldSpec.builder(String.class, "name") - .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", oType.getName()) - .build()); - superInterface = ClassName.get(this.singularBuilderClass, "Info"); + Pair path = splitClasspath(this.singularBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getName(); } else { - superInterface = ClassName.get(this.topLevelBuilderClass, "Info"); + Pair path = splitClasspath(this.topLevelBuilderClass); + superInterface = ClassName.get(path.getLeft(), path.getRight()); + name = oType.getName(); } + superInterface = superInterface.nestedClass("Info"); methods.add(MethodSpec.methodBuilder("getPaths").returns(ParameterizedTypeName.get(List.class, String.class)) .addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.paths").build()); methods.add(MethodSpec.methodBuilder("getPartialUri").returns(String.class).addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class).addStatement("return Info.partialUri").build()); if (!oType.getType().equals("top level")) { + classFields.add(FieldSpec.builder(String.class, "name") + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", name).build()); + classFields.add(FieldSpec.builder(ClassName.get("", "UriParams"), "uriParams") + .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC) + .initializer("new $T()", ClassName.get("", "UriParams")).build()); methods.add(MethodSpec.methodBuilder("getName").returns(String.class).addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class).addStatement("return Info.name").build()); + + methods.add(MethodSpec.methodBuilder("getUriParams").returns(ClassName.get("", "UriParams")) + .addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.uriParams") + .build()); + } + TypeSpec.Builder returnTypeSpec = TypeSpec.classBuilder("Info").addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addSuperinterface(superInterface).addFields(classFields).addMethods(methods); + if (!oType.getType().equals("top level")) { + returnTypeSpec.addType(createUriParamsClass(superInterface, oType)); + } + return returnTypeSpec.build(); + + } + + protected TypeSpec createUriParamsClass(ClassName parent, ObjectType oType) { + + List classFields = new ArrayList<>(); + Matcher params = Patterns.urlTemplatePattern.matcher(oType.getPartialUri()); + + while (params.find()) { + String value; + String name; + + if (params.group(2) != null) { + name = params.group(2); + } else { + name = params.group(1); + } + value = params.group(1); + + name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name); + + classFields.add(FieldSpec.builder(String.class, name, Modifier.PUBLIC, Modifier.FINAL) + .initializer("$S", value).build()); } - return TypeSpec.classBuilder("Info").addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addSuperinterface(superInterface).addFields(classFields).addMethods(methods).build(); + return TypeSpec.classBuilder("UriParams").addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) + .addFields(classFields).addSuperinterface(parent.nestedClass("UriParams")).build(); } protected String makeValidJavaVariable(String name) { diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java new file mode 100644 index 0000000000..8be22e1dcc --- /dev/null +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/Patterns.java @@ -0,0 +1,11 @@ +package org.onap.graphinventory.generate; + +import java.util.regex.Pattern; + +public class Patterns { + + public static final Pattern pluralPattern = Pattern.compile(".*(?/(?[^{]*$))"); + public static final Pattern singularPattern = Pattern.compile(".*(?/(?[^/{}]*)/\\{.*$)"); + public static final Pattern topLevelPattern = Pattern.compile("^/([^/]+)/.*"); + public static final Pattern urlTemplatePattern = Pattern.compile("\\{([^}.]+(?:\\.([^}]+))?)\\}"); +} diff --git a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java index b3ced65390..42305488bc 100644 --- a/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java +++ b/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.maven.plugin.logging.Log; import com.fasterxml.jackson.core.JsonProcessingException; @@ -33,10 +32,6 @@ public class SwaggerConverter { .filter(item -> !item.getKey().endsWith("/relationship-list/relationship")) .collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue())); - Pattern pluralPattern = Pattern.compile(".*(?/(?[^{]*$))"); - Pattern singularPattern = Pattern.compile(".*(?/(?[^/{}]*)/\\{.*$)"); - Pattern topLevelPattern = Pattern.compile("^/([^/]+)/.*"); - Pattern urlTemplatePattern = Pattern.compile("\\{([^}]+)\\}"); Matcher pluralMatcher; Matcher singularMatcher; Matcher topLevelMatcher; @@ -44,9 +39,9 @@ public class SwaggerConverter { Map output = new HashMap<>(); for (Map.Entry entry : paths.entrySet()) { - pluralMatcher = pluralPattern.matcher(entry.getKey()); - singularMatcher = singularPattern.matcher(entry.getKey()); - topLevelMatcher = topLevelPattern.matcher(entry.getKey()); + pluralMatcher = Patterns.pluralPattern.matcher(entry.getKey()); + singularMatcher = Patterns.singularPattern.matcher(entry.getKey()); + topLevelMatcher = Patterns.topLevelPattern.matcher(entry.getKey()); ObjectType item; if (pluralMatcher.matches()) { if (!output.containsKey(pluralMatcher.group("name"))) { @@ -158,7 +153,7 @@ public class SwaggerConverter { } if (!item.getValue().getFields().isEmpty()) { - Matcher templates = urlTemplatePattern.matcher(item.getValue().getPartialUri()); + Matcher templates = Patterns.urlTemplatePattern.matcher(item.getValue().getPartialUri()); List localFields = new ArrayList<>(); while (templates.find()) { localFields.add(templates.group(1)); -- cgit 1.2.3-korg