|
| 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.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.springframework.data.mongodb.core.query.Criteria.where; |
| 20 | +import static org.springframework.data.mongodb.core.query.Query.query; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.math.BigInteger; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.springframework.data.auditing.IsNewAwareAuditingHandler; |
| 30 | +import org.springframework.data.mapping.context.PersistentEntities; |
| 31 | +import org.springframework.data.mongodb.core.MongoTemplateTests.TypeWithNumbers; |
| 32 | +import org.springframework.data.mongodb.core.convert.MongoCustomConversions; |
| 33 | +import org.springframework.data.mongodb.core.convert.MongoCustomConversions.BigDecimalRepresentation; |
| 34 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 35 | +import org.springframework.data.mongodb.core.query.Query; |
| 36 | +import org.springframework.data.mongodb.core.query.Update; |
| 37 | +import org.springframework.data.mongodb.test.util.Client; |
| 38 | +import org.springframework.data.mongodb.test.util.MongoClientExtension; |
| 39 | +import org.springframework.data.mongodb.test.util.MongoTestTemplate; |
| 40 | + |
| 41 | +import com.mongodb.client.MongoClient; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests for {@link MongoTemplate} using string representation of {@link BigInteger} values. |
| 45 | + * |
| 46 | + * @author Christoph Strobl |
| 47 | + */ |
| 48 | +@ExtendWith(MongoClientExtension.class) |
| 49 | +public class BigDecimalToStringConvertingTemplateTests { |
| 50 | + |
| 51 | + public static final String DB_NAME = "mongo-template-tests"; |
| 52 | + |
| 53 | + static @Client MongoClient client; |
| 54 | + |
| 55 | + @SuppressWarnings("deprecation") MongoTestTemplate template = new MongoTestTemplate(cfg -> { |
| 56 | + |
| 57 | + cfg.configureDatabaseFactory(it -> { |
| 58 | + |
| 59 | + it.client(client); |
| 60 | + it.defaultDb(DB_NAME); |
| 61 | + }); |
| 62 | + |
| 63 | + cfg.configureConversion(it -> { |
| 64 | + it.customConversions( |
| 65 | + MongoCustomConversions.create(adapter -> adapter.bigDecimal(BigDecimalRepresentation.STRING))); |
| 66 | + }); |
| 67 | + |
| 68 | + cfg.configureMappingContext(it -> { |
| 69 | + it.autocreateIndex(false); |
| 70 | + }); |
| 71 | + |
| 72 | + cfg.configureAuditing(it -> { |
| 73 | + it.auditingHandler(ctx -> { |
| 74 | + return new IsNewAwareAuditingHandler(PersistentEntities.of(ctx)); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + @AfterEach |
| 80 | + public void cleanUp() { |
| 81 | + template.flush(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test // DATAMONGO-602 |
| 85 | + void testUsingAnInQueryWithBigIntegerId() { |
| 86 | + |
| 87 | + template.remove(new Query(), PersonWithIdPropertyOfTypeBigInteger.class); |
| 88 | + |
| 89 | + PersonWithIdPropertyOfTypeBigInteger p1 = new PersonWithIdPropertyOfTypeBigInteger(); |
| 90 | + p1.setFirstName("Sven"); |
| 91 | + p1.setAge(11); |
| 92 | + p1.setId(new BigInteger("2666666666666666665069473312490162649510603601")); |
| 93 | + template.insert(p1); |
| 94 | + PersonWithIdPropertyOfTypeBigInteger p2 = new PersonWithIdPropertyOfTypeBigInteger(); |
| 95 | + p2.setFirstName("Mary"); |
| 96 | + p2.setAge(21); |
| 97 | + p2.setId(new BigInteger("2666666666666666665069473312490162649510603602")); |
| 98 | + template.insert(p2); |
| 99 | + PersonWithIdPropertyOfTypeBigInteger p3 = new PersonWithIdPropertyOfTypeBigInteger(); |
| 100 | + p3.setFirstName("Ann"); |
| 101 | + p3.setAge(31); |
| 102 | + p3.setId(new BigInteger("2666666666666666665069473312490162649510603603")); |
| 103 | + template.insert(p3); |
| 104 | + PersonWithIdPropertyOfTypeBigInteger p4 = new PersonWithIdPropertyOfTypeBigInteger(); |
| 105 | + p4.setFirstName("John"); |
| 106 | + p4.setAge(41); |
| 107 | + p4.setId(new BigInteger("2666666666666666665069473312490162649510603604")); |
| 108 | + template.insert(p4); |
| 109 | + |
| 110 | + Query q1 = new Query(Criteria.where("age").in(11, 21, 41)); |
| 111 | + List<PersonWithIdPropertyOfTypeBigInteger> results1 = template.find(q1, PersonWithIdPropertyOfTypeBigInteger.class); |
| 112 | + Query q2 = new Query(Criteria.where("firstName").in("Ann", "Mary")); |
| 113 | + List<PersonWithIdPropertyOfTypeBigInteger> results2 = template.find(q2, PersonWithIdPropertyOfTypeBigInteger.class); |
| 114 | + Query q3 = new Query(Criteria.where("id").in(new BigInteger("2666666666666666665069473312490162649510603601"), |
| 115 | + new BigInteger("2666666666666666665069473312490162649510603604"))); |
| 116 | + List<PersonWithIdPropertyOfTypeBigInteger> results3 = template.find(q3, PersonWithIdPropertyOfTypeBigInteger.class); |
| 117 | + assertThat(results1.size()).isEqualTo(3); |
| 118 | + assertThat(results2.size()).isEqualTo(2); |
| 119 | + assertThat(results3.size()).isEqualTo(2); |
| 120 | + } |
| 121 | + |
| 122 | + @Test // DATAMONGO-1404 |
| 123 | + void updatesBigNumberValueUsingStringComparisonWhenUsingMaxOperator() { |
| 124 | + |
| 125 | + TypeWithNumbers twn = new TypeWithNumbers(); |
| 126 | + |
| 127 | + // Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules. |
| 128 | + // Therefore "80" is considered greater than "700" |
| 129 | + twn.bigIntegerVal = new BigInteger("600"); |
| 130 | + twn.bigDeciamVal = new BigDecimal("700.0"); |
| 131 | + |
| 132 | + template.save(twn); |
| 133 | + |
| 134 | + Update update = new Update()// |
| 135 | + .max("bigIntegerVal", new BigInteger("70")) // |
| 136 | + .max("bigDeciamVal", new BigDecimal("80")) // |
| 137 | + ; |
| 138 | + |
| 139 | + template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class); |
| 140 | + |
| 141 | + TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0); |
| 142 | + assertThat(loaded.bigIntegerVal).isEqualTo(new BigInteger("70")); |
| 143 | + assertThat(loaded.bigDeciamVal).isEqualTo(new BigDecimal("80")); |
| 144 | + } |
| 145 | + |
| 146 | + @Test // DATAMONGO-1404 |
| 147 | + void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() { |
| 148 | + |
| 149 | + TypeWithNumbers twn = new TypeWithNumbers(); |
| 150 | + |
| 151 | + // Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules. |
| 152 | + // Therefore "80" is considered greater than "700" |
| 153 | + twn.bigIntegerVal = new BigInteger("80"); |
| 154 | + twn.bigDeciamVal = new BigDecimal("90.0"); |
| 155 | + |
| 156 | + template.save(twn); |
| 157 | + |
| 158 | + Update update = new Update()// |
| 159 | + .min("bigIntegerVal", new BigInteger("700")) // |
| 160 | + .min("bigDeciamVal", new BigDecimal("800")) // |
| 161 | + ; |
| 162 | + |
| 163 | + template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class); |
| 164 | + |
| 165 | + TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0); |
| 166 | + assertThat(loaded.bigIntegerVal).isEqualTo(new BigInteger("700")); |
| 167 | + assertThat(loaded.bigDeciamVal).isEqualTo(new BigDecimal("800")); |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments