|
| 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 | +} |
0 commit comments