diff --git a/functions/snippets/pom.xml b/functions/snippets/pom.xml index d5cc0fc218f..6787b9a8593 100644 --- a/functions/snippets/pom.xml +++ b/functions/snippets/pom.xml @@ -67,6 +67,14 @@ 1.19.0 test + + + + com.google.cloud.functions + functions-framework-api + 1.0.0-alpha-2-rc3 + jar + diff --git a/functions/snippets/src/main/java/FirebaseAuth.java b/functions/snippets/src/main/java/FirebaseAuth.java new file mode 100644 index 00000000000..87a7df6242f --- /dev/null +++ b/functions/snippets/src/main/java/FirebaseAuth.java @@ -0,0 +1,38 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START functions_firebase_auth] +import com.google.cloud.functions.Context; +import com.google.cloud.functions.RawBackgroundFunction; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import java.util.logging.Logger; + +public class FirebaseAuth implements RawBackgroundFunction { + + // Use GSON (https://github.com/google/gson) to parse JSON content. + private Gson gsonParser = new Gson(); + + private static final Logger LOGGER = Logger.getLogger(FirebaseAuth.class.getName()); + + @Override + public void accept(String json, Context context) { + JsonObject body = gsonParser.fromJson(json, JsonObject.class); + LOGGER.info("Function triggered by event: " + json); + } +} + +// [END functions_firebase_auth] diff --git a/functions/snippets/src/test/java/SnippetsTests.java b/functions/snippets/src/test/java/SnippetsTests.java index 3d27e2ebc64..e34f9fe430b 100644 --- a/functions/snippets/src/test/java/SnippetsTests.java +++ b/functions/snippets/src/test/java/SnippetsTests.java @@ -217,4 +217,9 @@ public void helloExecutionCount() throws IOException { new Concepts().executionCount(request, response); assertThat(responseOut.toString(), containsString("Instance execution count: 1")); } + + @Test + public void firebaseAuth() throws IOException { + new FirebaseAuth().accept("{\"foo\": \"bar\"}", null); + } }