Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Jan 17, 2017
1 parent 0aa59f0 commit e174007
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package com.google.cloud.logging;

import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.LogEntry.Builder;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;

import java.util.Collections;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;
import java.util.logging.Handler;
Expand All @@ -45,6 +48,12 @@ public class LoggingHandlerTest {
.addLabel("levelName", "FINEST")
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
.build();
private static final LogEntry FINEST_ENHANCED_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINEST")
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
.addLabel("enhanced", "true")
.build();
private static final LogEntry FINER_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINER")
Expand Down Expand Up @@ -207,6 +216,33 @@ public void testPublishCustomResource() {
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
}

@Test
public void testEnhancedLogEntry() {
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
EasyMock.expect(options.getService()).andReturn(logging);
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
logging.writeAsync(ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME),
WriteOption.resource(resource));
EasyMock.expectLastCall().andReturn(Futures.immediateFuture(null));
EasyMock.replay(options, logging);
LoggingHandler.Enhancer enhancer = new LoggingHandler.Enhancer() {
@Override
public void enhanceMonitoredResource(com.google.cloud.MonitoredResource.Builder builder) {
throw new IllegalStateException();
}

@Override
public void enhanceLogEntry(Builder builder, LogRecord record) {
builder.addLabel("enhanced", "true");
}
};
Handler handler =
new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
handler.publish(new LogRecord(Level.FINEST, MESSAGE));
}

@Test
public void testReportFlushError() {
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
Expand Down

0 comments on commit e174007

Please sign in to comment.