This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs:add configure webhook to set form parameter as optional or requi…
…red sample and test Change-Id: I75bed489bd8abefca60263acb266501810c0e856
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
.../src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
72 changes: 72 additions & 0 deletions
72
.../test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |