|
| 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 | + * http://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.core.query; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 19 | +import static org.springframework.data.mongodb.test.util.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.bson.Document; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.Arguments; |
| 28 | +import org.junit.jupiter.params.provider.CsvSource; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | +import org.springframework.data.mongodb.core.query.Update.Position; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Christoph Strobl |
| 34 | + */ |
| 35 | +public class BasicUpdateUnitTests { |
| 36 | + |
| 37 | + @Test // GH-4918 |
| 38 | + void setOperationValueShouldAppendsOpsCorrectly() { |
| 39 | + |
| 40 | + BasicUpdate basicUpdate = new BasicUpdate("{}"); |
| 41 | + basicUpdate.setOperationValue("$set", "key1", "alt"); |
| 42 | + basicUpdate.setOperationValue("$set", "key2", "nps"); |
| 43 | + basicUpdate.setOperationValue("$unset", "key3", "x"); |
| 44 | + |
| 45 | + assertThat(basicUpdate.getUpdateObject()) |
| 46 | + .isEqualTo("{ '$set' : { 'key1' : 'alt', 'key2' : 'nps' }, '$unset' : { 'key3' : 'x' } }"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test // GH-4918 |
| 50 | + void setOperationErrorsOnNonMapType() { |
| 51 | + |
| 52 | + BasicUpdate basicUpdate = new BasicUpdate("{ '$set' : 1 }"); |
| 53 | + assertThatExceptionOfType(IllegalStateException.class) |
| 54 | + .isThrownBy(() -> basicUpdate.setOperationValue("$set", "k", "v")); |
| 55 | + } |
| 56 | + |
| 57 | + @ParameterizedTest // GH-4918 |
| 58 | + @CsvSource({ // |
| 59 | + "{ }, k1, false", // |
| 60 | + "{ '$set' : { 'k1' : 'v1' } }, k1, true", // |
| 61 | + "{ '$set' : { 'k1' : 'v1' } }, k2, false", // |
| 62 | + "{ '$set' : { 'k1.k2' : 'v1' } }, k1, false", // |
| 63 | + "{ '$set' : { 'k1.k2' : 'v1' } }, k1.k2, true", // |
| 64 | + "{ '$set' : { 'k1' : 'v1' } }, '', false", // |
| 65 | + "{ '$inc' : { 'k1' : 1 } }, k1, true" }) |
| 66 | + void modifiesLooksUpKeyCorrectly(String source, String key, boolean modified) { |
| 67 | + |
| 68 | + BasicUpdate basicUpdate = new BasicUpdate(source); |
| 69 | + assertThat(basicUpdate.modifies(key)).isEqualTo(modified); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest // GH-4918 |
| 73 | + @MethodSource("updateOpArgs") |
| 74 | + void updateOpsShouldNotOverrideExistingValues(String operator, Function<BasicUpdate, Update> updateFunction) { |
| 75 | + |
| 76 | + Document source = Document.parse("{ '%s' : { 'key-1' : 'value-1' } }".formatted(operator)); |
| 77 | + Update update = updateFunction.apply(new BasicUpdate(source)); |
| 78 | + |
| 79 | + assertThat(update.getUpdateObject()).containsEntry("%s.key-1".formatted(operator), "value-1") |
| 80 | + .containsKey("%s.key-2".formatted(operator)); |
| 81 | + } |
| 82 | + |
| 83 | + static Stream<Arguments> updateOpArgs() { |
| 84 | + |
| 85 | + return Stream.of( // |
| 86 | + Arguments.of("$set", (Function<BasicUpdate, Update>) update -> update.set("key-2", "value-2")), |
| 87 | + Arguments.of("$unset", (Function<BasicUpdate, Update>) update -> update.unset("key-2")), |
| 88 | + Arguments.of("$inc", (Function<BasicUpdate, Update>) update -> update.inc("key-2", 1)), |
| 89 | + Arguments.of("$push", (Function<BasicUpdate, Update>) update -> update.push("key-2", "value-2")), |
| 90 | + Arguments.of("$addToSet", (Function<BasicUpdate, Update>) update -> update.addToSet("key-2", "value-2")), |
| 91 | + Arguments.of("$pop", (Function<BasicUpdate, Update>) update -> update.pop("key-2", Position.FIRST)), |
| 92 | + Arguments.of("$pull", (Function<BasicUpdate, Update>) update -> update.pull("key-2", "value-2")), |
| 93 | + Arguments.of("$pullAll", |
| 94 | + (Function<BasicUpdate, Update>) update -> update.pullAll("key-2", new String[] { "value-2" })), |
| 95 | + Arguments.of("$rename", (Function<BasicUpdate, Update>) update -> update.rename("key-2", "value-2"))); |
| 96 | + }; |
| 97 | +} |
0 commit comments