camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [4/9] camel git commit: [CAMEL-9659] Add simple polling consumer
Date Tue, 01 Mar 2016 20:02:01 GMT
[CAMEL-9659] Add simple polling consumer


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

Branch: refs/heads/master
Commit: ce54b04e1f0b5f7178dd0ee606d1ec644f24658d
Parents: f80654f
Author: Daniel Kulp <dkulp@apache.org>
Authored: Mon Feb 29 15:40:22 2016 -0500
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Tue Mar 1 14:23:04 2016 -0500

----------------------------------------------------------------------
 .../camel/component/gridfs/GridFsConsumer.java  | 88 +++++++++++++++++++-
 .../camel/component/gridfs/GridFsEndpoint.java  | 43 +++++++++-
 .../camel/component/gridfs/GridFsProducer.java  | 14 ++--
 .../component/gridfs/AbstractMongoDbTest.java   | 15 +++-
 .../gridfs/GridFsConsumerOperationsTest.java    | 74 ----------------
 .../component/gridfs/GridFsConsumerTest.java    | 75 +++++++++++++++++
 .../gridfs/GridFsProducerOperationsTest.java    | 74 ++++++++++++++++
 7 files changed, 290 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/main/java/org/apache/camel/component/gridfs/GridFsConsumer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/component/gridfs/GridFsConsumer.java b/src/main/java/org/apache/camel/component/gridfs/GridFsConsumer.java
index 4f2ce66..240dd47 100644
--- a/src/main/java/org/apache/camel/component/gridfs/GridFsConsumer.java
+++ b/src/main/java/org/apache/camel/component/gridfs/GridFsConsumer.java
@@ -19,22 +19,102 @@
 
 package org.apache.camel.component.gridfs;
 
+import java.io.InputStream;
+import java.util.concurrent.ExecutorService;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCursor;
+import com.mongodb.DBObject;
+import com.mongodb.gridfs.GridFSDBFile;
+import com.mongodb.util.JSON;
+
+import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
 
 /**
  * 
  */
-public class GridFsConsumer extends DefaultConsumer {
-    final GridFsEndpoint ep;
-    
+public class GridFsConsumer extends DefaultConsumer implements Runnable {
+    final GridFsEndpoint endpoint;
+    private ExecutorService executor;
+
     /**
      * @param endpoint
      * @param processor
      */
     public GridFsConsumer(GridFsEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
-        ep = endpoint;
+        this.endpoint = endpoint;
+    }
+
+    
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (executor != null) {
+            executor.shutdown();
+            executor = null;
+        }
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        executor = endpoint.getCamelContext().getExecutorServiceManager().newFixedThreadPool(this,
endpoint.getEndpointUri(), 1);
+        executor.execute(this);
     }
 
+    @Override
+    public void run() {
+        DBCursor c = null;
+        java.util.Date fromDate = new java.util.Date();
+        try {
+            Thread.sleep(endpoint.getInitialDelay());
+            while (isStarted()) {                
+                if (c == null || c.getCursorId() == 0) {
+                    if (c != null) {
+                        c.close();
+                    }
+                    String queryString = endpoint.getQuery();
+                    DBObject query;
+                    if (queryString == null) {
+                        query = new BasicDBObject();
+                    } else {
+                        query = (DBObject) JSON.parse(queryString);
+                    }
+                    
+                    query.put("uploadDate", new BasicDBObject("$gte", fromDate));
+                    c = endpoint.getFilesCollection().find(query);
+                    fromDate = new java.util.Date();
+                }
+                while (c.hasNext() && isStarted()) {
+                    GridFSDBFile file = (GridFSDBFile)c.next();
+                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));
+                    
+                    Exchange exchange = endpoint.createExchange();
+                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
+                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
+                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
+                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
+                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
+                    try {
+                        getProcessor().process(exchange);
+                    } catch (Exception e) {
+                        // TODO Auto-generated catch block
+                        e.printStackTrace();
+                    }
+                }
+                Thread.sleep(endpoint.getDelay());
+            }
+        } catch (Throwable e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+        }
+        if (c != null) {
+            c.close();
+        }
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java b/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java
index cef109a..008e004 100644
--- a/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java
+++ b/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.gridfs;
 
 import com.mongodb.DB;
+import com.mongodb.DBCollection;
 import com.mongodb.Mongo;
 import com.mongodb.ReadPreference;
 import com.mongodb.WriteConcern;
@@ -35,6 +36,9 @@ import org.slf4j.LoggerFactory;
 @UriEndpoint(scheme = "gridfs", title = "MongoDBGridFS", syntax = "gridfs:connectionBean",

             label = "database,nosql")
 public class GridFsEndpoint extends DefaultEndpoint {
+    public static final String GRIDFS_OPERATION = "gridfs.operation";
+    public static final String GRIDFS_METADATA = "gridfs.metadata";
+    public static final String GRIDFS_CHUNKSIZE = "gridfs.chunksize";
 
     private static final Logger LOG = LoggerFactory.getLogger(GridFsEndpoint.class);
 
@@ -53,10 +57,18 @@ public class GridFsEndpoint extends DefaultEndpoint {
     @UriParam
     private String operation;
 
+    @UriParam
+    private String query;
+    @UriParam
+    private long initialDelay = 1000;
+    @UriParam
+    private long delay = 500;
+    
     
     private Mongo mongoConnection;
     private DB db;
     private GridFS gridFs;
+    private DBCollection filesCollection;
 
     public GridFsEndpoint(String uri, GridFsComponent component) {
         super(uri, component);
@@ -88,7 +100,11 @@ public class GridFsEndpoint extends DefaultEndpoint {
         if (db == null) {
             throw new IllegalStateException("Could not initialize GridFsComponent. Database
" + database + " does not exist.");
         }
-        gridFs = new GridFS(db, bucket == null ? GridFS.DEFAULT_BUCKET : bucket);
+        gridFs = new GridFS(db, bucket == null ? GridFS.DEFAULT_BUCKET : bucket) {
+            {
+                filesCollection = getFilesCollection();
+            }
+        };
     }
 
     
@@ -121,8 +137,6 @@ public class GridFsEndpoint extends DefaultEndpoint {
     
     
     // ======= Getters and setters ===============================================
-
-
     public String getConnectionBean() {
         return connectionBean;
     }
@@ -153,6 +167,25 @@ public class GridFsEndpoint extends DefaultEndpoint {
         this.bucket = bucket;
     }
     
+    public String getQuery() {
+        return query;
+    }
+    public void setQuery(String query) {
+        this.query = query;
+    }
+    public long getDelay() {
+        return delay;
+    }
+    public void setDelay(long delay) {
+        this.delay = delay;
+    }
+    public long getInitialDelay() {
+        return initialDelay;
+    }
+    public void setInitialDelay(long initialDelay) {
+        this.initialDelay = delay;
+    }
+    
     /**
      * Set the {@link WriteConcern} for write operations on MongoDB using the standard ones.
      * Resolved from the fields of the WriteConcern class by calling the {@link WriteConcern#valueOf(String)}
method.
@@ -225,4 +258,8 @@ public class GridFsEndpoint extends DefaultEndpoint {
     public void setGridFs(GridFS gridFs) {
         this.gridFs = gridFs;
     }
+    public DBCollection getFilesCollection() {
+        return filesCollection;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java b/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java
index 7954644..58416b1 100644
--- a/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java
+++ b/src/main/java/org/apache/camel/component/gridfs/GridFsProducer.java
@@ -31,11 +31,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 
 
-public class GridFsProducer extends DefaultProducer {
-    public static final String GRIDFS_OPERATION = "gridfs.operation";
-    public static final String GRIDFS_METADATA = "gridfs.metadata";
-    public static final String GRIDFS_CHUNKSIZE = "gridfs.chunksize";
-    
+public class GridFsProducer extends DefaultProducer {    
     private GridFsEndpoint endpoint;
 
     public GridFsProducer(GridFsEndpoint endpoint) {
@@ -46,11 +42,11 @@ public class GridFsProducer extends DefaultProducer {
     public void process(Exchange exchange) throws Exception {
         String operation = endpoint.getOperation();
         if (operation == null) {
-            operation = exchange.getIn().getHeader(GRIDFS_OPERATION, String.class);
+            operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
         }
         if (operation == null || "create".equals(operation)) {
             final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
-            Long chunkSize = exchange.getIn().getHeader(GRIDFS_CHUNKSIZE, Long.class);
+            Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE,
Long.class);
 
             InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
             GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
@@ -61,7 +57,7 @@ public class GridFsProducer extends DefaultProducer {
             if (ct != null) {
                 gfsFile.setContentType(ct);
             }
-            String metaData = exchange.getIn().getHeader(GRIDFS_METADATA, String.class);
+            String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA,
String.class);
             DBObject dbObject = (DBObject) JSON.parse(metaData);
             gfsFile.setMetaData(dbObject);
             gfsFile.save();
@@ -73,7 +69,7 @@ public class GridFsProducer extends DefaultProducer {
             final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
             GridFSDBFile file = endpoint.getGridFs().findOne(filename);
             if (file != null) {
-                exchange.getIn().setHeader(GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
+                exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
                 exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                 exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                 exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java b/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java
index b1c94b9..f4c2bff 100644
--- a/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java
+++ b/src/test/java/org/apache/camel/component/gridfs/AbstractMongoDbTest.java
@@ -29,8 +29,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
 
 public abstract class AbstractMongoDbTest extends CamelTestSupport {
 
-    protected static MongoClient mongo;
-    protected static GridFS gridfs;
+    protected MongoClient mongo;
+    protected GridFS gridfs;
 
     protected ApplicationContext applicationContext;
 
@@ -38,9 +38,18 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport {
     @Override
     public void doPostSetup() {
         mongo = applicationContext.getBean(MongoClient.class);
-        gridfs = new GridFS(mongo.getDB("test"));
+        gridfs = new GridFS(mongo.getDB("test"), getBucket());
     }
 
+    public String getBucket() {
+        return this.getClass().getSimpleName();
+    }
+    
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        mongo.close();
+    }
 
     @Override
     protected CamelContext createCamelContext() throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerOperationsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerOperationsTest.java
b/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerOperationsTest.java
deleted file mode 100644
index 8aaa0de..0000000
--- a/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerOperationsTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * 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.gridfs;
-
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.builder.RouteBuilder;
-import org.junit.Test;
-
-public class GridFsConsumerOperationsTest extends AbstractMongoDbTest {
-    
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() {
-                from("direct:create").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create");
-                from("direct:remove").to("gridfs:myDb?database={{mongodb.testDb}}&operation=remove");
-                from("direct:findOne").to("gridfs:myDb?database={{mongodb.testDb}}&operation=findOne");
-                from("direct:listAll").to("gridfs:myDb?database={{mongodb.testDb}}&operation=listAll");
-                from("direct:count").to("gridfs:myDb?database={{mongodb.testDb}}&operation=count");
-                from("direct:headerOp").to("gridfs:myDb?database={{mongodb.testDb}}");
-            }
-        };
-    }
-    
-    @Test
-    public void testOperations() throws Exception {
-        Map<String, Object> headers = new HashMap<String, Object>();
-        String fn = "filename.for.db.txt";
-        assertEquals(0, gridfs.find(fn).size());
-        
-        headers.put(Exchange.FILE_NAME, fn);
-        String data = "This is some stuff to go into the db";
-        template.requestBodyAndHeaders("direct:create", data, headers);
-        assertEquals(1, gridfs.find(fn).size());
-        assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers));
-        InputStream ins = template.requestBodyAndHeaders("direct:findOne", null, headers,
InputStream.class);
-        assertNotNull(ins);
-        byte b[] = new byte[2048];
-        int i = ins.read(b);
-        assertEquals(data, new String(b, 0, i, "utf-8"));
-        
-        headers.put(Exchange.FILE_NAME, "2-" + fn);
-        
-        template.requestBodyAndHeaders("direct:create", data + "data2", headers);
-        assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers));
-        assertEquals(2, template.requestBody("direct:count", null, Integer.class).intValue());
-        
-        String s = template.requestBody("direct:listAll", null, String.class);
-        assertTrue(s.contains("2-" + fn));
-        template.requestBodyAndHeaders("direct:remove", null, headers);
-        assertEquals(1, template.requestBody("direct:count", null, Integer.class).intValue());
-        s = template.requestBody("direct:listAll", null, String.class);
-        assertFalse(s.contains("2-" + fn));
-    }
-}
-

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java b/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.java
new file mode 100644
index 0000000..a84260c
--- /dev/null
+++ b/src/test/java/org/apache/camel/component/gridfs/GridFsConsumerTest.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.component.gridfs;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+import org.junit.Test;
+
+/**
+ * 
+ */
+public class GridFsConsumerTest extends AbstractMongoDbTest {
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:create").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket="
+ getBucket());
+                from("gridfs:myDb?database={{mongodb.testDb}}&bucket=" + getBucket()).convertBodyTo(String.class).to("mock:test");
+            }
+        };
+    }
+    
+    
+    @Test
+    public void test() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:test");
+        String data = "This is some stuff to go into the db";
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived(data);
+        
+        Map<String, Object> headers = new HashMap<String, Object>();
+        String fn = "filename.for.db.txt";
+        assertEquals(0, gridfs.find(fn).size());
+        
+        headers.put(Exchange.FILE_NAME, fn);
+        template.requestBodyAndHeaders("direct:create", data, headers);
+        
+        mock.assertIsSatisfied();
+        mock.reset();
+        
+        mock.expectedMessageCount(3);
+        mock.expectedBodiesReceived(data, data, data);
+        
+        headers.put(Exchange.FILE_NAME, fn + "_1");
+        template.requestBodyAndHeaders("direct:create", data, headers);
+        headers.put(Exchange.FILE_NAME, fn + "_2");
+        template.requestBodyAndHeaders("direct:create", data, headers);
+        headers.put(Exchange.FILE_NAME, fn + "_3");
+        template.requestBodyAndHeaders("direct:create", data, headers);
+        mock.assertIsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ce54b04e/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java
b/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java
new file mode 100644
index 0000000..df7882d
--- /dev/null
+++ b/src/test/java/org/apache/camel/component/gridfs/GridFsProducerOperationsTest.java
@@ -0,0 +1,74 @@
+/**
+ * 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.gridfs;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class GridFsProducerOperationsTest extends AbstractMongoDbTest {
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:create").to("gridfs:myDb?database={{mongodb.testDb}}&operation=create&bucket="
+ getBucket());
+                from("direct:remove").to("gridfs:myDb?database={{mongodb.testDb}}&operation=remove&bucket="
+ getBucket());
+                from("direct:findOne").to("gridfs:myDb?database={{mongodb.testDb}}&operation=findOne&bucket="
+ getBucket());
+                from("direct:listAll").to("gridfs:myDb?database={{mongodb.testDb}}&operation=listAll&bucket="
+ getBucket());
+                from("direct:count").to("gridfs:myDb?database={{mongodb.testDb}}&operation=count&bucket="
+ getBucket());
+                from("direct:headerOp").to("gridfs:myDb?database={{mongodb.testDb}}&bucket="
+ getBucket());
+            }
+        };
+    }
+    
+    @Test
+    public void testOperations() throws Exception {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        String fn = "filename.for.db.txt";
+        assertEquals(0, gridfs.find(fn).size());
+        
+        headers.put(Exchange.FILE_NAME, fn);
+        String data = "This is some stuff to go into the db";
+        template.requestBodyAndHeaders("direct:create", data, headers);
+        assertEquals(1, gridfs.find(fn).size());
+        assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers));
+        InputStream ins = template.requestBodyAndHeaders("direct:findOne", null, headers,
InputStream.class);
+        assertNotNull(ins);
+        byte b[] = new byte[2048];
+        int i = ins.read(b);
+        assertEquals(data, new String(b, 0, i, "utf-8"));
+        
+        headers.put(Exchange.FILE_NAME, "2-" + fn);
+        
+        template.requestBodyAndHeaders("direct:create", data + "data2", headers);
+        assertEquals(1, template.requestBodyAndHeaders("direct:count", null, headers));
+        assertEquals(2, template.requestBody("direct:count", null, Integer.class).intValue());
+        
+        String s = template.requestBody("direct:listAll", null, String.class);
+        assertTrue(s.contains("2-" + fn));
+        template.requestBodyAndHeaders("direct:remove", null, headers);
+        assertEquals(1, template.requestBody("direct:count", null, Integer.class).intValue());
+        s = template.requestBody("direct:listAll", null, String.class);
+        assertFalse(s.contains("2-" + fn));
+    }
+}
+


Mime
View raw message