Search in sources :

Example 1 with GridFSFindIterable

use of com.mongodb.client.gridfs.GridFSFindIterable in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method storesAndFindsSimpleDocumentWithMetadataObject.

// DATAMONGO-809
@Test
public void storesAndFindsSimpleDocumentWithMetadataObject() throws IOException {
    Metadata metadata = new Metadata();
    metadata.version = "1.0";
    ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
    GridFSFindIterable result = operations.find(query(whereMetaData("version").is("1.0")));
    result.into(files);
    assertThat(files).hasSize(1).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(reference);
}
Also used : Document(org.bson.Document) Criteria.where(org.springframework.data.mongodb.core.query.Criteria.where) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoGridFSException(com.mongodb.MongoGridFSException) IOException(java.io.IOException) Test(org.junit.Test) GridFsCriteria(org.springframework.data.mongodb.gridfs.GridFsCriteria) Query(org.springframework.data.mongodb.core.query.Query) ArrayList(java.util.ArrayList) List(java.util.List) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) ContextConfiguration(org.springframework.test.context.ContextConfiguration) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Assertions(org.assertj.core.api.Assertions) ObjectId(org.bson.types.ObjectId) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) BsonObjectId(org.bson.BsonObjectId) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Test(org.junit.Test)

Example 2 with GridFSFindIterable

use of com.mongodb.client.gridfs.GridFSFindIterable in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method writesMetadataCorrectly.

// DATAMONGO-6
@Test
public void writesMetadataCorrectly() throws IOException {
    Document metadata = new Document("key", "value");
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata);
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
    GridFSFindIterable result = operations.find(query(whereMetaData("key").is("value")));
    result.into(files);
    assertThat(files.size()).isEqualTo(1);
    assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Document(org.bson.Document) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 3 with GridFSFindIterable

use of com.mongodb.client.gridfs.GridFSFindIterable in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method storesAndFindsSimpleDocument.

// DATAMONGO-6
@Test
public void storesAndFindsSimpleDocument() throws IOException {
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml");
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
    GridFSFindIterable result = operations.find(query(where("_id").is(reference)));
    result.into(files);
    assertThat(files.size()).isEqualTo(1);
    assertThat(((BsonObjectId) files.get(0).getId()).getValue()).isEqualTo(reference);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 4 with GridFSFindIterable

use of com.mongodb.client.gridfs.GridFSFindIterable in project spring-data-mongodb by spring-projects.

the class GridFsTemplate method getResources.

/*
     * (non-Javadoc)
     * @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
     */
public GridFsResource[] getResources(String locationPattern) {
    if (!StringUtils.hasText(locationPattern)) {
        return new GridFsResource[0];
    }
    AntPath path = new AntPath(locationPattern);
    if (path.isPattern()) {
        GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex())));
        List<GridFsResource> resources = new ArrayList<>();
        for (GridFSFile file : files) {
            resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())));
        }
        return resources.toArray(new GridFsResource[0]);
    }
    return new GridFsResource[] { getResource(locationPattern) };
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) ArrayList(java.util.ArrayList) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable)

Example 5 with GridFSFindIterable

use of com.mongodb.client.gridfs.GridFSFindIterable in project georocket by georocket.

the class MongoDBStoreTest method validateAfterStoreDelete.

@Override
protected void validateAfterStoreDelete(TestContext context, Vertx vertx, String path, Handler<AsyncResult<Void>> handler) {
    vertx.executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            GridFSFindIterable files = gridFS.find();
            context.assertTrue(Iterables.isEmpty(files));
        }
        f.complete();
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)10 ArrayList (java.util.ArrayList)9 BsonObjectId (org.bson.BsonObjectId)8 ObjectId (org.bson.types.ObjectId)8 Test (org.junit.Test)8 Document (org.bson.Document)5 MongoGridFSException (com.mongodb.MongoGridFSException)4 IOException (java.io.IOException)4 List (java.util.List)4 Assertions (org.assertj.core.api.Assertions)4 Before (org.junit.Before)4 RunWith (org.junit.runner.RunWith)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 Resource (org.springframework.core.io.Resource)4 Sort (org.springframework.data.domain.Sort)4 Direction (org.springframework.data.domain.Sort.Direction)4 Criteria.where (org.springframework.data.mongodb.core.query.Criteria.where)4 Query (org.springframework.data.mongodb.core.query.Query)4