camel git commit: ComponentVerifier: support validation of group of options

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

camel git commit: ComponentVerifier: support validation of group of options

lburgazzoli-2
Repository: camel
Updated Branches:
  refs/heads/master d88768738 -> 9b341c7e9


ComponentVerifier: support validation of group of options


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

Branch: refs/heads/master
Commit: 9b341c7e9d0c43a8986a8e73ecea0508381f234b
Parents: d887687
Author: lburgazzoli <[hidden email]>
Authored: Mon Mar 20 16:49:46 2017 +0100
Committer: lburgazzoli <[hidden email]>
Committed: Mon Mar 20 17:17:23 2017 +0100

----------------------------------------------------------------------
 .../org/apache/camel/ComponentVerifier.java     |  5 ++
 .../camel/impl/verifier/OptionsGroup.java       | 75 ++++++++++++++++++++
 .../camel/impl/verifier/ResultBuilder.java      |  5 ++
 .../camel/impl/verifier/ResultErrorHelper.java  | 38 ++++++++++
 .../salesforce/SalesforceComponentVerifier.java | 24 +++++--
 .../SalesforceComponentVerifierTest.java        | 44 +++++++++++-
 6 files changed, 182 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/camel-core/src/main/java/org/apache/camel/ComponentVerifier.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/ComponentVerifier.java b/camel-core/src/main/java/org/apache/camel/ComponentVerifier.java
index 14bec23..488a0ba 100644
--- a/camel-core/src/main/java/org/apache/camel/ComponentVerifier.java
+++ b/camel-core/src/main/java/org/apache/camel/ComponentVerifier.java
@@ -26,9 +26,12 @@ public interface ComponentVerifier {
     String CODE_EXCEPTION = "exception";
     String CODE_INTERNAL = "internal";
     String CODE_MISSING_OPTION = "missing-option";
+    String CODE_MISSING_OPTION_GROUP = "missing-option-group";
     String CODE_UNKNOWN_OPTION = "unknown-option";
     String CODE_ILLEGAL_OPTION = "illegal-option";
+    String CODE_ILLEGAL_OPTION_GROUP_COMBINATION = "illegal-option-group-combination";
     String CODE_ILLEGAL_OPTION_VALUE = "illegal-option-value";
+    String CODE_INCOMPLETE_OPTION_GROUP = "incomplete-option-group";
     String CODE_UNSUPPORTED = "unsupported";
     String CODE_UNSUPPORTED_SCOPE = "unsupported-scope";
     String ERROR_TYPE_ATTRIBUTE = "error.type";
@@ -40,6 +43,8 @@ public interface ComponentVerifier {
     String HTTP_REDIRECT_LOCATION = "http.redirect.location";
     String EXCEPTION_CLASS = "exception.class";
     String EXCEPTION_INSTANCE = "exception.instance";
+    String GROUP_NAME = "group.name";
+    String GROUP_OPTIONS = "group.options";
 
     enum Scope {
         NONE,

http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/camel-core/src/main/java/org/apache/camel/impl/verifier/OptionsGroup.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/verifier/OptionsGroup.java b/camel-core/src/main/java/org/apache/camel/impl/verifier/OptionsGroup.java
new file mode 100644
index 0000000..dcfd668
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/impl/verifier/OptionsGroup.java
@@ -0,0 +1,75 @@
+/**
+ * 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.impl.verifier;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+public final class OptionsGroup implements Serializable {
+    private final String name;
+    private final Set<String> options;
+
+    public OptionsGroup(String name) {
+        this.name = name;
+        this.options = new HashSet<>();
+    }
+
+    public OptionsGroup(String name, Collection<String> options) {
+        this.name = name;
+        this.options = new HashSet<>(options);
+    }
+
+    public void addOption(String option) {
+        this.options.add(option);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Set<String> getOptions() {
+        return this.options;
+    }
+
+    public OptionsGroup option(String option) {
+        this.options.add(option);
+        return this;
+    }
+
+    public OptionsGroup options(String... options) {
+        for (String option : options) {
+            addOption(option);
+        }
+
+        return this;
+    }
+
+    public static OptionsGroup withName(String name) {
+        return new OptionsGroup(name);
+    }
+
+    public static OptionsGroup withName(Enum<?> enumItem) {
+        return new OptionsGroup(enumItem.name());
+    }
+
+    public static OptionsGroup withNameAndOptions(String name, String... options) {
+        return new OptionsGroup(name, Arrays.asList(options));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultBuilder.java b/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultBuilder.java
index 6a42a14..8d90195 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultBuilder.java
@@ -100,6 +100,11 @@ public final class ResultBuilder {
         return this;
     }
 
+    public ResultBuilder errors(List<ComponentVerifier.Error> errors) {
+        errors.forEach(this::error);
+        return this;
+    }
+
     // **********************************
     // Build
     // **********************************

http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultErrorHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultErrorHelper.java b/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultErrorHelper.java
index 2481b40..4603ce4 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultErrorHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/verifier/ResultErrorHelper.java
@@ -16,8 +16,15 @@
  */
 package org.apache.camel.impl.verifier;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 
 import org.apache.camel.ComponentVerifier;
 import org.apache.camel.util.ObjectHelper;
@@ -40,4 +47,35 @@ public final class ResultErrorHelper {
 
         return Optional.empty();
     }
+
+    public static List<ComponentVerifier.Error> requiresAny(Map<String, Object> parameters, OptionsGroup... groups) {
+        return requiresAny(parameters, Arrays.asList(groups));
+    }
+
+    public static List<ComponentVerifier.Error> requiresAny(Map<String, Object> parameters, Collection<OptionsGroup> groups) {
+        final List<ComponentVerifier.Error> errors = new ArrayList<>();
+        final Set<String> keys = new HashSet<>(parameters.keySet());
+
+        for (OptionsGroup group : groups) {
+            if (keys.containsAll(group.getOptions())) {
+                // All the options of this group are found so we are good
+                return Collections.emptyList();
+            } else {
+                ResultErrorBuilder builder = new ResultErrorBuilder()
+                    .code(ComponentVerifier.CODE_INCOMPLETE_OPTION_GROUP)
+                    .attribute(ComponentVerifier.GROUP_NAME, group.getName())
+                    .attribute(ComponentVerifier.GROUP_OPTIONS, String.join(",", group.getOptions()));
+
+                for (String option : group.getOptions()) {
+                    if (!parameters.containsKey(option)) {
+                        builder.parameter(option);
+                    }
+                }
+
+                errors.add(builder.build());
+            }
+        }
+
+        return errors;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponentVerifier.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponentVerifier.java b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponentVerifier.java
index dea18ca..28c0a8f 100644
--- a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponentVerifier.java
+++ b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/SalesforceComponentVerifier.java
@@ -23,11 +23,13 @@ import java.util.Optional;
 
 import org.apache.camel.ComponentVerifier;
 import org.apache.camel.NoSuchOptionException;
+import org.apache.camel.component.salesforce.SalesforceLoginConfig.Type;
 import org.apache.camel.component.salesforce.api.SalesforceException;
 import org.apache.camel.component.salesforce.api.dto.RestError;
 import org.apache.camel.component.salesforce.internal.SalesforceSession;
 import org.apache.camel.component.salesforce.internal.client.DefaultRestClient;
 import org.apache.camel.impl.verifier.DefaultComponentVerifier;
+import org.apache.camel.impl.verifier.OptionsGroup;
 import org.apache.camel.impl.verifier.ResultBuilder;
 import org.apache.camel.impl.verifier.ResultErrorBuilder;
 import org.apache.camel.impl.verifier.ResultErrorHelper;
@@ -52,14 +54,22 @@ public class SalesforceComponentVerifier extends DefaultComponentVerifier {
     protected Result verifyParameters(Map<String, Object> parameters) {
         // Validate mandatory component options, needed to be done here as these
         // options are not properly marked as mandatory in the catalog.
+        //
+        // Validation rules are borrowed from SalesforceLoginConfig's validate
+        // method, which support 3 workflow:
+        //
+        // - OAuth Username/Password Flow
+        // - OAuth Refresh Token Flow:
+        // - OAuth JWT Flow
+        //
         ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS)
-            .error(ResultErrorHelper.requiresOption("clientId", parameters))
-            .error(ResultErrorHelper.requiresOption("clientSecret", parameters));
-
-            // TODO: either refreshToken or userName and password
-            //.error(ResultErrorHelper.requiresOption("refreshToken", parameters))
-            //.error(ResultErrorHelper.requiresOption("userName", parameters))
-            //.error(ResultErrorHelper.requiresOption("password", parameters));
+            .errors(ResultErrorHelper.requiresAny(
+                parameters,
+                OptionsGroup.withName(Type.USERNAME_PASSWORD).options("clientId", "clientSecret", "userName", "password"),
+                OptionsGroup.withName(Type.REFRESH_TOKEN).options("clientId", "clientSecret", "refreshToken"),
+                OptionsGroup.withName(Type.JWT).options("clientId", "userName", "keystore")
+            )
+        );
 
         // Validate using the catalog
         super.verifyParametersAgainstCatalog(builder, parameters);

http://git-wip-us.apache.org/repos/asf/camel/blob/9b341c7e/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/SalesforceComponentVerifierTest.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/SalesforceComponentVerifierTest.java b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/SalesforceComponentVerifierTest.java
index cb1607a..3b150e2 100644
--- a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/SalesforceComponentVerifierTest.java
+++ b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/SalesforceComponentVerifierTest.java
@@ -57,6 +57,7 @@ public class SalesforceComponentVerifierTest extends CamelTestSupport {
         parameters.put("userName", USERNAME);
         parameters.put("password", PASSWORD);
 
+
         return parameters;
     }
 
@@ -81,6 +82,47 @@ public class SalesforceComponentVerifierTest extends CamelTestSupport {
     // Parameters validation
     // *********************************
 
+    @Test
+    public void testUsernamePasswordParameters() {
+        HashMap<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "clientId");
+        parameters.put("clientSecret", "clientSecret");
+        parameters.put("userName", "userName");
+        parameters.put("password", "password");
+
+        ComponentVerifier.Result result = getVerifier().verify(ComponentVerifier.Scope.PARAMETERS, parameters);
+
+        Assert.assertEquals(ComponentVerifier.Result.Status.OK, result.getStatus());
+    }
+
+    @Test
+    public void testRefreshTokenParameters() {
+        Map<String, Object> parameters = getParameters();
+        parameters.put("clientId", "clientId");
+        parameters.put("clientSecret", "clientSecret");
+        parameters.put("refreshToken", "refreshToken");
+
+        ComponentVerifier.Result result = getVerifier().verify(ComponentVerifier.Scope.PARAMETERS, parameters);
+
+        Assert.assertEquals(ComponentVerifier.Result.Status.OK, result.getStatus());
+    }
+
+    @Test
+    public void testWrongParameters() {
+        HashMap<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "clientId");
+        parameters.put("clientSecret", "clientSecret");
+        parameters.put("password", "password");
+
+        ComponentVerifier.Result result = getVerifier().verify(ComponentVerifier.Scope.PARAMETERS, parameters);
+
+        Assert.assertEquals(ComponentVerifier.Result.Status.ERROR, result.getStatus());
+        Assert.assertEquals(3, result.getErrors().size());
+
+        Assert.assertEquals(ComponentVerifier.CODE_INCOMPLETE_OPTION_GROUP, result.getErrors().get(0).getCode());
+        Assert.assertEquals(ComponentVerifier.CODE_INCOMPLETE_OPTION_GROUP, result.getErrors().get(1).getCode());
+        Assert.assertEquals(ComponentVerifier.CODE_INCOMPLETE_OPTION_GROUP, result.getErrors().get(2).getCode());
+    }
 
     // *********************************
     // Connectivity validation
@@ -102,8 +144,6 @@ public class SalesforceComponentVerifierTest extends CamelTestSupport {
         ComponentVerifier.Result result = getVerifier().verify(ComponentVerifier.Scope.CONNECTIVITY, parameters);
 
         Assert.assertEquals(ComponentVerifier.Result.Status.ERROR, result.getStatus());
-
-        Assert.assertEquals(ComponentVerifier.Result.Status.ERROR, result.getStatus());
         Assert.assertEquals(2, result.getErrors().size());
 
         // Exception