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
40
40
41
41
class EcsJsonSerializerTest {
42
42
43
+ private static final String ERROR_TYPE = "error.type" ;
44
+ private static final String ERROR_STACK_TRACE = "error.stack_trace" ;
45
+ private static final String ERROR_MESSAGE = "error.message" ;
46
+ private ObjectMapper objectMapper = new ObjectMapper ();
47
+
43
48
@ Test
44
49
void serializeExceptionAsString () throws IOException {
45
50
Exception exception = new Exception ("foo" );
46
51
StringBuilder jsonBuilder = new StringBuilder ();
47
52
jsonBuilder .append ('{' );
48
53
EcsJsonSerializer .serializeException (jsonBuilder , exception , false );
49
54
jsonBuilder .append ('}' );
50
- JsonNode jsonNode = new ObjectMapper () .readTree (jsonBuilder .toString ());
55
+ JsonNode jsonNode = objectMapper .readTree (jsonBuilder .toString ());
51
56
52
- assertThat (jsonNode .get ("error.type" ).textValue ()).isEqualTo (exception .getClass ().getName ());
53
- assertThat (jsonNode .get ("error.message" ).textValue ()).isEqualTo ("foo" );
57
+ assertThat (jsonNode .get (ERROR_TYPE ).textValue ()).isEqualTo (exception .getClass ().getName ());
58
+ assertThat (jsonNode .get (ERROR_MESSAGE ).textValue ()).isEqualTo ("foo" );
54
59
StringWriter stringWriter = new StringWriter ();
55
60
exception .printStackTrace (new PrintWriter (stringWriter ));
56
- assertThat (jsonNode .get ("error.stack_trace" ).textValue ()).isEqualTo (stringWriter .toString ());
61
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).textValue ()).isEqualTo (stringWriter .toString ());
57
62
}
58
63
59
64
@ Test
@@ -102,13 +107,13 @@ void serializeExceptionAsArray() throws IOException {
102
107
EcsJsonSerializer .serializeException (jsonBuilder , exception , true );
103
108
jsonBuilder .append ('}' );
104
109
System .out .println (jsonBuilder );
105
- JsonNode jsonNode = new ObjectMapper () .readTree (jsonBuilder .toString ());
110
+ JsonNode jsonNode = objectMapper .readTree (jsonBuilder .toString ());
106
111
107
- assertThat (jsonNode .get ("error.type" ).textValue ()).isEqualTo (exception .getClass ().getName ());
108
- assertThat (jsonNode .get ("error.message" ).textValue ()).isEqualTo ("foo" );
112
+ assertThat (jsonNode .get (ERROR_TYPE ).textValue ()).isEqualTo (exception .getClass ().getName ());
113
+ assertThat (jsonNode .get (ERROR_MESSAGE ).textValue ()).isEqualTo ("foo" );
109
114
StringWriter stringWriter = new StringWriter ();
110
115
exception .printStackTrace (new PrintWriter (stringWriter ));
111
- assertThat (StreamSupport .stream (jsonNode .get ("error.stack_trace" ).spliterator (), false )
116
+ assertThat (StreamSupport .stream (jsonNode .get (ERROR_STACK_TRACE ).spliterator (), false )
112
117
.map (JsonNode ::textValue )
113
118
.collect (Collectors .joining (System .lineSeparator (), "" , System .lineSeparator ())))
114
119
.isEqualTo (stringWriter .toString ());
@@ -121,6 +126,48 @@ void testRemoveIfEndsWith() {
121
126
assertRemoveIfEndsWith ("barfoo" , "foo" , "bar" );
122
127
}
123
128
129
+ @ Test
130
+ void serializeException () throws JsonProcessingException {
131
+ StringBuilder jsonBuilder = new StringBuilder ();
132
+ jsonBuilder .append ('{' );
133
+ EcsJsonSerializer .serializeException (jsonBuilder , "className" , "message" , "stacktrace\n caused by error" , false );
134
+ jsonBuilder .append ('}' );
135
+
136
+ JsonNode jsonNode = objectMapper .readTree (jsonBuilder .toString ());
137
+ assertThat (jsonNode .get (ERROR_TYPE ).textValue ()).isEqualTo ("className" );
138
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).isArray ()).isFalse ();
139
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).textValue ()).isEqualTo ("stacktrace\n caused by error" );
140
+ assertThat (jsonNode .get (ERROR_MESSAGE ).textValue ()).isEqualTo ("message" );
141
+ }
142
+
143
+ @ Test
144
+ void serializeExceptionWithStackTraceAsArray () throws JsonProcessingException {
145
+ StringBuilder jsonBuilder = new StringBuilder ();
146
+ jsonBuilder .append ('{' );
147
+ EcsJsonSerializer .serializeException (jsonBuilder , "className" , "message" , "stacktrace\n caused by error" , true );
148
+ jsonBuilder .append ('}' );
149
+
150
+ JsonNode jsonNode = objectMapper .readTree (jsonBuilder .toString ());
151
+ assertThat (jsonNode .get (ERROR_TYPE ).textValue ()).isEqualTo ("className" );
152
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).isArray ()).isTrue ();
153
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).get (0 ).textValue ()).isEqualTo ("stacktrace" );
154
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).get (1 ).textValue ()).isEqualTo ("caused by error" );
155
+ assertThat (jsonNode .get (ERROR_MESSAGE ).textValue ()).isEqualTo ("message" );
156
+ }
157
+
158
+ @ Test
159
+ void serializeExceptionWithNullMessage () throws JsonProcessingException {
160
+ StringBuilder jsonBuilder = new StringBuilder ();
161
+ jsonBuilder .append ('{' );
162
+ EcsJsonSerializer .serializeException (jsonBuilder , "className" , null , "stacktrace" , false );
163
+ jsonBuilder .append ('}' );
164
+
165
+ JsonNode jsonNode = objectMapper .readTree (jsonBuilder .toString ());
166
+ assertThat (jsonNode .get (ERROR_TYPE ).textValue ()).isEqualTo ("className" );
167
+ assertThat (jsonNode .get (ERROR_STACK_TRACE ).textValue ()).isEqualTo ("stacktrace" );
168
+ assertThat (jsonNode .get (ERROR_MESSAGE )).isNull ();
169
+ }
170
+
124
171
private void assertRemoveIfEndsWith (String builder , String ending , String expected ) {
125
172
StringBuilder sb = new StringBuilder (builder );
126
173
EcsJsonSerializer .removeIfEndsWith (sb , ending );
0 commit comments