34
34
public class LogEntryTest {
35
35
36
36
private static final String LOG_NAME = "syslog" ;
37
+ private static final String PROJECT = "project" ;
37
38
private static final MonitoredResource RESOURCE =
38
39
MonitoredResource .newBuilder ("cloudsql_database" )
39
40
.setLabels (ImmutableMap .of ("datasetId" , "myDataset" , "zone" , "myZone" ))
@@ -74,6 +75,11 @@ public String toString() {
74
75
JsonPayload .of (ImmutableMap .<String , Object >of ("key" , "val" ));
75
76
private static final ProtoPayload PROTO_PAYLOAD =
76
77
ProtoPayload .of (Any .pack (Empty .getDefaultInstance ()));
78
+ private static final LogDestinationName BILLING_NAME =
79
+ LogDestinationName .billingAccount ("000000-111111-222222" );
80
+ private static final LogDestinationName PROJECT_NAME = LogDestinationName .project (PROJECT );
81
+ private static final LogDestinationName FOLDER_NAME = LogDestinationName .folder ("123456789" );
82
+ private static final LogDestinationName ORG_NAME = LogDestinationName .organization ("1122334455" );
77
83
private static final LogEntry STRING_ENTRY =
78
84
LogEntry .newBuilder (STRING_PAYLOAD )
79
85
.setLogName (LOG_NAME )
@@ -122,6 +128,14 @@ public String toString() {
122
128
.setTraceSampled (TRACE_SAMPLED )
123
129
.setSourceLocation (SOURCE_LOCATION )
124
130
.build ();
131
+ private static final LogEntry STRING_ENTRY_BILLING =
132
+ STRING_ENTRY .toBuilder ().setDestination (BILLING_NAME ).build ();
133
+ private static final LogEntry STRING_ENTRY_PROJECT =
134
+ STRING_ENTRY .toBuilder ().setDestination (PROJECT_NAME ).build ();
135
+ private static final LogEntry STRING_ENTRY_FOLDER =
136
+ STRING_ENTRY .toBuilder ().setDestination (FOLDER_NAME ).build ();
137
+ private static final LogEntry STRING_ENTRY_ORG =
138
+ STRING_ENTRY .toBuilder ().setDestination (ORG_NAME ).build ();
125
139
126
140
@ Test
127
141
public void testOf () {
@@ -251,7 +265,7 @@ public void testBuilder() {
251
265
252
266
@ Test
253
267
public void testToBuilder () {
254
- compareLogEntry (STRING_ENTRY , STRING_ENTRY .toBuilder ().build ());
268
+ compareLogEntry (STRING_ENTRY , STRING_ENTRY .toBuilder ().build (), true );
255
269
HttpRequest request =
256
270
HttpRequest .newBuilder ()
257
271
.setRequestMethod (HttpRequest .RequestMethod .POST )
@@ -309,22 +323,45 @@ public void testToBuilder() {
309
323
.setTraceSampled (TRACE_SAMPLED )
310
324
.setSourceLocation (SOURCE_LOCATION )
311
325
.build ();
312
- compareLogEntry (STRING_ENTRY , logEntry );
326
+ compareLogEntry (STRING_ENTRY , logEntry , true );
313
327
}
314
328
315
329
@ Test
316
330
public void testToAndFromPb () {
317
- compareLogEntry (STRING_ENTRY , LogEntry .fromPb (STRING_ENTRY .toPb ("project" )));
318
- compareLogEntry (JSON_ENTRY , LogEntry .fromPb (JSON_ENTRY .toPb ("project" )));
319
- compareLogEntry (PROTO_ENTRY , LogEntry .fromPb (PROTO_ENTRY .toPb ("project" )));
331
+ compareLogEntry (STRING_ENTRY , LogEntry .fromPb (STRING_ENTRY .toPb (PROJECT )), false );
332
+ compareLogEntry (JSON_ENTRY , LogEntry .fromPb (JSON_ENTRY .toPb (PROJECT )), false );
333
+ compareLogEntry (PROTO_ENTRY , LogEntry .fromPb (PROTO_ENTRY .toPb (PROJECT )), false );
334
+ compareLogEntry (
335
+ STRING_ENTRY_BILLING , LogEntry .fromPb (STRING_ENTRY_BILLING .toPb (PROJECT )), true );
336
+ compareLogEntry (STRING_ENTRY_FOLDER , LogEntry .fromPb (STRING_ENTRY_FOLDER .toPb (PROJECT )), true );
337
+ compareLogEntry (STRING_ENTRY_ORG , LogEntry .fromPb (STRING_ENTRY_ORG .toPb (PROJECT )), true );
338
+ compareLogEntry (
339
+ STRING_ENTRY_PROJECT , LogEntry .fromPb (STRING_ENTRY_PROJECT .toPb (PROJECT )), true );
320
340
LogEntry logEntry = LogEntry .of (STRING_PAYLOAD );
321
- compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb ("project" )) );
341
+ compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb (PROJECT )), true );
322
342
logEntry = LogEntry .of (LOG_NAME , RESOURCE , STRING_PAYLOAD );
323
- compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb ("project" )));
343
+ compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb (PROJECT )), false );
344
+ logEntry =
345
+ LogEntry .newBuilder (STRING_PAYLOAD )
346
+ .setLogName (LOG_NAME )
347
+ .setResource (RESOURCE )
348
+ .setDestination (FOLDER_NAME )
349
+ .build ();
350
+ compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb (PROJECT )), true );
351
+ }
352
+
353
+ @ Test (expected = AssertionError .class )
354
+ public void testToAndFromPbWithExpectedFailure () {
355
+ LogEntry logEntry =
356
+ LogEntry .newBuilder (STRING_PAYLOAD ).setLogName (LOG_NAME ).setResource (RESOURCE ).build ();
357
+ compareLogEntry (logEntry , LogEntry .fromPb (logEntry .toPb (PROJECT )), true );
324
358
}
325
359
326
- private void compareLogEntry (LogEntry expected , LogEntry value ) {
327
- assertEquals (expected , value );
360
+ private void compareLogEntry (LogEntry expected , LogEntry value , Boolean extraValidations ) {
361
+ if (extraValidations ) {
362
+ assertEquals (expected .hashCode (), value .hashCode ());
363
+ assertEquals (expected , value );
364
+ }
328
365
assertEquals (expected .getLogName (), value .getLogName ());
329
366
assertEquals (expected .getResource (), value .getResource ());
330
367
assertEquals (expected .getTimestamp (), value .getTimestamp ());
@@ -341,6 +378,5 @@ private void compareLogEntry(LogEntry expected, LogEntry value) {
341
378
assertEquals (expected .getTraceSampled (), value .getTraceSampled ());
342
379
assertEquals (expected .getSourceLocation (), value .getSourceLocation ());
343
380
assertEquals (expected .getPayload (), value .getPayload ());
344
- assertEquals (expected .hashCode (), value .hashCode ());
345
381
}
346
382
}
0 commit comments