|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.repository; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import org.bson.Document; |
| 21 | +import org.bson.types.ObjectId; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.ComponentScan.Filter; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.FilterType; |
| 29 | +import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; |
| 30 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 31 | +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
| 32 | +import org.springframework.data.mongodb.test.util.Client; |
| 33 | +import org.springframework.data.mongodb.test.util.MongoClientExtension; |
| 34 | +import org.springframework.data.mongodb.test.util.MongoTestUtils; |
| 35 | +import org.springframework.data.repository.CrudRepository; |
| 36 | +import org.springframework.lang.Nullable; |
| 37 | +import org.springframework.test.context.ContextConfiguration; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | + |
| 40 | +import com.mongodb.client.MongoClient; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Christoph Strobl |
| 44 | + * @since 2025/03 |
| 45 | + */ |
| 46 | +@ExtendWith({ MongoClientExtension.class, SpringExtension.class }) |
| 47 | +@ContextConfiguration |
| 48 | +public class VersionedPersonRepositoryIntegrationTests { |
| 49 | + |
| 50 | + static @Client MongoClient mongoClient; |
| 51 | + |
| 52 | + @Autowired VersionedPersonRepository versionedPersonRepository; |
| 53 | + @Autowired MongoTemplate template; |
| 54 | + |
| 55 | + @Configuration |
| 56 | + @EnableMongoRepositories(considerNestedRepositories = true, |
| 57 | + includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = VersionedPersonRepository.class)) |
| 58 | + static class Config extends AbstractMongoClientConfiguration { |
| 59 | + |
| 60 | + @Override |
| 61 | + protected String getDatabaseName() { |
| 62 | + return "versioned-person-tests"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public MongoClient mongoClient() { |
| 67 | + return mongoClient; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @BeforeEach |
| 72 | + void beforeEach() { |
| 73 | + MongoTestUtils.flushCollection("versioned-person-tests", template.getCollectionName(VersionedPersonWithCounter.class), |
| 74 | + mongoClient); |
| 75 | + } |
| 76 | + |
| 77 | + @Test // GH-4918 |
| 78 | + void updatesVersionedTypeCorrectly() { |
| 79 | + |
| 80 | + VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
| 81 | + |
| 82 | + int updateCount = versionedPersonRepository.findAndSetFirstnameToLastnameByLastname(person.getLastname()); |
| 83 | + |
| 84 | + assertThat(updateCount).isOne(); |
| 85 | + |
| 86 | + Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
| 87 | + return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
| 88 | + }); |
| 89 | + |
| 90 | + assertThat(document).containsEntry("firstname", "Duckling").containsEntry("version", 1L); |
| 91 | + } |
| 92 | + |
| 93 | + @Test // GH-4918 |
| 94 | + void updatesVersionedTypeCorrectlyWhenUpdateIsUsingInc() { |
| 95 | + |
| 96 | + VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
| 97 | + |
| 98 | + int updateCount = versionedPersonRepository.findAndIncCounterByLastname(person.getLastname()); |
| 99 | + |
| 100 | + assertThat(updateCount).isOne(); |
| 101 | + |
| 102 | + Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
| 103 | + return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
| 104 | + }); |
| 105 | + |
| 106 | + assertThat(document).containsEntry("lastname", "Duckling").containsEntry("version", 1L).containsEntry("counter", 42); |
| 107 | + } |
| 108 | + |
| 109 | + @Test // GH-4918 |
| 110 | + void updatesVersionedTypeCorrectlyWhenUpdateCoversVersionBump() { |
| 111 | + |
| 112 | + VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
| 113 | + |
| 114 | + int updateCount = versionedPersonRepository.findAndSetFirstnameToLastnameIncVersionByLastname(person.getLastname(), |
| 115 | + 10); |
| 116 | + |
| 117 | + assertThat(updateCount).isOne(); |
| 118 | + |
| 119 | + Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
| 120 | + return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
| 121 | + }); |
| 122 | + |
| 123 | + assertThat(document).containsEntry("firstname", "Duckling").containsEntry("version", 10L); |
| 124 | + } |
| 125 | + |
| 126 | + public interface VersionedPersonRepository extends CrudRepository<VersionedPersonWithCounter, String> { |
| 127 | + |
| 128 | + @Update("{ '$set': { 'firstname' : ?0 } }") |
| 129 | + int findAndSetFirstnameToLastnameByLastname(String lastname); |
| 130 | + |
| 131 | + @Update("{ '$inc': { 'counter' : 42 } }") |
| 132 | + int findAndIncCounterByLastname(String lastname); |
| 133 | + |
| 134 | + @Update(""" |
| 135 | + { |
| 136 | + '$set': { 'firstname' : ?0 }, |
| 137 | + '$inc': { 'version' : ?1 } |
| 138 | + }""") |
| 139 | + int findAndSetFirstnameToLastnameIncVersionByLastname(String lastname, int incVersion); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + @org.springframework.data.mongodb.core.mapping.Document("versioned-person") |
| 144 | + static class VersionedPersonWithCounter extends VersionedPerson { |
| 145 | + |
| 146 | + int counter; |
| 147 | + |
| 148 | + public VersionedPersonWithCounter(String firstname, @Nullable String lastname) { |
| 149 | + super(firstname, lastname); |
| 150 | + } |
| 151 | + |
| 152 | + public int getCounter() { |
| 153 | + return counter; |
| 154 | + } |
| 155 | + |
| 156 | + public void setCounter(int counter) { |
| 157 | + this.counter = counter; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments