11
11
* the Apache License, Version 2.0 (the "License"); you may
12
12
* not use this file except in compliance with the License.
13
13
* You may obtain a copy of the License at
14
- *
14
+ *
15
15
* http://www.apache.org/licenses/LICENSE-2.0
16
- *
16
+ *
17
17
* Unless required by applicable law or agreed to in writing,
18
18
* software distributed under the License is distributed on an
19
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
27
27
28
28
import com .fasterxml .jackson .databind .JsonNode ;
29
29
import com .fasterxml .jackson .databind .ObjectMapper ;
30
+ import org .junit .jupiter .api .BeforeEach ;
30
31
import org .junit .jupiter .api .Test ;
31
32
32
33
import java .io .IOException ;
33
34
import java .time .Instant ;
34
35
import java .time .temporal .ChronoUnit ;
36
+ import java .util .ArrayList ;
37
+ import java .util .Iterator ;
38
+ import java .util .List ;
39
+ import java .util .Map ;
35
40
import java .util .stream .Collectors ;
36
41
import java .util .stream .StreamSupport ;
37
42
38
43
import static org .assertj .core .api .Assertions .assertThat ;
44
+ import static org .assertj .core .api .Assertions .assertThatCode ;
39
45
import static org .assertj .core .api .Assertions .within ;
40
46
41
47
public abstract class AbstractEcsLoggingTest {
42
48
43
49
protected ObjectMapper objectMapper = new ObjectMapper ();
50
+ private JsonNode spec ;
51
+
52
+ @ BeforeEach
53
+ final void setUpSpec () throws Exception {
54
+ spec = objectMapper .readTree (getClass ().getClassLoader ().getResource ("spec/spec.json" ));
55
+ }
44
56
45
57
@ Test
46
58
void testMetadata () throws Exception {
47
59
debug ("test" );
48
- assertThat (getLastLogLine ().get ("process.thread.name" ).textValue ()).isEqualTo (Thread .currentThread ().getName ());
49
- assertThat (getLastLogLine ().get ("service.name" ).textValue ()).isEqualTo ("test" );
50
- assertThat (Instant .parse (getLastLogLine ().get ("@timestamp" ).textValue ())).isCloseTo (Instant .now (), within (1 , ChronoUnit .MINUTES ));
51
- assertThat (getLastLogLine ().get ("log.level" ).textValue ()).isIn ("DEBUG" , "FINE" );
52
- assertThat (getLastLogLine ().get ("log.logger" )).isNotNull ();
53
- assertThat (getLastLogLine ().get ("event.dataset" ).textValue ()).isEqualTo ("testdataset.log" );
54
- assertThat (getLastLogLine ().get ("ecs.version" ).textValue ()).isEqualTo ("1.2.0" );
60
+ assertThat (getAndValidateLastLogLine ().get ("process.thread.name" ).textValue ()).isEqualTo (Thread .currentThread ().getName ());
61
+ assertThat (getAndValidateLastLogLine ().get ("service.name" ).textValue ()).isEqualTo ("test" );
62
+ assertThat (Instant .parse (getAndValidateLastLogLine ().get ("@timestamp" ).textValue ())).isCloseTo (Instant .now (), within (1 , ChronoUnit .MINUTES ));
63
+ assertThat (getAndValidateLastLogLine ().get ("log.level" ).textValue ()).isIn ("DEBUG" , "FINE" );
64
+ assertThat (getAndValidateLastLogLine ().get ("log.logger" )).isNotNull ();
65
+ assertThat (getAndValidateLastLogLine ().get ("event.dataset" ).textValue ()).isEqualTo ("testdataset.log" );
66
+ assertThat (getAndValidateLastLogLine ().get ("ecs.version" ).textValue ()).isEqualTo ("1.2.0" );
67
+ validateLog (getAndValidateLastLogLine ());
55
68
}
56
69
57
70
@ Test
58
71
void testSimpleLog () throws Exception {
59
72
debug ("test" );
60
- assertThat (getLastLogLine ().get ("message" ).textValue ()).isEqualTo ("test" );
73
+ assertThat (getAndValidateLastLogLine ().get ("message" ).textValue ()).isEqualTo ("test" );
74
+ }
75
+
76
+ void validateLog (JsonNode logLine ) {
77
+ Iterator <Map .Entry <String , JsonNode >> specFields = spec .get ("fields" ).fields ();
78
+ Iterator <String > iterator = logLine .fieldNames ();
79
+ List <String > logFieldNames = new ArrayList <>();
80
+ iterator .forEachRemaining (logFieldNames ::add );
81
+
82
+ while (specFields .hasNext ()) {
83
+ Map .Entry <String , JsonNode > specField = specFields .next ();
84
+ String specFieldName = specField .getKey ();
85
+ JsonNode specForField = specField .getValue ();
86
+ JsonNode fieldInLog = logLine .get (specFieldName );
87
+
88
+ validateRequiredField (logLine , specFieldName , specForField .get ("required" ).booleanValue ());
89
+ if (fieldInLog != null ) {
90
+ validateIndex (logLine , logFieldNames , specFieldName , specForField .get ("index" ));
91
+ validateType (fieldInLog , specForField .get ("type" ).textValue ());
92
+ }
93
+ }
94
+ }
95
+
96
+ private void validateRequiredField (JsonNode logLine , String specFieldName , boolean required ) {
97
+ if (required ) {
98
+ assertThat (logLine .get (specFieldName ))
99
+ .describedAs (logLine .toString ())
100
+ .isNotNull ();
101
+ }
102
+ }
103
+
104
+ private void validateIndex (JsonNode logLine , List <String > logFieldNames , String specFieldName , JsonNode index ) {
105
+ if (index != null ) {
106
+ assertThat (logFieldNames .get (index .intValue ()))
107
+ .describedAs (logLine .toString ())
108
+ .isEqualTo (specFieldName );
109
+ }
110
+ }
111
+
112
+ private void validateType (JsonNode fieldInLog , String type ) {
113
+ switch (type ) {
114
+ case "datetime" :
115
+ assertThatCode (() -> Instant .parse (fieldInLog .textValue ())).doesNotThrowAnyException ();
116
+ case "string" :
117
+ assertThat (fieldInLog .isTextual ()).isTrue ();
118
+ }
61
119
}
62
120
63
121
@ Test
@@ -75,22 +133,22 @@ void testSimpleParameterizedLog() throws Exception {
75
133
debug ("{} is not {}" , 1 , 2 );
76
134
}
77
135
78
- assertThat (getLastLogLine ().get ("message" ).textValue ()).isEqualTo ("1 is not 2" );
136
+ assertThat (getAndValidateLastLogLine ().get ("message" ).textValue ()).isEqualTo ("1 is not 2" );
79
137
}
80
138
81
139
@ Test
82
140
void testThreadContext () throws Exception {
83
141
if (putMdc ("foo" , "bar" )) {
84
142
debug ("test" );
85
- assertThat (getLastLogLine ().get ("foo" ).textValue ()).isEqualTo ("bar" );
143
+ assertThat (getAndValidateLastLogLine ().get ("foo" ).textValue ()).isEqualTo ("bar" );
86
144
}
87
145
}
88
146
89
147
@ Test
90
148
void testThreadContextStack () throws Exception {
91
149
if (putNdc ("foo" )) {
92
150
debug ("test" );
93
- assertThat (getLastLogLine ().get ("tags" ).iterator ().next ().textValue ()).isEqualTo ("foo" );
151
+ assertThat (getAndValidateLastLogLine ().get ("tags" ).iterator ().next ().textValue ()).isEqualTo ("foo" );
94
152
}
95
153
}
96
154
@@ -100,21 +158,21 @@ void testMdc() throws Exception {
100
158
putMdc ("span.id" , "foo" );
101
159
putMdc ("foo" , "bar" );
102
160
debug ("test" );
103
- assertThat (getLastLogLine ().get ("labels.transaction.id" )).isNull ();
104
- assertThat (getLastLogLine ().get ("transaction.id" ).textValue ())
161
+ assertThat (getAndValidateLastLogLine ().get ("labels.transaction.id" )).isNull ();
162
+ assertThat (getAndValidateLastLogLine ().get ("transaction.id" ).textValue ())
105
163
.isEqualTo ("0af7651916cd43dd8448eb211c80319c" );
106
- assertThat (getLastLogLine ().get ("span.id" ).textValue ()).isEqualTo ("foo" );
107
- assertThat (getLastLogLine ().get ("foo" ).textValue ()).isEqualTo ("bar" );
164
+ assertThat (getAndValidateLastLogLine ().get ("span.id" ).textValue ()).isEqualTo ("foo" );
165
+ assertThat (getAndValidateLastLogLine ().get ("foo" ).textValue ()).isEqualTo ("bar" );
108
166
}
109
167
}
110
168
111
169
@ Test
112
170
void testLogException () throws Exception {
113
171
error ("test" , new RuntimeException ("test" ));
114
- assertThat (getLastLogLine ().get ("log.level" ).textValue ()).isEqualTo ("ERROR" );
115
- assertThat (getLastLogLine ().get ("error.message" ).textValue ()).isEqualTo ("test" );
116
- assertThat (getLastLogLine ().get ("error.type" ).textValue ()).isEqualTo (RuntimeException .class .getName ());
117
- String stackTrace = StreamSupport .stream (getLastLogLine ().get ("error.stack_trace" ).spliterator (), false )
172
+ assertThat (getAndValidateLastLogLine ().get ("log.level" ).textValue ()).isEqualTo ("ERROR" );
173
+ assertThat (getAndValidateLastLogLine ().get ("error.message" ).textValue ()).isEqualTo ("test" );
174
+ assertThat (getAndValidateLastLogLine ().get ("error.type" ).textValue ()).isEqualTo (RuntimeException .class .getName ());
175
+ String stackTrace = StreamSupport .stream (getAndValidateLastLogLine ().get ("error.stack_trace" ).spliterator (), false )
118
176
.map (JsonNode ::textValue )
119
177
.collect (Collectors .joining ("\n " , "" , "\n " ));
120
178
assertThat (stackTrace ).contains ("at co.elastic.logging.AbstractEcsLoggingTest.testLogException" );
@@ -123,16 +181,16 @@ void testLogException() throws Exception {
123
181
@ Test
124
182
void testLogExceptionNullMessage () throws Exception {
125
183
error ("test" , new RuntimeException ());
126
- assertThat (getLastLogLine ().get ("error.message" )).isNull ();
127
- assertThat (getLastLogLine ().get ("error.type" ).textValue ()).isEqualTo (RuntimeException .class .getName ());
184
+ assertThat (getAndValidateLastLogLine ().get ("error.message" )).isNull ();
185
+ assertThat (getAndValidateLastLogLine ().get ("error.type" ).textValue ()).isEqualTo (RuntimeException .class .getName ());
128
186
}
129
187
130
188
@ Test
131
189
void testLogOrigin () throws Exception {
132
190
debug ("test" );
133
- assertThat (getLastLogLine ().get ("log.origin" ).get ("file.name" ).textValue ()).endsWith (".java" );
134
- assertThat (getLastLogLine ().get ("log.origin" ).get ("function" ).textValue ()).isEqualTo ("debug" );
135
- assertThat (getLastLogLine ().get ("log.origin" ).get ("file.line" ).intValue ()).isPositive ();
191
+ assertThat (getAndValidateLastLogLine ().get ("log.origin" ).get ("file.name" ).textValue ()).endsWith (".java" );
192
+ assertThat (getAndValidateLastLogLine ().get ("log.origin" ).get ("function" ).textValue ()).isEqualTo ("debug" );
193
+ assertThat (getAndValidateLastLogLine ().get ("log.origin" ).get ("file.line" ).intValue ()).isPositive ();
136
194
}
137
195
138
196
public boolean putMdc (String key , String value ) {
@@ -153,5 +211,11 @@ public ParameterizedLogSupport getParameterizedLogSettings() {
153
211
154
212
public abstract void error (String message , Throwable t );
155
213
214
+ public final JsonNode getAndValidateLastLogLine () throws IOException {
215
+ JsonNode log = getLastLogLine ();
216
+ validateLog (log );
217
+ return log ;
218
+ }
219
+
156
220
public abstract JsonNode getLastLogLine () throws IOException ;
157
221
}
0 commit comments