Skip to content

Commit 673c9fe

Browse files
committed
cleanup gax pom. add tests.
1 parent 439e071 commit 673c9fe

File tree

7 files changed

+185
-64
lines changed

7 files changed

+185
-64
lines changed

gapic-generator-java-pom-parent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<j2objc-annotations.version>3.0.0</j2objc-annotations.version>
3939
<threetenbp.version>1.7.0</threetenbp.version>
4040
<junit.version>5.11.2</junit.version>
41+
<slf4j.version>2.0.16</slf4j.version>
4142
</properties>
4243

4344
<developers>

gax-java/gax/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,6 @@
7373
<dependency>
7474
<groupId>org.slf4j</groupId>
7575
<artifactId>slf4j-api</artifactId>
76-
<version>2.0.16</version>
77-
</dependency>
78-
<!-- Logback for testing -->
79-
<dependency>
80-
<groupId>ch.qos.logback</groupId>
81-
<artifactId>logback-classic</artifactId>
82-
<!-- Latest version with java8 support -->
83-
<version>1.3.14</version>
84-
<scope>test</scope>
85-
</dependency>
86-
<dependency>
87-
<groupId>ch.qos.logback</groupId>
88-
<artifactId>logback-core</artifactId>
89-
<version>1.3.14</version>
90-
<scope>test</scope>
9176
</dependency>
9277
</dependencies>
9378

gax-java/gax/src/main/java/com/google/api/gax/logging/LogData.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,41 +104,6 @@ public abstract static class Builder {
104104

105105
// helper functions to convert to map for logging
106106
// todo: error handling?
107-
public Map<String, String> toMap() {
108-
Map<String, String> map = new HashMap<>();
109-
if (serviceName() != null) {
110-
map.put("serviceName", serviceName());
111-
}
112-
if (rpcName() != null) {
113-
map.put("rpcName", rpcName());
114-
}
115-
if (requestId() != null) {
116-
map.put("requestId", requestId());
117-
}
118-
if (requestHeaders() != null) {
119-
map.put("request.headers", requestHeaders());
120-
}
121-
if (requestPayload() != null) {
122-
map.put("request.payload", requestPayload());
123-
}
124-
if (responseStatus() != null) {
125-
map.put("response.status", responseStatus());
126-
}
127-
if (responseHeaders() != null) {
128-
map.put("response.headers", responseHeaders());
129-
}
130-
if (responsePayload() != null) {
131-
map.put("response.payload", gson.toJson(responsePayload()));
132-
}
133-
if (httpMethod() != null) {
134-
map.put("request.method", httpMethod());
135-
}
136-
if (httpUrl() != null) {
137-
map.put("request.url", httpUrl());
138-
}
139-
return map;
140-
}
141-
142107
public Map<String, String> toMapRequest() {
143108
Map<String, String> map = new HashMap<>();
144109
if (serviceName() != null) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.google.api.gax.logging;
32+
33+
import static com.google.common.truth.Truth.assertThat;
34+
35+
import com.google.common.collect.ImmutableMap;
36+
import com.google.gson.JsonParser;
37+
import java.util.Map;
38+
import org.junit.jupiter.api.Test;
39+
40+
class LogDataTest {
41+
42+
@Test
43+
void toMapResponse_correctlyConvertsData() {
44+
LogData logData =
45+
LogData.builder()
46+
.serviceName("MyService")
47+
.rpcName("myMethod")
48+
.requestHeaders("fake header")
49+
.requestId("abcd")
50+
.responsePayload(JsonParser.parseString("{\"key\": \"value\"}"))
51+
.build();
52+
53+
Map<String, String> expectedMap =
54+
ImmutableMap.of(
55+
"serviceName", "MyService",
56+
"rpcName", "myMethod",
57+
"response.payload", "{\"key\":\"value\"}",
58+
"requestId", "abcd");
59+
60+
assertThat(logData.toMapResponse()).isEqualTo(expectedMap);
61+
}
62+
63+
@Test
64+
void toMapRequest_correctlyConvertsData() {
65+
LogData logData =
66+
LogData.builder()
67+
.serviceName("MyService")
68+
.rpcName("myMethod")
69+
.requestHeaders("fake header")
70+
.requestId("abcd")
71+
.httpUrl("url")
72+
.responsePayload(JsonParser.parseString("{\"key\": \"value\"}"))
73+
.build();
74+
75+
Map<String, String> expectedMap =
76+
ImmutableMap.of(
77+
"serviceName", "MyService",
78+
"rpcName", "myMethod",
79+
"request.headers", "fake header",
80+
"requestId", "abcd",
81+
"request.url", "url");
82+
83+
assertThat(logData.toMapRequest()).isEqualTo(expectedMap);
84+
}
85+
}

gax-java/gax/src/test/java/com/google/api/gax/logging/LoggingUtilsTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import ch.qos.logback.core.ConsoleAppender;
4545
import com.google.api.gax.logging.LoggingUtils.LoggerFactoryProvider;
4646
import com.google.api.gax.rpc.internal.EnvironmentProvider;
47+
import java.util.HashMap;
48+
import java.util.Map;
4749
import org.junit.jupiter.api.AfterEach;
4850
import org.junit.jupiter.api.BeforeEach;
4951
import org.junit.jupiter.api.Test;
@@ -55,6 +57,7 @@
5557

5658
class LoggingUtilsTest {
5759

60+
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingUtilsTest.class);
5861
private EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
5962

6063
@BeforeEach
@@ -144,18 +147,29 @@ void testIsLoggingEnabled_defaultToFalse() {
144147
assertFalse(LoggingUtils.isLoggingEnabled());
145148
}
146149

147-
// @Test
148-
// public void testLogWithMDC_slf4jLogger() {
149-
// TestAppender.clearEvents();
150-
// Map<String, String> contextMap = new HashMap<>();
151-
// contextMap.put("key", "value");
152-
// LoggingUtils.logWithMDC(LOGGER, org.slf4j.event.Level.DEBUG, contextMap, "test message");
153-
//
154-
// assertEquals(1, TestAppender.events.size());
155-
// assertEquals("test message", TestAppender.events.get(0).getFormattedMessage());
156-
//
157-
// // Verify MDC content
158-
// ILoggingEvent event = TestAppender.events.get(0);
159-
// assertEquals("value", event.getMDCPropertyMap().get("key"));
160-
// }
150+
private TestAppender setupTestLogger(Class<?> clazz) {
151+
TestAppender testAppender = new TestAppender();
152+
testAppender.start();
153+
Logger logger = LoggerFactory.getLogger(clazz);
154+
((ch.qos.logback.classic.Logger) logger).addAppender(testAppender);
155+
return testAppender;
156+
}
157+
158+
@Test
159+
public void testLogWithMDC_slf4jLogger() {
160+
TestAppender testAppender = setupTestLogger(LoggingUtilsTest.class);
161+
Map<String, String> contextMap = new HashMap<>();
162+
contextMap.put("key", "value");
163+
LoggingUtils.logWithMDC(LOGGER, org.slf4j.event.Level.DEBUG, contextMap, "test message");
164+
165+
assertEquals(1, testAppender.events.size());
166+
assertEquals(
167+
"{\"message\":\"test message\",\"key\":\"value\"}",
168+
testAppender.events.get(0).getFormattedMessage());
169+
170+
// Verify MDC content
171+
ILoggingEvent event = testAppender.events.get(0);
172+
assertEquals("value", event.getMDCPropertyMap().get("key"));
173+
testAppender.stop();
174+
}
161175
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.google.api.gax.logging;
32+
33+
import ch.qos.logback.classic.spi.ILoggingEvent;
34+
import ch.qos.logback.core.AppenderBase;
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
38+
/** Logback appender used to set up tests. */
39+
public class TestAppender extends AppenderBase<ILoggingEvent> {
40+
public List<ILoggingEvent> events = new ArrayList<>();
41+
42+
@Override
43+
protected void append(ILoggingEvent eventObject) {
44+
// triggering Logback to capture the current MDC context and store it with the log event
45+
eventObject.getMDCPropertyMap();
46+
events.add(eventObject);
47+
}
48+
49+
public void clearEvents() {
50+
events.clear();
51+
}
52+
}

gax-java/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
<artifactId>graal-sdk</artifactId>
135135
<version>${graal-sdk.version}</version>
136136
</dependency>
137+
<dependency>
138+
<groupId>org.slf4j</groupId>
139+
<artifactId>slf4j-api</artifactId>
140+
<version>${slf4j.version}</version>
141+
</dependency>
137142
<dependency>
138143
<groupId>com.google.http-client</groupId>
139144
<artifactId>google-http-client-bom</artifactId>
@@ -205,6 +210,20 @@
205210
</exclusions>
206211
<scope>test</scope>
207212
</dependency>
213+
<!-- Logback for testing -->
214+
<dependency>
215+
<groupId>ch.qos.logback</groupId>
216+
<artifactId>logback-classic</artifactId>
217+
<!-- Latest version with java8 support -->
218+
<version>1.3.14</version>
219+
<scope>test</scope>
220+
</dependency>
221+
<dependency>
222+
<groupId>ch.qos.logback</groupId>
223+
<artifactId>logback-core</artifactId>
224+
<version>1.3.14</version>
225+
<scope>test</scope>
226+
</dependency>
208227
</dependencies>
209228

210229
<build>

0 commit comments

Comments
 (0)