Search in sources :

Example 31 with BsonObjectId

use of org.bson.BsonObjectId in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method queryingWithEmptyQueryReturnsAllFiles.

// DATAMONGO-534, DATAMONGO-1762
@Test
public void queryingWithEmptyQueryReturnsAllFiles() throws IOException {
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml");
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
    GridFSFindIterable result = operations.find(new Query());
    result.into(files);
    assertThat(files).hasSize(1).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(reference);
}
Also used : Document(org.bson.Document) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoGridFSException(com.mongodb.MongoGridFSException) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Assertions(org.assertj.core.api.Assertions) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) PageRequest(org.springframework.data.domain.PageRequest) IOException(java.io.IOException) Test(org.junit.Test) GridFsCriteria(org.springframework.data.mongodb.gridfs.GridFsCriteria) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) List(java.util.List) Stream(java.util.stream.Stream) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Entry(java.util.Map.Entry) ObjectId(org.bson.types.ObjectId) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) Query(org.springframework.data.mongodb.core.query.Query) 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 32 with BsonObjectId

use of org.bson.BsonObjectId in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method findsFilesByResourceLocation.

// DATAMONGO-6
@Test
public void findsFilesByResourceLocation() throws IOException {
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml");
    GridFsResource[] resources = operations.getResources("foo.xml");
    assertThat(resources).hasSize(1);
    assertThat(((BsonObjectId) resources[0].getId()).getValue()).isEqualTo(reference);
    assertThat(resources[0].contentLength()).isEqualTo(resource.contentLength());
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 33 with BsonObjectId

use of org.bson.BsonObjectId in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method storesContentType.

// DATAMONGO-503
@Test
public void storesContentType() throws IOException {
    ObjectId reference = operations.store(resource.getInputStream(), "foo2.xml", "application/xml");
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
    GridFSFindIterable result = operations.find(query(whereContentType().is("application/xml")));
    result.into(files);
    assertThat(files).hasSize(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 34 with BsonObjectId

use of org.bson.BsonObjectId in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method marshalsComplexMetadata.

// DATAMONGO-6
@Test
public void marshalsComplexMetadata() throws IOException {
    Metadata metadata = new Metadata();
    metadata.version = "1.0";
    ObjectId reference = operations.store(resource.getInputStream(), "foo.xml", metadata);
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
    GridFSFindIterable result = operations.find(query(whereFilename().is("foo.xml")));
    result.into(files);
    assertThat(files).hasSize(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 35 with BsonObjectId

use of org.bson.BsonObjectId in project spring-data-mongodb by spring-projects.

the class GridFsTemplateIntegrationTests method considersSortWhenQueryingFiles.

// DATAMONGO-534
@Test
public void considersSortWhenQueryingFiles() throws IOException {
    ObjectId second = operations.store(resource.getInputStream(), "foo.xml");
    ObjectId third = operations.store(resource.getInputStream(), "foobar.xml");
    ObjectId first = operations.store(resource.getInputStream(), "bar.xml");
    Query query = new Query().with(Sort.by(Direction.ASC, "filename"));
    List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<>();
    GridFSFindIterable result = operations.find(query);
    result.into(files);
    assertThat(files).hasSize(3).extracting(it -> ((BsonObjectId) it.getId()).getValue()).containsExactly(first, second, third);
}
Also used : Document(org.bson.Document) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoGridFSException(com.mongodb.MongoGridFSException) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) Assertions(org.assertj.core.api.Assertions) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) PageRequest(org.springframework.data.domain.PageRequest) IOException(java.io.IOException) Test(org.junit.Test) GridFsCriteria(org.springframework.data.mongodb.gridfs.GridFsCriteria) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) List(java.util.List) Stream(java.util.stream.Stream) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Entry(java.util.Map.Entry) ObjectId(org.bson.types.ObjectId) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) Query(org.springframework.data.mongodb.core.query.Query) 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)

Aggregations

BsonObjectId (org.bson.BsonObjectId)39 ObjectId (org.bson.types.ObjectId)30 Test (org.junit.Test)18 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)14 BsonString (org.bson.BsonString)13 ArrayList (java.util.ArrayList)11 Document (org.bson.Document)11 BsonDocument (org.bson.BsonDocument)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Test (org.junit.jupiter.api.Test)8 List (java.util.List)7 BsonValue (org.bson.BsonValue)7 MongoGridFSException (com.mongodb.MongoGridFSException)6 Date (java.util.Date)6 Map (java.util.Map)6 BsonArray (org.bson.BsonArray)6 IOException (java.io.IOException)5 Assertions (org.assertj.core.api.Assertions)5 Before (org.junit.Before)5