Get some of the drive-file API tests working

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

Branch: refs/heads/master
Commit: ffda80ee7a1c9305050071f2207a7c2905069f47
Parents: 72e5aad
Author: Jonathan Anstey <jans...@gmail.com>
Authored: Thu Jul 10 19:48:01 2014 -0230
Committer: Jonathan Anstey <jans...@gmail.com>
Committed: Mon Sep 8 09:06:09 2014 -0230

----------------------------------------------------------------------
 .../drive/AbstractGoogleDriveTestSupport.java   |  19 +--
 .../google/drive/DriveFilesIntegrationTest.java | 166 +++++++++++--------
 .../src/test/resources/camel-box-small.png      | Bin 0 -> 38856 bytes
 .../src/test/resources/test-options.properties  |  24 +++
 4 files changed, 133 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ffda80ee/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/AbstractGoogleDriveTestSupport.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/AbstractGoogleDriveTestSupport.java
 
b/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/AbstractGoogleDriveTestSupport.java
index 45f7b51..1626e99 100644
--- 
a/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/AbstractGoogleDriveTestSupport.java
+++ 
b/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/AbstractGoogleDriveTestSupport.java
@@ -17,13 +17,10 @@
 package org.apache.camel.component.google.drive;
 
 import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringReader;
-import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -72,13 +69,13 @@ public abstract class AbstractGoogleDriveTestSupport 
extends CamelTestSupport {
             throw new IOException(String.format("%s could not be loaded: %s", 
TEST_OPTIONS_PROPERTIES, e.getMessage()),
                 e);
         }
-
-        // cache test properties
-        refreshToken = properties.getProperty(REFRESH_TOKEN_PROPERTY);
-        testFolderId = properties.getProperty("testFolderId");
-        testFileId = properties.getProperty("testFileId");
-        testUserId = properties.getProperty("testUserId");
-
+//
+//        // cache test properties
+//        refreshToken = properties.getProperty(REFRESH_TOKEN_PROPERTY);
+//        testFolderId = properties.getProperty("testFolderId");
+//        testFileId = properties.getProperty("testFileId");
+//        testUserId = properties.getProperty("testUserId");
+//
         Map<String, Object> options = new HashMap<String, Object>();
         for (Map.Entry<Object, Object> entry : properties.entrySet()) {
             options.put(entry.getKey().toString(), entry.getValue());
@@ -92,7 +89,7 @@ public abstract class AbstractGoogleDriveTestSupport extends 
CamelTestSupport {
         final GoogleDriveComponent component = new 
GoogleDriveComponent(context);
 
         component.setConfiguration(configuration);
-        context.addComponent("box", component);
+        context.addComponent("google-drive", component);
 
         return context;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/ffda80ee/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/DriveFilesIntegrationTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/DriveFilesIntegrationTest.java
 
b/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/DriveFilesIntegrationTest.java
index 5b38298..c8cd5a6 100644
--- 
a/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/DriveFilesIntegrationTest.java
+++ 
b/components/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/DriveFilesIntegrationTest.java
@@ -5,6 +5,7 @@
 package org.apache.camel.component.google.drive;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.camel.builder.RouteBuilder;
@@ -12,98 +13,129 @@ import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import 
org.apache.camel.component.google.drive.internal.GoogleDriveApiCollection;
 import org.apache.camel.component.google.drive.internal.DriveFilesApiMethod;
 
+import com.google.api.client.http.FileContent;
+import com.google.api.client.util.DateTime;
+import com.google.api.services.drive.model.File;
+import com.google.api.services.drive.model.FileList;
+
 /**
  * Test class for com.google.api.services.drive.Drive$Files APIs.
- * TODO Move the file to src/test/java, populate parameter values, and remove 
@Ignore annotations.
- * The class source won't be generated again if the generator MOJO finds it 
under src/test/java.
  */
 public class DriveFilesIntegrationTest extends AbstractGoogleDriveTestSupport {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(DriveFilesIntegrationTest.class);
     private static final String PATH_PREFIX = 
GoogleDriveApiCollection.getCollection().getApiName(DriveFilesApiMethod.class).getName();
-
-    // TODO provide parameter values for copy
-    @Ignore
+    private static final String TEST_UPLOAD_FILE = 
"src/test/resources/log4j.properties";
+    private static final String TEST_UPLOAD_IMG = 
"src/test/resources/camel-box-small.png";
+    private static final java.io.File UPLOAD_FILE = new 
java.io.File(TEST_UPLOAD_FILE);
+    
     @Test
     public void testCopy() throws Exception {
+        File testFile = uploadTestFile();
+        String fromFileId = testFile.getId();
+        
+        File toFile = new File();
+        toFile.setTitle(UPLOAD_FILE.getName() + "_copy");
+        
         final Map<String, Object> headers = new HashMap<String, Object>();
         // parameter type is String
-        headers.put("CamelGoogleDrive.fileId", null);
+        headers.put("CamelGoogleDrive.fileId", fromFileId);
         // parameter type is com.google.api.services.drive.model.File
-        headers.put("CamelGoogleDrive.content", null);
+        headers.put("CamelGoogleDrive.content", toFile);
 
-        final com.google.api.services.drive.Drive.Files.Copy result = 
requestBodyAndHeaders("direct://COPY", null, headers);
+        final File result = requestBodyAndHeaders("direct://COPY", null, 
headers);
 
         assertNotNull("copy result", result);
+        assertEquals(toFile.getTitle(), result.getTitle());        
         LOG.debug("copy: " + result);
     }
 
-    // TODO provide parameter values for delete
-    @Ignore
     @Test
     public void testDelete() throws Exception {
+        File testFile = uploadTestFile();
+        String fileId = testFile.getId();
+        
         // using String message body for single parameter "fileId"
-        final com.google.api.services.drive.Drive.Files.Delete result = 
requestBody("direct://DELETE", null);
+        sendBody("direct://DELETE", fileId);
 
-        assertNotNull("delete result", result);
-        LOG.debug("delete: " + result);
+        // the file should be gone now
+        final File result = requestBody("direct://GET", fileId);
+        assertNull("get result", result);
     }
 
-    // TODO provide parameter values for get
-    @Ignore
     @Test
     public void testGet() throws Exception {
+        File testFile = uploadTestFile();
+        String fileId = testFile.getId();
+        
         // using String message body for single parameter "fileId"
-        final com.google.api.services.drive.Drive.Files.Get result = 
requestBody("direct://GET", null);
+        final File result = requestBody("direct://GET", fileId);
 
         assertNotNull("get result", result);
         LOG.debug("get: " + result);
     }
 
-    // TODO provide parameter values for insert
-    @Ignore
     @Test
     public void testInsert() throws Exception {
+        File file = new File();        
+        file.setTitle(UPLOAD_FILE.getName());
         // using com.google.api.services.drive.model.File message body for 
single parameter "content"
-        final com.google.api.services.drive.Drive.Files.Insert result = 
requestBody("direct://INSERT", null);
-
+        File result = requestBody("direct://INSERT", file);
         assertNotNull("insert result", result);
         LOG.debug("insert: " + result);
     }
 
-    // TODO provide parameter values for insert
-    @Ignore
-    @Test
-    public void testInsert_1() throws Exception {
+    private File uploadTestFile() {
+        File fileMetadata = new File();
+        fileMetadata.setTitle(UPLOAD_FILE.getName());
+        FileContent mediaContent = new FileContent(null, UPLOAD_FILE);
+        
         final Map<String, Object> headers = new HashMap<String, Object>();
         // parameter type is com.google.api.services.drive.model.File
-        headers.put("CamelGoogleDrive.content", null);
+        headers.put("CamelGoogleDrive.content", fileMetadata);
         // parameter type is 
com.google.api.client.http.AbstractInputStreamContent
-        headers.put("CamelGoogleDrive.mediaContent", null);
+        headers.put("CamelGoogleDrive.mediaContent", mediaContent);
 
-        final com.google.api.services.drive.Drive.Files.Insert result = 
requestBodyAndHeaders("direct://INSERT_1", null, headers);
+        File result = requestBodyAndHeaders("direct://INSERT_1", null, 
headers);
+        return result;
+    }
+
+    @Test
+    public void testInsert_1() throws Exception {        
+        File result = uploadTestFile();
 
         assertNotNull("insert result", result);
         LOG.debug("insert: " + result);
     }
 
-    @Ignore
     @Test
     public void testList() throws Exception {
-        final com.google.api.services.drive.Drive.Files.List result = 
requestBody("direct://LIST", null);
-
+        // upload a test file
+        File theTestFile = uploadTestFile();
+        
+        final FileList result = requestBody("direct://LIST", null);
         assertNotNull("list result", result);
         LOG.debug("list: " + result);
     }
 
-    // TODO provide parameter values for patch
     @Ignore
     @Test
     public void testPatch() throws Exception {
+        // TODO have to support setting patch parameters before callinfg 
execute like:
+        /*
+      File file = new File();
+      file.setTitle(newTitle);
+
+      // Rename the file using a patch request.
+      Files.Patch patchRequest = service.files().patch(fileId, file);
+      patchRequest.setFields("title");
+
+      File updatedFile = patchRequest.execute();
+         */        
+        
         final Map<String, Object> headers = new HashMap<String, Object>();
         // parameter type is String
         headers.put("CamelGoogleDrive.fileId", null);
@@ -116,68 +148,72 @@ public class DriveFilesIntegrationTest extends 
AbstractGoogleDriveTestSupport {
         LOG.debug("patch: " + result);
     }
 
-    // TODO provide parameter values for touch
-    @Ignore
     @Test
     public void testTouch() throws Exception {
+        File theTestFile = uploadTestFile();
+        DateTime createdDate = theTestFile.getModifiedDate();
         // using String message body for single parameter "fileId"
-        final com.google.api.services.drive.Drive.Files.Touch result = 
requestBody("direct://TOUCH", null);
+        File result = requestBody("direct://TOUCH", theTestFile.getId());
 
         assertNotNull("touch result", result);
-        LOG.debug("touch: " + result);
+        assertTrue(result.getModifiedDate().getValue() > 
createdDate.getValue());
     }
 
-    // TODO provide parameter values for trash
-    @Ignore
     @Test
     public void testTrash() throws Exception {
-        // using String message body for single parameter "fileId"
-        final com.google.api.services.drive.Drive.Files.Trash result = 
requestBody("direct://TRASH", null);
+        File testFile = uploadTestFile();
+        String fileId = testFile.getId();       
 
-        assertNotNull("trash result", result);
-        LOG.debug("trash: " + result);
-    }
+        assertNotNull("trash result", requestBody("direct://TRASH", fileId));
+        assertNotNull("untrash result", requestBody("direct://UNTRASH", 
fileId));
 
-    // TODO provide parameter values for untrash
-    @Ignore
-    @Test
-    public void testUntrash() throws Exception {
-        // using String message body for single parameter "fileId"
-        final com.google.api.services.drive.Drive.Files.Untrash result = 
requestBody("direct://UNTRASH", null);
-
-        assertNotNull("untrash result", result);
-        LOG.debug("untrash: " + result);
-    }
+    }   
 
-    // TODO provide parameter values for update
-    @Ignore
     @Test
     public void testUpdate() throws Exception {
+        File theTestFile = uploadTestFile();
+        
         final Map<String, Object> headers = new HashMap<String, Object>();
         // parameter type is String
-        headers.put("CamelGoogleDrive.fileId", null);
+        headers.put("CamelGoogleDrive.fileId", theTestFile.getId());
         // parameter type is com.google.api.services.drive.model.File
-        headers.put("CamelGoogleDrive.content", null);
+        headers.put("CamelGoogleDrive.content", theTestFile);
 
-        final com.google.api.services.drive.Drive.Files.Update result = 
requestBodyAndHeaders("direct://UPDATE", null, headers);
+        File result = requestBodyAndHeaders("direct://UPDATE", null, headers);
 
         assertNotNull("update result", result);
         LOG.debug("update: " + result);
     }
 
-    // TODO provide parameter values for update
-    @Ignore
     @Test
-    public void testUpdate_1() throws Exception {
+    public void testUpdate_1() throws Exception {       
+        
+        // First retrieve the file from the API.
+        File testFile = uploadTestFile();
+        String fileId = testFile.getId();
+        
+        // using String message body for single parameter "fileId"
+        final File file = requestBody("direct://GET", fileId);
+
+        // File's new metadata.
+        file.setTitle("camel.png");
+        file.setMimeType("application/vnd.google-apps.photo");
+
+        // File's new content.
+        java.io.File fileContent = new java.io.File(TEST_UPLOAD_IMG);
+        FileContent mediaContent = new 
FileContent("application/vnd.google-apps.photo", fileContent);
+
+        // Send the request to the API.
+        
         final Map<String, Object> headers = new HashMap<String, Object>();
         // parameter type is String
-        headers.put("CamelGoogleDrive.fileId", null);
+        headers.put("CamelGoogleDrive.fileId", fileId);
         // parameter type is com.google.api.services.drive.model.File
-        headers.put("CamelGoogleDrive.content", null);
+        headers.put("CamelGoogleDrive.content", file);
         // parameter type is 
com.google.api.client.http.AbstractInputStreamContent
-        headers.put("CamelGoogleDrive.mediaContent", null);
+        headers.put("CamelGoogleDrive.mediaContent", mediaContent);
 
-        final com.google.api.services.drive.Drive.Files.Update result = 
requestBodyAndHeaders("direct://UPDATE_1", null, headers);
+        File result = requestBodyAndHeaders("direct://UPDATE_1", null, 
headers);
 
         assertNotNull("update result", result);
         LOG.debug("update: " + result);

http://git-wip-us.apache.org/repos/asf/camel/blob/ffda80ee/components/camel-google-drive/src/test/resources/camel-box-small.png
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/test/resources/camel-box-small.png 
b/components/camel-google-drive/src/test/resources/camel-box-small.png
new file mode 100755
index 0000000..42a6417
Binary files /dev/null and 
b/components/camel-google-drive/src/test/resources/camel-box-small.png differ

http://git-wip-us.apache.org/repos/asf/camel/blob/ffda80ee/components/camel-google-drive/src/test/resources/test-options.properties
----------------------------------------------------------------------
diff --git 
a/components/camel-google-drive/src/test/resources/test-options.properties 
b/components/camel-google-drive/src/test/resources/test-options.properties
new file mode 100644
index 0000000..8e21b12
--- /dev/null
+++ b/components/camel-google-drive/src/test/resources/test-options.properties
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+#####################################
+## Login properties for Google Drive Component
+#####################################
+## Application client id and secret
+clientId=
+clientSecret=
+applicationName=

Reply via email to