Skip to content

Commit 93537b6

Browse files
committed
Remove mockito from powertools-logging.
1 parent d2e4ef5 commit 93537b6

File tree

2 files changed

+42
-76
lines changed

2 files changed

+42
-76
lines changed

powertools-logging/pom.xml

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
-->
1515

1616
<project xmlns="http://maven.apache.org/POM/4.0.0"
17-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1919
<modelVersion>4.0.0</modelVersion>
2020

2121
<parent>
@@ -67,11 +67,6 @@
6767
<artifactId>junit-jupiter-engine</artifactId>
6868
<scope>test</scope>
6969
</dependency>
70-
<dependency>
71-
<groupId>org.mockito</groupId>
72-
<artifactId>mockito-core</artifactId>
73-
<scope>test</scope>
74-
</dependency>
7570
<dependency>
7671
<groupId>org.slf4j</groupId>
7772
<artifactId>slf4j-simple</artifactId>
@@ -112,25 +107,26 @@
112107
<artifactId>jsonassert</artifactId>
113108
<scope>test</scope>
114109
</dependency>
110+
<dependency>
111+
<groupId>software.amazon.lambda</groupId>
112+
<artifactId>powertools-common</artifactId>
113+
<version>${project.version}</version>
114+
<type>test-jar</type>
115+
<scope>test</scope>
116+
</dependency>
115117
</dependencies>
116118
<profiles>
117119
<profile>
118120
<id>generate-graalvm-files</id>
119-
<dependencies>
120-
<dependency>
121-
<groupId>org.mockito</groupId>
122-
<artifactId>mockito-subclass</artifactId>
123-
<scope>test</scope>
124-
</dependency>
125-
</dependencies>
126121
<build>
127122
<plugins>
128123
<plugin>
129124
<groupId>org.apache.maven.plugins</groupId>
130125
<artifactId>maven-surefire-plugin</artifactId>
131126
<version>3.5.3</version>
132127
<configuration>
133-
<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,experimental-class-define-support
128+
<argLine>-Dorg.graalvm.nativeimage.imagecode=agent
129+
-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-logging,experimental-class-define-support
134130
--add-opens java.base/java.util=ALL-UNNAMED
135131
--add-opens java.base/java.lang=ALL-UNNAMED
136132
</argLine>
@@ -141,19 +137,12 @@
141137
</profile>
142138
<profile>
143139
<id>graalvm-native</id>
144-
<dependencies>
145-
<dependency>
146-
<groupId>org.mockito</groupId>
147-
<artifactId>mockito-subclass</artifactId>
148-
<scope>test</scope>
149-
</dependency>
150-
</dependencies>
151140
<build>
152141
<plugins>
153142
<plugin>
154143
<groupId>org.graalvm.buildtools</groupId>
155144
<artifactId>native-maven-plugin</artifactId>
156-
<version>0.11.0</version> <!-- or newer version -->
145+
<version>0.11.0</version>
157146
<extensions>true</extensions>
158147
<executions>
159148
<execution>
@@ -171,28 +160,11 @@
171160
<buildArg>--add-opens java.base/java.lang=ALL-UNNAMED</buildArg>
172161
<buildArg>--no-fallback</buildArg>
173162
<buildArg>-Dorg.graalvm.nativeimage.imagecode=agent</buildArg>
174-
<buildArg>-H:ClassInitialization=net.bytebuddy.ClassFileVersion:rerun</buildArg>
175-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.dispatcher.JavaDispatcher:rerun</buildArg>
176-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.Invoker$Dispatcher:rerun</buildArg>
177-
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.GraalImageCode:rerun</buildArg>
178163
<buildArg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger</buildArg>
179164
<buildArg>--initialize-at-build-time=org.slf4j.LoggerFactory</buildArg>
180165
<buildArg>--initialize-at-build-time=org.junit.Ignore</buildArg>
181166
<buildArg>--initialize-at-build-time=java.lang.annotation.Annotation</buildArg>
182167
<buildArg>--initialize-at-build-time=org.junit.runners.model.FrameworkField</buildArg>
183-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
184-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic</buildArg>
185-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$1</buildArg>
186-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Argument$BindingMechanic$2</buildArg>
187-
<buildArg>--initialize-at-build-time=net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader</buildArg>
188-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable</buildArg>
189-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$AbstractBase</buildArg>
190-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.type.TypeDescription$ForLoadedType</buildArg>
191-
<buildArg>--initialize-at-build-time=net.bytebuddy.description.method.MethodDescription$ForLoadedMethod</buildArg>
192-
<buildArg>--initialize-at-build-time=net.bytebuddy.implementation.bind.annotation.Super$Instantiation$2</buildArg>
193-
<buildArg>
194-
--trace-class-initialization=net.bytebuddy.description.type.TypeDescription$ForLoadedType,net.bytebuddy.description.method.MethodDescription$ForLoadedMethod,net.bytebuddy.description.method.MethodDescription$InDefinedShape$AbstractBase$ForLoadedExecutable
195-
</buildArg>
196168
<buildArg>--verbose</buildArg>
197169
</buildArgs>
198170
</configuration>

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspectTest.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static org.apache.commons.lang3.reflect.FieldUtils.writeStaticField;
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.contentOf;
21-
import static org.mockito.Mockito.when;
22-
import static org.mockito.MockitoAnnotations.openMocks;
2321
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_ARN;
2422
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_COLD_START;
2523
import static software.amazon.lambda.powertools.logging.internal.PowertoolsLoggedFields.FUNCTION_MEMORY_SIZE;
@@ -54,7 +52,6 @@
5452
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
5553
import org.junitpioneer.jupiter.SetEnvironmentVariable;
5654
import org.junitpioneer.jupiter.SetSystemProperty;
57-
import org.mockito.Mock;
5855
import org.slf4j.Logger;
5956
import org.slf4j.LoggerFactory;
6057
import org.slf4j.MDC;
@@ -72,6 +69,7 @@
7269
import com.fasterxml.jackson.databind.ObjectMapper;
7370

7471
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
72+
import software.amazon.lambda.powertools.common.stubs.TestLambdaContext;
7573
import software.amazon.lambda.powertools.logging.argument.StructuredArgument;
7674
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogAlbCorrelationId;
7775
import software.amazon.lambda.powertools.logging.handlers.PowertoolsLogApiGatewayHttpApiCorrelationId;
@@ -99,15 +97,13 @@ class LambdaLoggingAspectTest {
9997
private RequestStreamHandler requestStreamHandler;
10098
private RequestHandler<Object, Object> requestHandler;
10199

102-
@Mock
103100
private Context context;
104101

105102
@BeforeEach
106103
void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException {
107-
openMocks(this);
108104
MDC.clear();
109105
writeStaticField(LambdaHandlerProcessor.class, "IS_COLD_START", null, true);
110-
setupContext();
106+
context = new TestLambdaContext();
111107
requestHandler = new PowertoolsLogEnabled();
112108
requestStreamHandler = new PowertoolsLogEnabledForStream();
113109
resetLogLevel(Level.INFO);
@@ -124,7 +120,7 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
124120

125121
@AfterEach
126122
void cleanUp() throws IOException {
127-
//Make sure file is cleaned up before running full stack logging regression
123+
// Make sure file is cleaned up before running full stack logging regression
128124
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
129125
}
130126

@@ -224,7 +220,8 @@ void shouldLogWarnWhenPowertoolsLevelEnvVarIsWarnAndLambdaLevelVarIsInfo() throw
224220
assertThat(LOG.isInfoEnabled()).isFalse();
225221
assertThat(LOG.isWarnEnabled()).isTrue();
226222
File logFile = new File("target/logfile.json");
227-
assertThat(contentOf(logFile)).doesNotContain(" does not match AWS Lambda Advanced Logging Controls minimum log level");
223+
assertThat(contentOf(logFile))
224+
.doesNotContain(" does not match AWS Lambda Advanced Logging Controls minimum log level");
228225
}
229226

230227
@Test
@@ -241,7 +238,8 @@ void shouldLogInfoWhenPowertoolsLevelEnvVarIsInfoAndLambdaLevelVarIsWarn() throw
241238
assertThat(LOG.isInfoEnabled()).isTrue();
242239
File logFile = new File("target/logfile.json");
243240
// should log a warning as powertools level is lower than lambda level
244-
assertThat(contentOf(logFile)).contains("Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them.");
241+
assertThat(contentOf(logFile)).contains(
242+
"Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them.");
245243
}
246244

247245
@Test
@@ -265,11 +263,11 @@ void shouldSetLambdaContextWhenEnabled() {
265263

266264
assertThat(MDC.getCopyOfContextMap())
267265
.hasSize(EXPECTED_CONTEXT_SIZE)
268-
.containsEntry(FUNCTION_ARN.getName(), "testArn")
269-
.containsEntry(FUNCTION_MEMORY_SIZE.getName(), "10")
266+
.containsEntry(FUNCTION_ARN.getName(), "arn:aws:lambda:us-east-1:123456789012:function:test")
267+
.containsEntry(FUNCTION_MEMORY_SIZE.getName(), "128")
270268
.containsEntry(FUNCTION_VERSION.getName(), "1")
271-
.containsEntry(FUNCTION_NAME.getName(), "testFunction")
272-
.containsEntry(FUNCTION_REQUEST_ID.getName(), "RequestId")
269+
.containsEntry(FUNCTION_NAME.getName(), "test-function")
270+
.containsEntry(FUNCTION_REQUEST_ID.getName(), "test-request-id")
273271
.containsKey(FUNCTION_COLD_START.getName())
274272
.containsKey(SERVICE.getName());
275273
}
@@ -278,30 +276,30 @@ void shouldSetLambdaContextWhenEnabled() {
278276
void shouldSetLambdaContextForStreamHandlerWhenEnabled() throws IOException {
279277
requestStreamHandler = new PowertoolsLogEnabledForStream();
280278

281-
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(),
279+
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[] {}), new ByteArrayOutputStream(),
282280
context);
283281

284282
assertThat(MDC.getCopyOfContextMap())
285283
.hasSize(EXPECTED_CONTEXT_SIZE)
286-
.containsEntry(FUNCTION_ARN.getName(), "testArn")
287-
.containsEntry(FUNCTION_MEMORY_SIZE.getName(), "10")
284+
.containsEntry(FUNCTION_ARN.getName(), "arn:aws:lambda:us-east-1:123456789012:function:test")
285+
.containsEntry(FUNCTION_MEMORY_SIZE.getName(), "128")
288286
.containsEntry(FUNCTION_VERSION.getName(), "1")
289-
.containsEntry(FUNCTION_NAME.getName(), "testFunction")
290-
.containsEntry(FUNCTION_REQUEST_ID.getName(), "RequestId")
287+
.containsEntry(FUNCTION_NAME.getName(), "test-function")
288+
.containsEntry(FUNCTION_REQUEST_ID.getName(), "test-request-id")
291289
.containsKey(FUNCTION_COLD_START.getName())
292290
.containsKey(SERVICE.getName());
293291
}
294292

295293
@Test
296294
void shouldSetColdStartFlagOnFirstCallNotOnSecondCall() throws IOException {
297-
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(),
295+
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[] {}), new ByteArrayOutputStream(),
298296
context);
299297

300298
assertThat(MDC.getCopyOfContextMap())
301299
.hasSize(EXPECTED_CONTEXT_SIZE)
302300
.containsEntry(FUNCTION_COLD_START.getName(), "true");
303301

304-
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(),
302+
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[] {}), new ByteArrayOutputStream(),
305303
context);
306304

307305
assertThat(MDC.getCopyOfContextMap())
@@ -346,7 +344,7 @@ void shouldLogDebugWhenSamplingEqualsOne() {
346344
}
347345

348346
@Test
349-
void shouldLogDebugWhenSamplingEnvVarEqualsOne() throws IllegalAccessException {
347+
void shouldLogDebugWhenSamplingEnvVarEqualsOne() {
350348
// GIVEN
351349
LoggingConstants.POWERTOOLS_SAMPLING_RATE = "1";
352350
PowertoolsLogEnabled handler = new PowertoolsLogEnabled();
@@ -360,7 +358,7 @@ void shouldLogDebugWhenSamplingEnvVarEqualsOne() throws IllegalAccessException {
360358
}
361359

362360
@Test
363-
void shouldNotLogDebugWhenSamplingEnvVarIsTooBig() throws IllegalAccessException {
361+
void shouldNotLogDebugWhenSamplingEnvVarIsTooBig() {
364362
// GIVEN
365363
LoggingConstants.POWERTOOLS_SAMPLING_RATE = "42";
366364

@@ -378,7 +376,7 @@ void shouldNotLogDebugWhenSamplingEnvVarIsInvalid() {
378376
LoggingConstants.POWERTOOLS_SAMPLING_RATE = "NotANumber";
379377

380378
// WHEN
381-
requestHandler.handleRequest(new Object(), context);
379+
requestHandler.handleRequest(new Object(), context);
382380

383381
// THEN
384382
File logFile = new File("target/logfile.json");
@@ -509,7 +507,7 @@ void shouldNotLogEventForHandlerWhenEnvVariableSetToFalse() {
509507
requestHandler.handleRequest(singletonList("ListOfOneElement"), context);
510508

511509
// THEN
512-
TestLogger logger = (TestLogger) ((PowertoolsLogEventEnvVar)requestHandler).getLogger();
510+
TestLogger logger = (TestLogger) ((PowertoolsLogEventEnvVar) requestHandler).getLogger();
513511
assertThat(logger.getArguments()).isNull();
514512
}
515513

@@ -521,7 +519,8 @@ void shouldLogEventForStreamHandler() throws IOException {
521519
Map<String, String> map = Collections.singletonMap("key", "value");
522520

523521
// WHEN
524-
requestStreamHandler.handleRequest(new ByteArrayInputStream(new ObjectMapper().writeValueAsBytes(map)), output, context);
522+
requestStreamHandler.handleRequest(new ByteArrayInputStream(new ObjectMapper().writeValueAsBytes(map)), output,
523+
context);
525524

526525
// THEN
527526
assertThat(new String(output.toByteArray(), StandardCharsets.UTF_8))
@@ -586,7 +585,8 @@ void shouldLogResponseForStreamHandler() throws IOException {
586585
String input = "<user><firstName>Bob</firstName><lastName>The Sponge</lastName></user>";
587586

588587
// WHEN
589-
requestStreamHandler.handleRequest(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), output, context);
588+
requestStreamHandler.handleRequest(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), output,
589+
context);
590590

591591
// THEN
592592
assertThat(new String(output.toByteArray(), StandardCharsets.UTF_8))
@@ -737,7 +737,8 @@ void testMultipleLoggingManagers_shouldWarnAndSelectFirstOne() throws Unsupporte
737737
String output = outputStream.toString("UTF-8");
738738
assertThat(output)
739739
.contains("WARN. Multiple LoggingManagers were found on the classpath")
740-
.contains("WARN. Make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies")
740+
.contains(
741+
"WARN. Make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies")
741742
.contains("WARN. Using the first LoggingManager found on the classpath: [" + list.get(0) + "]");
742743
}
743744

@@ -757,19 +758,12 @@ void testNoLoggingManagers_shouldWarnAndCreateDefault() throws UnsupportedEncodi
757758
assertThat(output)
758759
.contains("ERROR. No LoggingManager was found on the classpath")
759760
.contains("ERROR. Applying default LoggingManager: POWERTOOLS_LOG_LEVEL variable is ignored")
760-
.contains("ERROR. Make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies");
761+
.contains(
762+
"ERROR. Make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies");
761763

762764
assertThat(loggingManager).isExactlyInstanceOf(DefautlLoggingManager.class);
763765
}
764766

765-
private void setupContext() {
766-
when(context.getFunctionName()).thenReturn("testFunction");
767-
when(context.getInvokedFunctionArn()).thenReturn("testArn");
768-
when(context.getFunctionVersion()).thenReturn("1");
769-
when(context.getMemoryLimitInMB()).thenReturn(10);
770-
when(context.getAwsRequestId()).thenReturn("RequestId");
771-
}
772-
773767
private void resetLogLevel(Level level)
774768
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
775769
Method setLogLevels = LambdaLoggingAspect.class.getDeclaredMethod("setLogLevels", Level.class);

0 commit comments

Comments
 (0)