Skip to content

Commit 39f94ed

Browse files
committed
add unit tests
1 parent 3179277 commit 39f94ed

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

java-sdk-logging/logback-extension/src/test/java/com/google/api/logging/SDKLoggingMdcJsonProviderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.fasterxml.jackson.core.JsonGenerator;
4444
import com.fasterxml.jackson.databind.JsonNode;
4545
import java.io.IOException;
46+
import java.util.Collections;
4647
import java.util.HashMap;
4748
import java.util.Map;
4849
import org.junit.jupiter.api.AfterEach;
@@ -149,6 +150,27 @@ void testWriteValidJsonStringAndValidPairToJsonTree() throws IOException {
149150
verify(generator, times(1)).writeObject(anyString());
150151
}
151152

153+
@Test
154+
void testWriteToJsonTreeIncludedKey() throws IOException {
155+
mdc.put(
156+
"json1",
157+
"{\n"
158+
+ " \"@version\": \"1\",\n"
159+
+ " \"textPayload\": \"Received response\",\n"
160+
+ " \"response.payload\": {\n"
161+
+ " \"name\": \"example\",\n"
162+
+ " \"state\": \"ACTIVE\"\n"
163+
+ " }\n"
164+
+ "}");
165+
mdc.put("example key", "example value");
166+
provider.setIncludeMdcKeyNames(Collections.singletonList("json1"));
167+
provider.writeTo(generator, event);
168+
verify(generator, times(1)).writeFieldName("json1");
169+
verify(generator, never()).writeFieldName("example key");
170+
verify(generator, times(1)).writeTree(any(JsonNode.class));
171+
verify(generator, never()).writeObject(anyString());
172+
}
173+
152174
@Test
153175
void testWriteInvalidJsonStringToString() throws IOException {
154176
mdc.put(

0 commit comments

Comments
 (0)