11/*
2- * Copyright 2025 the original author or authors.
2+ * Copyright 2025-present the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package example .springdata .mongodb .bulk ;
1717
18- import static org .assertj .core .api .Assertions .assertThat ;
19- import static org .springframework .data .mongodb .core .query .Criteria .where ;
18+ import static org .assertj .core .api .Assertions .* ;
19+ import static org .springframework .data .mongodb .core .query .Criteria .* ;
2020
2121import example .springdata .mongodb .util .MongoContainers ;
22+
2223import org .junit .jupiter .api .BeforeEach ;
2324import org .junit .jupiter .api .Test ;
25+
2426import org .springframework .beans .factory .annotation .Autowired ;
2527import org .springframework .boot .data .mongodb .test .autoconfigure .DataMongoTest ;
2628import org .springframework .data .mongodb .core .MongoOperations ;
3032import org .springframework .data .mongodb .core .query .Update ;
3133import org .springframework .test .context .DynamicPropertyRegistry ;
3234import org .springframework .test .context .DynamicPropertySource ;
35+
3336import org .testcontainers .junit .jupiter .Container ;
3437import org .testcontainers .junit .jupiter .Testcontainers ;
3538import org .testcontainers .mongodb .MongoDBContainer ;
3639
3740/**
38- * Integration test demonstrating {@link MongoOperations#bulkWrite(Bulk, BulkWriteOptions)} with a combination of
39- * insert and update operations in a single request.
41+ * Integration test demonstrating {@link MongoOperations#bulkWrite(Bulk, BulkWriteOptions)} with a combination of insert
42+ * and update operations in a single request.
4043 *
4144 * @author Christoph Strobl
4245 */
4346@ Testcontainers
4447@ DataMongoTest
4548class BulkWriteIntegrationTest {
4649
47- @ Container
48- private static MongoDBContainer mongoDBContainer = MongoContainers .getDefaultContainer ();
50+ @ Container private static MongoDBContainer mongoDBContainer = MongoContainers .getDefaultContainer ();
4951
5052 @ DynamicPropertySource
5153 static void setProperties (DynamicPropertyRegistry registry ) {
5254 registry .add ("spring.mongodb.uri" , mongoDBContainer ::getReplicaSetUrl );
5355 }
5456
55- @ Autowired
56- MongoOperations operations ;
57+ @ Autowired MongoOperations operations ;
5758
5859 @ BeforeEach
5960 void setUp () {
@@ -63,12 +64,12 @@ void setUp() {
6364 @ Test
6465 void bulkWriteInsertAndUpdateInOrder () {
6566
66- Bulk bulk = Bulk .create (builder -> builder . inCollection ( Jedi . class , spec -> spec
67- .insert ( new Jedi ( "Luke" , "Skywalker" ))
68- .insert (new Jedi ("Leia" , "Princess" ))
69- .updateOne (where ("firstname" ).is ("Leia" ), new Update ().set ("lastname" , "Organa" )))
70- .inCollection (Sith .class , spec -> spec . upsert ( where ( "name" ). is ( "Darth Sidious" ), Update . update ( "realName" , "Palpatine" )))
71- );
67+ Bulk bulk = Bulk .create (builder -> builder
68+ .inCollection ( Jedi . class ,
69+ spec -> spec . insert ( new Jedi ( "Luke" , "Skywalker" )) .insert (new Jedi ("Leia" , "Princess" ))
70+ .updateOne (where ("firstname" ).is ("Leia" ), new Update ().set ("lastname" , "Organa" )))
71+ .inCollection (Sith .class ,
72+ spec -> spec . upsert ( where ( "name" ). is ( "Darth Sidious" ), Update . update ( "realName" , "Palpatine" ))) );
7273
7374 BulkWriteResult result = operations .bulkWrite (bulk , BulkWriteOptions .ordered ());
7475
@@ -77,12 +78,9 @@ void bulkWriteInsertAndUpdateInOrder() {
7778 assertThat (result .modifiedCount ()).isOne (); // leia
7879 assertThat (result .upsertCount ()).isOne (); // darth sidious
7980
80- assertThat (operations .findAll (Jedi .class ))
81- .extracting (Jedi ::fullName )
82- .containsExactlyInAnyOrder ("Luke Skywalker" , "Leia Organa" );
81+ assertThat (operations .findAll (Jedi .class )).extracting (Jedi ::fullName ).containsExactlyInAnyOrder ("Luke Skywalker" ,
82+ "Leia Organa" );
8383
84- assertThat (operations .findAll (Sith .class ))
85- .extracting (Sith ::name )
86- .containsExactlyInAnyOrder ("Darth Sidious" );
84+ assertThat (operations .findAll (Sith .class )).extracting (Sith ::name ).containsExactlyInAnyOrder ("Darth Sidious" );
8785 }
8886}
0 commit comments