FirebaseFirestoreReactive implements RawBackgroundFunction { @Override public void accept(String json, Context context) { JsonObject body = gson.fromJson(json, JsonObject.class); JsonObject tempJson = body.getAsJsonObject("value"); String currentValue = null; if (tempJson != null) { tempJson = tempJson.getAsJsonObject("fields"); } if (tempJson != null) { tempJson = tempJson.getAsJsonObject("original"); } if (tempJson != null && tempJson.has("stringValue")) { currentValue = tempJson.get("stringValue").getAsString(); } if (currentValue == null) { throw new IllegalArgumentException("Malformed JSON: " + json); } String newValue = currentValue.toUpperCase(Locale.getDefault()); Map<String, String> newFields = Map.of("original", newValue); String affectedDoc = context.resource().split("/documents/")[1].replace("\"", ""); if (!currentValue.equals(newValue)) { logger.info(String.format("Replacing value: %s --> %s", currentValue, newValue)); try { FIRESTORE.document(affectedDoc).set(newFields, SetOptions.merge()).get(); } catch (ExecutionException | InterruptedException e) { logger.log(Level.SEVERE, "Error updating Firestore document: " + e.getMessage(), e); } } else { logger.info(String.format("Value is already upper-case.")); } } FirebaseFirestoreReactive();   FirebaseFirestoreReactive(Firestore firestore); @Override void accept(String json, Context context);  }
@Test public void functionsFirebaseReactive_shouldCapitalizeOriginalValue() { String jsonStr = gson.toJson(Map.of("value", Map.of("fields", Map.of("original", Map.of("stringValue", "foo"))))); MockContext context = new MockContext(); context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345"; FirebaseFirestoreReactive functionInstance = new FirebaseFirestoreReactive(firestoreMock); functionInstance.accept(jsonStr, context); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Replacing value: foo --> FOO"); }
@Test public void functionsFirebaseReactive_shouldIgnoreCapitalizedValues() { String jsonStr = gson.toJson(Map.of("value", Map.of("fields", Map.of("original", Map.of("stringValue", "FOO"))))); MockContext context = new MockContext(); context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345"; FirebaseFirestoreReactive functionInstance = new FirebaseFirestoreReactive(firestoreMock); functionInstance.accept(jsonStr, context); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Value is already upper-case."); }
@Test public void functionsFirebaseReactive_shouldReportBadJson() { String jsonStr = gson.toJson(Map.of("value", Map.of("fields", Map.of("original", Map.of("missingValue", "foo"))))); MockContext context = new MockContext(); context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345"; FirebaseFirestoreReactive functionInstance = new FirebaseFirestoreReactive(firestoreMock); IllegalArgumentException e = Assertions.assertThrows( IllegalArgumentException.class, () -> functionInstance.accept(jsonStr, context)); Truth.assertThat(e).hasMessageThat().startsWith("Malformed JSON"); }