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 2015/12/21 13:42:57 UTC

[3/8] camel git commit: Camel catalog - Add api to validate endpoint uri

Camel catalog - Add api to validate endpoint uri


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

Branch: refs/heads/camel-2.16.x
Commit: 4a40ecb60693825a970765f8cda8b267699bf3c2
Parents: 13ebce9
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Dec 19 17:03:35 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Dec 21 13:42:07 2015 +0100

----------------------------------------------------------------------
 .../org/apache/camel/catalog/CamelCatalog.java  |   7 +-
 .../camel/catalog/DefaultCamelCatalog.java      |  22 ++--
 .../apache/camel/catalog/ValidationResult.java  | 102 +++++++++++++++++++
 .../apache/camel/catalog/CamelCatalogTest.java  |  49 ++++-----
 4 files changed, 137 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4a40ecb6/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
index dc1a3ca..ac01b51 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
@@ -173,12 +173,9 @@ public interface CamelCatalog {
      * Parses and validates the endpoint uri and constructs a key/value properties of each option
      *
      * @param uri  the endpoint uri
-     * @return invalid properties as key/value properties of each invalid option, returns an empty map if no validation errors
+     * @return validation result
      */
-    Map<String, String> validateProperties(String uri) throws URISyntaxException;
-
-    // TODO: json with error instead of map
-    // with description of the error, index, etc
+    ValidationResult validateProperties(String uri) throws URISyntaxException;
 
     /**
      * Returns the component name from the given endpoint uri

http://git-wip-us.apache.org/repos/asf/camel/blob/4a40ecb6/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
index 13b3052..f0e4c3c 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
@@ -646,8 +646,8 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     @Override
-    public Map<String, String> validateProperties(String uri) throws URISyntaxException {
-        Map<String, String> answer = new LinkedHashMap<String, String>();
+    public ValidationResult validateProperties(String uri) throws URISyntaxException {
+        ValidationResult result = new ValidationResult();
 
         // parse the uri
         URI u = normalizeUri(uri);
@@ -665,14 +665,14 @@ public class DefaultCamelCatalog implements CamelCatalog {
             Map<String, String> row = getRow(rows, name);
             // unknown option
             if (row == null) {
-                answer.put(name, property.getValue());
+                result.addUnknown(name);
             } else {
                 // invalid value/type
 
                 // is required but the value is empty
                 boolean required = isPropertyRequired(rows, name);
                 if (required && isEmpty(value)) {
-                    answer.put(name, value);
+                    result.addRequired(name);
                 }
 
                 // is enum but the value is not within the enum range
@@ -687,7 +687,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                         }
                     }
                     if (!found) {
-                        answer.put(name, value);
+                        result.addInvalidEnum(name);
                     }
                 }
 
@@ -696,7 +696,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     // value must be a boolean
                     boolean bool = "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value);
                     if (!bool) {
-                        answer.put(name, value);
+                        result.addInvalidBoolean(name);
                     }
                 }
 
@@ -710,7 +710,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                         // ignore
                     }
                     if (!valid) {
-                        answer.put(name, value);
+                        result.addInvalidInteger(name);
                     }
                 }
 
@@ -719,12 +719,12 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     // value must be an number
                     boolean valid = false;
                     try {
-                        valid = Double.valueOf(value).isNaN() == false || Float.valueOf(value).isNaN() == false;
+                        valid = !Double.valueOf(value).isNaN() || !Float.valueOf(value).isNaN();
                     } catch (Exception e) {
                         // ignore
                     }
                     if (!valid) {
-                        answer.put(name, value);
+                        result.addInvalidNumber(name);
                     }
                 }
             }
@@ -740,12 +740,12 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     value = getPropertyDefaultValue(rows, name);
                 }
                 if (isEmpty(value)) {
-                    answer.put(name, value);
+                    result.addRequired(name);
                 }
             }
         }
 
-        return answer;
+        return result;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/4a40ecb6/platforms/catalog/src/main/java/org/apache/camel/catalog/ValidationResult.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/main/java/org/apache/camel/catalog/ValidationResult.java b/platforms/catalog/src/main/java/org/apache/camel/catalog/ValidationResult.java
new file mode 100644
index 0000000..2ea9528
--- /dev/null
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/ValidationResult.java
@@ -0,0 +1,102 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.catalog;
+
+import java.io.Serializable;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public class ValidationResult implements Serializable {
+
+    private Set<String> unknown;
+    private Set<String> required;
+    private Set<String> invalidEnum;
+    private Set<String> invalidBoolean;
+    private Set<String> invalidInteger;
+    private Set<String> invalidNumber;
+
+    public boolean isSuccess() {
+        return unknown == null && required == null && invalidEnum == null
+                && invalidBoolean == null && invalidInteger == null && invalidNumber == null;
+    }
+
+    public void addUnknown(String name) {
+        if (unknown == null) {
+            unknown = new LinkedHashSet<String>();
+        }
+        unknown.add(name);
+    }
+
+    public void addRequired(String name) {
+        if (required == null) {
+            required = new LinkedHashSet<String>();
+        }
+        required.add(name);
+    }
+
+    public void addInvalidEnum(String name) {
+        if (invalidEnum == null) {
+            invalidEnum = new LinkedHashSet<String>();
+        }
+        invalidEnum.add(name);
+    }
+
+    public void addInvalidBoolean(String name) {
+        if (invalidBoolean == null) {
+            invalidBoolean = new LinkedHashSet<String>();
+        }
+        invalidBoolean.add(name);
+    }
+
+    public void addInvalidInteger(String name) {
+        if (invalidInteger == null) {
+            invalidInteger = new LinkedHashSet<String>();
+        }
+        invalidInteger.add(name);
+    }
+
+    public void addInvalidNumber(String name) {
+        if (invalidNumber == null) {
+            invalidNumber = new LinkedHashSet<String>();
+        }
+        invalidNumber.add(name);
+    }
+
+    public Set<String> getUnknown() {
+        return unknown;
+    }
+
+    public Set<String> getRequired() {
+        return required;
+    }
+
+    public Set<String> getInvalidEnum() {
+        return invalidEnum;
+    }
+
+    public Set<String> getInvalidBoolean() {
+        return invalidBoolean;
+    }
+
+    public Set<String> getInvalidInteger() {
+        return invalidInteger;
+    }
+
+    public Set<String> getInvalidNumber() {
+        return invalidNumber;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4a40ecb6/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index 4c020da..59ec112 100644
--- a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -27,6 +27,7 @@ import org.junit.Test;
 
 import static org.apache.camel.catalog.CatalogHelper.loadText;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -389,45 +390,39 @@ public class CamelCatalogTest {
     @Test
     public void validateProperties() throws Exception {
         // valid
-        Map<String, String> map = catalog.validateProperties("log:mylog");
-        assertNotNull(map);
-        assertEquals(0, map.size());
+        ValidationResult result = catalog.validateProperties("log:mylog");
+        assertTrue(result.isSuccess());
 
         // unknown
-        map = catalog.validateProperties("log:mylog?level=WARN&foo=bar");
-        assertNotNull(map);
-        assertEquals(1, map.size());
-        assertEquals("bar", map.get("foo"));
+        result = catalog.validateProperties("log:mylog?level=WARN&foo=bar");
+        assertFalse(result.isSuccess());
+        assertTrue(result.getUnknown().contains("foo"));
 
         // enum
-        map = catalog.validateProperties("jms:unknown:myqueue");
-        assertNotNull(map);
-        assertEquals(1, map.size());
+        result = catalog.validateProperties("jms:unknown:myqueue");
+        assertFalse(result.isSuccess());
+        assertTrue(result.getInvalidEnum().contains("destinationType"));
 
         // okay
-        map = catalog.validateProperties("yammer:MESSAGES?accessToken=aaa&consumerKey=bbb&consumerSecret=ccc&useJson=true&initialDelay=500");
-        assertNotNull(map);
-        assertEquals(0, map.size());
+        result = catalog.validateProperties("yammer:MESSAGES?accessToken=aaa&consumerKey=bbb&consumerSecret=ccc&useJson=true&initialDelay=500");
+        assertTrue(result.isSuccess());
 
         // required / boolean / integer
-        map = catalog.validateProperties("yammer:MESSAGES?accessToken=aaa&consumerKey=&useJson=no&initialDelay=five");
-        assertNotNull(map);
-        assertEquals(4, map.size());
-        assertEquals(null, map.get("consumerKey"));
-        assertEquals(null, map.get("consumerSecret"));
-        assertEquals("no", map.get("useJson"));
-        assertEquals("five", map.get("initialDelay"));
+        result = catalog.validateProperties("yammer:MESSAGES?accessToken=aaa&consumerKey=&useJson=no&initialDelay=five");
+        assertFalse(result.isSuccess());
+        assertTrue(result.getRequired().contains("consumerKey"));
+        assertTrue(result.getRequired().contains("consumerSecret"));
+        assertTrue(result.getInvalidBoolean().contains("useJson"));
+        assertTrue(result.getInvalidInteger().contains("initialDelay"));
 
         // okay
-        map = catalog.validateProperties("mqtt:myqtt?reconnectBackOffMultiplier=2.5");
-        assertNotNull(map);
-        assertEquals(0, map.size());
+        result = catalog.validateProperties("mqtt:myqtt?reconnectBackOffMultiplier=2.5");
+        assertTrue(result.isSuccess());
 
         // number
-        map = catalog.validateProperties("mqtt:myqtt?reconnectBackOffMultiplier=five");
-        assertNotNull(map);
-        assertEquals(1, map.size());
-        assertEquals("five", map.get("reconnectBackOffMultiplier"));
+        result = catalog.validateProperties("mqtt:myqtt?reconnectBackOffMultiplier=five");
+        assertFalse(result.isSuccess());
+        assertTrue(result.getInvalidNumber().contains("reconnectBackOffMultiplier"));
     }
 
     @Test