|
| 1 | +/** |
| 2 | + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) |
| 3 | + * and/or other contributors as indicated by the @authors tag. See the |
| 4 | + * copyright.txt file in the distribution for a full listing of all |
| 5 | + * contributors. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.mapstruct.example; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.tuple; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import org.junit.Test; |
| 30 | +import org.mapstruct.example.dto.CustomerDto; |
| 31 | +import org.mapstruct.example.dto.OrderItemDto; |
| 32 | +import org.mapstruct.example.dto.OrderItemKeyDto; |
| 33 | +import org.mapstruct.example.mapper.Cloner; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * @author Sjaak Derksen |
| 38 | + */ |
| 39 | +public class ClonerTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testMapDtoToEntity() { |
| 43 | + |
| 44 | + CustomerDto customerDto = new CustomerDto(); |
| 45 | + customerDto.setId( 10L ); |
| 46 | + customerDto.setCustomerName("Jaques" ); |
| 47 | + OrderItemDto order1 = new OrderItemDto(); |
| 48 | + order1.setName ("Table" ); |
| 49 | + order1.setQuantity( 2L ); |
| 50 | + customerDto.setOrders( new ArrayList<>( Collections.singleton( order1 ) ) ); |
| 51 | + OrderItemKeyDto key = new OrderItemKeyDto(); |
| 52 | + key.setStockNumber( 5 ); |
| 53 | + Map stock = new HashMap( ); |
| 54 | + stock.put( key, order1 ); |
| 55 | + customerDto.setStock( stock ); |
| 56 | + |
| 57 | + CustomerDto customer = Cloner.MAPPER.clone( customerDto ); |
| 58 | + |
| 59 | + // check if cloned |
| 60 | + assertThat( customer.getId() ).isEqualTo( 10 ); |
| 61 | + assertThat( customer.getCustomerName() ).isEqualTo( "Jaques" ); |
| 62 | + assertThat( customer.getOrders() ) |
| 63 | + .extracting( "name", "quantity" ) |
| 64 | + .containsExactly( tuple( "Table", 2L ) ); |
| 65 | + assertThat( customer.getStock() ).isNotNull(); |
| 66 | + assertThat( customer.getStock() ).hasSize( 1 ); |
| 67 | + |
| 68 | + Map.Entry<OrderItemKeyDto, OrderItemDto> entry = customer.getStock().entrySet().iterator().next(); |
| 69 | + assertThat( entry.getKey().getStockNumber() ).isEqualTo( 5 ); |
| 70 | + assertThat( entry.getValue().getName() ).isEqualTo( "Table" ); |
| 71 | + assertThat( entry.getValue().getQuantity() ).isEqualTo( 2L ); |
| 72 | + |
| 73 | + // check mapper really created new objects |
| 74 | + assertThat( customer ).isNotSameAs( customerDto ); |
| 75 | + assertThat( customer.getOrders().get( 0 ) ).isNotEqualTo( order1 ); |
| 76 | + assertThat( entry.getKey() ).isNotEqualTo( key ); |
| 77 | + assertThat( entry.getValue() ).isNotEqualTo( order1 ); |
| 78 | + assertThat( entry.getValue() ).isNotEqualTo( customer.getOrders().get( 0 ) ); |
| 79 | + } |
| 80 | +} |
0 commit comments