-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from dm-drogeriemarkt/keyvalue
keyValue matcher, bump versions
- Loading branch information
Showing
20 changed files
with
514 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/target/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>de.acme</groupId> | ||
<artifactId>test-project-without-logstash</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT</name> | ||
<url>https://opensource.org/licenses/MIT</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<encoding>UTF-8</encoding> | ||
<log-capture.version>3.4.1-SNAPSHOT</log-capture.version> | ||
|
||
<lombok.version>1.18.22</lombok.version> | ||
<logback.version>1.2.11</logback.version> | ||
<junit.version>5.8.2</junit.version> | ||
<assertj-core.version>3.22.0</assertj-core.version> | ||
|
||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> | ||
<maven-compiler-plugin.version>3.9.0</maven-compiler-plugin.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${lombok.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.dm.infrastructure</groupId> | ||
<artifactId>log-capture</artifactId> | ||
<version>${log-capture.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj-core.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven-compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
<encoding>${encoding}</encoding> | ||
<showDeprecation>true</showDeprecation> | ||
<showWarnings>true</showWarnings> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>${maven-surefire-plugin.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
34 changes: 34 additions & 0 deletions
34
project-without-logstash/src/test/java/com/acme/LogCaptureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.acme; | ||
|
||
import de.dm.infrastructure.logcapture.LogCapture; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import static de.dm.infrastructure.logcapture.ExpectedKeyValue.keyValue; | ||
import static de.dm.infrastructure.logcapture.LogExpectation.info; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@Slf4j | ||
public class LogCaptureTest { | ||
@RegisterExtension | ||
LogCapture logCapture = LogCapture.forCurrentPackage(); | ||
|
||
@Test | ||
void assertionWorksDespiteLogstashNotBeingInTheClasspath() { | ||
log.info("hello"); | ||
logCapture.assertLogged(info("hello")); | ||
} | ||
|
||
@Test | ||
void errorMessageForKeyValueAssertion() { | ||
log.info("hello"); | ||
|
||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> | ||
logCapture.assertLogged(info("hello", keyValue("key", "value")))); | ||
|
||
assertThat(thrown).hasMessage("keyValue cannot be used for log assertions if logstash-logback-encoder " + | ||
"that provides StructuredArguments.keyValue(...) is not in the classpath."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/de/dm/infrastructure/logcapture/ExpectedKeyValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package de.dm.infrastructure.logcapture; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* define expected StructuredArgument.keyValue(...) from logstash with this matcher | ||
*/ | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ExpectedKeyValue implements LogEventMatcher { | ||
static final String LOGSTASH_MARKER_CLASS = "net.logstash.logback.marker.SingleFieldAppendingMarker"; | ||
private final String expectedKey; | ||
private final String expectedValue; | ||
|
||
@Override | ||
public boolean matches(LoggedEvent loggedEvent) { | ||
failIfLogstashIsNotInClasspath(); | ||
return ExpectedKeyValueLogstashDelegate.matches(loggedEvent, expectedKey, expectedValue); | ||
} | ||
|
||
@Override | ||
public String getNonMatchingErrorMessage(LoggedEvent loggedEvent) { | ||
failIfLogstashIsNotInClasspath(); | ||
return ExpectedKeyValueLogstashDelegate.getNonMatchingErrorMessage(loggedEvent, expectedKey, expectedValue); | ||
} | ||
|
||
@Override | ||
public String getMatcherTypeDescription() { | ||
return "key-value content"; | ||
} | ||
|
||
@Override | ||
public String getMatcherDetailDescription() { | ||
return format("keyValue content with key: \"%s\" and value: \"%s\"", expectedKey, expectedValue); | ||
} | ||
|
||
/** | ||
* use this in a log expectation to verify that something has been logged with a keyValue argument from logstashg's StructuredArgument | ||
* | ||
* @param expectedKey expected key | ||
* @param expectedValue expected value | ||
* | ||
* @return expected keyValue to use in log expectation | ||
*/ | ||
public static ExpectedKeyValue keyValue(String expectedKey, String expectedValue) { | ||
return new ExpectedKeyValue(expectedKey, expectedValue); | ||
} | ||
|
||
/** | ||
* use this in a log expectation to verify that something has been logged with a keyValue argument from logstashg's StructuredArgument | ||
* | ||
* @param expectedKey expected key | ||
* @param expectedValue expected value | ||
* | ||
* @return expected keyValue to use in log expectation | ||
*/ | ||
public static ExpectedKeyValue keyValue(String expectedKey, int expectedValue) { | ||
return new ExpectedKeyValue(expectedKey, String.valueOf(expectedValue)); | ||
} | ||
|
||
/** | ||
* use this in a log expectation to verify that something has been logged with a keyValue argument from logstashg's StructuredArgument | ||
* | ||
* @param expectedKey expected key | ||
* @param expectedValue expected value | ||
* | ||
* @return expected keyValue to use in log expectation | ||
*/ | ||
public static ExpectedKeyValue keyValue(String expectedKey, long expectedValue) { | ||
return new ExpectedKeyValue(expectedKey, String.valueOf(expectedValue)); | ||
} | ||
|
||
private void failIfLogstashIsNotInClasspath() { | ||
try { | ||
Class.forName(LOGSTASH_MARKER_CLASS, false, getClass().getClassLoader()); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalArgumentException("keyValue cannot be used for log assertions if " + | ||
"logstash-logback-encoder that provides StructuredArguments.keyValue(...) is not in the classpath."); | ||
} | ||
} | ||
} |
Oops, something went wrong.