|
| 1 | +/* |
| 2 | + * Copyright 2014-2021 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 example.springdata.mongodb.converters; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import example.springdata.mongodb.util.MongoContainers; |
| 21 | + |
| 22 | +import org.bson.Document; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; |
| 28 | +import org.springframework.data.geo.Point; |
| 29 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 30 | +import org.springframework.data.mongodb.core.query.Query; |
| 31 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 32 | +import org.springframework.test.context.DynamicPropertySource; |
| 33 | + |
| 34 | +import org.testcontainers.containers.MongoDBContainer; |
| 35 | +import org.testcontainers.junit.jupiter.Container; |
| 36 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 37 | + |
| 38 | +/** |
| 39 | + * Integration tests for {@link Customer} conversions. |
| 40 | + * |
| 41 | + * @author Mark Paluch |
| 42 | + */ |
| 43 | +@Testcontainers |
| 44 | +@DataMongoTest |
| 45 | +class CustomerIntegrationTests { |
| 46 | + |
| 47 | + @Container // |
| 48 | + private static MongoDBContainer mongoDBContainer = MongoContainers.getDefaultContainer(); |
| 49 | + |
| 50 | + @DynamicPropertySource |
| 51 | + static void setProperties(DynamicPropertyRegistry registry) { |
| 52 | + registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); |
| 53 | + } |
| 54 | + |
| 55 | + @Autowired MongoOperations operations; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setUp() { |
| 59 | + operations.remove(Customer.class).all(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Applies {@link org.springframework.data.mongodb.core.convert.MongoValueConverter} to individual properties. |
| 64 | + * {@code primary} and {@code secondary} are configured with different converters of which the first one uses JSON |
| 65 | + * conversion and the second converter interacts directly with a MongoDB {@link Document}. |
| 66 | + */ |
| 67 | + @Test |
| 68 | + void appliesConverters() { |
| 69 | + |
| 70 | + var primaryAddress = new Address(new Point(4.1, 5.6)); |
| 71 | + primaryAddress.setStreet("308 Negra Arroyo Lane"); |
| 72 | + primaryAddress.setZipCode("87104"); |
| 73 | + |
| 74 | + var secondaryAddress = new Address(new Point(6.1, 7.6)); |
| 75 | + secondaryAddress.setStreet("3828 Piermont Drive"); |
| 76 | + secondaryAddress.setZipCode("87111"); |
| 77 | + |
| 78 | + var customer = new Customer("Walter", "White"); |
| 79 | + customer.setPrimary(primaryAddress); |
| 80 | + customer.setSecondary(secondaryAddress); |
| 81 | + |
| 82 | + operations.insert(customer); |
| 83 | + |
| 84 | + Document document = operations.findOne(new Query(), Document.class, "customer"); |
| 85 | + |
| 86 | + assertThat(document.get("primary")) |
| 87 | + .isEqualTo("{\"location\":{\"x\":4.1,\"y\":5.6},\"street\":\"308 Negra Arroyo Lane\",\"zipCode\":\"87104\"}"); |
| 88 | + assertThat(document.get("secondary")).isEqualTo( |
| 89 | + new Document("x", secondaryAddress.getLocation().getX()).append("y", secondaryAddress.getLocation().getY())); |
| 90 | + |
| 91 | + Customer loadedCustomer = operations.findOne(new Query(), Customer.class, "customer"); |
| 92 | + |
| 93 | + // Primary address marshalled as JSON |
| 94 | + assertThat(loadedCustomer.getPrimary()).isNotNull(); |
| 95 | + assertThat(loadedCustomer.getPrimary().getStreet()).isEqualTo(primaryAddress.getStreet()); |
| 96 | + assertThat(loadedCustomer.getPrimary().getZipCode()).isEqualTo(primaryAddress.getZipCode()); |
| 97 | + assertThat(loadedCustomer.getPrimary().getLocation()).isEqualTo(primaryAddress.getLocation()); |
| 98 | + |
| 99 | + // Secondary address stores only location as the converter considers only points |
| 100 | + assertThat(loadedCustomer.getSecondary()).isNotNull(); |
| 101 | + assertThat(loadedCustomer.getSecondary().getStreet()).isNull(); |
| 102 | + assertThat(loadedCustomer.getSecondary().getLocation()).isEqualTo(secondaryAddress.getLocation()); |
| 103 | + |
| 104 | + } |
| 105 | +} |
0 commit comments