|
| 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 | +} |
0 commit comments