You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/11/17 20:21:27 UTC

[camel] branch master updated: CAMEL-15790: camel-catalog - Validate Endpoint properties now validates api syntax combo of apiName/methodName

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new d9ac457  CAMEL-15790: camel-catalog - Validate Endpoint properties now validates api syntax combo of apiName/methodName
d9ac457 is described below

commit d9ac45705a4ce126d5030d80fe3fd3d39132a0fa
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 17 21:20:40 2020 +0100

    CAMEL-15790: camel-catalog - Validate Endpoint properties now validates api syntax combo of apiName/methodName
---
 .../org/apache/camel/catalog/CamelCatalogTest.java | 24 +++++++++++++
 .../camel/catalog/impl/AbstractCamelCatalog.java   | 41 ++++++++++++++++++++++
 2 files changed, 65 insertions(+)

diff --git a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index dd7b02a..381072d 100644
--- a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -1213,6 +1213,30 @@ public class CamelCatalogTest {
         result = catalog.validateEndpointProperties(uri);
         assertFalse(result.isSuccess());
         assertTrue(result.getUnknown().contains("unknown"));
+
+        uri = "twilio:account/fetch";
+        result = catalog.validateEndpointProperties(uri);
+        assertTrue(result.isSuccess());
+        uri = "twilio:account/fetch?pathSid=123";
+        result = catalog.validateEndpointProperties(uri);
+        assertTrue(result.isSuccess());
+
+        uri = "twilio:account/update";
+        result = catalog.validateEndpointProperties(uri);
+        assertTrue(result.isSuccess());
+        uri = "twilio:account/update?pathSid=123";
+        result = catalog.validateEndpointProperties(uri);
+        assertTrue(result.isSuccess());
+        uri = "twilio:account/read";
+        result = catalog.validateEndpointProperties(uri);
+        assertFalse(result.isSuccess());
+        assertEquals(2, result.getEnumChoices("methodName").size());
+        assertTrue(result.getEnumChoices("methodName").contains("fetch"));
+        assertTrue(result.getEnumChoices("methodName").contains("update"));
+
+        uri = "twilio:account/read?pathSid=123";
+        result = catalog.validateEndpointProperties(uri);
+        assertFalse(result.isSuccess());
     }
 
     @Test
diff --git a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
index dba54f6..007b6b5 100644
--- a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
+++ b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
@@ -349,6 +349,47 @@ public abstract class AbstractCamelCatalog {
             }
         }
 
+        // for api component then check that the apiName/methodName combo is valid
+        if (model.isApi()) {
+            String[] apiSyntax = StringHelper.splitWords(model.getApiSyntax());
+            String key1 = properties.get(apiSyntax[0]);
+            String key2 = apiSyntax.length > 1 ? properties.get(apiSyntax[1]) : null;
+
+            if (key1 != null && key2 != null) {
+                ApiModel api = model.getApiOptions().stream().filter(o -> o.getName().equalsIgnoreCase(key1)).findFirst().orElse(null);
+                if (api == null) {
+                    result.addInvalidEnum(apiSyntax[0], key1);
+                    List<String> choices = model.getApiOptions().stream().map(ApiModel::getName).collect(Collectors.toList());
+                    result.addInvalidEnumChoices(apiSyntax[0], choices.toArray(new String[choices.size()]));
+                } else {
+                    // walk each method and match against its name/alias
+                    boolean found = false;
+                    for (ApiMethodModel m : api.getMethods()) {
+                        String key3 = apiMethodAlias(api, m);
+                        if (m.getName().equalsIgnoreCase(key2) || key2.equalsIgnoreCase(key3)) {
+                            found = true;
+                            break;
+                        }
+                    }
+                    if (!found) {
+                        result.addInvalidEnum(apiSyntax[1], key2);
+                        List<String> choices = api.getMethods().stream()
+                                .map(m -> {
+                                    // favour using method alias in choices
+                                    String answer = apiMethodAlias(api, m);
+                                    if (answer == null) {
+                                        answer = m.getName();
+                                    }
+                                    return answer;
+                                })
+                                .collect(Collectors.toList());
+
+                        result.addInvalidEnumChoices(apiSyntax[1], choices.toArray(new String[choices.size()]));
+                    }
+                }
+            }
+        }
+
         // now check if all required values are there, and that a default value does not exists
         for (BaseOptionModel row : rows.values()) {
             if (row.isRequired()) {