Skip to content

Commit d7f1083

Browse files
committed
Make input streams auto-closable in PowertoolsSerializerTest
1 parent 0bf3665 commit d7f1083

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

powertools-kafka/src/test/java/software/amazon/lambda/powertools/kafka/PowertoolsSerializerTest.java

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static Stream<InputType> inputTypes() {
6666
@ParameterizedTest
6767
@MethodSource("inputTypes")
6868
@SetEnvironmentVariable(key = "_HANDLER", value = "")
69-
void shouldUseDefaultDeserializerWhenHandlerNotFound(InputType inputType) throws JsonProcessingException {
69+
void shouldUseDefaultDeserializerWhenHandlerNotFound(InputType inputType) throws JsonProcessingException, IOException {
7070
// When
7171
PowertoolsSerializer serializer = new PowertoolsSerializer();
7272

@@ -77,8 +77,9 @@ void shouldUseDefaultDeserializerWhenHandlerNotFound(InputType inputType) throws
7777
// This will use the Lambda default deserializer (no Kafka logic)
7878
TestProductPojo result;
7979
if (inputType == InputType.INPUT_STREAM) {
80-
ByteArrayInputStream input = new ByteArrayInputStream(json.getBytes());
81-
result = serializer.fromJson(input, TestProductPojo.class);
80+
try (ByteArrayInputStream input = new ByteArrayInputStream(json.getBytes())) {
81+
result = serializer.fromJson(input, TestProductPojo.class);
82+
}
8283
} else {
8384
result = serializer.fromJson(json, TestProductPojo.class);
8485
}
@@ -92,7 +93,7 @@ void shouldUseDefaultDeserializerWhenHandlerNotFound(InputType inputType) throws
9293
@ParameterizedTest
9394
@MethodSource("inputTypes")
9495
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.DefaultHandler::handleRequest")
95-
void shouldUseLambdaDefaultDeserializer(InputType inputType) throws JsonProcessingException {
96+
void shouldUseLambdaDefaultDeserializer(InputType inputType) throws JsonProcessingException, IOException {
9697
// When
9798
PowertoolsSerializer serializer = new PowertoolsSerializer();
9899

@@ -103,8 +104,9 @@ void shouldUseLambdaDefaultDeserializer(InputType inputType) throws JsonProcessi
103104
// This will use the Lambda default deserializer (no Kafka logic)
104105
TestProductPojo result;
105106
if (inputType == InputType.INPUT_STREAM) {
106-
ByteArrayInputStream input = new ByteArrayInputStream(json.getBytes());
107-
result = serializer.fromJson(input, TestProductPojo.class);
107+
try (ByteArrayInputStream input = new ByteArrayInputStream(json.getBytes())) {
108+
result = serializer.fromJson(input, TestProductPojo.class);
109+
}
108110
} else {
109111
result = serializer.fromJson(json, TestProductPojo.class);
110112
}
@@ -138,30 +140,32 @@ void shouldHandleInputStreamType() throws IOException {
138140

139141
// Then
140142
String testInput = "This is a test string";
141-
ByteArrayInputStream inputStream = new ByteArrayInputStream(testInput.getBytes());
142-
143-
// This should return the input stream directly
144-
InputStream result = serializer.fromJson(inputStream, InputStream.class);
145-
146-
// Read the content to verify it's the same
147-
String resultString = new String(result.readAllBytes());
148-
assertThat(resultString).isEqualTo(testInput);
143+
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(testInput.getBytes())) {
144+
// This should return the input stream directly
145+
InputStream result = serializer.fromJson(inputStream, InputStream.class);
146+
147+
// Read the content to verify it's the same
148+
try (result) {
149+
String resultString = new String(result.readAllBytes());
150+
assertThat(resultString).isEqualTo(testInput);
151+
}
152+
}
149153
}
150154

151155
@Test
152-
void shouldConvertInputStreamToString() {
156+
void shouldConvertInputStreamToString() throws IOException {
153157
// When
154158
LambdaDefaultDeserializer deserializer = new LambdaDefaultDeserializer();
155159

156160
// Then
157161
String expected = "This is a test string";
158-
ByteArrayInputStream inputStream = new ByteArrayInputStream(expected.getBytes());
162+
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(expected.getBytes())) {
163+
// Convert InputStream to String
164+
String result = deserializer.fromJson(inputStream, String.class);
159165

160-
// Convert InputStream to String
161-
String result = deserializer.fromJson(inputStream, String.class);
162-
163-
// Verify the result
164-
assertThat(result).isEqualTo(expected);
166+
// Verify the result
167+
assertThat(result).isEqualTo(expected);
168+
}
165169
}
166170

167171
@Test
@@ -207,7 +211,7 @@ void shouldConvertStringToByteArray() {
207211
@ParameterizedTest
208212
@MethodSource("inputTypes")
209213
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.JsonHandler::handleRequest")
210-
void shouldUseKafkaJsonDeserializer(InputType inputType) throws JsonProcessingException {
214+
void shouldUseKafkaJsonDeserializer(InputType inputType) throws IOException {
211215
// When
212216
PowertoolsSerializer serializer = new PowertoolsSerializer();
213217

@@ -241,8 +245,9 @@ void shouldUseKafkaJsonDeserializer(InputType inputType) throws JsonProcessingEx
241245
ConsumerRecords<String, TestProductPojo> records;
242246

243247
if (inputType == InputType.INPUT_STREAM) {
244-
ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes());
245-
records = serializer.fromJson(input, type);
248+
try (ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes())) {
249+
records = serializer.fromJson(input, type);
250+
}
246251
} else {
247252
records = serializer.fromJson(kafkaJson, type);
248253
}
@@ -302,8 +307,9 @@ void shouldUseKafkaAvroDeserializer(InputType inputType) throws IOException {
302307
ConsumerRecords<String, software.amazon.lambda.powertools.kafka.serializers.test.avro.TestProduct> records;
303308

304309
if (inputType == InputType.INPUT_STREAM) {
305-
ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes());
306-
records = serializer.fromJson(input, type);
310+
try (ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes())) {
311+
records = serializer.fromJson(input, type);
312+
}
307313
} else {
308314
records = serializer.fromJson(kafkaJson, type);
309315
}
@@ -330,7 +336,7 @@ void shouldUseKafkaAvroDeserializer(InputType inputType) throws IOException {
330336
@ParameterizedTest
331337
@MethodSource("inputTypes")
332338
@SetEnvironmentVariable(key = "_HANDLER", value = "software.amazon.lambda.powertools.kafka.testutils.ProtobufHandler::handleRequest")
333-
void shouldUseKafkaProtobufDeserializer(InputType inputType) {
339+
void shouldUseKafkaProtobufDeserializer(InputType inputType) throws IOException {
334340
// When
335341
PowertoolsSerializer serializer = new PowertoolsSerializer();
336342

@@ -369,8 +375,9 @@ void shouldUseKafkaProtobufDeserializer(InputType inputType) {
369375
ConsumerRecords<String, software.amazon.lambda.powertools.kafka.serializers.test.protobuf.TestProduct> records;
370376

371377
if (inputType == InputType.INPUT_STREAM) {
372-
ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes());
373-
records = serializer.fromJson(input, type);
378+
try (ByteArrayInputStream input = new ByteArrayInputStream(kafkaJson.getBytes())) {
379+
records = serializer.fromJson(input, type);
380+
}
374381
} else {
375382
records = serializer.fromJson(kafkaJson, type);
376383
}

0 commit comments

Comments
 (0)