|
| 1 | +/**************************************************************** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one * |
| 3 | + * or more contributor license agreements. See the NOTICE file * |
| 4 | + * distributed with this work for additional information * |
| 5 | + * regarding copyright ownership. The ASF licenses this file * |
| 6 | + * to you under the Apache License, Version 2.0 (the * |
| 7 | + * "License"); you may not use this file except in compliance * |
| 8 | + * with the License. You may obtain a copy of the License at * |
| 9 | + * * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 11 | + * * |
| 12 | + * Unless required by applicable law or agreed to in writing, * |
| 13 | + * software distributed under the License is distributed on an * |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
| 15 | + * KIND, either express or implied. See the License for the * |
| 16 | + * specific language governing permissions and limitations * |
| 17 | + * under the License. * |
| 18 | + ****************************************************************/ |
| 19 | + |
| 20 | +package org.apache; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +import java.time.ZonedDateTime; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.UUID; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import org.apache.dto.BaseType; |
| 30 | +import org.apache.dto.FirstDTO; |
| 31 | +import org.apache.dto.FirstDomainObject; |
| 32 | +import org.apache.dto.SecondDTO; |
| 33 | +import org.apache.dto.SecondDomainObject; |
| 34 | +import org.apache.dto.TestModules; |
| 35 | +import org.apache.james.json.DTO; |
| 36 | +import org.apache.james.json.DTOConverter; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.params.ParameterizedTest; |
| 39 | +import org.junit.jupiter.params.provider.Arguments; |
| 40 | +import org.junit.jupiter.params.provider.MethodSource; |
| 41 | + |
| 42 | +class DTOConverterTest { |
| 43 | + private static final FirstDomainObject FIRST = new FirstDomainObject(Optional.of(1L), ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"), "first payload"); |
| 44 | + private static final FirstDTO FIRST_DTO = new FirstDTO("first", Optional.of(1L), "2016-04-03T02:01+07:00[Asia/Vientiane]", "first payload"); |
| 45 | + private static final SecondDomainObject SECOND = new SecondDomainObject(UUID.fromString("4a2c853f-7ffc-4ce3-9410-a47e85b3b741"), "second payload"); |
| 46 | + private static final SecondDTO SECOND_DTO = new SecondDTO("second", "4a2c853f-7ffc-4ce3-9410-a47e85b3b741", "second payload"); |
| 47 | + |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + @Test |
| 50 | + void shouldConvertFromKnownDTO() throws Exception { |
| 51 | + assertThat(DTOConverter.of(TestModules.FIRST_TYPE) |
| 52 | + .convert(FIRST_DTO)) |
| 53 | + .contains(FIRST); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldReturnEmptyWhenConvertingFromUnknownDTO() { |
| 58 | + assertThat(DTOConverter.of() |
| 59 | + .convert(FIRST_DTO)) |
| 60 | + .isEmpty(); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource |
| 65 | + void convertFromDomainObjectShouldHandleAllKnownTypes(BaseType domainObject, DTO dto) throws Exception { |
| 66 | + @SuppressWarnings("unchecked") |
| 67 | + DTOConverter<BaseType, DTO> serializer = DTOConverter.of( |
| 68 | + TestModules.FIRST_TYPE, |
| 69 | + TestModules.SECOND_TYPE); |
| 70 | + |
| 71 | + assertThat(serializer.convert(domainObject)) |
| 72 | + .hasValueSatisfying(result -> assertThat(result).isInstanceOf(dto.getClass()).isEqualToComparingFieldByField(dto)); |
| 73 | + } |
| 74 | + |
| 75 | + private static Stream<Arguments> convertFromDomainObjectShouldHandleAllKnownTypes() { |
| 76 | + return allKnownTypes(); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest |
| 80 | + @MethodSource |
| 81 | + void convertFromDTOShouldHandleAllKnownTypes(BaseType domainObject, DTO dto) throws Exception { |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + DTOConverter<BaseType, DTO> serializer = DTOConverter.of( |
| 84 | + TestModules.FIRST_TYPE, |
| 85 | + TestModules.SECOND_TYPE); |
| 86 | + |
| 87 | + assertThat(serializer.convert(dto)) |
| 88 | + .hasValueSatisfying(result -> assertThat(result).isInstanceOf(domainObject.getClass()).isEqualToComparingFieldByField(domainObject)); |
| 89 | + } |
| 90 | + |
| 91 | + private static Stream<Arguments> convertFromDTOShouldHandleAllKnownTypes() { |
| 92 | + return allKnownTypes(); |
| 93 | + } |
| 94 | + |
| 95 | + private static Stream<Arguments> allKnownTypes() { |
| 96 | + return Stream.of( |
| 97 | + Arguments.of(SECOND, SECOND_DTO), |
| 98 | + Arguments.of(FIRST, FIRST_DTO) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings("unchecked") |
| 103 | + @Test |
| 104 | + void shouldConvertFromKnownDomainObject() throws Exception { |
| 105 | + assertThat(DTOConverter.of(TestModules.FIRST_TYPE) |
| 106 | + .convert(FIRST)) |
| 107 | + .hasValueSatisfying(result -> assertThat(result).isInstanceOf(FirstDTO.class).isEqualToComparingFieldByField(FIRST_DTO)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void shouldReturnEmptyWhenConvertUnknownDomainObject() { |
| 112 | + assertThat(DTOConverter.of().convert(FIRST)) |
| 113 | + .isEmpty(); |
| 114 | + } |
| 115 | +} |
0 commit comments