diff --git a/aws-lambda-java-runtime-interface-client/pom.xml b/aws-lambda-java-runtime-interface-client/pom.xml index b5ea2721..e84cac0d 100644 --- a/aws-lambda-java-runtime-interface-client/pom.xml +++ b/aws-lambda-java-runtime-interface-client/pom.xml @@ -87,6 +87,12 @@ 4.11.0 test + + org.mockito + mockito-junit-jupiter + 4.11.0 + test + com.squareup.okhttp3 mockwebserver diff --git a/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/LambdaOutputStreamTest.java b/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/LambdaOutputStreamTest.java new file mode 100644 index 00000000..30146ea8 --- /dev/null +++ b/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/LambdaOutputStreamTest.java @@ -0,0 +1,81 @@ +/* +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +SPDX-License-Identifier: Apache-2.0 +*/ + +package com.amazonaws.services.lambda.runtime.api.client.util; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.io.OutputStream; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +public class LambdaOutputStreamTest { + + @Mock + private OutputStream mockInnerStream; + + private LambdaOutputStream lambdaOutputStream; + + @BeforeEach + void setUp() { + lambdaOutputStream = new LambdaOutputStream(mockInnerStream); + } + + @Test + void writeSingleByte() throws IOException { + int testByte = 65; // 'A' + lambdaOutputStream.write(testByte); + verify(mockInnerStream).write(new byte[]{(byte) testByte}, 0, 1); + } + + @Test + void writeByteArray() throws IOException { + byte[] testBytes = "test".getBytes(); + lambdaOutputStream.write(testBytes); + verify(mockInnerStream).write(testBytes, 0, testBytes.length); + } + + @Test + void writeOffsetLength() throws IOException { + byte[] testBytes = "test".getBytes(); + int offset = 1; + int length = 2; + lambdaOutputStream.write(testBytes, offset, length); + verify(mockInnerStream).write(testBytes, offset, length); + } + + @Test + void throwWriteSingleByte() throws IOException { + doThrow(new IOException("Test exception")) + .when(mockInnerStream) + .write(any(byte[].class), anyInt(), anyInt()); + assertThrows(IOException.class, () -> lambdaOutputStream.write(65)); + } + + @Test + void throwWriteByteArray() throws IOException { + byte[] testBytes = "test".getBytes(); + doThrow(new IOException("Test exception")) + .when(mockInnerStream) + .write(any(byte[].class), anyInt(), anyInt()); + assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes)); + } + + @Test + void throwWriteOffsetLength() throws IOException { + byte[] testBytes = "test".getBytes(); + doThrow(new IOException("Test exception")) + .when(mockInnerStream) + .write(any(byte[].class), anyInt(), anyInt()); + assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes, 1, 2)); + } +} diff --git a/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/UnsafeUtilTest.java b/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/UnsafeUtilTest.java new file mode 100644 index 00000000..b1f0592f --- /dev/null +++ b/aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/UnsafeUtilTest.java @@ -0,0 +1,56 @@ +/* +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +SPDX-License-Identifier: Apache-2.0 +*/ + +package com.amazonaws.services.lambda.runtime.api.client.util; + +import org.junit.jupiter.api.Test; +import java.lang.reflect.Field; +import static org.junit.jupiter.api.Assertions.*; + +public class UnsafeUtilTest { + + @Test + void testTheUnsafeIsInitialized() { + assertNotNull(UnsafeUtil.TheUnsafe); + } + + @Test + void testThrowException() { + Exception testException = new Exception("Test exception"); + + try { + UnsafeUtil.throwException(testException); + fail("Should have thrown an exception"); + } catch (Throwable e) { + assertEquals("Test exception", e.getMessage()); + assertSame(testException, e); + } + } + + @Test + void testDisableIllegalAccessWarning() { + assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning()); + try { + Class illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger"); + Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger"); + loggerField.setAccessible(true); + Object loggerValue = loggerField.get(null); + assertNull(loggerValue); + } catch (ClassNotFoundException e) { + assertTrue(true); + } catch (NoSuchFieldException e) { + assertTrue(true); + } catch (Exception e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + + @Test + void testPrivateConstructor() { + assertThrows(IllegalAccessException.class, () -> { + UnsafeUtil.class.getDeclaredConstructor().newInstance(); + }); + } +}