|
| 1 | +/* |
| 2 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package com.amazonaws.services.lambda.runtime.api.client; |
| 7 | + |
| 8 | +import com.amazonaws.services.lambda.runtime.CustomPojoSerializer; |
| 9 | +import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.io.ByteArrayOutputStream; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.io.OutputStream; |
| 20 | +import java.lang.reflect.Field; |
| 21 | +import java.lang.reflect.Type; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.Mockito.*; |
| 27 | + |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +class PojoSerializerLoaderTest { |
| 30 | + |
| 31 | + @Mock |
| 32 | + private CustomPojoSerializer mockSerializer; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp() throws Exception { |
| 36 | + resetStaticFields(); |
| 37 | + } |
| 38 | + |
| 39 | + private void resetStaticFields() throws Exception { |
| 40 | + Field serializerField = PojoSerializerLoader.class.getDeclaredField("customPojoSerializer"); |
| 41 | + serializerField.setAccessible(true); |
| 42 | + serializerField.set(null, null); |
| 43 | + |
| 44 | + Field initializedField = PojoSerializerLoader.class.getDeclaredField("initialized"); |
| 45 | + initializedField.setAccessible(true); |
| 46 | + initializedField.set(null, false); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + private void setMockSerializer(CustomPojoSerializer serializer) throws Exception { |
| 51 | + Field serializerField = PojoSerializerLoader.class.getDeclaredField("customPojoSerializer"); |
| 52 | + serializerField.setAccessible(true); |
| 53 | + serializerField.set(null, serializer); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testGetCustomerSerializerNoSerializerAvailable() throws Exception { |
| 58 | + PojoSerializer<Object> serializer = PojoSerializerLoader.getCustomerSerializer(String.class); |
| 59 | + assertNull(serializer); |
| 60 | + Field initializedField = PojoSerializerLoader.class.getDeclaredField("initialized"); |
| 61 | + initializedField.setAccessible(true); |
| 62 | + assert((Boolean) initializedField.get(null)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testGetCustomerSerializerWithValidSerializer() throws Exception { |
| 67 | + setMockSerializer(mockSerializer); |
| 68 | + String testInput = "test input"; |
| 69 | + String testOutput = "test output"; |
| 70 | + Type testType = String.class; |
| 71 | + when(mockSerializer.fromJson(any(InputStream.class), eq(testType))).thenReturn(testOutput); |
| 72 | + when(mockSerializer.fromJson(eq(testInput), eq(testType))).thenReturn(testOutput); |
| 73 | + |
| 74 | + PojoSerializer<Object> serializer = PojoSerializerLoader.getCustomerSerializer(testType); |
| 75 | + assertNotNull(serializer); |
| 76 | + |
| 77 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(testInput.getBytes()); |
| 78 | + Object result1 = serializer.fromJson(inputStream); |
| 79 | + assertEquals(testOutput, result1); |
| 80 | + |
| 81 | + Object result2 = serializer.fromJson(testInput); |
| 82 | + assertEquals(testOutput, result2); |
| 83 | + |
| 84 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 85 | + serializer.toJson(testInput, outputStream); |
| 86 | + verify(mockSerializer).toJson(eq(testInput), any(OutputStream.class), eq(testType)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testGetCustomerSerializerCachingBehavior() throws Exception { |
| 91 | + setMockSerializer(mockSerializer); |
| 92 | + |
| 93 | + Type testType = String.class; |
| 94 | + PojoSerializer<Object> serializer1 = PojoSerializerLoader.getCustomerSerializer(testType); |
| 95 | + PojoSerializer<Object> serializer2 = PojoSerializerLoader.getCustomerSerializer(testType); |
| 96 | + |
| 97 | + assertNotNull(serializer1); |
| 98 | + assertNotNull(serializer2); |
| 99 | + |
| 100 | + String testInput = "test"; |
| 101 | + serializer1.fromJson(testInput); |
| 102 | + serializer2.fromJson(testInput); |
| 103 | + |
| 104 | + verify(mockSerializer, times(2)).fromJson(eq(testInput), eq(testType)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testGetCustomerSerializerDifferentTypes() throws Exception { |
| 109 | + setMockSerializer(mockSerializer); |
| 110 | + |
| 111 | + PojoSerializer<Object> stringSerializer = PojoSerializerLoader.getCustomerSerializer(String.class); |
| 112 | + PojoSerializer<Object> integerSerializer = PojoSerializerLoader.getCustomerSerializer(Integer.class); |
| 113 | + |
| 114 | + assertNotNull(stringSerializer); |
| 115 | + assertNotNull(integerSerializer); |
| 116 | + |
| 117 | + String testString = "test"; |
| 118 | + Integer testInt = 123; |
| 119 | + |
| 120 | + stringSerializer.fromJson(testString); |
| 121 | + integerSerializer.fromJson(testInt.toString()); |
| 122 | + |
| 123 | + verify(mockSerializer).fromJson(eq(testString), eq(String.class)); |
| 124 | + verify(mockSerializer).fromJson(eq(testInt.toString()), eq(Integer.class)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testGetCustomerSerializerNullType() throws Exception { |
| 129 | + setMockSerializer(mockSerializer); |
| 130 | + |
| 131 | + PojoSerializer<Object> serializer = PojoSerializerLoader.getCustomerSerializer(null); |
| 132 | + assertNotNull(serializer); |
| 133 | + |
| 134 | + String testInput = "test"; |
| 135 | + serializer.fromJson(testInput); |
| 136 | + verify(mockSerializer).fromJson(eq(testInput), eq(null)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testGetCustomerSerializerExceptionHandling() throws Exception { |
| 141 | + setMockSerializer(mockSerializer); |
| 142 | + |
| 143 | + doThrow(new RuntimeException("Test exception")) |
| 144 | + .when(mockSerializer) |
| 145 | + .fromJson(any(String.class), any(Type.class)); |
| 146 | + |
| 147 | + PojoSerializer<Object> serializer = PojoSerializerLoader.getCustomerSerializer(String.class); |
| 148 | + assertNotNull(serializer); |
| 149 | + assertThrows(RuntimeException.class, () -> serializer.fromJson("test")); |
| 150 | + } |
| 151 | +} |
0 commit comments