FirebaseRtdb implements RawBackgroundFunction { @Override public void accept(String json, Context context) { logger.info("Function triggered by change to: " + context.resource()); JsonObject body = gson.fromJson(json, JsonObject.class); boolean isAdmin = false; if (body != null && body.has("auth")) { JsonObject authObj = body.getAsJsonObject("auth"); isAdmin = authObj.has("admin") && authObj.get("admin").getAsBoolean(); } logger.info("Admin?: " + isAdmin); if (body != null && body.has("delta")) { logger.info("Delta:"); logger.info(body.get("delta").toString()); } }  @Override void accept(String json, Context context);  }
@Test public void functionsFirebaseRtdb_shouldDefaultAdminToZero() { MockContext context = new MockContext(); context.resource = "resource_1"; new FirebaseRtdb().accept("", context); List<LogRecord> logs = LOG_HANDLER.getStoredLogRecords(); Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by change to: resource_1"); Truth.assertThat(logs.get(1).getMessage()).isEqualTo("Admin?: false"); }
@Test public void functionsFirebaseRtdb_shouldDisplayAdminStatus() { String jsonStr = gson.toJson(Map.of("auth", Map.of("admin", true))); MockContext context = new MockContext(); context.resource = "resource_1"; context.eventType = "event_type_2"; new FirebaseRtdb().accept(jsonStr, context); List<LogRecord> logs = LOG_HANDLER.getStoredLogRecords(); Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by change to: resource_1"); Truth.assertThat(logs.get(1).getMessage()).isEqualTo("Admin?: true"); }
@Test public void functionsFirebaseRtdb_shouldShowDelta() { String jsonStr = gson.toJson(Map.of("delta", Map.of("value", 2))); MockContext context = new MockContext(); context.resource = "resource_1"; context.eventType = "event_type_2"; new FirebaseRtdb().accept(jsonStr, context); List<LogRecord> logs = LOG_HANDLER.getStoredLogRecords(); Truth.assertThat(logs.size()).isEqualTo(4); Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by change to: resource_1"); Truth.assertThat(logs.get(2).getMessage()).isEqualTo("Delta:"); String expectedJsonStr = gson.toJson(Map.of("value", 2)); Truth.assertThat(logs.get(3).getMessage()).isEqualTo(expectedJsonStr); }