use of com.mongodb.client.test.WorkerCodecProvider in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldFindAndReplaceWithDocumentRequiringACustomEncoder.
@Test
public void shouldFindAndReplaceWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 7);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Document updateOperation = new Document("$inc", new Document("numberOfJobs", 1));
Worker updatedDocument = collection.findOneAndUpdate(new Document("name", "Pat"), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker returned from updateOneAndGet should have the", updatedDocument.getNumberOfJobs(), equalTo(8));
}
use of com.mongodb.client.test.WorkerCodecProvider in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 0);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 1);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan);
assertThat("Document, retrieved from getOneAndReplace, should match the document inserted before", returnedDocument, equalTo(pat));
}
use of com.mongodb.client.test.WorkerCodecProvider in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
Aggregations