You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/09/11 08:11:08 UTC

[camel] 01/03: CAMEL-12802 - Camel-google-mail, Camel-google-mail-stream: Add Google Mail verifier Extension

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

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

commit 786b0b8b3f2f2357cd3723c36a7a6d4350419e7b
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Tue Sep 11 09:55:48 2018 +0200

    CAMEL-12802 - Camel-google-mail, Camel-google-mail-stream: Add Google Mail verifier Extension
---
 .../component/google/mail/GoogleMailComponent.java |  2 +
 .../mail/GoogleMailComponentVerifierExtension.java | 81 ++++++++++++++++++++++
 .../mail/stream/GoogleMailStreamComponent.java     |  3 +-
 .../GoogleMailComponentVerifierExtensionTest.java  | 68 ++++++++++++++++++
 ...leMailStreamComponentVerifierExtensionTest.java | 68 ++++++++++++++++++
 .../src/test/resources/log4j2.properties           | 11 ++-
 6 files changed, 229 insertions(+), 4 deletions(-)

diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java
index 534e1da..c5fc1d5 100644
--- a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java
+++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java
@@ -37,10 +37,12 @@ public class GoogleMailComponent extends AbstractApiComponent<GoogleMailApiName,
 
     public GoogleMailComponent() {
         super(GoogleMailEndpoint.class, GoogleMailApiName.class, GoogleMailApiCollection.getCollection());
+        registerExtension(new GoogleMailComponentVerifierExtension());
     }
 
     public GoogleMailComponent(CamelContext context) {
         super(context, GoogleMailEndpoint.class, GoogleMailApiName.class, GoogleMailApiCollection.getCollection());
+        registerExtension(new GoogleMailComponentVerifierExtension());
     }
 
     @Override
diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtension.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtension.java
new file mode 100644
index 0000000..2b29d1e
--- /dev/null
+++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtension.java
@@ -0,0 +1,81 @@
+/**
+ * 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.component.google.mail;
+
+import java.util.Map;
+
+import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension;
+import org.apache.camel.component.extension.verifier.ResultBuilder;
+import org.apache.camel.component.extension.verifier.ResultErrorBuilder;
+import org.apache.camel.component.extension.verifier.ResultErrorHelper;
+import org.apache.camel.component.google.mail.BatchGoogleMailClientFactory;
+import org.apache.camel.component.google.mail.GoogleMailClientFactory;
+import org.apache.camel.component.google.mail.GoogleMailConfiguration;
+
+import com.google.api.services.gmail.Gmail;
+
+public class GoogleMailComponentVerifierExtension extends DefaultComponentVerifierExtension {
+
+    public GoogleMailComponentVerifierExtension() {
+        this("google-mail");
+    }
+
+    public GoogleMailComponentVerifierExtension(String scheme) {
+        super(scheme);
+    }
+
+
+    // *********************************
+    // Parameters validation
+    // *********************************
+
+    @Override
+    protected Result verifyParameters(Map<String, Object> parameters) {
+
+        ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS).error(ResultErrorHelper.requiresOption("applicationName", parameters))
+            .error(ResultErrorHelper.requiresOption("clientId", parameters)).error(ResultErrorHelper.requiresOption("clientSecret", parameters));
+
+        return builder.build();
+    }
+
+    // *********************************
+    // Connectivity validation
+    // *********************************
+
+    @Override
+    @SuppressWarnings("PMD.AvoidCatchingGenericException")
+    protected Result verifyConnectivity(Map<String, Object> parameters) {
+        ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY);
+
+        try {
+            GoogleMailConfiguration configuration = setProperties(new GoogleMailConfiguration(), parameters);
+            GoogleMailClientFactory clientFactory = new BatchGoogleMailClientFactory();
+            Gmail client = clientFactory.makeClient(configuration.getClientId(), configuration.getClientSecret(),
+                    configuration.getApplicationName(),
+                    configuration.getRefreshToken(), configuration.getAccessToken());
+            client.users().getProfile((String) parameters.get("userId")).execute();
+        } catch (Exception e) {
+            ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage())
+                .detail("gmail_exception_message", e.getMessage()).detail(VerificationError.ExceptionAttribute.EXCEPTION_CLASS, e.getClass().getName())
+                .detail(VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE, e);
+
+            builder.error(errorBuilder.build());
+        }
+
+        return builder.build();
+    }
+}
\ No newline at end of file
diff --git a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponent.java b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponent.java
index 3d115ef..e5740e3 100644
--- a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponent.java
+++ b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponent.java
@@ -24,6 +24,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.component.google.mail.BatchGoogleMailClientFactory;
 import org.apache.camel.component.google.mail.GoogleMailClientFactory;
+import org.apache.camel.component.google.mail.GoogleMailComponentVerifierExtension;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.spi.Metadata;
 
@@ -45,7 +46,7 @@ public class GoogleMailStreamComponent extends DefaultComponent {
 
     public GoogleMailStreamComponent(CamelContext context) {
         super(context);
-
+        registerExtension(new GoogleMailComponentVerifierExtension());
         this.configuration = new GoogleMailStreamConfiguration();
     }
 
diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtensionTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtensionTest.java
new file mode 100644
index 0000000..c288d99
--- /dev/null
+++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailComponentVerifierExtensionTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.component.google.mail;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Component;
+import org.apache.camel.component.extension.ComponentVerifierExtension;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class GoogleMailComponentVerifierExtensionTest extends CamelTestSupport {
+
+    // *************************************************
+    // Tests (parameters)
+    // *************************************************
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testParameters() throws Exception {
+        Component component = context().getComponent("google-mail");
+
+        ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
+
+        Map<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "l");
+        parameters.put("clientSecret", "k");
+        parameters.put("applicationName", "test");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
+    }
+
+    @Test
+    public void testConnectivity() throws Exception {
+        Component component = context().getComponent("google-mail");
+        ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
+
+        Map<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "l");
+        parameters.put("clientSecret", "k");
+        parameters.put("userId", "test@gmail.com");
+        parameters.put("applicationName", "test");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
+    }
+
+}
diff --git a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponentVerifierExtensionTest.java b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponentVerifierExtensionTest.java
new file mode 100644
index 0000000..46a0ca6
--- /dev/null
+++ b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamComponentVerifierExtensionTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.component.google.mail.stream;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Component;
+import org.apache.camel.component.extension.ComponentVerifierExtension;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class GoogleMailStreamComponentVerifierExtensionTest extends CamelTestSupport {
+
+    // *************************************************
+    // Tests (parameters)
+    // *************************************************
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testParameters() throws Exception {
+        Component component = context().getComponent("google-mail-stream");
+
+        ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
+
+        Map<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "l");
+        parameters.put("clientSecret", "k");
+        parameters.put("applicationName", "test");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
+    }
+
+    @Test
+    public void testConnectivity() throws Exception {
+        Component component = context().getComponent("google-mail-stream");
+        ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
+
+        Map<String, Object> parameters = new HashMap<>();
+        parameters.put("clientId", "l");
+        parameters.put("clientSecret", "k");
+        parameters.put("userId", "test@gmail.com");
+        parameters.put("applicationName", "test");
+
+        ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
+        Assert.assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
+    }
+
+}
diff --git a/components/camel-google-mail/src/test/resources/log4j2.properties b/components/camel-google-mail/src/test/resources/log4j2.properties
index d9f0508..60459e2 100644
--- a/components/camel-google-mail/src/test/resources/log4j2.properties
+++ b/components/camel-google-mail/src/test/resources/log4j2.properties
@@ -15,9 +15,14 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/camel-google-mail-test.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
 appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
-appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
-rootLogger.level = INFO
-rootLogger.appenderRef.out.ref = out
+appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+rootLogger.level = DEBUG
+rootLogger.appenderRef.file.ref = file