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"); + } +}