|
| 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.api.client.runtimeapi.dto.InvocationRequest; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | +import static org.mockito.Mockito.*; |
| 13 | + |
| 14 | +class LambdaRequestHandlerTest { |
| 15 | + |
| 16 | + private InvocationRequest mockRequest; |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + void setUp() { |
| 20 | + mockRequest = mock(InvocationRequest.class); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void testInitErrorHandler() { |
| 25 | + String className = "com.example.TestClass"; |
| 26 | + Exception testException = new RuntimeException("initialization error"); |
| 27 | + |
| 28 | + LambdaRequestHandler handler = LambdaRequestHandler.initErrorHandler(testException, className); |
| 29 | + |
| 30 | + assertNotNull(handler); |
| 31 | + assertTrue(handler instanceof LambdaRequestHandler.UserFaultHandler); |
| 32 | + |
| 33 | + LambdaRequestHandler.UserFaultHandler userFaultHandler = (LambdaRequestHandler.UserFaultHandler) handler; |
| 34 | + UserFault fault = userFaultHandler.fault; |
| 35 | + |
| 36 | + assertNotNull(fault); |
| 37 | + assertEquals("Error loading class " + className + ": initialization error", fault.msg); |
| 38 | + assertEquals("java.lang.RuntimeException", fault.exception); |
| 39 | + assertTrue(fault.fatal); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testClassNotFound() { |
| 44 | + String className = "com.example.MissingClass"; |
| 45 | + Exception testException = new ClassNotFoundException("class not found"); |
| 46 | + |
| 47 | + LambdaRequestHandler handler = LambdaRequestHandler.classNotFound(testException, className); |
| 48 | + |
| 49 | + assertNotNull(handler); |
| 50 | + assertTrue(handler instanceof LambdaRequestHandler.UserFaultHandler); |
| 51 | + |
| 52 | + LambdaRequestHandler.UserFaultHandler userFaultHandler = (LambdaRequestHandler.UserFaultHandler) handler; |
| 53 | + UserFault fault = userFaultHandler.fault; |
| 54 | + |
| 55 | + assertNotNull(fault); |
| 56 | + assertEquals("Class not found: " + className, fault.msg); |
| 57 | + assertEquals("java.lang.ClassNotFoundException", fault.exception); |
| 58 | + assertFalse(fault.fatal); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testUserFaultHandlerConstructor() { |
| 63 | + UserFault testFault = new UserFault("test message", "TestException", "test trace"); |
| 64 | + LambdaRequestHandler.UserFaultHandler handler = new LambdaRequestHandler.UserFaultHandler(testFault); |
| 65 | + |
| 66 | + assertNotNull(handler); |
| 67 | + assertSame(testFault, handler.fault); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testUserFaultHandlerCallThrowsFault() { |
| 72 | + UserFault testFault = new UserFault("test message", "TestException", "test trace"); |
| 73 | + LambdaRequestHandler.UserFaultHandler handler = new LambdaRequestHandler.UserFaultHandler(testFault); |
| 74 | + |
| 75 | + UserFault thrownFault = assertThrows(UserFault.class, () -> handler.call(mockRequest)); |
| 76 | + assertSame(testFault, thrownFault); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testInitErrorHandlerWithNullMessage() { |
| 81 | + String className = "com.example.TestClass"; |
| 82 | + Exception testException = new RuntimeException(); |
| 83 | + |
| 84 | + LambdaRequestHandler handler = LambdaRequestHandler.initErrorHandler(testException, className); |
| 85 | + |
| 86 | + assertNotNull(handler); |
| 87 | + assertTrue(handler instanceof LambdaRequestHandler.UserFaultHandler); |
| 88 | + |
| 89 | + LambdaRequestHandler.UserFaultHandler userFaultHandler = (LambdaRequestHandler.UserFaultHandler) handler; |
| 90 | + UserFault fault = userFaultHandler.fault; |
| 91 | + |
| 92 | + assertNotNull(fault); |
| 93 | + assertEquals("Error loading class " + className, fault.msg); |
| 94 | + assertEquals("java.lang.RuntimeException", fault.exception); |
| 95 | + assertTrue(fault.fatal); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testInitErrorHandlerWithNullClassName() { |
| 100 | + Exception testException = new RuntimeException("test error"); |
| 101 | + |
| 102 | + LambdaRequestHandler handler = LambdaRequestHandler.initErrorHandler(testException, null); |
| 103 | + |
| 104 | + assertNotNull(handler); |
| 105 | + assertTrue(handler instanceof LambdaRequestHandler.UserFaultHandler); |
| 106 | + |
| 107 | + LambdaRequestHandler.UserFaultHandler userFaultHandler = (LambdaRequestHandler.UserFaultHandler) handler; |
| 108 | + UserFault fault = userFaultHandler.fault; |
| 109 | + |
| 110 | + assertNotNull(fault); |
| 111 | + assertEquals("Error loading class null: test error", fault.msg); |
| 112 | + assertEquals("java.lang.RuntimeException", fault.exception); |
| 113 | + assertTrue(fault.fatal); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testClassNotFoundWithNullClassName() { |
| 118 | + Exception testException = new ClassNotFoundException("test error"); |
| 119 | + |
| 120 | + LambdaRequestHandler handler = LambdaRequestHandler.classNotFound(testException, null); |
| 121 | + |
| 122 | + assertNotNull(handler); |
| 123 | + assertTrue(handler instanceof LambdaRequestHandler.UserFaultHandler); |
| 124 | + |
| 125 | + LambdaRequestHandler.UserFaultHandler userFaultHandler = (LambdaRequestHandler.UserFaultHandler) handler; |
| 126 | + UserFault fault = userFaultHandler.fault; |
| 127 | + |
| 128 | + assertNotNull(fault); |
| 129 | + assertEquals("Class not found: null", fault.msg); |
| 130 | + assertEquals("java.lang.ClassNotFoundException", fault.exception); |
| 131 | + assertFalse(fault.fatal); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testUserFaultHandlerCallWithNullRequest() { |
| 136 | + UserFault testFault = new UserFault("test message", "TestException", "test trace"); |
| 137 | + LambdaRequestHandler.UserFaultHandler handler = new LambdaRequestHandler.UserFaultHandler(testFault); |
| 138 | + |
| 139 | + UserFault thrownFault = assertThrows(UserFault.class, () -> handler.call(null)); |
| 140 | + assertSame(testFault, thrownFault); |
| 141 | + } |
| 142 | +} |
0 commit comments