|
| 1 | +/* |
| 2 | +* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | +* A copy of the License is located at |
| 7 | +* |
| 8 | +* http://aws.amazon.com/apache2.0 |
| 9 | +* |
| 10 | +* or in the "license" file accompanying this file. This file is distributed |
| 11 | +* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | +* express or implied. See the License for the specific language governing |
| 13 | +* permissions and limitations under the License. |
| 14 | +*/ |
| 15 | +package software.amazon.cloudformation.resource; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 19 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.List; |
| 22 | +import lombok.Data; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +public class TypeConverterTest { |
| 26 | + @Data |
| 27 | + public static class ComplexObject { |
| 28 | + @JsonProperty("Key") |
| 29 | + private String key; |
| 30 | + |
| 31 | + @JsonProperty("Value") |
| 32 | + private String value; |
| 33 | + } |
| 34 | + |
| 35 | + public static final String LIST_OF_OBJECTS = "[{\"Key\" : \"Key\",\"Value\" :\"Value\"},{\"Key\" : \"Key\",\"Value\":\"Value\"}]"; |
| 36 | + public static final String OBJECT = "{\"Key\" : \"Key\",\"Value\" : \"Value\"}"; |
| 37 | + |
| 38 | + final TypeReference<ComplexObject> typeReferenceComplexObject = new TypeReference<ComplexObject>() { |
| 39 | + }; |
| 40 | + final TypeReference<List<ComplexObject>> typeReferenceComplexObjectList = new TypeReference<List<ComplexObject>>() { |
| 41 | + }; |
| 42 | + |
| 43 | + @Test |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + public void castMultiTypePropertyToList() throws IOException { |
| 46 | + final Serializer ser = new Serializer(); |
| 47 | + |
| 48 | + // mimics if multitype was casted to an object |
| 49 | + final Object multiTypeProperty = ser.deserialize(LIST_OF_OBJECTS, new TypeReference<Object>() { |
| 50 | + }); |
| 51 | + |
| 52 | + Object converted = TypeConverter.convertProperty(multiTypeProperty, typeReferenceComplexObject, |
| 53 | + typeReferenceComplexObjectList); |
| 54 | + |
| 55 | + assertThat(converted).isInstanceOf(List.class); |
| 56 | + |
| 57 | + List<ComplexObject> complexObject = (List<ComplexObject>) converted; |
| 58 | + assertThat(complexObject.size()).isEqualTo(2); |
| 59 | + assertThat(complexObject.get(0)).isInstanceOf(ComplexObject.class); |
| 60 | + assertThat(complexObject.get(0).key).isEqualTo("Key"); |
| 61 | + assertThat(complexObject.get(0).value).isEqualTo("Value"); |
| 62 | + assertThat(complexObject.get(1).key).isEqualTo("Key"); |
| 63 | + assertThat(complexObject.get(1).value).isEqualTo("Value"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void castMultiTypePropertyToObject() throws IOException { |
| 68 | + final Serializer ser = new Serializer(); |
| 69 | + |
| 70 | + // mimics if multitype was casted to an object |
| 71 | + final Object multiTypeProperty = ser.deserialize(OBJECT, new TypeReference<Object>() { |
| 72 | + }); |
| 73 | + |
| 74 | + Object converted = TypeConverter.convertProperty(multiTypeProperty, typeReferenceComplexObject, |
| 75 | + typeReferenceComplexObjectList); |
| 76 | + assertThat(converted).isInstanceOf(ComplexObject.class); |
| 77 | + ComplexObject complexObject = (ComplexObject) converted; |
| 78 | + assertThat(complexObject.key).isEqualTo("Key"); |
| 79 | + assertThat(complexObject.value).isEqualTo("Value"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void castMultiTypePropertyWithInvalidReferences() throws IOException { |
| 84 | + final Serializer ser = new Serializer(); |
| 85 | + |
| 86 | + // mimics if multitype was casted to an object |
| 87 | + final Object multiTypeProperty = ser.deserialize(OBJECT, new TypeReference<Object>() { |
| 88 | + }); |
| 89 | + |
| 90 | + try { |
| 91 | + Object converted = TypeConverter.convertProperty(multiTypeProperty, new TypeReference<Integer>() { |
| 92 | + }, new TypeReference<Boolean>() { |
| 93 | + }); |
| 94 | + |
| 95 | + } catch (RuntimeException e) { |
| 96 | + assertThat(e.getMessage()).isEqualTo("No Suitable Type Reference"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments