File tree 3 files changed +18
-19
lines changed
functions/v2/pubsub/subscribe-to-topic
3 files changed +18
-19
lines changed Original file line number Diff line number Diff line change 44
44
<version >1.0.4</version >
45
45
<scope >provided</scope >
46
46
</dependency >
47
-
48
- <dependency >
49
- <groupId >io.cloudevents</groupId >
50
- <artifactId >cloudevents-api</artifactId >
51
- <version >2.2.0</version >
52
- </dependency >
53
-
54
47
<dependency >
55
48
<groupId >com.google.code.gson</groupId >
56
49
<artifactId >gson</artifactId >
57
- <version >2.8.7</version >
50
+ <version >2.8.8</version >
51
+ <scope >compile</scope >
58
52
</dependency >
59
-
60
53
<!-- The following dependencies are only required for testing -->
61
54
<dependency >
62
55
<groupId >com.google.truth</groupId >
Original file line number Diff line number Diff line change 17
17
package functions ;
18
18
19
19
// [START functions_cloudevent_pubsub]
20
-
21
20
import com .google .cloud .functions .CloudEventsFunction ;
22
21
import com .google .gson .Gson ;
23
22
import functions .eventpojos .PubSubBody ;
@@ -31,13 +30,19 @@ public class SubscribeToTopic implements CloudEventsFunction {
31
30
32
31
@ Override
33
32
public void accept (CloudEvent event ) {
34
- Gson gson = new Gson ();
35
- PubSubBody pubSubBody = gson .fromJson (new String (event .getData ().toBytes ()), PubSubBody .class );
36
- String messageString =
37
- new String (
38
- Base64 .getDecoder ().decode (pubSubBody .getMessage ().getData ()), StandardCharsets .UTF_8 );
33
+ logger .info ("Event: " + event .getId ());
34
+ logger .info ("Event Type: " + event .getType ());
39
35
40
- logger .info (messageString );
36
+ if (event .getData () != null ) {
37
+ String cloudEventData = new String (event .getData ().toBytes (), StandardCharsets .UTF_8 );
38
+ Gson gson = new Gson ();
39
+ PubSubBody body = gson .fromJson (cloudEventData , PubSubBody .class );
40
+ // Retrieve PubSub message data
41
+ String encodedData = body .getMessage ().getData ();
42
+ String decodedData =
43
+ new String (Base64 .getDecoder ().decode (encodedData ), StandardCharsets .UTF_8 );
44
+ logger .info ("Event data: " + decodedData );
45
+ }
41
46
}
42
47
}
43
48
// [END functions_cloudevent_pubsub]
Original file line number Diff line number Diff line change @@ -41,8 +41,8 @@ public static void beforeClass() {
41
41
42
42
@ Test
43
43
public void functionsPubsubSubscribe_shouldPrintPubsubMessage () throws Exception {
44
-
45
- String encodedMessage = Base64 .getEncoder ().encodeToString ("Hello World" .getBytes ());
44
+ String msg = "Hello World" ;
45
+ String encodedMessage = Base64 .getEncoder ().encodeToString (msg .getBytes ());
46
46
String encodedData = new String ("{\" message\" : { \" data\" : \" " + encodedMessage + "\" } }" );
47
47
48
48
CloudEvent event =
@@ -55,6 +55,7 @@ public void functionsPubsubSubscribe_shouldPrintPubsubMessage() throws Exception
55
55
56
56
new SubscribeToTopic ().accept (event );
57
57
58
- assertThat ("Hello World" ).isEqualTo (logHandler .getStoredLogRecords ().get (0 ).getMessage ());
58
+ assertThat ("Event data: " + msg ).isEqualTo (
59
+ logHandler .getStoredLogRecords ().get (2 ).getMessage ());
59
60
}
60
61
}
You can’t perform that action at this time.
0 commit comments