Skip to content

Commit adbb679

Browse files
refactor(invoke): replace @slf4j with runContext.logger() (#660)
* refactor(invoke): replace @slf4j with runContext.logger() * test: add logger mock to fix NPE in invoke tests
1 parent 6c954c6 commit adbb679

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/main/java/io/kestra/plugin/aws/lambda/Invoke.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.kestra.core.models.annotations.Example;
88
import io.kestra.core.models.annotations.Metric;
99
import io.kestra.core.models.annotations.Plugin;
10-
import io.kestra.core.models.annotations.PluginProperty;
1110
import io.kestra.core.models.executions.metrics.Counter;
1211
import io.kestra.core.models.executions.metrics.Timer;
1312
import io.kestra.core.models.property.Property;
@@ -117,6 +116,7 @@ public Output run(RunContext runContext) throws Exception {
117116
var requestPayload = runContext.render(this.functionPayload).asMap(String.class, Object.class).isEmpty() ?
118117
null :
119118
runContext.render(this.functionPayload).asMap(String.class, Object.class);
119+
var logger = runContext.logger();
120120

121121
try (var lambda = client(runContext)) {
122122
var builder = InvokeRequest.builder().functionName(functionArn);
@@ -137,8 +137,8 @@ public Output run(RunContext runContext) throws Exception {
137137
// details and throw - this will throw an exception in any case
138138
handleError(functionArn, contentType, res.payload());
139139
}
140-
if (log.isDebugEnabled()) {
141-
log.debug("Lambda {} invoked successfully", functionArn);
140+
if (logger.isDebugEnabled()) {
141+
logger.debug("Lambda {} invoked successfully", functionArn);
142142
}
143143
Output out = handleContent(runContext, functionArn, contentType, res.payload());
144144
runContext.metric(Timer.of("duration", Duration.ofNanos(System.nanoTime() - start)));
@@ -201,7 +201,7 @@ void handleError(String functionArn, ContentType contentType, SdkBytes payload)
201201
try {
202202
errorPayload = payload.asUtf8String();
203203
} catch (UncheckedIOException e) {
204-
log.warn("Lambda function respone payload cannot be read as UTF8 string: {}",
204+
log.warn("Lambda function response payload cannot be read as UTF8 string: {}",
205205
e.getMessage());
206206
errorPayload = null;
207207
}
@@ -223,6 +223,7 @@ void handleError(String functionArn, ContentType contentType, SdkBytes payload)
223223
@VisibleForTesting
224224
Output handleContent(RunContext runContext, String functionArn, ContentType contentType,
225225
SdkBytes payload) {
226+
var logger = runContext.logger();
226227
try (var dataStream = payload.asInputStream()) {
227228
File tempFile = runContext.workingDir().createTempFile().toFile();
228229
// noinspection ResultOfMethodCallIgnored
@@ -232,8 +233,8 @@ Output handleContent(RunContext runContext, String functionArn, ContentType cont
232233
// Doing the same as in S3Service.download()
233234
runContext.metric(Counter.of("file.size", size));
234235
var uri = runContext.storage().putFile(tempFile);
235-
if (log.isDebugEnabled()) {
236-
log.debug("Lambda invokation task completed {}: response type: {}, file: `{}",
236+
if (logger.isDebugEnabled()) {
237+
logger.debug("Lambda invokation task completed {}: response type: {}, file: `{}",
237238
functionArn, contentType, uri);
238239
}
239240
return Output.builder()

src/test/java/io/kestra/plugin/aws/lambda/InvokeUnitTest.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
package io.kestra.plugin.aws.lambda;
22

3-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertNotNull;
6-
import static org.junit.jupiter.api.Assertions.assertThrows;
7-
import static org.junit.jupiter.api.Assertions.assertTrue;
8-
import static org.mockito.ArgumentMatchers.any;
9-
import static org.mockito.ArgumentMatchers.anyString;
10-
import static org.mockito.ArgumentMatchers.eq;
11-
import static org.mockito.BDDMockito.given;
12-
import static org.mockito.Mockito.*;
13-
14-
import java.io.ByteArrayInputStream;
15-
import java.io.File;
16-
import java.io.IOException;
17-
import java.net.URI;
18-
import java.nio.file.Files;
19-
import java.util.Collections;
20-
import java.util.Map;
21-
import java.util.Optional;
22-
3+
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
234
import io.kestra.core.models.property.Property;
5+
import io.kestra.core.runners.RunContext;
246
import io.kestra.core.runners.RunContextProperty;
7+
import io.kestra.core.runners.WorkingDir;
258
import io.kestra.core.storages.Storage;
9+
import io.kestra.plugin.aws.lambda.Invoke.Output;
2610
import org.apache.http.entity.ContentType;
2711
import org.junit.jupiter.api.AfterEach;
2812
import org.junit.jupiter.api.BeforeEach;
@@ -33,16 +17,29 @@
3317
import org.mockito.invocation.InvocationOnMock;
3418
import org.mockito.junit.jupiter.MockitoExtension;
3519
import org.mockito.stubbing.Answer;
36-
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
37-
import io.kestra.core.runners.RunContext;
38-
import io.kestra.core.runners.WorkingDir;
39-
import io.kestra.plugin.aws.lambda.Invoke.Output;
20+
import org.slf4j.Logger;
4021
import software.amazon.awssdk.core.SdkBytes;
4122
import software.amazon.awssdk.http.SdkHttpResponse;
4223
import software.amazon.awssdk.services.lambda.LambdaClient;
4324
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
4425
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
4526

27+
import java.io.ByteArrayInputStream;
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.net.URI;
31+
import java.nio.file.Files;
32+
import java.util.Collections;
33+
import java.util.Map;
34+
import java.util.Optional;
35+
36+
import static org.junit.jupiter.api.Assertions.*;
37+
import static org.mockito.ArgumentMatchers.any;
38+
import static org.mockito.ArgumentMatchers.eq;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.Mockito.doReturn;
41+
import static org.mockito.Mockito.spy;
42+
4643
@ExtendWith(MockitoExtension.class)
4744
public class InvokeUnitTest {
4845

@@ -60,6 +57,9 @@ public class InvokeUnitTest {
6057
@Mock(strictness = Strictness.LENIENT)
6158
private WorkingDir workingDir;
6259

60+
@Mock(strictness = Strictness.LENIENT)
61+
private Logger logger;
62+
6363
private File tempFile;
6464

6565
private String testValue;
@@ -70,6 +70,7 @@ void setUp() throws IOException, IllegalVariableEvaluationException {
7070
given(context.workingDir()).willReturn(workingDir);
7171
given(context.workingDir().createTempFile()).willReturn(Files.createTempFile("test", "lambdainvoke"));
7272
given(context.metric(any())).willReturn(context);
73+
given(context.logger()).willReturn(logger);
7374
given(context.render(any(Property.class))).willAnswer(new Answer<RunContextProperty<String>>() {
7475
@Override
7576
public RunContextProperty<String> answer(InvocationOnMock invocation) throws Throwable {

0 commit comments

Comments
 (0)