Skip to content

test: add coverage for utils #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aws-lambda-java-runtime-interface-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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();
});
}
}