diff --git a/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java b/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java new file mode 100644 index 000000000..e792c8ad8 --- /dev/null +++ b/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java @@ -0,0 +1,64 @@ +/* + * Copyright 2022 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; + +// The following snippet is used in https://cloud.google.com/dialogflow/cx/docs/concept/webhook + +// [START dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required] + +// TODO: Change class name to Example +// 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 ConfigureWebhookToSetFormParametersAsOptionalOrRequired implements HttpFunction { + @Override + public void service(HttpRequest request, HttpResponse response) throws Exception { + JsonParser parser = new JsonParser(); + Gson gson = new GsonBuilder().create(); + JsonObject parsedRequest = gson.fromJson(request.getReader(), JsonObject.class); + + // For more information on the structure of this object, visit https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/Fulfillment + JsonObject responseObject = null; + + // Constructing the response jsonObject + responseObject = + parser + .parse( + "{\"page_info\":{\"form_info\":{\"parameter_info\":[{\"display_name\":\"order-number\",\"required\":\"true\",\"state\":\"VALID\"}]}}}") + .getAsJsonObject(); + BufferedWriter writer = response.getWriter(); + + System.out.println("Parameter Info: \n"); + System.out.println(responseObject.toString()); + // System.out.println(responseObject.pageInfo.formInfo.parameterInfo[0]); + + //Sends the responseObject + writer.write(responseObject.toString()); + } +} +// [END dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required] diff --git a/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest.java b/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest.java new file mode 100644 index 000000000..74e28a00f --- /dev/null +++ b/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2022 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 ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest { + + @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 jsonString = "{'fulfillmentInfo': {'tag': 'optional-or-required'}}"; + + BufferedReader jsonReader = new BufferedReader(new StringReader(jsonString)); + + when(request.getReader()).thenReturn(jsonReader); + + new ConfigureWebhookToSetFormParametersAsOptionalOrRequired().service(request, response); + writerOut.flush(); + + String expectedResponse = "{\"page_info\":{\"form_info\":{\"parameter_info\":[{\"display_name\":\"order-number\",\"required\":\"true\",\"state\":\"VALID\"}]}}}"; + + assertThat(responseOut.toString()).isEqualTo(expectedResponse); + } +} \ No newline at end of file