FirebaseFirestore implements RawBackgroundFunction { @Override public void accept(String json, Context context) { JsonObject body = gson.fromJson(json, JsonObject.class); logger.info("Function triggered by event on: " + context.resource()); logger.info("Event type: " + context.eventType()); if (body != null && body.has("oldValue")) { logger.info("Old value:"); logger.info(body.get("oldValue").getAsString()); } if (body != null && body.has("value")) { logger.info("New value:"); logger.info(body.get("value").getAsString()); } }  @Override void accept(String json, Context context);  }
@Test public void functionsFirebaseFirestore_shouldIgnoreMissingValuesTest() { MockContext context = new MockContext(); context.resource = "resource_1"; context.eventType = "event_type_2"; new FirebaseFirestore().accept("", context); List<LogRecord> logs = LOG_HANDLER.getStoredLogRecords(); Truth.assertThat(logs.size()).isEqualTo(2); Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by event on: resource_1"); Truth.assertThat(logs.get(1).getMessage()).isEqualTo("Event type: event_type_2"); }
@Test public void functionsFirebaseFirestore_shouldProcessPresentValues() { String jsonStr = gson.toJson(Map.of("oldValue", 999, "value", 777)); MockContext context = new MockContext(); context.resource = "resource_1"; context.eventType = "event_type_2"; new FirebaseFirestore().accept(jsonStr, context); List<LogRecord> logs = LOG_HANDLER.getStoredLogRecords(); Truth.assertThat(logs.size()).isEqualTo(6); Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by event on: resource_1"); Truth.assertThat(logs.get(1).getMessage()).isEqualTo("Event type: event_type_2"); Truth.assertThat(logs.get(2).getMessage()).isEqualTo("Old value:"); Truth.assertThat(logs.get(4).getMessage()).isEqualTo("New value:"); }