Browse Source
This change makes sure basic update appends values to operations instead of overriding them. This change aligns the behaviour with Update and fixes issues where using the Update annotation with versioned entities can lead to loss of update information. Closes: #4918 Original pull request: #49214.4.x
4 changed files with 309 additions and 12 deletions
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
/* |
||||
* Copyright 2025 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.core.query; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.springframework.data.mongodb.test.util.Assertions.assertThat; |
||||
|
||||
import java.util.function.Function; |
||||
import java.util.stream.Stream; |
||||
|
||||
import org.bson.Document; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.params.ParameterizedTest; |
||||
import org.junit.jupiter.params.provider.Arguments; |
||||
import org.junit.jupiter.params.provider.CsvSource; |
||||
import org.junit.jupiter.params.provider.MethodSource; |
||||
import org.springframework.data.mongodb.core.query.Update.Position; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
*/ |
||||
public class BasicUpdateUnitTests { |
||||
|
||||
@Test // GH-4918
|
||||
void setOperationValueShouldAppendsOpsCorrectly() { |
||||
|
||||
BasicUpdate basicUpdate = new BasicUpdate("{}"); |
||||
basicUpdate.setOperationValue("$set", "key1", "alt"); |
||||
basicUpdate.setOperationValue("$set", "key2", "nps"); |
||||
basicUpdate.setOperationValue("$unset", "key3", "x"); |
||||
|
||||
assertThat(basicUpdate.getUpdateObject()) |
||||
.isEqualTo("{ '$set' : { 'key1' : 'alt', 'key2' : 'nps' }, '$unset' : { 'key3' : 'x' } }"); |
||||
} |
||||
|
||||
@Test // GH-4918
|
||||
void setOperationErrorsOnNonMapType() { |
||||
|
||||
BasicUpdate basicUpdate = new BasicUpdate("{ '$set' : 1 }"); |
||||
assertThatExceptionOfType(IllegalStateException.class) |
||||
.isThrownBy(() -> basicUpdate.setOperationValue("$set", "k", "v")); |
||||
} |
||||
|
||||
@ParameterizedTest // GH-4918
|
||||
@CsvSource({ //
|
||||
"{ }, k1, false", //
|
||||
"{ '$set' : { 'k1' : 'v1' } }, k1, true", //
|
||||
"{ '$set' : { 'k1' : 'v1' } }, k2, false", //
|
||||
"{ '$set' : { 'k1.k2' : 'v1' } }, k1, false", //
|
||||
"{ '$set' : { 'k1.k2' : 'v1' } }, k1.k2, true", //
|
||||
"{ '$set' : { 'k1' : 'v1' } }, '', false", //
|
||||
"{ '$inc' : { 'k1' : 1 } }, k1, true" }) |
||||
void modifiesLooksUpKeyCorrectly(String source, String key, boolean modified) { |
||||
|
||||
BasicUpdate basicUpdate = new BasicUpdate(source); |
||||
assertThat(basicUpdate.modifies(key)).isEqualTo(modified); |
||||
} |
||||
|
||||
@ParameterizedTest // GH-4918
|
||||
@MethodSource("updateOpArgs") |
||||
void updateOpsShouldNotOverrideExistingValues(String operator, Function<BasicUpdate, Update> updateFunction) { |
||||
|
||||
Document source = Document.parse("{ '%s' : { 'key-1' : 'value-1' } }".formatted(operator)); |
||||
Update update = updateFunction.apply(new BasicUpdate(source)); |
||||
|
||||
assertThat(update.getUpdateObject()).containsEntry("%s.key-1".formatted(operator), "value-1") |
||||
.containsKey("%s.key-2".formatted(operator)); |
||||
} |
||||
|
||||
static Stream<Arguments> updateOpArgs() { |
||||
|
||||
return Stream.of( //
|
||||
Arguments.of("$set", (Function<BasicUpdate, Update>) update -> update.set("key-2", "value-2")), |
||||
Arguments.of("$unset", (Function<BasicUpdate, Update>) update -> update.unset("key-2")), |
||||
Arguments.of("$inc", (Function<BasicUpdate, Update>) update -> update.inc("key-2", 1)), |
||||
Arguments.of("$push", (Function<BasicUpdate, Update>) update -> update.push("key-2", "value-2")), |
||||
Arguments.of("$addToSet", (Function<BasicUpdate, Update>) update -> update.addToSet("key-2", "value-2")), |
||||
Arguments.of("$pop", (Function<BasicUpdate, Update>) update -> update.pop("key-2", Position.FIRST)), |
||||
Arguments.of("$pull", (Function<BasicUpdate, Update>) update -> update.pull("key-2", "value-2")), |
||||
Arguments.of("$pullAll", |
||||
(Function<BasicUpdate, Update>) update -> update.pullAll("key-2", new String[] { "value-2" })), |
||||
Arguments.of("$rename", (Function<BasicUpdate, Update>) update -> update.rename("key-2", "value-2"))); |
||||
}; |
||||
} |
||||
@ -0,0 +1,160 @@
@@ -0,0 +1,160 @@
|
||||
/* |
||||
* Copyright 2025 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.repository; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import org.bson.Document; |
||||
import org.bson.types.ObjectId; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.ComponentScan.Filter; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.FilterType; |
||||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; |
||||
import org.springframework.data.mongodb.core.MongoTemplate; |
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
||||
import org.springframework.data.mongodb.test.util.Client; |
||||
import org.springframework.data.mongodb.test.util.MongoClientExtension; |
||||
import org.springframework.data.mongodb.test.util.MongoTestUtils; |
||||
import org.springframework.data.repository.CrudRepository; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import com.mongodb.client.MongoClient; |
||||
|
||||
/** |
||||
* @author Christoph Strobl |
||||
* @since 2025/03 |
||||
*/ |
||||
@ExtendWith({ MongoClientExtension.class, SpringExtension.class }) |
||||
@ContextConfiguration |
||||
public class VersionedPersonRepositoryIntegrationTests { |
||||
|
||||
static @Client MongoClient mongoClient; |
||||
|
||||
@Autowired VersionedPersonRepository versionedPersonRepository; |
||||
@Autowired MongoTemplate template; |
||||
|
||||
@Configuration |
||||
@EnableMongoRepositories(considerNestedRepositories = true, |
||||
includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = VersionedPersonRepository.class)) |
||||
static class Config extends AbstractMongoClientConfiguration { |
||||
|
||||
@Override |
||||
protected String getDatabaseName() { |
||||
return "versioned-person-tests"; |
||||
} |
||||
|
||||
@Override |
||||
public MongoClient mongoClient() { |
||||
return mongoClient; |
||||
} |
||||
} |
||||
|
||||
@BeforeEach |
||||
void beforeEach() { |
||||
MongoTestUtils.flushCollection("versioned-person-tests", template.getCollectionName(VersionedPersonWithCounter.class), |
||||
mongoClient); |
||||
} |
||||
|
||||
@Test // GH-4918
|
||||
void updatesVersionedTypeCorrectly() { |
||||
|
||||
VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
||||
|
||||
int updateCount = versionedPersonRepository.findAndSetFirstnameToLastnameByLastname(person.getLastname()); |
||||
|
||||
assertThat(updateCount).isOne(); |
||||
|
||||
Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
||||
return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
||||
}); |
||||
|
||||
assertThat(document).containsEntry("firstname", "Duckling").containsEntry("version", 1L); |
||||
} |
||||
|
||||
@Test // GH-4918
|
||||
void updatesVersionedTypeCorrectlyWhenUpdateIsUsingInc() { |
||||
|
||||
VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
||||
|
||||
int updateCount = versionedPersonRepository.findAndIncCounterByLastname(person.getLastname()); |
||||
|
||||
assertThat(updateCount).isOne(); |
||||
|
||||
Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
||||
return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
||||
}); |
||||
|
||||
assertThat(document).containsEntry("lastname", "Duckling").containsEntry("version", 1L).containsEntry("counter", 42); |
||||
} |
||||
|
||||
@Test // GH-4918
|
||||
void updatesVersionedTypeCorrectlyWhenUpdateCoversVersionBump() { |
||||
|
||||
VersionedPerson person = template.insert(VersionedPersonWithCounter.class).one(new VersionedPersonWithCounter("Donald", "Duckling")); |
||||
|
||||
int updateCount = versionedPersonRepository.findAndSetFirstnameToLastnameIncVersionByLastname(person.getLastname(), |
||||
10); |
||||
|
||||
assertThat(updateCount).isOne(); |
||||
|
||||
Document document = template.execute(VersionedPersonWithCounter.class, collection -> { |
||||
return collection.find(new Document("_id", new ObjectId(person.getId()))).first(); |
||||
}); |
||||
|
||||
assertThat(document).containsEntry("firstname", "Duckling").containsEntry("version", 10L); |
||||
} |
||||
|
||||
public interface VersionedPersonRepository extends CrudRepository<VersionedPersonWithCounter, String> { |
||||
|
||||
@Update("{ '$set': { 'firstname' : ?0 } }") |
||||
int findAndSetFirstnameToLastnameByLastname(String lastname); |
||||
|
||||
@Update("{ '$inc': { 'counter' : 42 } }") |
||||
int findAndIncCounterByLastname(String lastname); |
||||
|
||||
@Update(""" |
||||
{ |
||||
'$set': { 'firstname' : ?0 }, |
||||
'$inc': { 'version' : ?1 } |
||||
}""") |
||||
int findAndSetFirstnameToLastnameIncVersionByLastname(String lastname, int incVersion); |
||||
|
||||
} |
||||
|
||||
@org.springframework.data.mongodb.core.mapping.Document("versioned-person") |
||||
static class VersionedPersonWithCounter extends VersionedPerson { |
||||
|
||||
int counter; |
||||
|
||||
public VersionedPersonWithCounter(String firstname, @Nullable String lastname) { |
||||
super(firstname, lastname); |
||||
} |
||||
|
||||
public int getCounter() { |
||||
return counter; |
||||
} |
||||
|
||||
public void setCounter(int counter) { |
||||
this.counter = counter; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue