@Test public void testWriteLogEntries() { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2)); }
@Test public void testWriteLogEntriesWithSeverityFlushEnabled() { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.setFlushSeverity(Severity.DEFAULT); logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2)); }
.addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(mockRpcResponse);
WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform(ImmutableList.of(LOG_ENTRY1), LogEntry.toPbFunction(PROJECT))) .build();
@Test public void testWriteLogEntriesAsync() throws ExecutionException, InterruptedException { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2)); logging.flush(); }
private static WriteLogEntriesRequest writeLogEntriesRequest( LoggingOptions serviceOptions, Iterable<LogEntry> logEntries, Map<Option.OptionType, ?> options) { String projectId = serviceOptions.getProjectId(); WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder(); String logName = LOG_NAME.get(options); if (logName != null) { builder.setLogName(ProjectLogName.of(projectId, logName).toString()); } MonitoredResource resource = RESOURCE.get(options); if (resource != null) { builder.setResource(resource.toPb()); } Map<String, String> labels = LABELS.get(options); if (labels != null) { builder.putAllLabels(labels); } builder.addAllEntries(Iterables.transform(logEntries, LogEntry.toPbFunction(projectId))); return builder.build(); }
@Test public void testListLogEntriesAyncEmpty() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<LogEntry> page = logging.listLogEntriesAsync().get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
@Test public void testListLogEntriesEmpty() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
@Test public void testListLogEntries() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
@Test public void testWriteLogEntriesDoesnotEnableFlushByDefault() { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform( ImmutableList.of( LOG_ENTRY1, LOG_ENTRY2.toBuilder().setSeverity(Severity.EMERGENCY).build()), LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<WriteLogEntriesResponse> apiFuture = SettableApiFuture.create(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(apiFuture); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of( LOG_ENTRY1, LOG_ENTRY2.toBuilder().setSeverity(Severity.EMERGENCY).build())); }
@Test public void testListLogEntriesAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<LogEntry> page = logging.listLogEntriesAsync().get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
@Test public void testWriteLogEntriesWithOptions() { Map<String, String> labels = ImmutableMap.of("key", "value"); WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .putAllLabels(labels) .setLogName(LOG_NAME_PB) .setResource(MONITORED_RESOURCE.toPb()) .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), WriteOption.logName(LOG_NAME), WriteOption.resource(MONITORED_RESOURCE), WriteOption.labels(labels)); }
@Test public void testWriteLogEntriesAsyncWithOptions() { Map<String, String> labels = ImmutableMap.of("key", "value"); WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .putAllLabels(labels) .setLogName(LOG_NAME_PB) .setResource(MONITORED_RESOURCE.toPb()) .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), WriteOption.logName(LOG_NAME), WriteOption.resource(MONITORED_RESOURCE), WriteOption.labels(labels)); logging.flush(); }
@Test public void testListLogEntriesWithOptions() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder() .addProjectIds(PROJECT) .setOrderBy("timestamp desc") .setFilter("logName:syslog") .build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries( EntryListOption.filter("logName:syslog"), EntryListOption.sortOrder(SortingField.TIMESTAMP, Logging.SortingOrder.DESCENDING)); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor1) .addAllEntries(Lists.transform(descriptorList1, LogEntry.toPbFunction(PROJECT))) .build(); String cursor2 = "nextCursor"; ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor2) .addAllEntries(Lists.transform(descriptorList2, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor1) .addAllEntries(Lists.transform(descriptorList1, LogEntry.toPbFunction(PROJECT))) .build(); String cursor2 = "nextCursor"; ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor2) .addAllEntries(Lists.transform(descriptorList2, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
@Test public void testListLogEntriesAsyncWithOptions() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder() .addProjectIds(PROJECT) .setOrderBy("timestamp desc") .setFilter("logName:syslog") .build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<LogEntry> page = logging .listLogEntriesAsync( EntryListOption.filter("logName:syslog"), EntryListOption.sortOrder(SortingField.TIMESTAMP, Logging.SortingOrder.DESCENDING)) .get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
private static WriteLogEntriesRequest writeLogEntriesRequest( LoggingOptions serviceOptions, Iterable<LogEntry> logEntries, Map<Option.OptionType, ?> options) { String projectId = serviceOptions.getProjectId(); WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder(); String logName = LOG_NAME.get(options); if (logName != null) { builder.setLogName(ProjectLogName.of(projectId, logName).toString()); } MonitoredResource resource = RESOURCE.get(options); if (resource != null) { builder.setResource(resource.toPb()); } Map<String, String> labels = LABELS.get(options); if (labels != null) { builder.putAllLabels(labels); } builder.addAllEntries(Iterables.transform(logEntries, LogEntry.toPbFunction(projectId))); return builder.build(); }