|
| 1 | +/* |
| 2 | + * Copyright 2024 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.convert; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.data.annotation.Reference; |
| 23 | +import org.springframework.data.mapping.context.SampleMappingContext; |
| 24 | +import org.springframework.data.mapping.model.EntityInstantiators; |
| 25 | + |
| 26 | +/** |
| 27 | + * Unit tests for {@link DtoInstantiatingConverter}. |
| 28 | + * |
| 29 | + * @author Mark Paluch |
| 30 | + */ |
| 31 | +class DtoInstantiatingConverterUnitTests { |
| 32 | + |
| 33 | + @Test // GH- 3104 |
| 34 | + void dtoProjectionShouldConsiderPropertiesAndAssociations() { |
| 35 | + |
| 36 | + TheOtherThing ref = new TheOtherThing(); |
| 37 | + MyAssociativeEntity entity = new MyAssociativeEntity("1", ref, "foo"); |
| 38 | + DtoInstantiatingConverter converter = new DtoInstantiatingConverter(DtoProjection.class, new SampleMappingContext(), |
| 39 | + new EntityInstantiators()); |
| 40 | + |
| 41 | + DtoProjection projection = (DtoProjection) converter.convert(entity); |
| 42 | + |
| 43 | + assertThat(projection.id).isEqualTo("1"); |
| 44 | + assertThat(projection.ref).isSameAs(ref); |
| 45 | + } |
| 46 | + |
| 47 | + static class MyAssociativeEntity { |
| 48 | + |
| 49 | + String id; |
| 50 | + |
| 51 | + @Reference TheOtherThing ref; |
| 52 | + |
| 53 | + String somethingElse; |
| 54 | + |
| 55 | + public MyAssociativeEntity(String id, TheOtherThing ref, String somethingElse) { |
| 56 | + this.id = id; |
| 57 | + this.ref = ref; |
| 58 | + this.somethingElse = somethingElse; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static class DtoProjection { |
| 63 | + |
| 64 | + String id; |
| 65 | + |
| 66 | + @Reference TheOtherThing ref; |
| 67 | + } |
| 68 | + |
| 69 | + static class TheOtherThing { |
| 70 | + String id; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments