|
2 | 2 |
|
3 | 3 | import static com.google.cloud.functions.invoker.BackgroundFunctionExecutor.backgroundFunctionTypeArgument;
|
4 | 4 | import static com.google.common.truth.Truth8.assertThat;
|
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertNotNull; |
5 | 7 |
|
6 | 8 | import com.google.cloud.functions.BackgroundFunction;
|
7 | 9 | import com.google.cloud.functions.Context;
|
| 10 | +import com.google.gson.JsonObject; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStreamReader; |
| 14 | +import java.io.Reader; |
8 | 15 | import java.util.Map;
|
9 | 16 | import org.junit.Test;
|
10 | 17 | import org.junit.runner.RunWith;
|
11 | 18 | import org.junit.runners.JUnit4;
|
12 | 19 |
|
| 20 | + |
13 | 21 | @RunWith(JUnit4.class)
|
14 | 22 | public class BackgroundFunctionExecutorTest {
|
15 | 23 | private static class PubSubMessage {
|
@@ -62,4 +70,38 @@ public void backgroundFunctionTypeArgument_raw() {
|
62 | 70 | (Class<? extends BackgroundFunction<?>>) (Class<?>) ForgotTypeParameter.class;
|
63 | 71 | assertThat(backgroundFunctionTypeArgument(c)).isEmpty();
|
64 | 72 | }
|
| 73 | + |
| 74 | + @Test |
| 75 | + public void parseLegacyEventPubSub() throws IOException { |
| 76 | + try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("/pubsub_background.json"))) { |
| 77 | + Event event = BackgroundFunctionExecutor.parseLegacyEvent(reader); |
| 78 | + |
| 79 | + Context context = event.getContext(); |
| 80 | + assertEquals("google.pubsub.topic.publish", context.eventType()); |
| 81 | + assertEquals("1", context.eventId()); |
| 82 | + assertEquals("2021-06-28T05:46:32.390Z", context.timestamp()); |
| 83 | + |
| 84 | + JsonObject data = event.getData().getAsJsonObject(); |
| 85 | + assertEquals(data.get("data").getAsString(), "eyJmb28iOiJiYXIifQ=="); |
| 86 | + String attr = data.get("attributes").getAsJsonObject().get("test").getAsString(); |
| 87 | + assertEquals(attr, "123"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void parseLegacyEventPubSubEmulator() throws IOException { |
| 93 | + try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("/pubsub_emulator.json"))) { |
| 94 | + Event event = BackgroundFunctionExecutor.parseLegacyEvent(reader); |
| 95 | + |
| 96 | + Context context = event.getContext(); |
| 97 | + assertEquals("google.pubsub.topic.publish", context.eventType()); |
| 98 | + assertEquals("1", context.eventId()); |
| 99 | + assertNotNull(context.timestamp()); |
| 100 | + |
| 101 | + JsonObject data = event.getData().getAsJsonObject(); |
| 102 | + assertEquals(data.get("data").getAsString(), "eyJmb28iOiJiYXIifQ=="); |
| 103 | + String attr = data.get("attributes").getAsJsonObject().get("test").getAsString(); |
| 104 | + assertEquals(attr, "123"); |
| 105 | + } |
| 106 | + } |
65 | 107 | }
|
0 commit comments