CAMEL-10381 - fix NPE from component configuration

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

Branch: refs/heads/camel-2.17.x
Commit: 592e7cf7de4cbe64066b3f814ced35f4917767ac
Parents: 9fc87f2
Author: Jonathan Anstey <jans...@gmail.com>
Authored: Tue Oct 11 16:39:15 2016 -0230
Committer: Jonathan Anstey <jans...@gmail.com>
Committed: Tue Oct 11 16:43:42 2016 -0230

----------------------------------------------------------------------
 .../google/mail/GoogleMailComponent.java        | 10 ++-
 .../google/mail/GoogleMailEndpoint.java         |  2 +-
 .../google/mail/GmailConfigurationTest.java     | 73 ++++++++++++++++++++
 3 files changed, 81 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/592e7cf7/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailComponent.java
----------------------------------------------------------------------
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 b6896d6..7871a51 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
@@ -45,10 +45,11 @@ public class GoogleMailComponent extends 
AbstractApiComponent<GoogleMailApiName,
         return GoogleMailApiName.fromValue(apiNameStr);
     }
 
-    public Gmail getClient() {
+    public Gmail getClient(GoogleMailConfiguration googleMailConfiguration) {
         if (client == null) {
-            client = 
getClientFactory().makeClient(configuration.getClientId(), 
configuration.getClientSecret(), configuration.getScopes(), 
configuration.getApplicationName(),
-                    configuration.getRefreshToken(), 
configuration.getAccessToken());
+            client = 
getClientFactory().makeClient(googleMailConfiguration.getClientId(), 
googleMailConfiguration.getClientSecret(), 
+                    googleMailConfiguration.getScopes(), 
googleMailConfiguration.getApplicationName(),
+                    googleMailConfiguration.getRefreshToken(), 
googleMailConfiguration.getAccessToken());
         }
         return client;
     }
@@ -70,6 +71,9 @@ public class GoogleMailComponent extends 
AbstractApiComponent<GoogleMailApiName,
 
     @Override
     public GoogleMailConfiguration getConfiguration() {
+        if (configuration == null) {
+            configuration = new GoogleMailConfiguration();
+        }
         return super.getConfiguration();
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/592e7cf7/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java
 
b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java
index b07cfde..7810839 100644
--- 
a/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java
+++ 
b/components/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailEndpoint.java
@@ -107,7 +107,7 @@ public class GoogleMailEndpoint extends 
AbstractApiEndpoint<GoogleMailApiName, G
     }
 
     public Gmail getClient() {
-        return ((GoogleMailComponent) getComponent()).getClient();
+        return ((GoogleMailComponent) getComponent()).getClient(configuration);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/592e7cf7/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java
 
b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java
new file mode 100644
index 0000000..cc53619
--- /dev/null
+++ 
b/components/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GmailConfigurationTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import 
org.apache.camel.component.google.mail.internal.GmailUsersMessagesApiMethod;
+import org.apache.camel.component.google.mail.internal.GoogleMailApiCollection;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test class for {@link com.google.api.services.gmail.Gmail$Users$Messages}
+ * APIs.
+ */
+public class GmailConfigurationTest extends AbstractGoogleMailTestSupport {
+
+    // userid of the currently authenticated user
+    public static final String CURRENT_USERID = "me";
+    private static final Logger LOG = 
LoggerFactory.getLogger(GmailConfigurationTest.class);
+    private static final String PATH_PREFIX = 
GoogleMailApiCollection.getCollection().getApiName(GmailUsersMessagesApiMethod.class).getName();
+    private static final String TEST_URI = "google-mail://" + PATH_PREFIX + 
"/send?clientId=a&clientSecret=b&applicationName=c&accessToken=d&refreshToken=e";
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        final CamelContext context = new DefaultCamelContext(createRegistry());
+
+        // add GoogleMailComponent to Camel context but don't set up 
configuration
+        final GoogleMailComponent component = new GoogleMailComponent(context);
+        context.addComponent("google-mail", component);
+
+        return context;
+    }
+    
+    @Test
+    public void testConfiguration() throws Exception {
+        GoogleMailEndpoint endpoint = getMandatoryEndpoint(TEST_URI, 
GoogleMailEndpoint.class);
+        GoogleMailConfiguration configuration = endpoint.getConfiguration();
+        assertNotNull(configuration);
+        assertEquals("a", configuration.getClientId());
+        assertEquals("b", configuration.getClientSecret());
+        assertEquals("c", configuration.getApplicationName());
+        assertEquals("d", configuration.getAccessToken());
+        assertEquals("e", configuration.getRefreshToken());        
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // test route for send
+                from("direct://SEND").to(TEST_URI);
+            }
+        };
+    }
+}

Reply via email to