|
| 1 | +package liquibase.ext.mongodb.statement; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * Liquibase MongoDB Extension |
| 6 | + * %% |
| 7 | + * Copyright (C) 2021 Mastercard |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 10 | + * You may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import com.mongodb.client.FindIterable; |
| 24 | +import com.mongodb.client.MongoCursor; |
| 25 | +import liquibase.ext.AbstractMongoIntegrationTest; |
| 26 | +import org.bson.Document; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static liquibase.ext.mongodb.TestUtils.COLLECTION_NAME_1; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +class FindOneAndUpdateStatementIT extends AbstractMongoIntegrationTest { |
| 34 | + |
| 35 | + private final Document first = new Document("name", "first"); |
| 36 | + private final Document second = new Document("name", "second"); |
| 37 | + private final Document modified = new Document("name", "modified"); |
| 38 | + private final Document update = new Document("$set", modified); |
| 39 | + private final Document sort = new Document("name", -1); |
| 40 | + private final Document emptyDocument = new Document(); |
| 41 | + |
| 42 | + private String collectionName; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void createCollectionName() { |
| 46 | + collectionName = COLLECTION_NAME_1 + System.nanoTime(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testUpdateWhenNoDocumentFound() { |
| 51 | + int updated = new FindOneAndUpdateStatement(collectionName, emptyDocument, update, emptyDocument) |
| 52 | + .update(database); |
| 53 | + assertThat(updated).isEqualTo(0); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testUpdateWhenDocumentFound() { |
| 58 | + |
| 59 | + new InsertOneStatement(collectionName, first).execute(database); |
| 60 | + |
| 61 | + int updated = new FindOneAndUpdateStatement(collectionName, emptyDocument, update, emptyDocument) |
| 62 | + .update(database); |
| 63 | + assertThat(updated).isEqualTo(1); |
| 64 | + |
| 65 | + final FindIterable<Document> docs = mongoDatabase.getCollection(collectionName).find(); |
| 66 | + assertThat(docs).hasSize(1); |
| 67 | + assertThat(docs.iterator().next()) |
| 68 | + .containsEntry("name", "modified"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testUpdateWithMatchingFilter() { |
| 73 | + |
| 74 | + new InsertOneStatement(collectionName, first).execute(database); |
| 75 | + new InsertOneStatement(collectionName, second).execute(database); |
| 76 | + |
| 77 | + int updated = new FindOneAndUpdateStatement(collectionName, second, update, emptyDocument) |
| 78 | + .update(database); |
| 79 | + assertThat(updated).isEqualTo(1); |
| 80 | + |
| 81 | + final FindIterable<Document> docs = mongoDatabase.getCollection(collectionName).find(modified); |
| 82 | + assertThat(docs).hasSize(1); |
| 83 | + assertThat(docs.iterator().next()) |
| 84 | + .containsEntry("name", "modified"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testUpdateWhenDocumentFoundWithSort() { |
| 89 | + |
| 90 | + new InsertOneStatement(collectionName, first).execute(database); |
| 91 | + new InsertOneStatement(collectionName, second).execute(database); |
| 92 | + |
| 93 | + int updated = new FindOneAndUpdateStatement(collectionName, emptyDocument, update, sort) |
| 94 | + .update(database); |
| 95 | + assertThat(updated).isEqualTo(1); |
| 96 | + |
| 97 | + final FindIterable<Document> docs = mongoDatabase.getCollection(collectionName).find() |
| 98 | + .sort(new Document("name",1)); |
| 99 | + assertThat(docs).hasSize(2); |
| 100 | + MongoCursor<Document> iterator = docs.iterator(); |
| 101 | + assertThat(iterator.next()) |
| 102 | + .containsEntry("name", "first"); |
| 103 | + assertThat(iterator.next()) |
| 104 | + .containsEntry("name", "modified"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void toStringJs() { |
| 109 | + final FindOneAndUpdateStatement statement = new FindOneAndUpdateStatement(COLLECTION_NAME_1, first, modified, sort); |
| 110 | + assertThat(statement.toJs()) |
| 111 | + .isEqualTo(statement.toString()) |
| 112 | + .isEqualTo("db.runCommand({\"findAndModify\": \"collectionName\", " + |
| 113 | + "\"query\": {\"name\": \"first\"}, " + |
| 114 | + "\"update\": {\"name\": \"modified\"}, " + |
| 115 | + "\"sort\": {\"name\": -1}});"); |
| 116 | + } |
| 117 | +} |
0 commit comments