// 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.JsonObject;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
package com.google.cloud.functions.invoker.testfunctions;


/**
 * Extract the targetFile property from the data of the JSON payload, and write to it a JSON
 * encoding of this payload and the context. The JSON format is chosen to be identical to the
 * EventFlow format that we currently use in GCF, and the file that we write should in fact be
 * identical to the JSON payload that the Functions Framework received from the client in the test.
 * This will need to be rewritten when we switch to CloudEvents.
 */
public class BackgroundSnoop implements RawBackgroundFunction {
  @Override
  public void accept(String json, Context context) {
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
    String targetFile = jsonObject.get("targetFile").getAsString();
    if (targetFile == null) {
      throw new IllegalArgumentException("Expected targetFile in JSON payload");
    }
    JsonObject resourceJson = gson.fromJson(context.resource(), JsonObject.class);
    JsonObject contextJson = new JsonObject();
// 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/core/src/test/java/com/google/cloud/functions/invoker/testfunctions/BackgroundSnoop.java
import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;

package com.google.cloud.functions.invoker.testfunctions;


/**
 * Extract the targetFile property from the data of the JSON payload, and write to it a JSON
 * encoding of this payload and the context. The JSON format is chosen to be identical to the
 * EventFlow format that we currently use in GCF, and the file that we write should in fact be
 * identical to the JSON payload that the Functions Framework received from the client in the test.
 * This will need to be rewritten when we switch to CloudEvents.
 */
public class BackgroundSnoop implements RawBackgroundFunction {
  @Override
  public void accept(String json, Context context) {
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
    String targetFile = jsonObject.get("targetFile").getAsString();
    if (targetFile == null) {
      throw new IllegalArgumentException("Expected targetFile in JSON payload");
    }
    JsonObject resourceJson = gson.fromJson(context.resource(), JsonObject.class);
    JsonObject contextJson = new JsonObject();
    contextJson.addProperty("eventId", context.eventId());