From 2cf13226bd0b150a4e60287dbf7cdae9147335df Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 11 Nov 2021 10:28:08 -0800 Subject: [PATCH] samples: added webhook sample (#313) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * samples: added webhook sample * fixed lint * added cloud function dependecy * moved cloud function dependecy * changed pom file * moved maven dependency * changed dependecy * added scope * added mocks * added dependency * Added plugin * Orginized code * modified pom file * removed httprequest from test * added to poms * Changed test to include functions * removed scope * change cloud function version * reverted version * changed cloud function version to 1.0.4 * added dependency to more pom files * added plugin * removed dependency from install-wo-bom * removed plugin * added dependency * added plugin * Added Dependency to samples pom file * lint fix * changed dependencies * Added Dependencies * added dependency * lint fix * lint fix * removed dependency * removed dependency * added snapshot dependency * samples: added webhook sample * fixed lint * added cloud function dependecy * moved cloud function dependecy * changed pom file * moved maven dependency * changed dependecy * added scope * added mocks * added dependency * Added plugin * Orginized code * modified pom file * removed httprequest from test * added to poms * Changed test to include functions * removed scope * change cloud function version * reverted version * changed cloud function version to 1.0.4 * added dependency to more pom files * added plugin * removed dependency from install-wo-bom * removed plugin * added dependency * added plugin * Added Dependency to samples pom file * lint fix * changed dependencies * Added Dependencies * added dependency * lint fix * lint fix * removed dependency * removed dependency * added snapshot dependency * Add debugging flags * debug * debug * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update pom.xml add the closing magic tag Co-authored-by: averikitsch Co-authored-by: Owl Bot Co-authored-by: Les Vogel --- dialogflow-cx/snippets/pom.xml | 23 +++++- .../src/main/java/dialogflow/cx/Example.java | 67 +++++++++++++++++ .../test/java/dialogflow/cx/ExampleIT.java | 72 +++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/Example.java create mode 100644 dialogflow-cx/snippets/src/test/java/dialogflow/cx/ExampleIT.java diff --git a/dialogflow-cx/snippets/pom.xml b/dialogflow-cx/snippets/pom.xml index 5fc006bd103..a1f8bc350ed 100644 --- a/dialogflow-cx/snippets/pom.xml +++ b/dialogflow-cx/snippets/pom.xml @@ -30,13 +30,34 @@ google-cloud-dialogflow-cx 0.10.1 - + + com.google.code.gson + gson + 2.8.7 + + + com.google.cloud.functions + functions-framework-api + 1.0.4 + junit junit 4.13.2 test + + com.google.cloud.functions + function-maven-plugin + 0.9.7 + test + + + org.mockito + mockito-core + 3.12.4 + test + com.google.truth truth diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/Example.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/Example.java new file mode 100644 index 00000000000..f91c8362a4a --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/Example.java @@ -0,0 +1,67 @@ +/* + * Copyright 2021 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. + */ + +package dialogflow.cx; + +// [START dialogflow_webhook] + +// TODO: add GSON dependency to Pom file +// (https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5) +// TODO: Uncomment the line bellow before running cloud function +// package com.example; + +import com.google.cloud.functions.HttpFunction; +import com.google.cloud.functions.HttpRequest; +import com.google.cloud.functions.HttpResponse; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.io.BufferedWriter; + +public class Example implements HttpFunction { + + public void service(HttpRequest request, HttpResponse response) throws Exception { + JsonParser parser = new JsonParser(); + Gson gson = new GsonBuilder().create(); + + JsonObject job = gson.fromJson(request.getReader(), JsonObject.class); + String str = job.getAsJsonObject("fulfillmentInfo").getAsJsonPrimitive("tag").toString(); + JsonObject o = null; + String a = '"' + "Default Welcome Intent" + '"'; + String b = '"' + "get-agent-name" + '"'; + String responseText = ""; + + if (str.equals(a)) { + responseText = '"' + "Hello from a Java GCF Webhook" + '"'; + } else if (str.equals(b)) { + responseText = '"' + "My name is Flowhook" + '"'; + } else { + responseText = '"' + "Sorry I didn't get that" + '"'; + } + + o = + parser + .parse( + "{ \"fulfillment_response\": { \"messages\": [ { \"text\": { \"text\": [" + + responseText + + "] } } ] } }") + .getAsJsonObject(); + BufferedWriter writer = response.getWriter(); + writer.write(o.toString()); + } +} +// [END dialogflow_webhook] diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/ExampleIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/ExampleIT.java new file mode 100644 index 00000000000..a62338c1772 --- /dev/null +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/ExampleIT.java @@ -0,0 +1,72 @@ +/* + * Copyright 2021 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. + */ + +package dialogflow.cx; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import com.google.cloud.functions.HttpRequest; +import com.google.cloud.functions.HttpResponse; +import com.google.gson.Gson; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class ExampleIT { + + @Mock private HttpRequest request; + @Mock private HttpResponse response; + + private BufferedWriter writerOut; + private StringWriter responseOut; + private static final Gson gson = new Gson(); + + @Before + public void beforeTest() throws IOException { + MockitoAnnotations.initMocks(this); + + // use an empty string as the default request content + BufferedReader reader = new BufferedReader(new StringReader("")); + when(request.getReader()).thenReturn(reader); + + responseOut = new StringWriter(); + writerOut = new BufferedWriter(responseOut); + when(response.getWriter()).thenReturn(writerOut); + } + + @Test + public void helloHttp_bodyParamsPost() throws IOException, Exception { + + String firstHalf = "{\fulfillmentInfo\": {\"tag\": \"Default Welcome Intent\",}"; + String secondHalf = ",\"text\": \"hi\",\"languageCode\": \"en\",}"; + + BufferedReader jsonReader = new BufferedReader(new StringReader(firstHalf + secondHalf)); + + when(request.getReader()).thenReturn(jsonReader); + + new Example().service(request, response); + writerOut.flush(); + + assertThat(responseOut.toString()).contains("Hello from a Java GCF Webhook"); + } +}