From 71bb6bb3a6abca15f87b13f08a64fe000204f38d Mon Sep 17 00:00:00 2001 From: "Jessie (Xiaoyu) Jiang" <70290069+jessiexjiang27@users.noreply.github.com> Date: Thu, 10 Dec 2020 15:40:41 -0500 Subject: [PATCH] samples: add sample code to query regional Dialogflow CX agent (#100) * samples: add sample code to query regional Dialogflow CX agent Add logic to query regional Dialogflow CX endpoint for the following methods: - CreateFlow - CreateIntent - CreatePage - DetectIntent - DetectIntentStream * Fix: use equals instead of == for string comparison --- .../main/java/dialogflow/cx/CreateFlow.java | 11 +++- .../main/java/dialogflow/cx/CreateIntent.java | 11 +++- .../main/java/dialogflow/cx/CreatePage.java | 11 +++- .../main/java/dialogflow/cx/DetectIntent.java | 11 +++- .../dialogflow/cx/DetectIntentStream.java | 11 +++- .../test/java/dialogflow/cx/CreateFlowIT.java | 64 ++++++++++++++---- .../java/dialogflow/cx/CreateIntentIT.java | 60 +++++++++++++---- .../test/java/dialogflow/cx/CreatePageIT.java | 65 +++++++++++++++---- .../java/dialogflow/cx/DetectIntentIT.java | 27 ++++++-- .../dialogflow/cx/DetectIntentStreamIT.java | 25 +++++-- 10 files changed, 242 insertions(+), 54 deletions(-) diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java index 90ff3eae318..7098aa861fb 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java @@ -23,6 +23,7 @@ import com.google.cloud.dialogflow.cx.v3beta1.EventHandler; import com.google.cloud.dialogflow.cx.v3beta1.Flow; import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient; +import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings; import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment; import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage; import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text; @@ -41,8 +42,16 @@ public static Flow createFlow( String agentId, Map eventsToFulfillmentMessages) throws IOException, ApiException { + FlowsSettings.Builder flowsSettingsBuilder = FlowsSettings.newBuilder(); + if (locationId.equals("global")) { + flowsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); + } else { + flowsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); + } + FlowsSettings flowsSettings = flowsSettingsBuilder.build(); + // Instantiates a client - try (FlowsClient flowsClient = FlowsClient.create()) { + try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) { // Set the project agent name using the projectID (my-project-id), locationID (global), and // agentID (UUID). AgentName parent = AgentName.of(projectId, locationId, agentId); diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java index 6aca7382b45..d9e473585bc 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java @@ -24,6 +24,7 @@ import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase; import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase.Part; import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient; +import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -38,8 +39,16 @@ public static Intent createIntent( String agentId, List trainingPhrasesParts) throws IOException, ApiException { + IntentsSettings.Builder intentsSettingsBuilder = IntentsSettings.newBuilder(); + if (locationId.equals("global")) { + intentsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); + } else { + intentsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); + } + IntentsSettings intentsSettings = intentsSettingsBuilder.build(); + // Instantiates a client - try (IntentsClient intentsClient = IntentsClient.create()) { + try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) { // Set the project agent name using the projectID (my-project-id), locationID (global), and // agentID (UUID). AgentName parent = AgentName.of(projectId, locationId, agentId); diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java index 8b9688521f8..4418e8970f8 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java @@ -26,6 +26,7 @@ import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment; import com.google.cloud.dialogflow.cx.v3beta1.Page; import com.google.cloud.dialogflow.cx.v3beta1.PagesClient; +import com.google.cloud.dialogflow.cx.v3beta1.PagesSettings; import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage; import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text; import java.io.IOException; @@ -42,8 +43,16 @@ public static Page createPage( String flowId, List entryTexts) throws IOException, ApiException { + PagesSettings.Builder pagesSettingsBuilder = PagesSettings.newBuilder(); + if (locationId.equals("global")) { + pagesSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); + } else { + pagesSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); + } + PagesSettings pagesSettings = pagesSettingsBuilder.build(); + // Instantiates a client - try (PagesClient pagesClient = PagesClient.create()) { + try (PagesClient pagesClient = PagesClient.create(pagesSettings)) { // Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID) // and flowID (UUID). FlowName parent = FlowName.of(projectId, locationId, agentId, flowId); diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java index 94dffb8f3b1..d89067ad8ce 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java @@ -25,6 +25,7 @@ import com.google.cloud.dialogflow.cx.v3beta1.QueryResult; import com.google.cloud.dialogflow.cx.v3beta1.SessionName; import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient; +import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings; import com.google.cloud.dialogflow.cx.v3beta1.TextInput; import com.google.common.collect.Maps; import java.io.IOException; @@ -42,9 +43,17 @@ public static Map detectIntent( List texts, String languageCode) throws IOException, ApiException { + SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder(); + if (locationId.equals("global")) { + sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); + } else { + sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); + } + SessionsSettings sessionsSettings = sessionsSettingsBuilder.build(); + Map queryResults = Maps.newHashMap(); // Instantiates a client - try (SessionsClient sessionsClient = SessionsClient.create()) { + try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { // Set the session name using the projectID (my-project-id), locationID (global), agentID // (UUID), and sessionId (UUID). SessionName session = SessionName.of(projectId, locationId, agentId, sessionId); diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java index 3bab80a0150..125581c83e0 100644 --- a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java @@ -27,6 +27,7 @@ import com.google.cloud.dialogflow.cx.v3beta1.QueryResult; import com.google.cloud.dialogflow.cx.v3beta1.SessionName; import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient; +import com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings; import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentRequest; import com.google.cloud.dialogflow.cx.v3beta1.StreamingDetectIntentResponse; import com.google.protobuf.ByteString; @@ -39,8 +40,16 @@ public class DetectIntentStream { public static void detectIntentStream( String projectId, String locationId, String agentId, String sessionId, String audioFilePath) throws ApiException, IOException { + SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder(); + if (locationId.equals("global")) { + sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443"); + } else { + sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443"); + } + SessionsSettings sessionsSettings = sessionsSettingsBuilder.build(); + // Instantiates a client - try (SessionsClient sessionsClient = SessionsClient.create()) { + try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) { // Set the session name using the projectID (my-project-id), locationID (global), agentID // (UUID), and sessionId (UUID). // Using the same `sessionId` between requests allows continuation of the conversation. diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateFlowIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateFlowIT.java index f5356828bbc..1c9e02135b5 100644 --- a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateFlowIT.java +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateFlowIT.java @@ -20,10 +20,11 @@ import com.google.cloud.dialogflow.cx.v3beta1.Flow; import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient; +import com.google.cloud.dialogflow.cx.v3beta1.FlowsSettings; import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.UUID; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -35,30 +36,67 @@ public class CreateFlowIT { private static String DISPLAY_NAME = "flow-" + UUID.randomUUID().toString(); private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static String LOCATION = "global"; - private static String AGENT_ID = + private static String LOCATION_GLOBAL = "global"; + private static String LOCATION_REGIONAL = "us-central1"; + private static String AGENT_ID_GLOBAL = System.getenv() - .getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + private static String AGENT_ID_REGIONAL = + System.getenv() + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014"); private static Map EVENT_TO_FULFILLMENT_MESSAGES = ImmutableMap.of("event-1", "message-1", "event-2", "message-2"); - private static String newFlowName; - @After - public void tearDown() throws Exception { - // Delete the newly created Flow. - if (newFlowName != null) { + private static String newFlowNameGlobal; + private static String newFlowNameRegional; + + @AfterClass + public static void tearDown() throws Exception { + // Delete the newly created Flow in the global location. + if (newFlowNameGlobal != null) { try (FlowsClient flowsClient = FlowsClient.create()) { - flowsClient.deleteFlow(newFlowName); + flowsClient.deleteFlow(newFlowNameGlobal); } } + + // Delete the newly created Flow in the regional location. + if (newFlowNameRegional != null) { + FlowsSettings flowsSettings = + FlowsSettings.newBuilder() + .setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443") + .build(); + try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) { + flowsClient.deleteFlow(newFlowNameRegional); + } + } + } + + @Test + public void testCreateFlowGlobal() throws Exception { + Flow result = + CreateFlow.createFlow( + DISPLAY_NAME, + PROJECT_ID, + LOCATION_GLOBAL, + AGENT_ID_GLOBAL, + EVENT_TO_FULFILLMENT_MESSAGES); + newFlowNameGlobal = result.getName(); + + assertEquals(result.getDisplayName(), DISPLAY_NAME); + // Number of added event handlers + 2 default event handlers. + assertEquals(result.getEventHandlersCount(), EVENT_TO_FULFILLMENT_MESSAGES.size() + 2); } @Test - public void testCreateFlow() throws Exception { + public void testCreateFlowRegional() throws Exception { Flow result = CreateFlow.createFlow( - DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, EVENT_TO_FULFILLMENT_MESSAGES); - newFlowName = result.getName(); + DISPLAY_NAME, + PROJECT_ID, + LOCATION_REGIONAL, + AGENT_ID_REGIONAL, + EVENT_TO_FULFILLMENT_MESSAGES); + newFlowNameRegional = result.getName(); assertEquals(result.getDisplayName(), DISPLAY_NAME); // Number of added event handlers + 2 default event handlers. diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateIntentIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateIntentIT.java index 67580d3c4f5..9cd42c025b8 100644 --- a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateIntentIT.java +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateIntentIT.java @@ -22,10 +22,11 @@ import com.google.cloud.dialogflow.cx.v3beta1.Intent; import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase; import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient; +import com.google.cloud.dialogflow.cx.v3beta1.IntentsSettings; import java.util.Arrays; import java.util.List; import java.util.UUID; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -37,29 +38,62 @@ public class CreateIntentIT { private static String DISPLAY_NAME = "intent-" + UUID.randomUUID().toString(); private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static String LOCATION = "global"; - private static String AGENT_ID = + private static String LOCATION_GLOBAL = "global"; + private static String LOCATION_REGIONAL = "us-central1"; + private static String AGENT_ID_GLOBAL = System.getenv() - .getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + private static String AGENT_ID_REGIONAL = + System.getenv() + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014"); private static List TRAINING_PHRASES_PARTS = Arrays.asList("red", "blue", "green"); - private static String newIntentName; - @After - public void tearDown() throws Exception { - // Delete the newly created Intent. - if (newIntentName != null) { + private static String newIntentNameGlobal; + private static String newIntentNameRegional; + + @AfterClass + public static void tearDown() throws Exception { + // Delete the newly created Intent in the global location. + if (newIntentNameGlobal != null) { try (IntentsClient intentsClient = IntentsClient.create()) { - intentsClient.deleteIntent(newIntentName); + intentsClient.deleteIntent(newIntentNameGlobal); + } + } + + // Delete the newly created Intent in the regional location. + if (newIntentNameRegional != null) { + IntentsSettings intentsSettings = + IntentsSettings.newBuilder() + .setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443") + .build(); + try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) { + intentsClient.deleteIntent(newIntentNameRegional); } } } @Test - public void testCreateIntent() throws Exception { + public void testCreateIntentGlobal() throws Exception { + Intent result = + CreateIntent.createIntent( + DISPLAY_NAME, PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, TRAINING_PHRASES_PARTS); + newIntentNameGlobal = result.getName(); + + assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size()); + for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) { + assertEquals(trainingPhrase.getPartsCount(), 1); + String partText = trainingPhrase.getParts(0).getText(); + assertTrue(partText.equals("red") || partText.equals("blue") || partText.equals("green")); + } + } + + @Test + public void testCreateIntentRegional() throws Exception { Intent result = CreateIntent.createIntent( - DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, TRAINING_PHRASES_PARTS); - newIntentName = result.getName(); + DISPLAY_NAME, PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, TRAINING_PHRASES_PARTS); + newIntentNameRegional = result.getName(); + System.out.println("intent name new:" + newIntentNameRegional); assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size()); for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) { diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreatePageIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreatePageIT.java index 4b3c26601ad..649d5a6c8fd 100644 --- a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreatePageIT.java +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreatePageIT.java @@ -20,10 +20,11 @@ import com.google.cloud.dialogflow.cx.v3beta1.Page; import com.google.cloud.dialogflow.cx.v3beta1.PagesClient; +import com.google.cloud.dialogflow.cx.v3beta1.PagesSettings; import java.util.Arrays; import java.util.List; import java.util.UUID; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -35,30 +36,68 @@ public class CreatePageIT { private static String DISPLAY_NAME = "page-" + UUID.randomUUID().toString(); private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static String LOCATION = "global"; - private static String AGENT_ID = + private static String LOCATION_GLOBAL = "global"; + private static String LOCATION_REGIONAL = "us-central1"; + private static String AGENT_ID_GLOBAL = System.getenv() - .getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + private static String AGENT_ID_REGIONAL = + System.getenv() + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014"); private static String DEFAULT_START_FLOW_ID = "00000000-0000-0000-0000-000000000000"; private static List ENTRY_TEXTS = Arrays.asList("Hi", "Hello", "How can I help you?"); - private static String newPageName; - @After - public void tearDown() throws Exception { - // Delete the newly created Page. - if (newPageName != null) { + private static String newPageNameGlobal; + private static String newPageNameRegional; + + @AfterClass + public static void tearDown() throws Exception { + // Delete the newly created Page in the global location. + if (newPageNameGlobal != null) { try (PagesClient pagesClient = PagesClient.create()) { - pagesClient.deletePage(newPageName); + pagesClient.deletePage(newPageNameGlobal); } } + + // Delete the newly created Page in the regional location. + if (newPageNameRegional != null) { + PagesSettings pagesSettings = + PagesSettings.newBuilder() + .setEndpoint(LOCATION_REGIONAL + "-dialogflow.googleapis.com:443") + .build(); + try (PagesClient pagesClient = PagesClient.create(pagesSettings)) { + pagesClient.deletePage(newPageNameRegional); + } + } + } + + @Test + public void testCreatePageGlobal() throws Exception { + Page result = + CreatePage.createPage( + DISPLAY_NAME, + PROJECT_ID, + LOCATION_GLOBAL, + AGENT_ID_GLOBAL, + DEFAULT_START_FLOW_ID, + ENTRY_TEXTS); + newPageNameGlobal = result.getName(); + + assertEquals(result.getDisplayName(), DISPLAY_NAME); + assertEquals(result.getEntryFulfillment().getMessagesCount(), ENTRY_TEXTS.size()); } @Test - public void testCreatePage() throws Exception { + public void testCreatePageRegional() throws Exception { Page result = CreatePage.createPage( - DISPLAY_NAME, PROJECT_ID, LOCATION, AGENT_ID, DEFAULT_START_FLOW_ID, ENTRY_TEXTS); - newPageName = result.getName(); + DISPLAY_NAME, + PROJECT_ID, + LOCATION_REGIONAL, + AGENT_ID_REGIONAL, + DEFAULT_START_FLOW_ID, + ENTRY_TEXTS); + newPageNameRegional = result.getName(); assertEquals(result.getDisplayName(), DISPLAY_NAME); assertEquals(result.getEntryFulfillment().getMessagesCount(), ENTRY_TEXTS.size()); diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentIT.java index 2a7e362a561..bf0e300a77d 100644 --- a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentIT.java +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentIT.java @@ -33,18 +33,35 @@ public class DetectIntentIT { private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static String LOCATION = "global"; - private static String AGENT_ID = + private static String LOCATION_GLOBAL = "global"; + private static String LOCATION_REGIONAL = "us-central1"; + private static String AGENT_ID_GLOBAL = System.getenv() - .getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + private static String AGENT_ID_REGIONAL = + System.getenv() + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014"); private static String SESSION_ID = UUID.randomUUID().toString(); private static String LANGUAGE_CODE = "en-US"; private static List TEXTS = Arrays.asList("hello", "book a meeting room"); @Test - public void testDetectIntent() throws Exception { + public void testDetectIntentGlobal() throws Exception { + Map queryResults = + DetectIntent.detectIntent( + PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, SESSION_ID, TEXTS, LANGUAGE_CODE); + assertEquals(queryResults.size(), TEXTS.size()); + for (int i = 0; i < TEXTS.size(); i++) { + String text = TEXTS.get(i); + assertEquals(queryResults.get(text).getText(), text); + } + } + + @Test + public void testDetectIntentRegional() throws Exception { Map queryResults = - DetectIntent.detectIntent(PROJECT_ID, LOCATION, AGENT_ID, SESSION_ID, TEXTS, LANGUAGE_CODE); + DetectIntent.detectIntent( + PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, SESSION_ID, TEXTS, LANGUAGE_CODE); assertEquals(queryResults.size(), TEXTS.size()); for (int i = 0; i < TEXTS.size(); i++) { String text = TEXTS.get(i); diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentStreamIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentStreamIT.java index 5a86c6e4fe9..4ef71b0edb7 100644 --- a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentStreamIT.java +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/DetectIntentStreamIT.java @@ -35,10 +35,14 @@ public class DetectIntentStreamIT { private static String AUDIO_FILE_PATH = "resources/book_a_room.wav"; private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static String LOCATION = "global"; - private static String AGENT_ID = + private static String LOCATION_GLOBAL = "global"; + private static String LOCATION_REGIONAL = "us-central1"; + private static String AGENT_ID_GLOBAL = System.getenv() - .getOrDefault("DIALOGFLOW_CX_AGENT_ID", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_GLOBAL", "b8d0e85d-0741-4e6d-a66a-3671184b7b93"); + private static String AGENT_ID_REGIONAL = + System.getenv() + .getOrDefault("DIALOGFLOW_CX_AGENT_ID_REGIONAL", "1ea2bf10-d5ef-4442-b93f-a917d1991014"); private static String SESSION_ID = UUID.randomUUID().toString(); private ByteArrayOutputStream bout; private PrintStream original; @@ -57,9 +61,20 @@ public void tearDown() { } @Test - public void testDetectIntentStream() throws IOException { + public void testDetectIntentStreamGlobal() throws IOException { + DetectIntentStream.detectIntentStream( + PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, SESSION_ID, AUDIO_FILE_PATH); + + String output = bout.toString(); + + assertThat(output).contains("Detected Intent"); + assertThat(output).contains("book"); + } + + @Test + public void testDetectIntentStreamRegional() throws IOException { DetectIntentStream.detectIntentStream( - PROJECT_ID, LOCATION, AGENT_ID, SESSION_ID, AUDIO_FILE_PATH); + PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, SESSION_ID, AUDIO_FILE_PATH); String output = bout.toString();