Skip to content

Commit 96021e4

Browse files
committed
Remove mockito from powertools-logging-log4j.
1 parent 93537b6 commit 96021e4

File tree

3 files changed

+31
-78
lines changed

3 files changed

+31
-78
lines changed

powertools-logging/powertools-logging-log4j/pom.xml

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

77
<parent>
@@ -51,11 +51,6 @@
5151
<artifactId>junit-jupiter-engine</artifactId>
5252
<scope>test</scope>
5353
</dependency>
54-
<dependency>
55-
<groupId>org.mockito</groupId>
56-
<artifactId>mockito-core</artifactId>
57-
<scope>test</scope>
58-
</dependency>
5954
<dependency>
6055
<groupId>org.junit-pioneer</groupId>
6156
<artifactId>junit-pioneer</artifactId>
@@ -91,25 +86,26 @@
9186
<artifactId>jsonassert</artifactId>
9287
<scope>test</scope>
9388
</dependency>
89+
<dependency>
90+
<groupId>software.amazon.lambda</groupId>
91+
<artifactId>powertools-common</artifactId>
92+
<version>${project.version}</version>
93+
<type>test-jar</type>
94+
<scope>test</scope>
95+
</dependency>
9496
</dependencies>
9597
<profiles>
9698
<profile>
9799
<id>generate-graalvm-files</id>
98-
<dependencies>
99-
<dependency>
100-
<groupId>org.mockito</groupId>
101-
<artifactId>mockito-subclass</artifactId>
102-
<scope>test</scope>
103-
</dependency>
104-
</dependencies>
105100
<build>
106101
<plugins>
107102
<plugin>
108103
<groupId>org.apache.maven.plugins</groupId>
109104
<artifactId>maven-surefire-plugin</artifactId>
110105
<version>3.5.3</version>
111106
<configuration>
112-
<argLine>-Dmockito.mock.maker=subclass -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-logging-log4j,experimental-class-define-support
107+
<argLine>-Dorg.graalvm.nativeimage.imagecode=agent
108+
-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-logging-log4j,experimental-class-define-support
113109
--add-opens java.base/java.util=ALL-UNNAMED
114110
--add-opens java.base/java.lang=ALL-UNNAMED
115111
</argLine>
@@ -120,13 +116,6 @@
120116
</profile>
121117
<profile>
122118
<id>graalvm-native</id>
123-
<dependencies>
124-
<dependency>
125-
<groupId>org.mockito</groupId>
126-
<artifactId>mockito-subclass</artifactId>
127-
<scope>test</scope>
128-
</dependency>
129-
</dependencies>
130119
<build>
131120
<plugins>
132121
<plugin>
@@ -150,29 +139,11 @@
150139
<buildArg>--add-opens java.base/java.lang=ALL-UNNAMED</buildArg>
151140
<buildArg>--no-fallback</buildArg>
152141
<buildArg>-Dorg.graalvm.nativeimage.imagecode=agent</buildArg>
153-
<buildArg>-H:ClassInitialization=net.bytebuddy.ClassFileVersion:rerun</buildArg>
154-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.dispatcher.JavaDispatcher:rerun</buildArg>
155-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.Invoker$Dispatcher:rerun</buildArg>
156-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.GraalImageCode:rerun</buildArg>
157142
<buildArg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger</buildArg>
158143
<buildArg>--initialize-at-build-time=org.slf4j.LoggerFactory</buildArg>
159144
<buildArg>--initialize-at-build-time=org.junit.Ignore</buildArg>
160145
<buildArg>--initialize-at-build-time=java.lang.annotation.Annotation</buildArg>
161146
<buildArg>--initialize-at-build-time=org.junit.runners.model.FrameworkField</buildArg>
162-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
163-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic</buildArg>
164-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$1</buildArg>
165-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$2</buildArg>
166-
<buildArg>--initialize-at-build-time=net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader</buildArg>
167-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable</buildArg>
168-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
169-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$ForLoadedType</buildArg>
170-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$ForLoadedMethod</buildArg>
171-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Super$Instantiation$2</buildArg>
172-
<buildArg>
173-
--trace-class-initialization=net.bytebuddy.description.type.TypeDescription$ForLoadedType,net.bytebuddy.description.method.MethodDescription$ForLoadedMethod,net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable
174-
</buildArg>
175-
<buildArg>--verbose</buildArg>
176147
</buildArgs>
177148
</configuration>
178149
</plugin>

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowerToolsResolverFactoryTest.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,36 @@
1717
import static org.apache.commons.lang3.reflect.FieldUtils.writeStaticField;
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.contentOf;
20-
import static org.mockito.Mockito.when;
21-
import static org.mockito.MockitoAnnotations.openMocks;
2220

23-
import com.amazonaws.services.lambda.runtime.Context;
2421
import java.io.File;
2522
import java.io.IOException;
2623
import java.nio.channels.FileChannel;
2724
import java.nio.file.NoSuchFileException;
2825
import java.nio.file.Paths;
2926
import java.nio.file.StandardOpenOption;
27+
3028
import org.junit.jupiter.api.AfterEach;
3129
import org.junit.jupiter.api.BeforeEach;
3230
import org.junit.jupiter.api.Order;
3331
import org.junit.jupiter.api.Test;
34-
import org.mockito.Mock;
3532
import org.slf4j.MDC;
33+
34+
import com.amazonaws.services.lambda.runtime.Context;
35+
3636
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
37+
import software.amazon.lambda.powertools.common.stubs.TestLambdaContext;
3738
import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled;
3839

3940
@Order(1)
4041
class PowerToolsResolverFactoryTest {
4142

42-
@Mock
4343
private Context context;
4444

4545
@BeforeEach
4646
void setUp() throws IllegalAccessException, IOException {
47-
openMocks(this);
4847
MDC.clear();
4948
writeStaticField(LambdaHandlerProcessor.class, "IS_COLD_START", null, true);
50-
setupContext();
49+
context = new TestLambdaContext();
5150
// Make sure file is cleaned up before running tests
5251
try {
5352
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
@@ -58,7 +57,7 @@ void setUp() throws IllegalAccessException, IOException {
5857
}
5958

6059
@AfterEach
61-
void cleanUp() throws IOException{
60+
void cleanUp() throws IOException {
6261
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
6362
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
6463
}
@@ -70,7 +69,7 @@ void shouldLogInJsonFormat() {
7069

7170
File logFile = new File("target/logfile.json");
7271
assertThat(contentOf(logFile)).contains(
73-
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"function_memory_size\":1024,\"function_name\":\"testFunction\",\"function_request_id\":\"RequestId\",\"function_version\":\"1\",\"service\":\"testLog4j\",\"timestamp\":")
72+
"{\"level\":\"DEBUG\",\"message\":\"Test debug event\",\"cold_start\":true,\"function_arn\":\"arn:aws:lambda:us-east-1:123456789012:function:test\",\"function_memory_size\":128,\"function_name\":\"test-function\",\"function_request_id\":\"test-request-id\",\"function_version\":\"1\",\"service\":\"testLog4j\",\"timestamp\":")
7473
.contains("\"xray_trace_id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
7574
}
7675

@@ -81,15 +80,7 @@ void shouldLogInEcsFormat() {
8180

8281
File logFile = new File("target/ecslogfile.json");
8382
assertThat(contentOf(logFile)).contains(
84-
"\"ecs.version\":\"1.2.0\",\"log.level\":\"DEBUG\",\"message\":\"Test debug event\",\"service.name\":\"testLog4j\",\"service.version\":\"1\",\"log.logger\":\"software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled\",\"process.thread.name\":\"main\",\"cloud.provider\":\"aws\",\"cloud.service.name\":\"lambda\",\"cloud.region\":\"eu-central-1\",\"cloud.account.id\":\"012345678910\",\"faas.id\":\"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1\",\"faas.name\":\"testFunction\",\"faas.version\":\"1\",\"faas.memory\":1024,\"faas.execution\":\"RequestId\",\"faas.coldstart\":true,\"trace.id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
83+
"\"ecs.version\":\"1.2.0\",\"log.level\":\"DEBUG\",\"message\":\"Test debug event\",\"service.name\":\"testLog4j\",\"service.version\":\"1\",\"log.logger\":\"software.amazon.lambda.powertools.logging.internal.handler.PowertoolsLogEnabled\",\"process.thread.name\":\"main\",\"cloud.provider\":\"aws\",\"cloud.service.name\":\"lambda\",\"cloud.region\":\"eu-central-1\",\"cloud.account.id\":\"123456789012\",\"faas.id\":\"arn:aws:lambda:us-east-1:123456789012:function:test\",\"faas.name\":\"test-function\",\"faas.version\":\"1\",\"faas.memory\":128,\"faas.execution\":\"test-request-id\",\"faas.coldstart\":true,\"trace.id\":\"1-63441c4a-abcdef012345678912345678\",\"myKey\":\"myValue\"}\n");
8584
}
8685

87-
private void setupContext() {
88-
when(context.getFunctionName()).thenReturn("testFunction");
89-
when(context.getInvokedFunctionArn()).thenReturn(
90-
"arn:aws:lambda:eu-west-1:012345678910:function:testFunction:1");
91-
when(context.getFunctionVersion()).thenReturn("1");
92-
when(context.getMemoryLimitInMB()).thenReturn(1024);
93-
when(context.getAwsRequestId()).thenReturn("RequestId");
94-
}
9586
}

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolverArgumentsTest.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.assertj.core.api.Assertions.contentOf;
19-
import static org.mockito.Mockito.when;
20-
import static org.mockito.MockitoAnnotations.openMocks;
2119

22-
import com.amazonaws.services.lambda.runtime.Context;
23-
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
2420
import java.io.File;
2521
import java.io.IOException;
2622
import java.nio.channels.FileChannel;
@@ -29,27 +25,29 @@
2925
import java.nio.file.StandardOpenOption;
3026
import java.util.Arrays;
3127
import java.util.Collections;
28+
3229
import org.junit.jupiter.api.AfterEach;
3330
import org.junit.jupiter.api.BeforeEach;
3431
import org.junit.jupiter.api.Order;
3532
import org.junit.jupiter.api.Test;
36-
import org.mockito.Mock;
3733
import org.slf4j.MDC;
3834

35+
import com.amazonaws.services.lambda.runtime.Context;
36+
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
37+
38+
import software.amazon.lambda.powertools.common.stubs.TestLambdaContext;
3939
import software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields;
4040
import software.amazon.lambda.powertools.logging.internal.handler.PowertoolsArguments;
4141

4242
@Order(2)
4343
class PowertoolsResolverArgumentsTest {
4444

45-
@Mock
4645
private Context context;
4746

4847
@BeforeEach
4948
void setUp() throws IOException {
50-
openMocks(this);
5149
MDC.clear();
52-
setupContext();
50+
context = new TestLambdaContext();
5351

5452
try {
5553
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
@@ -61,7 +59,7 @@ void setUp() throws IOException {
6159

6260
@AfterEach
6361
void cleanUp() throws IOException {
64-
//Make sure file is cleaned up before running full stack logging regression
62+
// Make sure file is cleaned up before running full stack logging regression
6563
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
6664
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
6765
}
@@ -74,7 +72,7 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
7472
msg.setMessageId("1212abcd");
7573
msg.setBody("plop");
7674
msg.setEventSource("eb");
77-
msg.setAwsRegion("eu-west-1");
75+
msg.setAwsRegion("us-east-1");
7876
SQSEvent.MessageAttribute attribute = new SQSEvent.MessageAttribute();
7977
attribute.setStringListValues(Arrays.asList("val1", "val2", "val3"));
8078
msg.setMessageAttributes(Collections.singletonMap("keyAttribute", attribute));
@@ -86,7 +84,7 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
8684
File logFile = new File("target/logfile.json");
8785
assertThat(contentOf(logFile))
8886
.contains(
89-
"\"input\":{\"awsRegion\":\"eu-west-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
87+
"\"input\":{\"awsRegion\":\"us-east-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
9088
.contains("\"message\":\"1212abcd\"")
9189
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
9290
// Reserved keys should be ignored
@@ -106,7 +104,7 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
106104
msg.setMessageId("1212abcd");
107105
msg.setBody("plop");
108106
msg.setEventSource("eb");
109-
msg.setAwsRegion("eu-west-1");
107+
msg.setAwsRegion("us-east-1");
110108
SQSEvent.MessageAttribute attribute = new SQSEvent.MessageAttribute();
111109
attribute.setStringListValues(Arrays.asList("val1", "val2", "val3"));
112110
msg.setMessageAttributes(Collections.singletonMap("keyAttribute", attribute));
@@ -118,7 +116,7 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
118116
File logFile = new File("target/logfile.json");
119117
assertThat(contentOf(logFile))
120118
.contains(
121-
"\"input\":{\"awsRegion\":\"eu-west-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
119+
"\"input\":{\"awsRegion\":\"us-east-1\",\"body\":\"plop\",\"eventSource\":\"eb\",\"messageAttributes\":{\"keyAttribute\":{\"stringListValues\":[\"val1\",\"val2\",\"val3\"]}},\"messageId\":\"1212abcd\"}")
122120
.contains("\"message\":\"1212abcd\"")
123121
.contains("\"message\":\"Message body = plop and id = \\\"1212abcd\\\"\"");
124122

@@ -131,11 +129,4 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
131129
});
132130
}
133131

134-
private void setupContext() {
135-
when(context.getFunctionName()).thenReturn("testFunction");
136-
when(context.getInvokedFunctionArn()).thenReturn("testArn");
137-
when(context.getFunctionVersion()).thenReturn("1");
138-
when(context.getMemoryLimitInMB()).thenReturn(10);
139-
when(context.getAwsRequestId()).thenReturn("RequestId");
140-
}
141132
}

0 commit comments

Comments
 (0)