Skip to content

Commit e174007

Browse files
committed
Added unit test
1 parent 0aa59f0 commit e174007

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
package com.google.cloud.logging;
1818

1919
import com.google.cloud.MonitoredResource;
20+
import com.google.cloud.logging.LogEntry.Builder;
2021
import com.google.cloud.logging.Logging.WriteOption;
2122
import com.google.cloud.logging.Payload.StringPayload;
2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.collect.ImmutableMap;
2425
import com.google.common.util.concurrent.Futures;
26+
27+
import java.util.Collections;
2528
import java.util.logging.ErrorManager;
2629
import java.util.logging.Formatter;
2730
import java.util.logging.Handler;
@@ -45,6 +48,12 @@ public class LoggingHandlerTest {
4548
.addLabel("levelName", "FINEST")
4649
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
4750
.build();
51+
private static final LogEntry FINEST_ENHANCED_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
52+
.setSeverity(Severity.DEBUG)
53+
.addLabel("levelName", "FINEST")
54+
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
55+
.addLabel("enhanced", "true")
56+
.build();
4857
private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
4958
.setSeverity(Severity.DEBUG)
5059
.addLabel("levelName", "FINER")
@@ -207,6 +216,33 @@ public void testPublishCustomResource() {
207216
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
208217
}
209218

219+
@Test
220+
public void testEnhancedLogEntry() {
221+
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
222+
EasyMock.expect(options.getService()).andReturn(logging);
223+
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
224+
logging.writeAsync(ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME),
225+
WriteOption.resource(resource));
226+
EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null));
227+
EasyMock.replay(options, logging);
228+
LoggingHandler.Enhancer enhancer = new LoggingHandler.Enhancer() {
229+
@Override
230+
public void enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder builder) {
231+
throw new IllegalStateException();
232+
}
233+
234+
@Override
235+
public void enhanceLogEntry(Builder builder, LogRecord record) {
236+
builder.addLabel("enhanced", "true");
237+
}
238+
};
239+
Handler handler =
240+
new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
241+
handler.setLevel(Level.ALL);
242+
handler.setFormatter(new TestFormatter());
243+
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
244+
}
245+
210246
@Test
211247
public void testReportFlushError() {
212248
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();

0 commit comments

Comments
 (0)