|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache license, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the license for the specific language governing permissions and |
| 15 | + * limitations under the license. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.layout.template.json.resolver; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.Marker; |
| 22 | +import org.apache.logging.log4j.MarkerManager; |
| 23 | +import org.apache.logging.log4j.core.LogEvent; |
| 24 | +import org.apache.logging.log4j.core.impl.Log4jLogEvent; |
| 25 | +import org.apache.logging.log4j.layout.template.json.JsonTemplateLayout; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.apache.logging.log4j.layout.template.json.TestHelpers.*; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +class MarkerResolverTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void should_have_a_marker_name() { |
| 35 | + final String eventTemplate = writeJson(asMap( |
| 36 | + "marker", |
| 37 | + asMap( |
| 38 | + "$resolver", "marker", |
| 39 | + "field", "name" |
| 40 | + ) |
| 41 | + )); |
| 42 | + |
| 43 | + // Create the layout. |
| 44 | + final JsonTemplateLayout layout = JsonTemplateLayout |
| 45 | + .newBuilder() |
| 46 | + .setConfiguration(CONFIGURATION) |
| 47 | + .setEventTemplate(eventTemplate) |
| 48 | + .build(); |
| 49 | + |
| 50 | + // Create the log event. |
| 51 | + final Marker marker = MarkerManager.getMarker("MARKER"); |
| 52 | + final LogEvent logEvent = Log4jLogEvent |
| 53 | + .newBuilder() |
| 54 | + .setMarker(marker) |
| 55 | + .build(); |
| 56 | + |
| 57 | + // Check the serialized event. |
| 58 | + usingSerializedLogEventAccessor(layout, logEvent, accessor -> { |
| 59 | + assertThat(accessor.getString("marker")).isEqualTo("MARKER"); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void should_list_parents_as_array() { |
| 65 | + final String eventTemplate = writeJson(asMap( |
| 66 | + "parents", |
| 67 | + asMap( |
| 68 | + "$resolver", "marker", |
| 69 | + "field", "parents" |
| 70 | + ) |
| 71 | + )); |
| 72 | + |
| 73 | + // Create the layout. |
| 74 | + final JsonTemplateLayout layout = JsonTemplateLayout |
| 75 | + .newBuilder() |
| 76 | + .setConfiguration(CONFIGURATION) |
| 77 | + .setEventTemplate(eventTemplate) |
| 78 | + .build(); |
| 79 | + |
| 80 | + // Create the log event. |
| 81 | + final Marker PARENT_MARKER_1 = MarkerManager.getMarker("PARENT_MARKER_NAME_1"); |
| 82 | + final Marker PARENT_MARKER_2 = MarkerManager.getMarker("PARENT_MARKER_NAME_2"); |
| 83 | + final Marker CHILD_MARKER = MarkerManager.getMarker("CHILD_MARKER_NAME"); |
| 84 | + CHILD_MARKER.setParents(PARENT_MARKER_1, PARENT_MARKER_2); |
| 85 | + |
| 86 | + final LogEvent logEvent = Log4jLogEvent |
| 87 | + .newBuilder() |
| 88 | + .setMarker(CHILD_MARKER) |
| 89 | + .build(); |
| 90 | + |
| 91 | + // Check the serialized event. |
| 92 | + usingSerializedLogEventAccessor(layout, logEvent, accessor -> { |
| 93 | + assertThat(accessor.getList("parents", String.class)).hasSize(2); |
| 94 | + assertThat(accessor.getList("parents", String.class)).containsAll( |
| 95 | + Arrays.asList( |
| 96 | + "PARENT_MARKER_NAME_1", |
| 97 | + "PARENT_MARKER_NAME_2" |
| 98 | + ) |
| 99 | + ); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments