1
1
/*
2
- Copyright 2016, Google, Inc.
2
+ Copyright 2017 Google Inc.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
21
+ import com .google .cloud .MonitoredResource ;
22
+ import com .google .cloud .logging .LogEntry ;
21
23
import com .google .cloud .logging .Logging ;
22
24
import com .google .cloud .logging .LoggingOptions ;
25
+ import com .google .cloud .logging .Payload .StringPayload ;
26
+ import java .io .ByteArrayOutputStream ;
27
+ import java .io .PrintStream ;
28
+ import java .util .Collections ;
23
29
import org .junit .After ;
24
30
import org .junit .Before ;
25
31
import org .junit .Test ;
26
32
import org .junit .runner .RunWith ;
27
33
import org .junit .runners .JUnit4 ;
28
34
29
- import java .io .ByteArrayOutputStream ;
30
- import java .io .PrintStream ;
31
-
32
35
/**
33
36
* Tests for quickstart sample.
34
37
*/
35
38
@ RunWith (JUnit4 .class )
36
39
@ SuppressWarnings ("checkstyle:abbreviationaswordinname" )
37
- public class QuickstartSampleIT {
40
+ public class LoggingIT {
38
41
39
42
private ByteArrayOutputStream bout ;
40
43
private PrintStream out ;
44
+ private Logging logging = LoggingOptions .getDefaultInstance ().getService ();
41
45
42
- private static final void deleteMyLog () {
43
- Logging logging = LoggingOptions .getDefaultInstance ().getService ();
44
-
45
- logging .deleteLog ("my-log" );
46
+ private void deleteLog (String logName ) {
47
+ logging .deleteLog (logName );
46
48
}
47
49
48
50
@ Before
49
51
public void setUp () {
50
- deleteMyLog ();
51
-
52
52
bout = new ByteArrayOutputStream ();
53
53
out = new PrintStream (bout );
54
54
System .setOut (out );
@@ -57,13 +57,36 @@ public void setUp() {
57
57
@ After
58
58
public void tearDown () {
59
59
System .setOut (null );
60
- deleteMyLog ();
61
60
}
62
61
63
62
@ Test
64
63
public void testQuickstart () throws Exception {
65
- QuickstartSample .main ("my-log" );
64
+ String logName = "my-log" ;
65
+ deleteLog (logName );
66
+ QuickstartSample .main (logName );
66
67
String got = bout .toString ();
67
68
assertThat (got ).contains ("Logged: Hello, world!" );
69
+ deleteLog (logName );
70
+ }
71
+
72
+ @ Test (timeout = 10000 )
73
+ public void testWriteAndListLogs () throws Exception {
74
+ String logName = "test-log" ;
75
+ deleteLog (logName );
76
+ // write a log entry
77
+ LogEntry entry = LogEntry .newBuilder (StringPayload .of ("Hello world again" ))
78
+ .setLogName (logName )
79
+ .setResource (MonitoredResource .newBuilder ("global" ).build ())
80
+ .build ();
81
+ logging .write (Collections .singleton (entry ));
82
+ // flush out log immediately
83
+ logging .flush ();
84
+ bout .reset ();
85
+ while (bout .toString ().isEmpty ()) {
86
+ ListLogs .main (logName );
87
+ Thread .sleep (1000 );
88
+ }
89
+ assertThat (bout .toString ().contains ("Hello world again" )).isTrue ();
90
+ deleteLog (logName );
68
91
}
69
92
}
0 commit comments