|
| 1 | +package org.mapstruct.extensions.spring.example; |
| 2 | + |
| 3 | +import static org.assertj.core.api.BDDAssertions.then; |
| 4 | +import static org.mapstruct.extensions.spring.example.CarType.OTHER; |
| 5 | +import static org.mapstruct.extensions.spring.example.SeatMaterial.LEATHER; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.context.annotation.ComponentScan; |
| 14 | +import org.springframework.core.convert.support.ConfigurableConversionService; |
| 15 | +import org.springframework.core.convert.support.DefaultConversionService; |
| 16 | +import org.springframework.stereotype.Component; |
| 17 | +import org.springframework.test.context.ContextConfiguration; |
| 18 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 19 | +import org.mapstruct.extensions.spring.example.custombean.CarMapper; |
| 20 | +import org.mapstruct.extensions.spring.example.custombean.SeatConfigurationMapper; |
| 21 | + |
| 22 | +@ExtendWith(SpringExtension.class) |
| 23 | +@ContextConfiguration( |
| 24 | + classes = {ConversionServiceAdapterIntegrationTest.AdditionalBeanConfiguration.class}) |
| 25 | +public class ConversionServiceAdapterIntegrationTest { |
| 26 | + private static final String TEST_MAKE = "Volvo"; |
| 27 | + private static final CarType TEST_CAR_TYPE = OTHER; |
| 28 | + protected static final int TEST_NUMBER_OF_SEATS = 5; |
| 29 | + protected static final SeatMaterial TEST_SEAT_MATERIAL = LEATHER; |
| 30 | + |
| 31 | + @Autowired private CarMapper carMapper; |
| 32 | + @Autowired private SeatConfigurationMapper seatConfigurationMapper; |
| 33 | +@Autowired @Qualifier("myConversionService") |
| 34 | +private ConfigurableConversionService conversionService; |
| 35 | + |
| 36 | + @ComponentScan("org.mapstruct.extensions.spring") |
| 37 | + @Component |
| 38 | + static class AdditionalBeanConfiguration { |
| 39 | + @Bean |
| 40 | + ConfigurableConversionService getConversionService() { |
| 41 | + return new DefaultConversionService(); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + ConfigurableConversionService myConversionService() { |
| 46 | + return new DefaultConversionService(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void addMappersToConversionService() { |
| 52 | + conversionService.addConverter(carMapper); |
| 53 | + conversionService.addConverter(seatConfigurationMapper); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldKnowAllMappers() { |
| 58 | + then(conversionService.canConvert(Car.class, CarDto.class)).isTrue(); |
| 59 | + then(conversionService.canConvert(SeatConfiguration.class, SeatConfigurationDto.class)) |
| 60 | + .isTrue(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void shouldMapAllAttributes() { |
| 65 | + // Given |
| 66 | + final Car car = new Car(); |
| 67 | + car.setMake(TEST_MAKE); |
| 68 | + car.setType(TEST_CAR_TYPE); |
| 69 | + final SeatConfiguration seatConfiguration = new SeatConfiguration(); |
| 70 | + seatConfiguration.setSeatMaterial(TEST_SEAT_MATERIAL); |
| 71 | + seatConfiguration.setNumberOfSeats(TEST_NUMBER_OF_SEATS); |
| 72 | + car.setSeatConfiguration(seatConfiguration); |
| 73 | + |
| 74 | + // When |
| 75 | + final CarDto mappedCar = conversionService.convert(car, CarDto.class); |
| 76 | + |
| 77 | + // Then |
| 78 | + then(mappedCar).isNotNull(); |
| 79 | + then(mappedCar.getMake()).isEqualTo(TEST_MAKE); |
| 80 | + then(mappedCar.getType()).isEqualTo(String.valueOf(TEST_CAR_TYPE)); |
| 81 | + final SeatConfigurationDto mappedCarSeats = mappedCar.getSeats(); |
| 82 | + then(mappedCarSeats).isNotNull(); |
| 83 | + then(mappedCarSeats.getSeatCount()).isEqualTo(TEST_NUMBER_OF_SEATS); |
| 84 | + then(mappedCarSeats.getMaterial()).isEqualTo(String.valueOf(TEST_SEAT_MATERIAL)); |
| 85 | + } |
| 86 | +} |
0 commit comments