Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
simonz130 committed Nov 11, 2020
1 parent ddfd50d commit a0e16a9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
package com.google.cloud.logging;

/**
* Encapsulates implementation of default time filter.
* This is needed for testing since we can't mock static classes
* with EasyMock
* Encapsulates implementation of default time filter. This is needed for testing since we can't
* mock static classes with EasyMock
*/
public interface ITimestampDefaultFilter {

/**
* Creates a default filter with timestamp to query Cloud Logging
* @return The filter using timestamp field
*/
String createDefaultTimestampFilter();
/**
* Creates a default filter with timestamp to query Cloud Logging
*
* @return The filter using timestamp field
*/
String createDefaultTimestampFilter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,9 @@ static ListLogEntriesRequest listLogEntriesRequest(
// of 24 hours back to be inline with gcloud behavior for the same API
if (filter != null) {
if (!filter.toLowerCase().contains("timestamp")) {
filter = String.format("%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter());
filter =
String.format(
"%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter());
}
builder.setFilter(filter);
} else {
Expand All @@ -806,12 +808,12 @@ private static ApiFuture<AsyncPage<LogEntry>> listLogEntriesAsync(
@Override
public AsyncPage<LogEntry> apply(ListLogEntriesResponse listLogEntriesResponse) {
List<LogEntry> entries =
listLogEntriesResponse.getEntriesList() == null
listLogEntriesResponse.getEntriesList() == null
? ImmutableList.<LogEntry>of()
: Lists.transform(
listLogEntriesResponse.getEntriesList(), LogEntry.FROM_PB_FUNCTION);
listLogEntriesResponse.getEntriesList(), LogEntry.FROM_PB_FUNCTION);
String cursor =
listLogEntriesResponse.getNextPageToken().equals("")
listLogEntriesResponse.getNextPageToken().equals("")
? null
: listLogEntriesResponse.getNextPageToken();
return new AsyncPageImpl<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import java.util.TimeZone;

public class TimestampDefaultFilter implements ITimestampDefaultFilter {
@Override
public String createDefaultTimestampFilter() {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\"";
}
@Override
public String createDefaultTimestampFilter() {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\"";
}

private Date yesterday() {
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);
private Date yesterday() {
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);

return calendar.getTime();
}
return calendar.getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@
import com.google.logging.v2.WriteLogEntriesResponse;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -192,12 +190,13 @@ public void setUp() {
// However when testing, the time when it was called by the test and by the method
// implementation might differ by microseconds so we use the same time filter implementation
// for test and in "real" method
LoggingImpl.defaultTimestampFilterCreator = new ITimestampDefaultFilter() {
@Override
public String createDefaultTimestampFilter() {
return "timestamp>=\"2020-10-30T15:39:09Z\"";
}
};
LoggingImpl.defaultTimestampFilterCreator =
new ITimestampDefaultFilter() {
@Override
public String createDefaultTimestampFilter() {
return "timestamp>=\"2020-10-30T15:39:09Z\"";
}
};
}

@After
Expand Down Expand Up @@ -1744,7 +1743,8 @@ public void testListLogEntries() {
.addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT)))
.build();
ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response);
EasyMock.expect(loggingRpcMock.list(EasyMock.anyObject(ListLogEntriesRequest.class))).andReturn(futureResponse);
EasyMock.expect(loggingRpcMock.list(EasyMock.anyObject(ListLogEntriesRequest.class)))
.andReturn(futureResponse);
EasyMock.replay(loggingRpcMock);
Page<LogEntry> page = logging.listLogEntries();
assertEquals(cursor, page.getNextPageToken());
Expand All @@ -1757,7 +1757,8 @@ public void testListLogEntriesNextPage() throws ExecutionException, InterruptedE
EasyMock.replay(rpcFactoryMock);
logging = options.getService();

String defaultTimeFilter = LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter();
String defaultTimeFilter =
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
Expand Down Expand Up @@ -1832,7 +1833,9 @@ public void testListLogEntriesWithOptions() {
.addResourceNames(PROJECT_PB)
.setOrderBy("timestamp desc")
.setFilter(
String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter()))
String.format(
"logName:syslog AND %s",
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter()))
.build();
List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
Expand Down Expand Up @@ -1948,7 +1951,10 @@ public void testListLogEntriesAsyncWithOptions() throws ExecutionException, Inte
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
String filter = String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter());
String filter =
String.format(
"logName:syslog AND %s",
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter());
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,40 @@

package com.google.cloud.logging;

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import javax.management.timer.Timer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import javax.management.timer.Timer;
import org.junit.Test;

public class TimestampDefaultFilterTest {

@Test
public void DefaultTimestampFilterTest() {
ITimestampDefaultFilter filter = new TimestampDefaultFilter();

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);
Date expected = calendar.getTime();

// Timestamp filter exists
String defaultFilter = filter.createDefaultTimestampFilter();
assertTrue(defaultFilter.contains("timestamp>="));

// Time is last 24 hours
try {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date actual = rfcDateFormat.parse(defaultFilter.substring(12, defaultFilter.length() - 1));
assertTrue(Math.abs(expected.getTime() - actual.getTime()) < Timer.ONE_MINUTE);
}
catch (java.text.ParseException ex) {
fail(); // Just fail if exception is thrown
}
@Test
public void DefaultTimestampFilterTest() {
ITimestampDefaultFilter filter = new TimestampDefaultFilter();

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);
Date expected = calendar.getTime();

// Timestamp filter exists
String defaultFilter = filter.createDefaultTimestampFilter();
assertTrue(defaultFilter.contains("timestamp>="));

// Time is last 24 hours
try {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date actual = rfcDateFormat.parse(defaultFilter.substring(12, defaultFilter.length() - 1));
assertTrue(Math.abs(expected.getTime() - actual.getTime()) < Timer.ONE_MINUTE);
} catch (java.text.ParseException ex) {
fail(); // Just fail if exception is thrown
}
}
}

0 comments on commit a0e16a9

Please sign in to comment.