@@ -46,6 +46,9 @@ class WriteModelStrategyTest {
4646 private static final ReplaceOneBusinessKeyStrategy REPLACE_ONE_BUSINESS_KEY_STRATEGY = new ReplaceOneBusinessKeyStrategy ();
4747 private static final UpdateOneTimestampsStrategy UPDATE_ONE_TIMESTAMPS_STRATEGY = new UpdateOneTimestampsStrategy ();
4848
49+ private static final UpdateOneBusinessKeyTimestampStrategy
50+ UPDATE_ONE_BUSINESS_KEY_TIMESTAMPS_STRATEGY = new UpdateOneBusinessKeyTimestampStrategy ();
51+
4952 private static final BsonDocument FILTER_DOC_DELETE_DEFAULT = BsonDocument .parse ("{_id: {id: 1234}}" );
5053 private static final BsonDocument FILTER_DOC_REPLACE_DEFAULT = BsonDocument .parse ("{_id: 1234}" );
5154 private static final BsonDocument REPLACEMENT_DOC_DEFAULT = BsonDocument .parse ("{_id: 1234, first_name: 'Grace', last_name: 'Hopper'}" );
@@ -189,4 +192,38 @@ void testUpdateOneTimestampsStrategyWithValidSinkDocument() {
189192 assertEquals (FILTER_DOC_UPDATE_TIMESTAMPS , writeModel .getFilter ());
190193 assertTrue (writeModel .getOptions ().isUpsert (), "update expected to be done in upsert mode" );
191194 }
195+
196+ @ Test
197+ @ DisplayName ("when value document is missing for UpdateOneBusinessKeyTimestampStrategy then DataException" )
198+ void testUpdateOneBusinessKeyTimestampsStrategyWithMissingValueDocument () {
199+ assertThrows (DataException .class ,
200+ () -> UPDATE_ONE_BUSINESS_KEY_TIMESTAMPS_STRATEGY .createWriteModel (new SinkDocument (new BsonDocument (), null )));
201+ }
202+
203+ @ Test
204+ @ DisplayName ("when sink document is valid for UpdateOneBusinessKeyTimestampStrategy then correct UpdateOneModel" )
205+ void testUpdateOneBusinessKeyTimestampsStrategyWithValidSinkDocument () {
206+ BsonDocument valueDoc = BsonDocument .parse ("{_id: 1234, first_name: 'Grace', last_name: 'Hopper', active: false}" );
207+
208+ WriteModel <BsonDocument > result = UPDATE_ONE_TIMESTAMPS_STRATEGY .createWriteModel (new SinkDocument (null , valueDoc ));
209+ assertTrue (result instanceof UpdateOneModel , "result expected to be of type UpdateOneModel" );
210+
211+ UpdateOneModel <BsonDocument > writeModel = (UpdateOneModel <BsonDocument >) result ;
212+
213+ //NOTE: This test case can only check:
214+ // i) for both fields to be available
215+ // ii) having the correct BSON type (BsonDateTime)
216+ // iii) and be initially equal
217+ // The exact dateTime value is not directly testable here.
218+ BsonDocument updateDoc = (BsonDocument ) writeModel .getUpdate ();
219+
220+ BsonDateTime modifiedTS = updateDoc .getDocument ("$set" ).getDateTime (UpdateOneBusinessKeyTimestampStrategy .FIELD_NAME_MODIFIED_TS );
221+ BsonDateTime insertedTS = updateDoc .getDocument ("$setOnInsert" )
222+ .getDateTime (UpdateOneBusinessKeyTimestampStrategy .FIELD_NAME_INSERTED_TS );
223+
224+ assertEquals (insertedTS , modifiedTS , "modified and inserted timestamps must initially be equal" );
225+ assertTrue (writeModel .getFilter () instanceof BsonDocument , "filter expected to be of type BsonDocument" );
226+ assertEquals (FILTER_DOC_UPDATE_TIMESTAMPS , writeModel .getFilter ());
227+ assertTrue (writeModel .getOptions ().isUpsert (), "update expected to be done in upsert mode" );
228+ }
192229}
0 commit comments