use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.
the class MongoTemplateUpdateTests method aggregateUpdateWithSet.
// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithSet() {
Score score1 = new Score(1, "Maya", Arrays.asList(10, 5, 10), Arrays.asList(10, 8), 0);
Score score2 = new Score(2, "Ryan", Arrays.asList(5, 6, 5), Arrays.asList(8, 8), 8);
template.insertAll(Arrays.asList(score1, score2));
AggregationUpdate update = AggregationUpdate.update().set(//
SetOperation.builder().set("totalHomework").toValueOf(ArithmeticOperators.valueOf("homework").sum()).and().set("totalQuiz").toValueOf(//
ArithmeticOperators.valueOf("quiz").sum())).set(//
SetOperation.builder().set("totalScore").toValueOf(ArithmeticOperators.valueOf("totalHomework").add("totalQuiz").add("extraCredit")));
template.update(Score.class).apply(update).all();
//
assertThat(collection(Score.class).find(new org.bson.Document()).into(new ArrayList<>())).containsExactlyInAnyOrder(org.bson.Document.parse("{\"_id\" : 1, \"student\" : \"Maya\", \"homework\" : [ 10, 5, 10 ], \"quiz\" : [ 10, 8 ], \"extraCredit\" : 0, \"totalHomework\" : 25, \"totalQuiz\" : 18, \"totalScore\" : 43, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Score\"}"), org.bson.Document.parse("{ \"_id\" : 2, \"student\" : \"Ryan\", \"homework\" : [ 5, 6, 5 ], \"quiz\" : [ 8, 8 ], \"extraCredit\" : 8, \"totalHomework\" : 16, \"totalQuiz\" : 16, \"totalScore\" : 40, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Score\"}"));
}
use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.
the class MongoTemplateUpdateTests method versionedAggregateUpdateWithSet.
// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void versionedAggregateUpdateWithSet() {
Versioned source = template.insert(Versioned.class).one(new Versioned("id-1", "value-0"));
AggregationUpdate update = AggregationUpdate.update().set("value").toValue("changed");
template.update(Versioned.class).matching(Query.query(Criteria.where("id").is(source.id))).apply(update).first();
assertThat(collection(Versioned.class).find(new org.bson.Document("_id", source.id)).limit(1).into(new ArrayList<>())).containsExactly(new org.bson.Document("_id", source.id).append("version", 1L).append("value", "changed").append("_class", "org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Versioned"));
}
use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.
the class MongoTemplateUpdateTests method aggregationUpdateUpsertsCorrectly.
// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregationUpdateUpsertsCorrectly() {
AggregationUpdate update = AggregationUpdate.update().set("title").toValue("The Burning White");
template.update(Book.class).matching(Query.query(Criteria.where("id").is(1))).apply(update).upsert();
assertThat(all(Book.class)).containsExactly(org.bson.Document.parse("{\"_id\" : 1, \"title\" : \"The Burning White\" }"));
}
use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.
the class MongoTemplateUpdateTests method aggregateUpdateWithUnset.
// DATAMONGO-2331
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.2")
void aggregateUpdateWithUnset() {
Book antelopeAntics = new Book();
antelopeAntics.id = 1;
antelopeAntics.title = "Antelope Antics";
antelopeAntics.isbn = "0001122223334";
antelopeAntics.author = new Author("Auntie", "An");
antelopeAntics.stock = new ArrayList<>();
antelopeAntics.stock.add(new Warehouse("A", 5));
antelopeAntics.stock.add(new Warehouse("B", 15));
Book beesBabble = new Book();
beesBabble.id = 2;
beesBabble.title = "Bees Babble";
beesBabble.isbn = "999999999333";
beesBabble.author = new Author("Bee", "Bumble");
beesBabble.stock = new ArrayList<>();
beesBabble.stock.add(new Warehouse("A", 2));
beesBabble.stock.add(new Warehouse("B", 5));
template.insertAll(Arrays.asList(antelopeAntics, beesBabble));
AggregationUpdate update = AggregationUpdate.update().unset("isbn", "stock");
template.update(Book.class).apply(update).all();
//
assertThat(all(Book.class)).containsExactlyInAnyOrder(org.bson.Document.parse("{ \"_id\" : 1, \"title\" : \"Antelope Antics\", \"author\" : { \"last\" : \"An\", \"first\" : \"Auntie\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"), org.bson.Document.parse("{ \"_id\" : 2, \"title\" : \"Bees Babble\", \"author\" : { \"last\" : \"Bumble\", \"first\" : \"Bee\" }, \"_class\" : \"org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Book\" }"));
}
use of org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method watchesDatabaseCorrectly.
// DATAMONGO-2012
@Test
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
@EnableIfReplicaSetAvailable
void watchesDatabaseCorrectly() throws InterruptedException {
template.dropCollection(Person.class).onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
template.dropCollection("personX").onErrorResume(it -> Mono.empty()).as(StepVerifier::create).verifyComplete();
template.createCollection(Person.class).as(StepVerifier::create).expectNextCount(1).verifyComplete();
template.createCollection("personX").as(StepVerifier::create).expectNextCount(1).verifyComplete();
BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
Disposable disposable = template.changeStream(ChangeStreamOptions.empty(), Person.class).doOnNext(documents::add).subscribe();
// just give it some time to link to the collection.
Thread.sleep(500);
Person person1 = new Person("Spring", 38);
Person person2 = new Person("Data", 37);
Person person3 = new Person("MongoDB", 39);
//
template.save(person1).as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
//
template.save(person2).as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
//
template.save(person3, "personX").as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
// just give it some time to link receive all events
Thread.sleep(500);
try {
assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsExactly(person1, person2, person3);
} finally {
disposable.dispose();
}
}
Aggregations