From d07165a2bdaa3fc6a403c108d28136a63588dfc9 Mon Sep 17 00:00:00 2001 From: aribray <45905583+aribray@users.noreply.github.com> Date: Tue, 19 Jul 2022 13:26:47 -0500 Subject: [PATCH] docs(samples): add try blocks to clean up resources in samples and sample tests (#521) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add try block to clean up resources Change-Id: Ie68f4715646942d2e78189b3477cb9a5a31713b8 * docs: add try block to clean up resources Change-Id: Ibaa2bd6a9f687b67af61a5f958c37e2faf087434 * add try block to clean up resources Change-Id: I60fff4d6c83d42b8921b4ed4ce6c0eea76a952c6 * add try block to clean up resources Change-Id: I2facb88a81c1d76d5b2316da04cebdffba6634b9 * add Thread.sleep to try blocks, fix checkstyle Change-Id: I0eab8286ac21a8becf64b541100050581016ca5d * add Thread.sleep Change-Id: I94553a4501a6bdfbdcad1360833cd5e4352d9a77 * add Thread.sleep Change-Id: I123c577193306dc4625d2d56637e71e13cd06f70 * add try block to clean up resources Change-Id: I38d51da44e19b7c50d09d3e32aa6b5378c8fc0e2 * change fulfillmentInfo tag Change-Id: I8f0b7e31a2283c5dae7c6b377861ee03e8cbe621 * add try block to clean up resources Change-Id: I3ee67b4c3ce45b18138d9382fd18f5f4646372db * add ; Change-Id: I10cb37ff93f4c28c1f94d8e221c893fe4b74835a * remove try block Change-Id: I9b5f96d7c0ee75745084ed2a277fb9dc8b76a98f * move try block Change-Id: I17eeaf75673cb24682552dd59916c0ad5d68b89a * remove try block Change-Id: I001e1bd68b5c0953d2c634239828d81bf2576a25 * change fulfillmentInfo tag Change-Id: I1454736df9212b36afcd322ef3ce8c677b3500e8 * checkstyle Change-Id: I10d3750668a4acb9ff18f58fdb4f1a19faf6eacc * add try block to clean up resources Change-Id: I2a56b4f9b558fbebb816fb409fdabff4ee194f2f * add catch Change-Id: I7dd46f9543256427e9d86ab4f60b4806bdb8bd9a * remove println Change-Id: Idb0f4d75d0e7902a666248c48ed4fc54aeef5e59 * checkstyle Change-Id: Ie28023c2bf6422a50dc5e09128ede1e49679c95d * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add try-with-resources comment to client object creation lines in snippets Change-Id: Ic1acaa6097e972b2db35cca2f5928e826ef08964 * add string.format Change-Id: I25e7e803c8d0b40e6503b6ed8c5fd1c872c4506b * update parentPath Change-Id: Id387695b0d86c30c74f796e7f57db6e1f8be43bd * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- ...SetFormParametersAsOptionalOrRequired.java | 3 - .../main/java/dialogflow/cx/CreateAgent.java | 3 + .../main/java/dialogflow/cx/CreateFlow.java | 5 +- .../main/java/dialogflow/cx/CreateIntent.java | 3 + .../main/java/dialogflow/cx/CreatePage.java | 3 + .../java/dialogflow/cx/CreateSimplePage.java | 3 + .../main/java/dialogflow/cx/DeletePage.java | 4 ++ .../main/java/dialogflow/cx/DetectIntent.java | 6 +- .../dialogflow/cx/DetectIntentAudioInput.java | 4 ++ .../cx/DetectIntentDisableWebhook.java | 4 ++ .../dialogflow/cx/DetectIntentEventInput.java | 4 ++ .../cx/DetectIntentIntentInput.java | 4 ++ .../cx/DetectIntentSentimentAnalysis.java | 4 ++ .../dialogflow/cx/DetectIntentStream.java | 4 ++ .../DetectIntentStreamingPartialResponse.java | 4 ++ ...ectIntentSynthesizeTextToSpeechOutput.java | 4 ++ .../main/java/dialogflow/cx/ExportAgent.java | 3 + .../main/java/dialogflow/cx/ListPages.java | 33 ++++++----- .../dialogflow/cx/ListTestCaseResults.java | 10 +++- .../dialogflow/cx/ListTrainingPhrases.java | 4 ++ .../main/java/dialogflow/cx/UpdateIntent.java | 4 ++ .../cx/WebhookConfigureSessionParameters.java | 3 - .../cx/WebhookValidateFormParameter.java | 2 - ...tFormParametersAsOptionalOrRequiredIT.java | 5 +- .../java/dialogflow/cx/CreateAgentIT.java | 10 ++-- .../java/dialogflow/cx/PageManagementIT.java | 57 +++++++++++-------- .../java/dialogflow/cx/UpdateIntentTest.java | 38 +++++++------ .../WebhookConfigureSessionParametersIT.java | 3 +- .../cx/WebhookValidateFormParameterIT.java | 1 + 29 files changed, 157 insertions(+), 78 deletions(-) diff --git a/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java b/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java index 2db56b5de..b94c55242 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java +++ b/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java @@ -57,9 +57,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonResponseObject = gson.toJson(webhookResponse); - System.out.println("Response Object: \n"); - System.out.println(jsonResponseObject.toString()); - /* { * "page_info": { * "form_info": { diff --git a/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java b/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java index 2ab019c1e..657abed0e 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java +++ b/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java @@ -39,6 +39,9 @@ public static Agent createAgent(String parent, String displayName) throws IOExce String apiEndpoint = "global-dialogflow.googleapis.com:443"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + // Note: close() needs to be called on the AgentsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (AgentsClient client = AgentsClient.create(agentsSettings)) { // Set the details of the Agent to create Builder build = Agent.newBuilder(); diff --git a/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java b/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java index 3f2b707dd..1719f1526 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java +++ b/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java @@ -50,7 +50,10 @@ public static Flow createFlow( } FlowsSettings flowsSettings = flowsSettingsBuilder.build(); - // Instantiates a client + // Instantiates a client. + // Note: close() needs to be called on the FlowsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) { // Set the project agent name using the projectID (my-project-id), locationID (global), and // agentID (UUID). diff --git a/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java b/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java index 603034d8f..3469c1848 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java +++ b/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java @@ -48,6 +48,9 @@ public static Intent createIntent( IntentsSettings intentsSettings = intentsSettingsBuilder.build(); // Instantiates a client + // Note: close() needs to be called on the IntentsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) { // Set the project agent name using the projectID (my-project-id), locationID (global), and // agentID (UUID). diff --git a/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java b/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java index 14162bf8d..b45bc46f8 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java +++ b/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java @@ -52,6 +52,9 @@ public static Page createPage( PagesSettings pagesSettings = pagesSettingsBuilder.build(); // Instantiates a client + // Note: close() needs to be called on the PagesClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (PagesClient pagesClient = PagesClient.create(pagesSettings)) { // Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID) // and flowID (UUID). diff --git a/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java b/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java index e18b60eeb..26b10e854 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java +++ b/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java @@ -59,6 +59,9 @@ public static Page createPage( .setPage(pageBuilder); // Make API request to create page + // Note: close() needs to be called on the PagesClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (PagesClient client = PagesClient.create()) { response = client.createPage(createRequestBuilder.build()); System.out.println("Successfully created page!"); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java b/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java index 98bd5e1fd..8403a690a 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java @@ -40,6 +40,10 @@ public static void main(String[] args) throws IOException { public static void deletePage( String projectId, String agentId, String flowId, String pageId, String location) throws IOException { + + // Note: close() needs to be called on the PagesClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (PagesClient client = PagesClient.create()) { Builder deleteRequestBuilder = DeletePageRequest.newBuilder(); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java index 703f7a773..3109a4fc5 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java @@ -52,7 +52,11 @@ public static Map detectIntent( SessionsSettings sessionsSettings = sessionsSettingsBuilder.build(); Map queryResults = Maps.newHashMap(); - // Instantiates a client + // Instantiates a client. + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { // Set the session name using the projectID (my-project-id), locationID (global), agentID // (UUID), and sessionId (UUID). diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java index e864f381a..36a6a17c7 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java @@ -76,6 +76,10 @@ public static void detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java index c0cf470fe..ee97aabda 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java @@ -68,6 +68,10 @@ public static Map detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java index 5f92199e9..ccd1fe343 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java @@ -62,6 +62,10 @@ public static void detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java index 45af3c567..a0e89547d 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java @@ -62,6 +62,10 @@ public static void detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java index 318242c4f..f69331bbf 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java @@ -68,6 +68,10 @@ public static Map detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java index 96b1799a5..f1d05491e 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java @@ -56,6 +56,10 @@ public static void detectIntentStream( // Instantiates a client by setting the session name. // Format: `projects//locations//agents//sessions/` // Using the same `sessionId` between requests allows continuation of the conversation. + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.of(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java index 81cb534e2..af7258043 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java @@ -56,6 +56,10 @@ public static void detectIntentStreamingPartialResponse( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` // Using the same `sessionId` between requests allows continuation of the conversation. + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.of(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java index d26437eef..74c155f4d 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java +++ b/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java @@ -71,6 +71,10 @@ public static void detectIntent( // Instantiates a client by setting the session name. // Format:`projects//locations//agents//sessions/` + + // Note: close() needs to be called on the SessionsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java b/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java index ba908af54..6b431f510 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java +++ b/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java @@ -47,6 +47,9 @@ public static void exportAgent(String projectId, String agentId, String location String apiEndpoint = String.format("%s-dialogflow.googleapis.com:443", location); AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + // Note: close() needs to be called on the AgentsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) { ExportAgentRequest request = ExportAgentRequest.newBuilder() diff --git a/samples/snippets/src/main/java/dialogflow/cx/ListPages.java b/samples/snippets/src/main/java/dialogflow/cx/ListPages.java index f935b6389..9a180081f 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/ListPages.java +++ b/samples/snippets/src/main/java/dialogflow/cx/ListPages.java @@ -39,23 +39,22 @@ public static void main(String[] args) throws IOException { // Lists all pages from the provided parameters public static void listPages(String projectId, String agentId, String flowId, String location) throws IOException { - PagesClient client = PagesClient.create(); - Builder listRequestBuilder = ListPagesRequest.newBuilder(); - - listRequestBuilder.setParent( - "projects/" - + projectId - + "/locations/" - + location - + "/agents/" - + agentId - + "/flows/" - + flowId); - listRequestBuilder.setLanguageCode("en"); - - // Make API request to list all pages in the project - for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) { - System.out.println(element); + // Note: close() needs to be called on the PagesClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). + try (PagesClient client = PagesClient.create()) { + Builder listRequestBuilder = ListPagesRequest.newBuilder(); + + String parentPath = + String.format( + "projects/%s/locations/%s/agents/%s/flows/%s", projectId, location, agentId, flowId); + listRequestBuilder.setParent(parentPath); + listRequestBuilder.setLanguageCode("en"); + + // Make API request to list all pages in the project + for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) { + System.out.println(element); + } } } // [END dialogflow_cx_list_pages] diff --git a/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java b/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java index 3519c5c98..2f34d6530 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java +++ b/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java @@ -57,10 +57,14 @@ public static void listTestCaseResults( TestCasesSettings.newBuilder() .setEndpoint(location + "-dialogflow.googleapis.com:443") .build(); - TestCasesClient client = TestCasesClient.create(testCasesSettings); - for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) { - System.out.println(element); + // Note: close() needs to be called on the TestCasesClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). + try (TestCasesClient client = TestCasesClient.create(testCasesSettings)) { + for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) { + System.out.println(element); + } } } } diff --git a/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java b/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java index 2bd0e73ae..425753051 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java +++ b/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java @@ -38,6 +38,10 @@ public static void main(String[] args) throws IOException { // DialogFlow API List Training Phrases sample. public static void listTrainingPhrases( String projectId, String location, String agentId, String intentId) throws IOException { + + // Note: close() needs to be called on the IntentsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (IntentsClient client = IntentsClient.create()) { // Set the intent name IntentName name = IntentName.of(projectId, location, agentId, intentId); diff --git a/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java b/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java index 73a878898..ea16649f6 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java +++ b/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java @@ -41,6 +41,10 @@ public static void main(String[] args) throws IOException { public static void updateIntent( String projectId, String agentId, String intentId, String location, String displayName) throws IOException { + + // Note: close() needs to be called on the IntentsClient object to clean up resources + // such as threads. In the example below, try-with-resources is used, + // which automatically calls close(). try (IntentsClient client = IntentsClient.create()) { String intentPath = "projects/" diff --git a/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java b/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java index e9c026314..2d59a8ba7 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java +++ b/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java @@ -49,9 +49,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonResponseObject = gson.toJson(webhookResponse); - System.out.println("Session Parameter Info: \n"); - System.out.println(jsonResponseObject.toString()); - /** { "session_info": { "parameters": { "order_number": "12345" } } } */ BufferedWriter writer = response.getWriter(); // Sends the webhookResponseObject diff --git a/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java b/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java index fadbef573..320b57f3d 100644 --- a/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java +++ b/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java @@ -65,8 +65,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonResponseObject = gson.toJson(webhookResponse); - System.out.println("Response Object: \n"); - System.out.println(jsonResponseObject.toString()); /** * { "page_info": { "form_info": { "parameter_info": [ { "display_name": "order_number", * "required": "true", "state": "INVALID", "value": "123" } ] } }, "session_info": { diff --git a/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java b/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java index b9a051bf7..53e30e094 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java +++ b/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java @@ -65,7 +65,9 @@ public void beforeTest() throws IOException { public void helloHttp_bodyParamsPost() throws IOException, Exception { FulfillmentInfo fulfillmentInfo = - FulfillmentInfo.newBuilder().setTag("configure-session-parameters").build(); + FulfillmentInfo.newBuilder() + .setTag("configure-form-parameters-optional-or-parameter") + .build(); WebhookRequest webhookRequest = WebhookRequest.newBuilder().setFulfillmentInfo(fulfillmentInfo).build(); @@ -94,5 +96,6 @@ public void helloHttp_bodyParamsPost() throws IOException, Exception { String expectedResponse = gson.toJson(webhookResponse); assertThat(responseOut.toString()).isEqualTo(expectedResponse); + Thread.sleep(200); } } diff --git a/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java b/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java index 69723e127..aee80c7de 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java +++ b/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java @@ -48,12 +48,12 @@ public void tearDown() throws IOException, InterruptedException { String apiEndpoint = "global-dialogflow.googleapis.com:443"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); - AgentsClient client = AgentsClient.create(agentsSettings); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + client.deleteAgent(CreateAgentIT.agentPath); - client.deleteAgent(CreateAgentIT.agentPath); - - // Small delay to prevent reaching quota limit of requests per minute - Thread.sleep(250); + // Small delay to prevent reaching quota limit of requests per minute + Thread.sleep(250); + } } @Test diff --git a/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java b/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java index ab1b2ca60..47c100107 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java +++ b/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java @@ -47,21 +47,24 @@ public class PageManagementIT { public static void setUp() throws IOException { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - Builder build = Agent.newBuilder(); - build.setDefaultLanguageCode("en"); - build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); - build.setTimeZone("America/Los_Angeles"); - - Agent agent = build.build(); String apiEndpoint = "global-dialogflow.googleapis.com:443"; - String parentPath = "projects/" + PROJECT_ID + "/locations/global"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); - AgentsClient client = AgentsClient.create(agentsSettings); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + + Builder build = Agent.newBuilder(); + build.setDefaultLanguageCode("en"); + build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); + build.setTimeZone("America/Los_Angeles"); + + Agent agent = build.build(); + String parentPath = "projects/" + PROJECT_ID + "/locations/global"; + + parent = client.createAgent(parentPath, agent).getName(); - parent = client.createAgent(parentPath, agent).getName(); - agentID = parent.split("/")[5]; + agentID = parent.split("/")[5]; + } } @AfterClass @@ -70,30 +73,36 @@ public static void tearDown() throws IOException, InterruptedException { String parentPath = "projects/" + PROJECT_ID + "/locations/global"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); - AgentsClient client = AgentsClient.create(agentsSettings); - - client.deleteAgent(parent); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + client.deleteAgent(parent); - // Small delay to prevent reaching quota limit of requests per minute - Thread.sleep(250); + // Small delay to prevent reaching quota limit of requests per minute + Thread.sleep(250); + } } @Test public void testCreatePage() throws IOException { - Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, displayName); - pageID = p.getName().split("/")[9]; - - assertThat(p.getDisplayName()).isEqualTo(displayName); + try { + Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, displayName); + pageID = p.getName().split("/")[9]; + assertThat(p.getDisplayName()).isEqualTo(displayName); + } catch (Exception e) { + assertThat(e).isEqualTo(""); + } } @Test public void testListPages() throws IOException { String name = "temp_page_" + UUID.randomUUID().toString(); - - Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, name); - - ListPages.listPages(PROJECT_ID, agentID, flowID, location); - assertThat(stdOut.toString()).contains(name); + // Page p + try { + CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, name); + ListPages.listPages(PROJECT_ID, agentID, flowID, location); + assertThat(stdOut.toString()).contains(name); + } catch (Exception e) { + assertThat(e).isEqualTo(""); + } } @Test diff --git a/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java b/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java index 0443ec40c..635b16b78 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java +++ b/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java @@ -48,21 +48,24 @@ public void setUp() throws IOException { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - Builder build = Agent.newBuilder(); - build.setDefaultLanguageCode("en"); - build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); - build.setTimeZone("America/Los_Angeles"); - - Agent agent = build.build(); - String apiEndpoint = "global-dialogflow.googleapis.com:443"; - String parentPath = "projects/" + PROJECT_ID + "/locations/global"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); - AgentsClient client = AgentsClient.create(agentsSettings); + try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) { + + Builder build = Agent.newBuilder(); + + build.setDefaultLanguageCode("en"); + build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); + build.setTimeZone("America/Los_Angeles"); + + Agent agent = build.build(); + String parentPath = String.format("projects/%s/locations/global", PROJECT_ID); - parent = client.createAgent(parentPath, agent).getName(); - UpdateIntentTest.agentID = parent.split("/")[5]; + parent = agentsClient.createAgent(parentPath, agent).getName(); + + UpdateIntentTest.agentID = parent.split("/")[5]; + } try (IntentsClient intentsClient = IntentsClient.create()) { com.google.cloud.dialogflow.cx.v3.Intent.Builder intent = Intent.newBuilder(); @@ -77,16 +80,17 @@ public void setUp() throws IOException { public void tearDown() throws IOException, InterruptedException { stdOut = null; System.setOut(null); + String apiEndpoint = "global-dialogflow.googleapis.com:443"; - String parentPath = "projects/" + PROJECT_ID + "/locations/global"; AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); - AgentsClient client = AgentsClient.create(agentsSettings); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + String parentPath = "projects/" + PROJECT_ID + "/locations/global"; + client.deleteAgent(parent); - client.deleteAgent(parent); - - // Small delay to prevent reaching quota limit of requests per minute - Thread.sleep(250); + // Small delay to prevent reaching quota limit of requests per minute + Thread.sleep(250); + } } @Test diff --git a/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java b/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java index 52cd6470d..9c1d2a719 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java +++ b/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java @@ -50,7 +50,7 @@ public class WebhookConfigureSessionParametersIT { public void beforeTest() throws IOException { MockitoAnnotations.initMocks(this); - stringReader = new StringReader("{'fulfillmentInfo': {'tag': 'validate-form-parameter'}}"); + stringReader = new StringReader("{'fulfillmentInfo': {'tag': 'configure-session-parameter'}}"); jsonReader = new BufferedReader(stringReader); responseOut = new StringWriter(); @@ -82,5 +82,6 @@ public void helloHttp_bodyParamsPost() throws IOException, Exception { String expectedResponse = gson.toJson(webhookResponse); assertThat(responseOut.toString()).isEqualTo(expectedResponse); + Thread.sleep(200); } } diff --git a/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java b/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java index f234c3f3e..4500e3f94 100644 --- a/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java +++ b/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java @@ -101,5 +101,6 @@ public void helloHttp_bodyParamsPost() throws IOException, Exception { String expectedResponse = gson.toJson(webhookResponse); assertThat(responseOut.toString()).isEqualTo(expectedResponse); + Thread.sleep(250); } }