19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static org .mockito .ArgumentMatchers .any ;
21
21
import static org .mockito .Mockito .RETURNS_DEEP_STUBS ;
22
+ import static org .mockito .Mockito .times ;
23
+ import static org .mockito .Mockito .verify ;
22
24
import static org .powermock .api .mockito .PowerMockito .mock ;
23
25
import static org .powermock .api .mockito .PowerMockito .when ;
24
26
35
37
import java .io .InputStream ;
36
38
import java .io .StringReader ;
37
39
import java .io .StringWriter ;
40
+ import java .net .HttpURLConnection ;
38
41
import java .nio .charset .StandardCharsets ;
39
42
import java .time .Instant ;
40
43
import java .time .ZoneOffset ;
41
44
import java .time .ZonedDateTime ;
42
45
import java .util .Base64 ;
46
+ import java .util .Date ;
43
47
import java .util .List ;
44
48
import java .util .Optional ;
45
49
import java .util .logging .LogRecord ;
@@ -74,6 +78,8 @@ public class SnippetsTests {
74
78
private static final Logger BACKGROUND_LOGGER = Logger .getLogger (HelloBackground .class .getName ());
75
79
private static final Logger PUBSUB_LOGGER = Logger .getLogger (HelloPubSub .class .getName ());
76
80
private static final Logger GCS_LOGGER = Logger .getLogger (HelloGcs .class .getName ());
81
+ private static final Logger GCS_GENERIC_LOGGER = Logger .getLogger (
82
+ HelloGcsGeneric .class .getName ());
77
83
private static final Logger STACKDRIVER_LOGGER = Logger .getLogger (
78
84
StackdriverLogging .class .getName ());
79
85
private static final Logger RETRY_LOGGER = Logger .getLogger (RetryPubSub .class .getName ());
@@ -109,6 +115,7 @@ public static void beforeClass() {
109
115
REMOTE_CONFIG_LOGGER .addHandler (logHandler );
110
116
AUTH_LOGGER .addHandler (logHandler );
111
117
REACTIVE_LOGGER .addHandler (logHandler );
118
+ GCS_GENERIC_LOGGER .addHandler (logHandler );
112
119
}
113
120
114
121
@ Before
@@ -131,14 +138,6 @@ public void beforeTest() throws IOException {
131
138
firestoreMock = mock (Firestore .class );
132
139
when (firestoreMock .document (any ())).thenReturn (referenceMock );
133
140
134
- // Use the same logging handler for all tests
135
- Logger .getLogger (HelloBackground .class .getName ()).addHandler (logHandler );
136
- Logger .getLogger (HelloPubSub .class .getName ()).addHandler (logHandler );
137
- Logger .getLogger (HelloGcs .class .getName ()).addHandler (logHandler );
138
- Logger .getLogger (StackdriverLogging .class .getName ()).addHandler (logHandler );
139
- Logger .getLogger (RetryPubSub .class .getName ()).addHandler (logHandler );
140
- Logger .getLogger (InfiniteRetryPubSub .class .getName ()).addHandler (logHandler );
141
-
142
141
logHandler .clear ();
143
142
}
144
143
@@ -156,6 +155,26 @@ public void helloWorldTest() throws IOException {
156
155
assertThat (responseOut .toString ()).contains ("Hello World!" );
157
156
}
158
157
158
+ @ Test
159
+ public void functionsHelloworldStorageGeneric_shouldPrintEvent () throws IOException {
160
+ GcsEvent event = new GcsEvent ();
161
+ event .bucket = "some-bucket" ;
162
+ event .name = "some-file.txt" ;
163
+ event .timeCreated = new Date ();
164
+ event .updated = new Date ();
165
+
166
+ MockContext context = new MockContext ();
167
+ context .eventType = "google.storage.object.metadataUpdate" ;
168
+
169
+ new HelloGcsGeneric ().accept (event , context );
170
+
171
+ List <LogRecord > logs = logHandler .getStoredLogRecords ();
172
+ assertThat (logs .get (1 ).getMessage ()).isEqualTo (
173
+ "Event Type: google.storage.object.metadataUpdate" );
174
+ assertThat (logs .get (2 ).getMessage ()).isEqualTo ("Bucket: some-bucket" );
175
+ assertThat (logs .get (3 ).getMessage ()).isEqualTo ("File: some-file.txt" );
176
+ }
177
+
159
178
@ Test
160
179
public void logHelloWorldTest () throws IOException {
161
180
new LogHelloWorld ().service (request , response );
@@ -329,6 +348,39 @@ public void helloHttp_bodyParamsPost() throws IOException {
329
348
assertThat (responseOut .toString ()).isEqualTo ("Hello Jane!" );
330
349
}
331
350
351
+ @ Test
352
+ public void functionsHelloworldMethod_shouldAcceptGet () throws IOException {
353
+ when (request .getMethod ()).thenReturn ("GET" );
354
+
355
+ new HelloMethod ().service (request , response );
356
+
357
+ writerOut .flush ();
358
+ verify (response , times (1 )).setStatusCode (HttpURLConnection .HTTP_OK );
359
+ assertThat (responseOut .toString ()).isEqualTo ("Hello world!" );
360
+ }
361
+
362
+ @ Test
363
+ public void functionsHelloworldMethod_shouldForbidPut () throws IOException {
364
+ when (request .getMethod ()).thenReturn ("PUT" );
365
+
366
+ new HelloMethod ().service (request , response );
367
+
368
+ writerOut .flush ();
369
+ verify (response , times (1 )).setStatusCode (HttpURLConnection .HTTP_FORBIDDEN );
370
+ assertThat (responseOut .toString ()).isEqualTo ("Forbidden!" );
371
+ }
372
+
373
+ @ Test
374
+ public void functionsHelloworldMethod_shouldErrorOnPost () throws IOException {
375
+ when (request .getMethod ()).thenReturn ("POST" );
376
+
377
+ new HelloMethod ().service (request , response );
378
+
379
+ writerOut .flush ();
380
+ verify (response , times (1 )).setStatusCode (HttpURLConnection .HTTP_BAD_METHOD );
381
+ assertThat (responseOut .toString ()).isEqualTo ("Something blew up!" );
382
+ }
383
+
332
384
@ Test (expected = RuntimeException .class )
333
385
public void retryPubsub_handlesRetryMsg () throws IOException {
334
386
String data = "{\" retry\" : true}" ;
0 commit comments