camel git commit: CAMEL-11175 REST DSL Swagger generator default ...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

camel git commit: CAMEL-11175 REST DSL Swagger generator default ...

zregvart
Repository: camel
Updated Branches:
  refs/heads/master bee43cefb -> b8efc2bb3


CAMEL-11175 REST DSL Swagger generator default ...

...generated class/package names

This refines default class name and package name generation.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b8efc2bb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b8efc2bb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b8efc2bb

Branch: refs/heads/master
Commit: b8efc2bb3c26f1a7ab3f873f80ce9e746c65d0aa
Parents: bee43ce
Author: Zoran Regvart <[hidden email]>
Authored: Thu Apr 20 13:26:54 2017 +0200
Committer: Zoran Regvart <[hidden email]>
Committed: Thu Apr 20 13:26:54 2017 +0200

----------------------------------------------------------------------
 .../swagger/RestDslSourceCodeGenerator.java     |  25 +++--
 .../swagger/RestDslSourceCodeGeneratorTest.java | 106 +++++++++++++++++++
 2 files changed, 125 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b8efc2bb/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
----------------------------------------------------------------------
diff --git a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
index 252bc40..62c8f70 100644
--- a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
+++ b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
@@ -38,11 +38,11 @@ import org.apache.camel.util.ObjectHelper;
 import static org.apache.camel.util.StringHelper.notEmpty;
 
 public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<RestDslSourceCodeGenerator<T>> {
-    private static final String DEFAULT_CLASS_NAME = "RestDslRoute";
+    static final String DEFAULT_CLASS_NAME = "RestDslRoute";
 
-    private static final String DEFAULT_INDENT = "    ";
+    static final String DEFAULT_PACKAGE_NAME = "rest.dsl.generated";
 
-    private static final String DEFAULT_PACKAGE_NAME = "rest.dsl.generated";
+    private static final String DEFAULT_INDENT = "    ";
 
     private Function<Swagger, String> classNameGenerator = RestDslSourceCodeGenerator::generateClassName;
 
@@ -147,8 +147,15 @@ public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<Res
             return DEFAULT_CLASS_NAME;
         }
 
-        return title.chars().filter(Character::isJavaIdentifierPart).boxed().collect(Collector.of(StringBuilder::new,
-            StringBuilder::appendCodePoint, StringBuilder::append, StringBuilder::toString));
+        final String className = title.chars().filter(Character::isJavaIdentifierPart).filter(c -> c < 'z').boxed()
+            .collect(Collector.of(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append,
+                StringBuilder::toString));
+
+        if (className.isEmpty() || !Character.isJavaIdentifierStart(className.charAt(0))) {
+            return DEFAULT_CLASS_NAME;
+        }
+
+        return className;
     }
 
     static String generatePackageName(final Swagger swagger) {
@@ -157,7 +164,13 @@ public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<Res
         if (ObjectHelper.isNotEmpty(host)) {
             final StringBuilder packageName = new StringBuilder();
 
-            final String[] parts = host.split("\\.");
+            final String hostWithoutPort = host.replaceFirst(":.*", "");
+
+            if ("localhost".equalsIgnoreCase(hostWithoutPort)) {
+                return DEFAULT_PACKAGE_NAME;
+            }
+
+            final String[] parts = hostWithoutPort.split("\\.");
 
             for (int i = parts.length - 1; i >= 0; i--) {
                 packageName.append(parts[i]);

http://git-wip-us.apache.org/repos/asf/camel/blob/b8efc2bb/tooling/swagger-rest-dsl-generator/src/test/java/org/apache/camel/generator/swagger/RestDslSourceCodeGeneratorTest.java
----------------------------------------------------------------------
diff --git a/tooling/swagger-rest-dsl-generator/src/test/java/org/apache/camel/generator/swagger/RestDslSourceCodeGeneratorTest.java b/tooling/swagger-rest-dsl-generator/src/test/java/org/apache/camel/generator/swagger/RestDslSourceCodeGeneratorTest.java
new file mode 100644
index 0000000..15e716c
--- /dev/null
+++ b/tooling/swagger-rest-dsl-generator/src/test/java/org/apache/camel/generator/swagger/RestDslSourceCodeGeneratorTest.java
@@ -0,0 +1,106 @@
+/**
+ * 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.camel.generator.swagger;
+
+import io.swagger.models.Info;
+import io.swagger.models.Swagger;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RestDslSourceCodeGeneratorTest {
+
+    @Test
+    public void shouldCreatePackageNamesFromHostnames() {
+        final Swagger swagger = new Swagger();
+        swagger.setHost("api.example.org");
+
+        assertThat(RestDslSourceCodeGenerator.generatePackageName(swagger)).isEqualTo("org.example.api");
+    }
+
+    @Test
+    public void shouldCreatePackageNamesFromHostnamesWithPorts() {
+        final Swagger swagger = new Swagger();
+        swagger.setHost("api.example.org:8080");
+
+        assertThat(RestDslSourceCodeGenerator.generatePackageName(swagger)).isEqualTo("org.example.api");
+    }
+
+    @Test
+    public void shouldGenerateClassNameFromTitle() {
+        final Swagger swagger = new Swagger();
+        swagger.info(new Info().title("Example API"));
+
+        assertThat(RestDslSourceCodeGenerator.generateClassName(swagger)).isEqualTo("ExampleAPI");
+    }
+
+    @Test
+    public void shouldGenerateClassNameFromTitleWithNonValidJavaIdentifiers() {
+        final Swagger swagger = new Swagger();
+        swagger.info(new Info().title("Example-API 2.0"));
+
+        assertThat(RestDslSourceCodeGenerator.generateClassName(swagger)).isEqualTo("ExampleAPI20");
+    }
+
+    @Test
+    public void shouldUseDefaultClassNameIfInfoOrTitleIsNotPresent() {
+        final Swagger swagger = new Swagger();
+
+        assertThat(RestDslSourceCodeGenerator.generateClassName(swagger))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_CLASS_NAME);
+
+        assertThat(RestDslSourceCodeGenerator.generateClassName(swagger.info(new Info())))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_CLASS_NAME);
+    }
+
+    @Test
+    public void shouldUseDefaultClassNameIfTitleContainsOnlyNonValidJavaIdentifiers() {
+        final Swagger swagger = new Swagger();
+        swagger.info(new Info().title("\\%/4"));
+
+        assertThat(RestDslSourceCodeGenerator.generateClassName(swagger))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_CLASS_NAME);
+    }
+
+    @Test
+    public void shouldUseDefaultPackageNameForLocalhost() {
+        final Swagger swagger = new Swagger();
+        swagger.setHost("localhost");
+
+        assertThat(RestDslSourceCodeGenerator.generatePackageName(swagger))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_PACKAGE_NAME);
+    }
+
+    @Test
+    public void shouldUseDefaultPackageNameForLocalhostWithPort() {
+        final Swagger swagger = new Swagger();
+        swagger.setHost("localhost:8080");
+
+        assertThat(RestDslSourceCodeGenerator.generatePackageName(swagger))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_PACKAGE_NAME);
+    }
+
+    @Test
+    public void shouldUseDefaultPackageNameIfNoHostIsSpecified() {
+        final Swagger swagger = new Swagger();
+
+        assertThat(RestDslSourceCodeGenerator.generatePackageName(swagger))
+            .isEqualTo(RestDslSourceCodeGenerator.DEFAULT_PACKAGE_NAME);
+    }
+}

Loading...