// Path: functions-framework-api/src/main/java/com/google/cloud/functions/Context.java
// public interface Context {
//   /**
//    * Returns event ID.
//    *
//    * @return event ID
//    */
//   String eventId();
// 
//   /**
//    * Returns event timestamp.
//    *
//    * @return event timestamp
//    */
//   String timestamp();
// 
//   /**
//    * Returns event type.
//    *
//    * @return event type
//    */
//   String eventType();
// 
//   /**
//    * Returns event resource.
//    *
//    * @return event resource
//    */
//   String resource();
// 
//   /**
//    * Returns additional attributes from this event. For CloudEvents, the entries in this map will
//    * include the <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#required-attributes">required
//    * attributes</a> and may include <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#required-attributes">optional
//    * attributes</a> and <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#extension-context-attributes">
//    * extension attributes</a>.
//    *
//    * <p>The map returned by this method may be empty but is never null.
//    *
//    * @return additional attributes form this event.
//    */
//   default Map<String, String> attributes() {
//     return Collections.emptyMap();
//   }
// }
// 
// Path: functions-framework-api/src/main/java/com/google/cloud/functions/RawBackgroundFunction.java
// @FunctionalInterface
// public interface RawBackgroundFunction {
//   /**
//    * Called to service an incoming event. This interface is implemented by user code to provide the
//    * action for a given background function. If this method throws any exception (including any
//    * {@link Error}) then the HTTP response will have a 500 status code.
//    *
//    * @param json the payload of the event, as a JSON string.
//    * @param context the context of the event. This is a set of values that every event has,
//    *     separately from the payload, such as timestamp and event type.
//    * @throws Exception to produce a 500 status code in the HTTP response.
//    */
//   void accept(String json, Context context) throws Exception;
// }


import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;
import java.io.FileWriter;
package com.google.cloud.functions.conformance;


/**
 * This class is used by the Functions Framework Conformance Tools to validate the framework's
 * Background Event API. It can be run with the following command:
 *
 * <pre>{@code
 * $ functions-framework-conformance-client \
 *   -cmd="mvn function:run -Drun.functionTarget=com.google.cloud.functions.conformance.BackgroundEventConformanceFunction" \
 *   -type=legacyevent \
 *   -buildpacks=false \
 *   -validate-mapping=false \
 *   -start-delay=10
 * }</pre>
 */
public class BackgroundEventConformanceFunction implements RawBackgroundFunction {

  private static final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();

  @Override
  public void accept(String data, Context context) throws Exception {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter("function_output.json"))) {
      writer.write(serialize(data, context));
    }
  }

  /** Create a structured JSON representation of the request context and data */
// Path: functions-framework-api/src/main/java/com/google/cloud/functions/Context.java
// public interface Context {
//   /**
//    * Returns event ID.
//    *
//    * @return event ID
//    */
//   String eventId();
// 
//   /**
//    * Returns event timestamp.
//    *
//    * @return event timestamp
//    */
//   String timestamp();
// 
//   /**
//    * Returns event type.
//    *
//    * @return event type
//    */
//   String eventType();
// 
//   /**
//    * Returns event resource.
//    *
//    * @return event resource
//    */
//   String resource();
// 
//   /**
//    * Returns additional attributes from this event. For CloudEvents, the entries in this map will
//    * include the <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#required-attributes">required
//    * attributes</a> and may include <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#required-attributes">optional
//    * attributes</a> and <a
//    * href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#extension-context-attributes">
//    * extension attributes</a>.
//    *
//    * <p>The map returned by this method may be empty but is never null.
//    *
//    * @return additional attributes form this event.
//    */
//   default Map<String, String> attributes() {
//     return Collections.emptyMap();
//   }
// }
// 
// Path: functions-framework-api/src/main/java/com/google/cloud/functions/RawBackgroundFunction.java
// @FunctionalInterface
// public interface RawBackgroundFunction {
//   /**
//    * Called to service an incoming event. This interface is implemented by user code to provide the
//    * action for a given background function. If this method throws any exception (including any
//    * {@link Error}) then the HTTP response will have a 500 status code.
//    *
//    * @param json the payload of the event, as a JSON string.
//    * @param context the context of the event. This is a set of values that every event has,
//    *     separately from the payload, such as timestamp and event type.
//    * @throws Exception to produce a 500 status code in the HTTP response.
//    */
//   void accept(String json, Context context) throws Exception;
// }


// Path: invoker/conformance/src/main/java/com/google/cloud/functions/conformance/BackgroundEventConformanceFunction.java
import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;
import java.io.FileWriter;

package com.google.cloud.functions.conformance;


/**
 * This class is used by the Functions Framework Conformance Tools to validate the framework's
 * Background Event API. It can be run with the following command:
 *
 * <pre>{@code
 * $ functions-framework-conformance-client \
 *   -cmd="mvn function:run -Drun.functionTarget=com.google.cloud.functions.conformance.BackgroundEventConformanceFunction" \
 *   -type=legacyevent \
 *   -buildpacks=false \
 *   -validate-mapping=false \
 *   -start-delay=10
 * }</pre>
 */
public class BackgroundEventConformanceFunction implements RawBackgroundFunction {

  private static final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();

  @Override
  public void accept(String data, Context context) throws Exception {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter("function_output.json"))) {
      writer.write(serialize(data, context));
    }
  }

  /** Create a structured JSON representation of the request context and data */
  private String serialize(String data, Context context) {