Skip to content

Commit 5e072e4

Browse files
authored
test: add coverage for utils (#523)
* test: add coverage for utils * test: add coverage for utils
1 parent cfb3ece commit 5e072e4

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

Diff for: aws-lambda-java-runtime-interface-client/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<version>4.11.0</version>
8888
<scope>test</scope>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.mockito</groupId>
92+
<artifactId>mockito-junit-jupiter</artifactId>
93+
<version>4.11.0</version>
94+
<scope>test</scope>
95+
</dependency>
9096
<dependency>
9197
<groupId>com.squareup.okhttp3</groupId>
9298
<artifactId>mockwebserver</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.util;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.jupiter.MockitoExtension;
13+
14+
import java.io.IOException;
15+
import java.io.OutputStream;
16+
17+
import static org.mockito.Mockito.*;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
@ExtendWith(MockitoExtension.class)
21+
public class LambdaOutputStreamTest {
22+
23+
@Mock
24+
private OutputStream mockInnerStream;
25+
26+
private LambdaOutputStream lambdaOutputStream;
27+
28+
@BeforeEach
29+
void setUp() {
30+
lambdaOutputStream = new LambdaOutputStream(mockInnerStream);
31+
}
32+
33+
@Test
34+
void writeSingleByte() throws IOException {
35+
int testByte = 65; // 'A'
36+
lambdaOutputStream.write(testByte);
37+
verify(mockInnerStream).write(new byte[]{(byte) testByte}, 0, 1);
38+
}
39+
40+
@Test
41+
void writeByteArray() throws IOException {
42+
byte[] testBytes = "test".getBytes();
43+
lambdaOutputStream.write(testBytes);
44+
verify(mockInnerStream).write(testBytes, 0, testBytes.length);
45+
}
46+
47+
@Test
48+
void writeOffsetLength() throws IOException {
49+
byte[] testBytes = "test".getBytes();
50+
int offset = 1;
51+
int length = 2;
52+
lambdaOutputStream.write(testBytes, offset, length);
53+
verify(mockInnerStream).write(testBytes, offset, length);
54+
}
55+
56+
@Test
57+
void throwWriteSingleByte() throws IOException {
58+
doThrow(new IOException("Test exception"))
59+
.when(mockInnerStream)
60+
.write(any(byte[].class), anyInt(), anyInt());
61+
assertThrows(IOException.class, () -> lambdaOutputStream.write(65));
62+
}
63+
64+
@Test
65+
void throwWriteByteArray() throws IOException {
66+
byte[] testBytes = "test".getBytes();
67+
doThrow(new IOException("Test exception"))
68+
.when(mockInnerStream)
69+
.write(any(byte[].class), anyInt(), anyInt());
70+
assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes));
71+
}
72+
73+
@Test
74+
void throwWriteOffsetLength() throws IOException {
75+
byte[] testBytes = "test".getBytes();
76+
doThrow(new IOException("Test exception"))
77+
.when(mockInnerStream)
78+
.write(any(byte[].class), anyInt(), anyInt());
79+
assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes, 1, 2));
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.util;
7+
8+
import org.junit.jupiter.api.Test;
9+
import java.lang.reflect.Field;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
public class UnsafeUtilTest {
13+
14+
@Test
15+
void testTheUnsafeIsInitialized() {
16+
assertNotNull(UnsafeUtil.TheUnsafe);
17+
}
18+
19+
@Test
20+
void testThrowException() {
21+
Exception testException = new Exception("Test exception");
22+
23+
try {
24+
UnsafeUtil.throwException(testException);
25+
fail("Should have thrown an exception");
26+
} catch (Throwable e) {
27+
assertEquals("Test exception", e.getMessage());
28+
assertSame(testException, e);
29+
}
30+
}
31+
32+
@Test
33+
void testDisableIllegalAccessWarning() {
34+
assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning());
35+
try {
36+
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
37+
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
38+
loggerField.setAccessible(true);
39+
Object loggerValue = loggerField.get(null);
40+
assertNull(loggerValue);
41+
} catch (ClassNotFoundException e) {
42+
assertTrue(true);
43+
} catch (NoSuchFieldException e) {
44+
assertTrue(true);
45+
} catch (Exception e) {
46+
fail("Unexpected exception: " + e.getMessage());
47+
}
48+
}
49+
50+
@Test
51+
void testPrivateConstructor() {
52+
assertThrows(IllegalAccessException.class, () -> {
53+
UnsafeUtil.class.getDeclaredConstructor().newInstance();
54+
});
55+
}
56+
}

0 commit comments

Comments
 (0)