Skip to content

Commit 382bd10

Browse files
Add conformance tests for background events (GoogleCloudPlatform#97)
Previously we had been running conformance tests for background function signature types. This enables these tests in the conformance github action. Running the tests uncovered one bug in which JSON fiels with null values were being dropped from the data param passed to RawBackgroundFunctions. Fixing this is a non-breaking change so I bumped the minor verison of the maven package.
1 parent e96a3d9 commit 382bd10

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

.github/workflows/conformance.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,27 @@ jobs:
3434
run: (cd invoker/ && mvn install)
3535

3636
- name: Run HTTP conformance tests
37-
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected].9
37+
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected].12
3838
with:
3939
functionType: 'http'
4040
useBuildpacks: false
41-
validateMapping: false
4241
cmd: "'mvn -f invoker/conformance/pom.xml function:run -Drun.functionTarget=com.google.cloud.functions.conformance.HttpConformanceFunction'"
4342
startDelay: 10
4443

44+
- name: Run background event conformance tests
45+
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected]
46+
with:
47+
functionType: 'legacyevent'
48+
useBuildpacks: false
49+
validateMapping: false
50+
cmd: "'mvn -f invoker/conformance/pom.xml function:run -Drun.functionTarget=com.google.cloud.functions.conformance.BackgroundEventConformanceFunction'"
51+
startDelay: 10
52+
4553
- name: Run cloudevent conformance tests
46-
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected].9
54+
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected].12
4755
with:
4856
functionType: 'cloudevent'
4957
useBuildpacks: false
58+
validateMapping: false
5059
cmd: "'mvn -f invoker/conformance/pom.xml function:run -Drun.functionTarget=com.google.cloud.functions.conformance.CloudEventsConformanceFunction'"
5160
startDelay: 10
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.google.cloud.functions.conformance;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.cloud.functions.Context;
8+
import com.google.cloud.functions.RawBackgroundFunction;
9+
import java.io.BufferedWriter;
10+
import java.io.FileWriter;
11+
12+
/**
13+
* This class is used by the Functions Framework Conformance Tools to validate
14+
* the framework's Background Event API. It can be run with the following
15+
* command:
16+
*
17+
* <pre>
18+
* {@code
19+
* $ functions-framework-conformance-client \
20+
* -cmd="mvn function:run -Drun.functionTarget=com.google.cloud.functions.conformance.BackgroundEventConformanceFunction" \
21+
* -type=legacyevent \
22+
* -buildpacks=false \
23+
* -validate-mapping=false \
24+
* -start-delay=10
25+
* }
26+
* </pre>
27+
*/
28+
public class BackgroundEventConformanceFunction implements RawBackgroundFunction {
29+
30+
private static final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
31+
32+
@Override
33+
public void accept(String data, Context context) throws Exception {
34+
try (BufferedWriter writer = new BufferedWriter(new FileWriter("function_output.json"))) {
35+
writer.write(serialize(data, context));
36+
}
37+
}
38+
39+
/**
40+
* Create a structured JSON representation of the request context and data
41+
*/
42+
private String serialize(String data, Context context) {
43+
JsonObject contextJson = new JsonObject();
44+
contextJson.addProperty("eventId", context.eventId());
45+
contextJson.addProperty("timestamp", context.timestamp());
46+
contextJson.addProperty("eventType", context.eventType());
47+
48+
JsonElement resource = gson.fromJson(context.resource(), JsonElement.class);
49+
contextJson.add("resource", resource);
50+
51+
JsonObject dataJson = gson.fromJson(data, JsonObject.class);
52+
53+
JsonObject json = new JsonObject();
54+
json.add("data", dataJson);
55+
json.add("context", contextJson);
56+
return gson.toJson(json);
57+
}
58+
59+
}

invoker/core/src/main/java/com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ final ClassLoader functionClassLoader() {
241241
}
242242

243243
private static class RawFunctionExecutor extends FunctionExecutor<Map<?, ?>> {
244+
private static Gson gson = new GsonBuilder().serializeNulls().create();
244245
private final RawBackgroundFunction function;
245246

246247
RawFunctionExecutor(RawBackgroundFunction function) {
@@ -250,7 +251,7 @@ private static class RawFunctionExecutor extends FunctionExecutor<Map<?, ?>> {
250251

251252
@Override
252253
void serviceLegacyEvent(Event legacyEvent) throws Exception {
253-
function.accept(new Gson().toJson(legacyEvent.getData()), legacyEvent.getContext());
254+
function.accept(gson.toJson(legacyEvent.getData()), legacyEvent.getContext());
254255
}
255256

256257
@Override

0 commit comments

Comments
 (0)