|
| 1 | +package org.mapstruct.extensions.spring.example; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | +import org.mapstruct.extensions.spring.example.customconfiguration.ConversionServiceAdapter; |
| 6 | +import org.mapstruct.extensions.spring.example.customconfiguration.ConverterScan; |
| 7 | +import org.mapstruct.extensions.spring.example.customconfiguration.MapperSpringConfig; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.ComponentScan; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.core.convert.ConversionService; |
| 13 | +import org.springframework.core.convert.TypeDescriptor; |
| 14 | +import org.springframework.core.convert.support.DefaultConversionService; |
| 15 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static org.assertj.core.api.BDDAssertions.then; |
| 21 | +import static org.mapstruct.extensions.spring.example.CarType.OTHER; |
| 22 | +import static org.mapstruct.extensions.spring.example.SeatMaterial.LEATHER; |
| 23 | +import static org.mapstruct.extensions.spring.example.WheelPosition.RIGHT_FRONT; |
| 24 | + |
| 25 | +@ExtendWith(SpringExtension.class) |
| 26 | +public class ConversionServiceAdapterIntegrationTest { |
| 27 | + private static final String TEST_MAKE = "Volvo"; |
| 28 | + private static final CarType TEST_CAR_TYPE = OTHER; |
| 29 | + private static final int TEST_NUMBER_OF_SEATS = 5; |
| 30 | + private static final SeatMaterial TEST_SEAT_MATERIAL = LEATHER; |
| 31 | + private static final int TEST_DIAMETER = 20; |
| 32 | + private static final WheelPosition TEST_WHEEL_POSITION = RIGHT_FRONT; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private ConversionService conversionService; |
| 36 | + |
| 37 | + @Configuration |
| 38 | + @ComponentScan("org.mapstruct.extensions.spring.example.customconfiguration") |
| 39 | + static class ScanConfiguration { |
| 40 | + @Bean |
| 41 | + public ConversionService conversionService() { |
| 42 | + return new DefaultConversionService(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void shouldKnowAllMappers() { |
| 48 | + then(conversionService.canConvert(Car.class, CarDto.class)).isTrue(); |
| 49 | + then(conversionService.canConvert(SeatConfiguration.class, SeatConfigurationDto.class)).isTrue(); |
| 50 | + then(conversionService.canConvert(Wheel.class, WheelDto.class)).isTrue(); |
| 51 | + then(conversionService.canConvert(Wheels.class, List.class)).isTrue(); |
| 52 | + then(conversionService.canConvert(List.class, Wheels.class)).isTrue(); |
| 53 | + then(conversionService.canConvert( |
| 54 | + TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(WheelDto.class)), |
| 55 | + TypeDescriptor.valueOf((Wheels.class)))) |
| 56 | + .isTrue(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void shouldMapAllAttributes() { |
| 61 | + // Given |
| 62 | + final Car car = new Car(); |
| 63 | + car.setMake(TEST_MAKE); |
| 64 | + car.setType(TEST_CAR_TYPE); |
| 65 | + final SeatConfiguration seatConfiguration = new SeatConfiguration(); |
| 66 | + seatConfiguration.setSeatMaterial(TEST_SEAT_MATERIAL); |
| 67 | + seatConfiguration.setNumberOfSeats(TEST_NUMBER_OF_SEATS); |
| 68 | + car.setSeatConfiguration(seatConfiguration); |
| 69 | + final Wheels wheels = new Wheels(); |
| 70 | + final ArrayList<Wheel> wheelsList = new ArrayList<>(); |
| 71 | + final Wheel wheel = new Wheel(); |
| 72 | + wheel.setDiameter(TEST_DIAMETER); |
| 73 | + wheel.setPosition(TEST_WHEEL_POSITION); |
| 74 | + wheelsList.add(wheel); |
| 75 | + wheels.setWheelsList(wheelsList); |
| 76 | + car.setWheels(wheels); |
| 77 | + |
| 78 | + // When |
| 79 | + final CarDto mappedCar = conversionService.convert(car, CarDto.class); |
| 80 | + |
| 81 | + // Then |
| 82 | + then(mappedCar).isNotNull(); |
| 83 | + then(mappedCar.getMake()).isEqualTo(TEST_MAKE); |
| 84 | + then(mappedCar.getType()).isEqualTo(String.valueOf(TEST_CAR_TYPE)); |
| 85 | + final SeatConfigurationDto mappedCarSeats = mappedCar.getSeats(); |
| 86 | + then(mappedCarSeats).isNotNull(); |
| 87 | + then(mappedCarSeats.getSeatCount()).isEqualTo(TEST_NUMBER_OF_SEATS); |
| 88 | + then(mappedCarSeats.getMaterial()).isEqualTo(String.valueOf(TEST_SEAT_MATERIAL)); |
| 89 | + final WheelDto expectedWheelDto = new WheelDto(); |
| 90 | + expectedWheelDto.setPosition(String.valueOf(TEST_WHEEL_POSITION)); |
| 91 | + expectedWheelDto.setDiameter(TEST_DIAMETER); |
| 92 | + then(mappedCar.getWheels()).hasSize(1).containsExactly(expectedWheelDto); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void shouldMapGenericSourceType() { |
| 97 | + // Given |
| 98 | + final WheelDto dto = new WheelDto(); |
| 99 | + dto.setPosition(String.valueOf(TEST_WHEEL_POSITION)); |
| 100 | + dto.setDiameter(TEST_DIAMETER); |
| 101 | + final List<WheelDto> dtoList = new ArrayList<>(); |
| 102 | + dtoList.add(dto); |
| 103 | + |
| 104 | + // When |
| 105 | + final Wheels convertedWheels = conversionService.convert(dtoList, Wheels.class); |
| 106 | + |
| 107 | + // Then |
| 108 | + final Wheel expectedWheel = new Wheel(); |
| 109 | + expectedWheel.setPosition(TEST_WHEEL_POSITION); |
| 110 | + expectedWheel.setDiameter(TEST_DIAMETER); |
| 111 | + then(convertedWheels).isNotNull().hasSize(1).containsExactly(expectedWheel); |
| 112 | + } |
| 113 | +} |
0 commit comments