From 6526525fa4494b2a5bfc97d8deec2f698283a477 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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java index 2db56b5ded46..b94c55242baa 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java index 2ab019c1e9c0..657abed0ee4d 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java index 3f2b707ddb45..1719f1526394 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java index 603034d8f56e..3469c1848534 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java index 14162bf8d092..b45bc46f81fe 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreatePage.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java index e18b60eeb98e..26b10e854225 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java index 98bd5e1fdead..8403a690a993 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DeletePage.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java index 703f7a77393c..3109a4fc5a86 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntent.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java index e864f381a034..36a6a17c727f 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java index c0cf470fefa9..ee97aabdab92 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java index 5f92199e9b96..ccd1fe3430c3 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java index 45af3c56795f..a0e89547db68 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java index 318242c4fb5d..f69331bbf29e 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java index 96b1799a5bd4..f1d05491e38e 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java index 81cb534e2856..af7258043660 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java index d26437eefff0..74c155f4d0bd 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java index ba908af542e1..6b431f5108c1 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListPages.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListPages.java index f935b6389117..9a180081f673 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListPages.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java index 3519c5c983cd..2f34d6530c1c 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java index 2bd0e73ae9e4..4257530513db 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java index 73a87889880e..ea16649f66a3 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java index e9c0263141b5..2d59a8ba7e18 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java b/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java index fadbef57360e..320b57f3d654 100644 --- a/java-dialogflow-cx/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java index b9a051bf7b08..53e30e094e68 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java index 69723e127bbb..aee80c7dee1f 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java index ab1b2ca60325..47c100107d4a 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/PageManagementIT.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java index 0443ec40cb73..635b16b781bb 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java index 52cd6470d734..9c1d2a719298 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookConfigureSessionParametersIT.java +++ b/java-dialogflow-cx/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/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java b/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java index f234c3f3e1ca..4500e3f9467b 100644 --- a/java-dialogflow-cx/samples/snippets/src/test/java/dialogflow/cx/WebhookValidateFormParameterIT.java +++ b/java-dialogflow-cx/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); } }