Skip to content

Commit 51bc934

Browse files
authored
Update GCFv2 PubSub sample (GoogleCloudPlatform#5971)
* Update GCFv2 PubSub sample * remove unneeded attributes * remove cloudevent pojo * lint * lint
1 parent efa476c commit 51bc934

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

functions/v2/pubsub/subscribe-to-topic/pom.xml

+2-9
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,12 @@
4444
<version>1.0.4</version>
4545
<scope>provided</scope>
4646
</dependency>
47-
48-
<dependency>
49-
<groupId>io.cloudevents</groupId>
50-
<artifactId>cloudevents-api</artifactId>
51-
<version>2.2.0</version>
52-
</dependency>
53-
5447
<dependency>
5548
<groupId>com.google.code.gson</groupId>
5649
<artifactId>gson</artifactId>
57-
<version>2.8.7</version>
50+
<version>2.8.8</version>
51+
<scope>compile</scope>
5852
</dependency>
59-
6053
<!-- The following dependencies are only required for testing -->
6154
<dependency>
6255
<groupId>com.google.truth</groupId>

functions/v2/pubsub/subscribe-to-topic/src/main/java/functions/SubscribeToTopic.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package functions;
1818

1919
// [START functions_cloudevent_pubsub]
20-
2120
import com.google.cloud.functions.CloudEventsFunction;
2221
import com.google.gson.Gson;
2322
import functions.eventpojos.PubSubBody;
@@ -31,13 +30,19 @@ public class SubscribeToTopic implements CloudEventsFunction {
3130

3231
@Override
3332
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());
3935

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+
}
4146
}
4247
}
4348
// [END functions_cloudevent_pubsub]

functions/v2/pubsub/subscribe-to-topic/src/test/java/functions/SubscribeToTopicTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static void beforeClass() {
4141

4242
@Test
4343
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());
4646
String encodedData = new String("{\"message\": { \"data\": \"" + encodedMessage + "\"} }");
4747

4848
CloudEvent event =
@@ -55,6 +55,7 @@ public void functionsPubsubSubscribe_shouldPrintPubsubMessage() throws Exception
5555

5656
new SubscribeToTopic().accept(event);
5757

58-
assertThat("Hello World").isEqualTo(logHandler.getStoredLogRecords().get(0).getMessage());
58+
assertThat("Event data: " + msg).isEqualTo(
59+
logHandler.getStoredLogRecords().get(2).getMessage());
5960
}
6061
}

0 commit comments

Comments
 (0)