Skip to content

Commit ca560c7

Browse files
committed
add helper method to create comparison expression
1 parent 2b535b5 commit ca560c7

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public abstract class BaseSystemTest {
7777
*/
7878
protected abstract String formatForTest(String resourceName);
7979

80+
/**
81+
* Creates a comparison expression for logging filter.
82+
*/
83+
protected abstract <V> String createComparisonExpression(String name, String op, V value);
84+
8085
@Test
8186
public void testCreateGetUpdateAndDeleteSink() {
8287
String name = formatForTest("test-create-get-update-sink");
@@ -356,7 +361,7 @@ public void testWriteAndListLogEntries() throws InterruptedException {
356361
.build();
357362
logging().write(ImmutableList.of(firstEntry));
358363
logging().write(ImmutableList.of(secondEntry));
359-
String filter = "logName = " + logName.toString();
364+
String filter = createComparisonExpression("logName", "=", logName);
360365
EntryListOption[] options = {EntryListOption.filter(filter), EntryListOption.pageSize(1)};
361366
Page<LogEntry> page = logging().listLogEntries(options);
362367
while (Iterators.size(page.iterateAll().iterator()) < 2) {
@@ -416,7 +421,7 @@ public void testWriteAndListLogEntriesAsync() throws InterruptedException, Execu
416421
WriteOption.resource(MonitoredResource.newBuilder("global").build()),
417422
WriteOption.logName(logId));
418423
logging().flush();
419-
String filter = "logName = " + logName.toString();
424+
String filter = createComparisonExpression("logName", "=", logName);
420425
EntryListOption[] options = {EntryListOption.filter(filter), EntryListOption.pageSize(1)};
421426
AsyncPage<LogEntry> page = logging().listLogEntriesAsync(options).get();
422427
while (Iterators.size(page.iterateAll().iterator()) < 2) {
@@ -457,7 +462,7 @@ public void testDeleteNonExistingLog() {
457462

458463
@Test
459464
public void testDeleteNonExistingLogAsync() throws ExecutionException, InterruptedException {
460-
String logId= formatForTest("test-delete-non-existing-log-async");
465+
String logId = formatForTest("test-delete-non-existing-log-async");
461466
assertFalse(logging().deleteLogAsync(logId).get());
462467
}
463468

@@ -472,7 +477,7 @@ public void testLoggingHandler() throws InterruptedException {
472477
logger.addHandler(handler);
473478
logger.setLevel(Level.INFO);
474479
logger.info("Message");
475-
String filter = "logName = " + logName.toString();
480+
String filter = createComparisonExpression("logName", "=", logName);
476481
Iterator<LogEntry> iterator =
477482
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
478483
while (!iterator.hasNext()) {
@@ -516,7 +521,7 @@ public void testSyncLoggingHandler() throws InterruptedException {
516521
logger.addHandler(handler);
517522
logger.setLevel(Level.WARNING);
518523
logger.warning("Message");
519-
String filter = "logName = " + logName.toString();
524+
String filter = createComparisonExpression("logName", "=", logName);
520525
Iterator<LogEntry> iterator =
521526
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
522527
while (!iterator.hasNext()) {

google-cloud-logging/src/test/java/com/google/cloud/logging/it/ITLoggingTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,16 @@ protected Logging logging() {
5252
protected String formatForTest(String resourceName) {
5353
return RemoteLoggingHelper.formatForTest(resourceName);
5454
}
55+
56+
/**
57+
* Creates a comparison expression for logging filter.
58+
* comparison = name OP value OP = "<=" | "<" * | ">=" | ">" | "!=" | "=" | ":"
59+
*
60+
* @see <a href="https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Logs
61+
* Filters Documentation</a>
62+
*/
63+
@Override
64+
protected <V> String createComparisonExpression(String name, String op, V value) {
65+
return name + " " + op + " " + "\"" + value.toString() + "\"";
66+
}
5567
}

0 commit comments

Comments
 (0)