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