Skip to content

Commit a7f5a5e

Browse files
committed
add tests. feedback. minor cleanups.
1 parent 445fe08 commit a7f5a5e

File tree

6 files changed

+230
-83
lines changed

6 files changed

+230
-83
lines changed

oauth2_http/java/com/google/auth/oauth2/LoggingConfigs.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
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+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth.oauth2;
233

334
import org.slf4j.ILoggerFactory;
@@ -24,12 +55,11 @@ static Logger getLogger(Class<?> clazz) {
2455

2556
// constructor with LoggerFactoryProvider to make testing easier
2657
static Logger getLogger(Class<?> clazz, LoggerFactoryProvider factoryProvider) {
27-
if (loggingEnabled) {
28-
return factoryProvider.getLoggerFactory().getLogger(clazz.getName());
29-
} else {
58+
if (!loggingEnabled) {
3059
// use SLF4j's NOP logger regardless of bindings
3160
return NO_OP_LOGGER;
3261
}
62+
return factoryProvider.getLoggerFactory().getLogger(clazz.getName());
3363
}
3464

3565
static boolean isLoggingEnabled() {

oauth2_http/java/com/google/auth/oauth2/LoggingUtils.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
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+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth.oauth2;
233

334
import com.google.api.client.http.HttpRequest;
@@ -43,8 +74,8 @@ static void logWithMDC(
4374
}
4475

4576
static void logRequest(HttpRequest request, Logger logger, String message) {
46-
if (logger.isInfoEnabled()) {
47-
try {
77+
try {
78+
if (logger.isInfoEnabled()) {
4879
Map<String, String> loggingDataMap = new HashMap<>();
4980
loggingDataMap.put("request.method", request.getRequestMethod());
5081
loggingDataMap.put("request.url", request.getUrl().toString());
@@ -75,39 +106,37 @@ static void logRequest(HttpRequest request, Logger logger, String message) {
75106

76107
logWithMDC(logger, org.slf4j.event.Level.INFO, loggingDataMap, message);
77108
}
78-
} catch (Exception e) {
79-
logger.error("Error logging request: ", e);
80109
}
110+
} catch (Exception e) {
111+
logger.error("Error logging request: ", e);
81112
}
82113
}
83114

84115
static void logResponse(HttpResponse response, Logger logger, String message) {
85-
if (logger.isInfoEnabled()) {
86-
try {
116+
try {
117+
if (logger.isInfoEnabled()) {
87118
Map<String, String> responseLogDataMap = new HashMap<>();
88119
responseLogDataMap.put("response.status", String.valueOf(response.getStatusCode()));
89120
responseLogDataMap.put("response.status.message", response.getStatusMessage());
90121

91-
Map<String, Object> headers = new HashMap<>();
92-
response.getHeaders().forEach((key, val) -> headers.put(key, val));
122+
Map<String, Object> headers = new HashMap<>(response.getHeaders());
93123
responseLogDataMap.put("response.headers", headers.toString());
94124
logWithMDC(logger, org.slf4j.event.Level.INFO, responseLogDataMap, message);
95-
} catch (Exception e) {
96-
97-
logger.error("Error logging response: ", e);
98125
}
126+
} catch (Exception e) {
127+
128+
logger.error("Error logging response: ", e);
99129
}
100130
}
101131

102132
static void logGenericData(GenericData genericData, Logger logger, String message) {
103-
if (logger.isDebugEnabled()) {
104-
try {
105-
133+
try {
134+
if (logger.isDebugEnabled()) {
106135
Map<String, String> contextMap = parseGenericData(genericData);
107136
logWithMDC(logger, org.slf4j.event.Level.DEBUG, contextMap, message);
108-
} catch (Exception e) {
109-
logger.error("Error logging GenericData: ", e);
110137
}
138+
} catch (Exception e) {
139+
logger.error("Error logging GenericData: ", e);
111140
}
112141
}
113142

oauth2_http/javatests/com/google/auth/TestAppender.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
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+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth;
233

334
import ch.qos.logback.classic.spi.ILoggingEvent;
@@ -11,6 +42,8 @@ public class TestAppender extends AppenderBase<ILoggingEvent> {
1142

1243
@Override
1344
protected void append(ILoggingEvent eventObject) {
45+
// triggering Logback to capture the current MDC context and store it with the log event
46+
eventObject.getMDCPropertyMap();
1447
events.add(eventObject);
1548
}
1649

oauth2_http/javatests/com/google/auth/oauth2/LoggingConfigsTest.java

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
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+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
132
package com.google.auth.oauth2;
233

334
import static org.junit.Assert.assertEquals;
435
import static org.junit.Assert.assertFalse;
536
import static org.junit.Assert.assertNotEquals;
37+
import static org.junit.Assert.assertNotNull;
638
import static org.junit.Assert.assertTrue;
739
import static org.mockito.ArgumentMatchers.anyString;
840
import static org.mockito.Mockito.mock;
941
import static org.mockito.Mockito.when;
1042

1143
import ch.qos.logback.classic.LoggerContext;
12-
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
13-
import ch.qos.logback.classic.spi.ILoggingEvent;
14-
import ch.qos.logback.core.ConsoleAppender;
15-
import com.google.auth.TestAppender;
1644
import com.google.auth.oauth2.LoggingConfigs.LoggerFactoryProvider;
17-
import java.util.HashMap;
18-
import java.util.Map;
1945
import org.junit.After;
2046
import org.junit.Before;
2147
import org.junit.Test;
@@ -33,26 +59,6 @@ public class LoggingConfigsTest {
3359
@Before
3460
public void setup() {
3561
testEnvironmentProvider = new TestEnvironmentProvider();
36-
// need to setup a ConsoleAppender and attach to root logger because TestAppender
37-
// does not correctly capture MDC info
38-
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
39-
40-
PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
41-
patternLayoutEncoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n");
42-
patternLayoutEncoder.setContext(loggerContext);
43-
44-
patternLayoutEncoder.start();
45-
46-
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
47-
consoleAppender.setEncoder(patternLayoutEncoder);
48-
49-
consoleAppender.setContext(loggerContext);
50-
consoleAppender.setName("CONSOLE");
51-
52-
consoleAppender.start();
53-
54-
ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
55-
rootLogger.addAppender(consoleAppender);
5662
}
5763

5864
@After
@@ -66,8 +72,8 @@ public void testGetLogger_loggingEnabled_slf4jBindingPresent() {
6672
testEnvironmentProvider.setEnv("GOOGLE_SDK_JAVA_LOGGING", "true");
6773
LoggingConfigs.setEnvironmentProvider(testEnvironmentProvider);
6874
Logger logger = LoggingConfigs.getLogger(LoggingConfigsTest.class);
69-
assertTrue(logger instanceof org.slf4j.Logger);
70-
assertNotEquals(logger.getClass(), NOPLogger.class);
75+
assertNotNull(logger);
76+
assertNotEquals(NOPLogger.class, logger.getClass());
7177
}
7278

7379
@Test
@@ -115,19 +121,4 @@ public void testIsLoggingEnabled_defaultToFalse() {
115121
LoggingConfigs.setEnvironmentProvider(testEnvironmentProvider);
116122
assertFalse(LoggingConfigs.isLoggingEnabled());
117123
}
118-
119-
@Test
120-
public void testLogWithMDC_slf4jLogger() {
121-
TestAppender.clearEvents();
122-
Map<String, String> contextMap = new HashMap<>();
123-
contextMap.put("key", "value");
124-
LoggingUtils.logWithMDC(LOGGER, org.slf4j.event.Level.DEBUG, contextMap, "test message");
125-
126-
assertEquals(1, TestAppender.events.size());
127-
assertEquals("test message", TestAppender.events.get(0).getFormattedMessage());
128-
129-
// Verify MDC content
130-
ILoggingEvent event = TestAppender.events.get(0);
131-
assertEquals("value", event.getMDCPropertyMap().get("key"));
132-
}
133124
}
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+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package com.google.auth.oauth2;
33+
34+
import static org.junit.Assert.*;
35+
36+
import ch.qos.logback.classic.spi.ILoggingEvent;
37+
import com.google.api.client.util.GenericData;
38+
import com.google.auth.TestAppender;
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
import org.junit.Test;
42+
import org.mockito.Mockito;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
45+
46+
public class LoggingUtilsTest {
47+
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingUtilsTest.class);
48+
49+
@Test
50+
public void testLogWithMDC_slf4jLogger() {
51+
TestAppender.clearEvents();
52+
Map<String, String> contextMap = new HashMap<>();
53+
contextMap.put("key1", "value1");
54+
contextMap.put("key2", "value2");
55+
LoggingUtils.logWithMDC(LOGGER, org.slf4j.event.Level.DEBUG, contextMap, "test message");
56+
57+
assertEquals(1, TestAppender.events.size());
58+
assertEquals("test message", TestAppender.events.get(0).getFormattedMessage());
59+
60+
// Verify MDC content
61+
ILoggingEvent event = TestAppender.events.get(0);
62+
assertEquals(2, event.getMDCPropertyMap().size());
63+
assertEquals("value1", event.getMDCPropertyMap().get("key1"));
64+
assertEquals("value2", event.getMDCPropertyMap().get("key2"));
65+
}
66+
67+
@Test
68+
public void testLogGenericData() {
69+
TestAppender.clearEvents();
70+
GenericData genericData = Mockito.mock(GenericData.class);
71+
72+
GenericData data = new GenericData();
73+
data.put("key1", "value1");
74+
data.put("token", "value2");
75+
76+
LoggingUtils.logGenericData(data, LOGGER, "test generic data");
77+
78+
assertEquals(1, TestAppender.events.size());
79+
Map<String, String> mdcPropertyMap = TestAppender.events.get(0).getMDCPropertyMap();
80+
assertEquals(2, mdcPropertyMap.size());
81+
assertEquals("value1", mdcPropertyMap.get("key1"));
82+
assertNotNull(mdcPropertyMap.get("token"));
83+
assertNotEquals("value2", mdcPropertyMap.get("token"));
84+
}
85+
}

0 commit comments

Comments
 (0)