Skip to content

Commit e53e3b2

Browse files
Add conformance tests (GoogleCloudPlatform#84)
* Add conformance tests * add github action * Address code review comments Typos, target version, try with resources, java time * Use GoogleCloudPlatform/functions-framework-conformance/[email protected]
1 parent e8293e5 commit e53e3b2

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

.github/workflows/conformance.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Java Conformance CI
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
java: [
13+
11.x
14+
# 12.x,
15+
# 13.x
16+
]
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Set up JDK ${{ matrix.java }}
21+
uses: actions/setup-java@v1
22+
with:
23+
java-version: ${{ matrix.java }}
24+
25+
- name: Setup Go
26+
uses: actions/setup-go@v2
27+
with:
28+
go-version: '1.15'
29+
30+
- name: Build API with Maven
31+
run: (cd functions-framework-api/ && mvn install)
32+
33+
- name: Build invoker with Maven
34+
run: (cd invoker/ && mvn install)
35+
36+
- name: Run HTTP conformance tests
37+
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected]
38+
with:
39+
functionType: 'http'
40+
useBuildpacks: false
41+
validateMapping: false
42+
cmd: "'mvn -f invoker/conformance/pom.xml function:run -Drun.functionTarget=com.google.cloud.functions.conformance.HttpConformanceFunction'"
43+
startDelay: 10
44+
45+
- name: Run cloudevent conformance tests
46+
uses: GoogleCloudPlatform/functions-framework-conformance/[email protected]
47+
with:
48+
functionType: 'cloudevent'
49+
useBuildpacks: false
50+
validateMapping: false
51+
cmd: "'mvn -f invoker/conformance/pom.xml function:run -Drun.functionTarget=com.google.cloud.functions.conformance.CloudEventsConformanceFunction'"
52+
startDelay: 10

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ properties
4747

4848
# IDE
4949
.vscode/
50+
51+
# Conformance testing
52+
function_output.json
53+
serverlog_stderr.txt
54+
serverlog_stdout.txt

invoker/conformance/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>java-function-invoker-parent</artifactId>
6+
<groupId>com.google.cloud.functions.invoker</groupId>
7+
<version>1.0.3-SNAPSHOT</version>
8+
</parent>
9+
10+
<groupId>com.google.cloud.functions.invoker</groupId>
11+
<artifactId>conformance</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
14+
<name>GCF Confromance Tests</name>
15+
<description>
16+
A GCF project used to validate conformance to the Functions Framework contract
17+
using the Functions Framework Conformance tools.
18+
</description>
19+
<url>https://github.com/GoogleCloudPlatform/functions-framework-conformance</url>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<maven.compiler.source>11</maven.compiler.source>
24+
<maven.compiler.target>11</maven.compiler.target>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.google.cloud.functions</groupId>
30+
<artifactId>functions-framework-api</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.google.code.gson</groupId>
34+
<artifactId>gson</artifactId>
35+
<version>2.8.6</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<pluginManagement>
41+
<plugins>
42+
<plugin>
43+
<groupId>com.google.cloud.functions</groupId>
44+
<artifactId>function-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</pluginManagement>
48+
</build>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.cloud.functions.conformance;
16+
17+
import static java.nio.charset.StandardCharsets.UTF_8;
18+
19+
import com.google.cloud.functions.CloudEventsFunction;
20+
import com.google.gson.Gson;
21+
import com.google.gson.GsonBuilder;
22+
import com.google.gson.JsonObject;
23+
import io.cloudevents.CloudEvent;
24+
import java.io.BufferedWriter;
25+
import java.io.FileWriter;
26+
import java.time.format.DateTimeFormatter;
27+
28+
/**
29+
* This class is used by the Functions Framework Conformance Tools to validate
30+
* the framework's Cloud Events API. It can be run with the following command:
31+
*
32+
* <pre>{@code
33+
* $ functions-framework-conformance-client \
34+
* -cmd="mvn function:run -Drun.functionTarget=com.google.cloud.functions.conformance.CloudEventsConformanceFunction" \
35+
* -type=cloudevent \
36+
* -buildpacks=false \
37+
* -validate-mapping=false \
38+
* -start-delay=5
39+
* }</pre>
40+
*/
41+
public class CloudEventsConformanceFunction implements CloudEventsFunction {
42+
43+
private static final Gson gson = new GsonBuilder()
44+
.serializeNulls()
45+
.setPrettyPrinting()
46+
.create();
47+
48+
@Override
49+
public void accept(CloudEvent event) throws Exception {
50+
try (BufferedWriter writer = new BufferedWriter(new FileWriter("function_output.json"))) {
51+
writer.write(serialize(event));
52+
}
53+
}
54+
55+
/**
56+
* Create a structured JSON representation of a cloud event
57+
*/
58+
private String serialize(CloudEvent event) {
59+
JsonObject jsonEvent = new JsonObject();
60+
61+
jsonEvent.addProperty("id", event.getId());
62+
jsonEvent.addProperty("source", event.getSource().toString());
63+
jsonEvent.addProperty("type", event.getType());
64+
jsonEvent.addProperty("datacontenttype", event.getDataContentType());
65+
jsonEvent.addProperty("subject", event.getSubject());
66+
jsonEvent.addProperty("specversion", event.getSpecVersion().toString());
67+
68+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
69+
jsonEvent.addProperty("time", event.getTime().format(formatter));
70+
71+
String payloadJson = new String(event.getData().toBytes(), UTF_8);
72+
JsonObject jsonObject = new Gson().fromJson(payloadJson, JsonObject.class);
73+
jsonEvent.add("data", jsonObject);
74+
75+
return gson.toJson(jsonEvent);
76+
}
77+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.cloud.functions.conformance;
16+
17+
import com.google.cloud.functions.HttpFunction;
18+
import com.google.cloud.functions.HttpRequest;
19+
import com.google.cloud.functions.HttpResponse;
20+
import java.io.BufferedReader;
21+
import java.io.BufferedWriter;
22+
import java.io.FileWriter;
23+
import java.io.IOException;
24+
25+
/**
26+
* This class is used by the Functions Framework Conformance Tools to validate
27+
* the framework's HTTP API. It can be run with the following command:
28+
*
29+
* <pre>{@code
30+
* $ functions-framework-conformance-client \
31+
* -cmd="mvn function:run -Drun.functionTarget=com.google.cloud.functions.conformance.HttpConformanceFunction" \
32+
* -type=http \
33+
* -buildpacks=false \
34+
* -validate-mapping=false \
35+
* -start-delay=5
36+
* }</pre>
37+
*/
38+
public class HttpConformanceFunction implements HttpFunction {
39+
40+
@Override
41+
public void service(HttpRequest request, HttpResponse response)
42+
throws IOException {
43+
try (BufferedReader reader = request.getReader();
44+
BufferedWriter writer = new BufferedWriter(new FileWriter("function_output.json"))) {
45+
int c;
46+
while ((c = reader.read()) != -1) {
47+
writer.write(c);
48+
}
49+
}
50+
}
51+
}

invoker/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<module>core</module>
2929
<module>testfunction</module>
3030
<module>function-maven-plugin</module>
31+
<module>conformance</module>
3132
</modules>
3233

3334
<properties>

0 commit comments

Comments
 (0)