Skip to content

Commit af5d261

Browse files
committed
Convert tests matching on raw JSON strings to use Jackson
1 parent 5b27c90 commit af5d261

File tree

2 files changed

+56
-74
lines changed

2 files changed

+56
-74
lines changed

jboss-logmanager-ecs-formatter/src/test/java/co/elastic/logging/jboss/logmanager/EcsFormatterTest.java

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,23 @@
2424
*/
2525
package co.elastic.logging.jboss.logmanager;
2626

27-
import static org.assertj.core.api.Assertions.assertThat;
28-
29-
import java.io.PrintWriter;
30-
import java.time.Instant;
31-
import java.util.Map;
32-
27+
import com.fasterxml.jackson.databind.JsonNode;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
3329
import org.jboss.logmanager.ExtLogRecord;
3430
import org.jboss.logmanager.Level;
3531
import org.junit.jupiter.api.BeforeEach;
3632
import org.junit.jupiter.api.Test;
3733

34+
import java.io.PrintWriter;
35+
import java.time.Instant;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
3839
class EcsFormatterTest {
3940

4041
private final EcsFormatter formatter = new EcsFormatter();
41-
4242
private final ExtLogRecord record = new ExtLogRecord(Level.INFO, "Example Message", "ExampleLoggerClass");
43+
private final ObjectMapper objectMapper = new ObjectMapper();
4344

4445
@BeforeEach
4546
void setUp() {
@@ -53,35 +54,25 @@ void setUp() {
5354
}
5455

5556
@Test
56-
void testSingleNDCInformationLogging() {
57+
void testSingleNDCInformationLogging() throws Exception {
5758
record.setNdc("exampleTag");
58-
59-
assertThat(formatter.format(record)).isEqualTo("{" +
60-
"\"@timestamp\":\"1970-01-01T00:00:00.005Z\", " +
61-
"\"log.level\": \"INFO\", " +
62-
"\"message\":\"Example Message\", " +
63-
"\"process.thread.name\":\"ExampleThread\"," +
64-
"\"log.logger\":\"ExampleLogger\"," +
65-
"\"tags\":[\"exampleTag\"]" +
66-
"}\n");
59+
JsonNode result = objectMapper.readTree(formatter.format(record));
60+
assertThat(result.get("tags")).hasSize(1);
61+
assertThat(result.get("tags").get(0).textValue()).isEqualTo("exampleTag");
6762
}
6863

6964
@Test
70-
void testMultipleNDCInformationLogging() {
65+
void testMultipleNDCInformationLogging() throws Exception {
7166
record.setNdc("exampleTag1.exampleTag2");
7267

73-
assertThat(formatter.format(record)).isEqualTo("{" +
74-
"\"@timestamp\":\"1970-01-01T00:00:00.005Z\", " +
75-
"\"log.level\": \"INFO\", " +
76-
"\"message\":\"Example Message\", " +
77-
"\"process.thread.name\":\"ExampleThread\"," +
78-
"\"log.logger\":\"ExampleLogger\"," +
79-
"\"tags\":[\"exampleTag1\",\"exampleTag2\"]" +
80-
"}\n");
68+
JsonNode result = objectMapper.readTree(formatter.format(record));
69+
assertThat(result.get("tags")).hasSize(2);
70+
assertThat(result.get("tags").get(0).textValue()).isEqualTo("exampleTag1");
71+
assertThat(result.get("tags").get(1).textValue()).isEqualTo("exampleTag2");
8172
}
8273

8374
@Test
84-
void testExceptionLogging() {
75+
void testExceptionLogging() throws Exception {
8576
record.setThrown(new RuntimeException("Example Exception Message") {
8677
@Override
8778
public void printStackTrace(PrintWriter pw) {
@@ -90,16 +81,12 @@ public void printStackTrace(PrintWriter pw) {
9081
}
9182
});
9283

93-
assertThat(formatter.format(record).replace("\\r\\n", "\\n")).isEqualTo("{" +
94-
"\"@timestamp\":\"1970-01-01T00:00:00.005Z\", " +
95-
"\"log.level\": \"INFO\", " +
96-
"\"message\":\"Example Message\", " +
97-
"\"process.thread.name\":\"ExampleThread\"," +
98-
"\"log.logger\":\"ExampleLogger\"," +
99-
"\"error.type\":\"co.elastic.logging.jboss.logmanager.EcsFormatterTest$1\"," +
100-
"\"error.message\":\"Example Exception Message\"," +
101-
"\"error.stack_trace\":\"co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message\\n\\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)\\n\"" +
102-
"}\n");
84+
JsonNode result = objectMapper.readTree(formatter.format(record));
85+
assertThat(result.get("error.type").textValue()).isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1");
86+
assertThat(result.get("error.message").textValue()).isEqualTo("Example Exception Message");
87+
assertThat(result.get("error.stack_trace").textValue())
88+
.isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message\n" +
89+
"\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)\n");
10390
}
10491
}
10592

jul-ecs-formatter/src/test/java/co/elastic/logging/jul/EcsFormatterTest.java

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.logging.Level;
3131
import java.util.logging.LogRecord;
3232

33+
import com.fasterxml.jackson.databind.JsonNode;
34+
import com.fasterxml.jackson.databind.ObjectMapper;
3335
import org.junit.jupiter.api.BeforeEach;
3436
import org.junit.jupiter.api.Test;
3537

@@ -38,76 +40,69 @@ public class EcsFormatterTest {
3840
private final EcsFormatter formatter = new EcsFormatter();
3941

4042
private final LogRecord record = new LogRecord(Level.INFO, "Example Meesage");
43+
private final ObjectMapper objectMapper = new ObjectMapper();
4144

42-
@Test
43-
public void testFormatWithIncludeOriginFlag() {
45+
@BeforeEach
46+
void setUp() {
47+
record.setInstant(Instant.ofEpochMilli(5));
48+
record.setSourceClassName("ExampleClass");
49+
record.setSourceMethodName("exampleMethod");
50+
record.setThreadID(7);
51+
record.setLoggerName("ExampleLogger");
52+
}
4453

54+
@Test
55+
public void testFormatWithIncludeOriginFlag() throws Exception {
4556
formatter.setIncludeOrigin(true);
4657

4758
final String result = formatter.format(record);
4859

49-
assertThat(result).isEqualTo(
50-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7,\"log.logger\":\"ExampleLogger\",\"log.origin\":{\"file.name\":\"ExampleClass.java\",\"function\":\"exampleMethod\"}}\n");
60+
assertThat(objectMapper.readTree(result).get("log.origin").get("file.name").textValue()).isEqualTo("ExampleClass.java");
61+
assertThat(objectMapper.readTree(result).get("log.origin").get("function").textValue()).isEqualTo("exampleMethod");
5162
}
5263

5364
@Test
54-
public void testFormatWithoutIncludeOriginFlag() {
55-
56-
final String result = formatter.format(record);
57-
58-
assertThat(result).isEqualTo(
59-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7,\"log.logger\":\"ExampleLogger\"}\n");
65+
public void testFormatWithoutIncludeOriginFlag() throws Exception {
66+
final JsonNode result = objectMapper.readTree(formatter.format(record));
67+
assertThat(result.get("log.origin")).isNull();
6068
}
6169

6270
@Test
63-
public void testFormatWithoutLoggerName() {
71+
public void testFormatWithoutLoggerName() throws Exception {
6472
record.setLoggerName(null);
6573

66-
final String result = formatter.format(record);
74+
final JsonNode result = objectMapper.readTree(formatter.format(record));
6775

68-
assertThat(result).isEqualTo(
69-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7}\n");
76+
assertThat(result.get("log.logger")).isNull();
7077
}
7178

7279
@Test
73-
public void testFormatWithEmptyLoggerName() {
80+
public void testFormatWithEmptyLoggerName() throws Exception {
7481
record.setLoggerName("");
7582

76-
final String result = formatter.format(record);
83+
final JsonNode result = objectMapper.readTree(formatter.format(record));
7784

78-
assertThat(result).isEqualTo(
79-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7,\"log.logger\":\"\"}\n");
85+
assertThat(result.get("log.logger").textValue()).isEmpty();
8086
}
8187

8288
@Test
83-
public void testFormatWithInnerClassName() {
89+
public void testFormatWithInnerClassName() throws Exception {
8490
formatter.setIncludeOrigin(true);
8591
record.setSourceClassName("test.ExampleClass$InnerClass");
8692

87-
final String result = formatter.format(record);
88-
89-
assertThat(result).isEqualTo(
90-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7,\"log.logger\":\"ExampleLogger\",\"log.origin\":{\"file.name\":\"ExampleClass.java\",\"function\":\"exampleMethod\"}}\n");
93+
JsonNode result = objectMapper.readTree(formatter.format(record));
94+
assertThat(result.get("log.origin").get("file.name").textValue()).isEqualTo("ExampleClass.java");
95+
assertThat(result.get("log.origin").get("function").textValue()).isEqualTo("exampleMethod");
9196
}
9297

9398
@Test
94-
public void testFormatWithInvalidClassName() {
99+
public void testFormatWithInvalidClassName() throws Exception {
95100
formatter.setIncludeOrigin(true);
96101
record.setSourceClassName("$test.ExampleClass");
97102

98-
final String result = formatter.format(record);
99-
100-
assertThat(result).isEqualTo(
101-
"{\"@timestamp\":\"1970-01-01T00:00:00.005Z\", \"log.level\": \"INFO\", \"message\":\"Example Meesage\", \"process.thread.id\":7,\"log.logger\":\"ExampleLogger\",\"log.origin\":{\"file.name\":\"<Unknown>\",\"function\":\"exampleMethod\"}}\n");
102-
}
103-
104-
@BeforeEach
105-
void setUp() {
106-
record.setInstant(Instant.ofEpochMilli(5));
107-
record.setSourceClassName("ExampleClass");
108-
record.setSourceMethodName("exampleMethod");
109-
record.setThreadID(7);
110-
record.setLoggerName("ExampleLogger");
103+
JsonNode result = objectMapper.readTree(formatter.format(record));
104+
assertThat(result.get("log.origin").get("file.name").textValue()).isEqualTo("<Unknown>");
105+
assertThat(result.get("log.origin").get("function").textValue()).isEqualTo("exampleMethod");
111106
}
112107

113108
}

0 commit comments

Comments
 (0)