diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentKnowledge.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentKnowledge.java index 35307f6c127..96a04fa6e26 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentKnowledge.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentKnowledge.java @@ -28,7 +28,6 @@ import com.google.cloud.dialogflow.v2beta1.SessionName; import com.google.cloud.dialogflow.v2beta1.SessionsClient; import com.google.cloud.dialogflow.v2beta1.TextInput; -import com.google.cloud.dialogflow.v2beta1.TextInput.Builder; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; @@ -67,7 +66,8 @@ public static Map detectIntentKnowledge( // Detect intents for each text input for (String text : texts) { // Set the text and language code (en-US) for the query - Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode); + TextInput.Builder textInput = + TextInput.newBuilder().setText(text).setLanguageCode(languageCode); // Build the query with the TextInput QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentTexts.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentTexts.java index b29c65d9352..83e6a813910 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentTexts.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentTexts.java @@ -24,7 +24,6 @@ import com.google.cloud.dialogflow.v2.SessionName; import com.google.cloud.dialogflow.v2.SessionsClient; import com.google.cloud.dialogflow.v2.TextInput; -import com.google.cloud.dialogflow.v2.TextInput.Builder; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; @@ -61,7 +60,8 @@ public static Map detectIntentTexts( // Detect intents for each text input for (String text : texts) { // Set the text (hello) and language code (en-US) for the query - Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode); + TextInput.Builder textInput = + TextInput.newBuilder().setText(text).setLanguageCode(languageCode); // Build the query with the TextInput QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithSentimentAnalysis.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithSentimentAnalysis.java index 482692f50d9..4f3dce487ac 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithSentimentAnalysis.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithSentimentAnalysis.java @@ -25,7 +25,6 @@ import com.google.cloud.dialogflow.v2.SessionName; import com.google.cloud.dialogflow.v2.SessionsClient; import com.google.cloud.dialogflow.v2.TextInput; -import com.google.cloud.dialogflow.v2.TextInput.Builder; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; @@ -59,7 +58,8 @@ public static Map detectIntentSentimentAnalysis( // Detect intents for each text input for (String text : texts) { // Set the text (hello) and language code (en-US) for the query - Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode); + TextInput.Builder textInput = + TextInput.newBuilder().setText(text).setLanguageCode(languageCode); // Build the query with the TextInput QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithTextToSpeechResponse.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithTextToSpeechResponse.java index 912b221791d..933adf88558 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithTextToSpeechResponse.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentWithTextToSpeechResponse.java @@ -25,7 +25,6 @@ import com.google.cloud.dialogflow.v2.SessionName; import com.google.cloud.dialogflow.v2.SessionsClient; import com.google.cloud.dialogflow.v2.TextInput; -import com.google.cloud.dialogflow.v2.TextInput.Builder; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; @@ -59,7 +58,8 @@ public static Map detectIntentWithTexttoSpeech( // Detect intents for each text input for (String text : texts) { // Set the text (hello) and language code (en-US) for the query - Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode); + TextInput.Builder textInput = + TextInput.newBuilder().setText(text).setLanguageCode(languageCode); // Build the query with the TextInput QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DocumentManagement.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DocumentManagement.java index 76e5c956938..7d84edc39d4 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DocumentManagement.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DocumentManagement.java @@ -138,4 +138,6 @@ public static void deleteDocument(String documentName) throws Exception { System.out.format("The document has been deleted."); } } + + // [END dialogflow_delete_document] } diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/EntityManagement.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/EntityManagement.java index 5ebdd0e96d5..a4b64208de0 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/EntityManagement.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/EntityManagement.java @@ -30,33 +30,6 @@ * DialogFlow API Entity sample. */ public class EntityManagement { - // [START dialogflow_list_entities] - - /** - * List entities - * - * @param projectId Project/agent id. - * @param entityTypeId The id of the entity_type. - * @return List of found entities. - */ - public static List listEntities(String projectId, String entityTypeId) throws Exception { - // Instantiates a client - try (EntityTypesClient entityTypesClient = EntityTypesClient.create()) { - // Set the entity type name using the projectID (my-project-id) and entityTypeId (KIND_LIST) - EntityTypeName name = EntityTypeName.of(projectId, entityTypeId); - - // Performs the get entity type request - EntityType entityType = entityTypesClient.getEntityType(name); - List entities = entityType.getEntitiesList(); - for (Entity entity : entities) { - System.out.format("Entity value: %s\n", entity.getValue()); - System.out.format("Entity synonyms: %s\n", entity.getSynonymsList().toString()); - } - return entities; - } - } - // [END dialogflow_list_entities] - // [START dialogflow_create_entity] /** diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java index 607f48238f2..78f2b7e6ff8 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java @@ -23,37 +23,12 @@ import java.util.List; public class KnowledgeBaseManagement { - // [START dialogflow_list_knowledge_base] - - /** - * List Knowledge bases - * - * @param projectId Project/agent id. - * @return The KnowledgeBases found in projectId. - */ - public static List listKnowledgeBases(String projectId) throws Exception { - List knowledgeBases = Lists.newArrayList(); - // Instantiates a client - try (KnowledgeBasesClient knowledgeBasesClient = KnowledgeBasesClient.create()) { - // Set the entity type name using the projectID (my-project-id) and entityTypeId (KIND_LIST) - ProjectName projectName = ProjectName.of(projectId); - for (KnowledgeBase knowledgeBase : - knowledgeBasesClient.listKnowledgeBases(projectName).iterateAll()) { - System.out.format(" - Display Name: %s\n", knowledgeBase.getDisplayName()); - System.out.format(" - Knowledge ID: %s\n", knowledgeBase.getName()); - knowledgeBases.add(knowledgeBase); - } - } - return knowledgeBases; - } - // [END dialogflow_list_knowledge_base] - // [START dialogflow_create_knowledge_base] /** * Create a Knowledge base * - * @param projectId Project/agent id. + * @param projectId Project/agent id. * @param displayName Name of the knowledge base. * @return The created KnowledgeBase. */ @@ -72,36 +47,4 @@ public static KnowledgeBase createKnowledgeBase(String projectId, String display } } // [END dialogflow_create_knowledge_base] - - // [START dialogflow_get_knowledge_base] - - /** - * @param knowledgeBaseName Knowledge base id. - * @return The retrieved KnowledgeBase. - */ - public static KnowledgeBase getKnowledgeBase(String knowledgeBaseName) throws Exception { - // Instantiates a client - try (KnowledgeBasesClient knowledgeBasesClient = KnowledgeBasesClient.create()) { - KnowledgeBase response = knowledgeBasesClient.getKnowledgeBase(knowledgeBaseName); - System.out.format("Got Knowledge Base:\n"); - System.out.format(" - Display Name: %s\n", response.getDisplayName()); - System.out.format(" - Knowledge ID: %s\n", response.getName()); - return response; - } - } - // [END dialogflow_get_knowledge_base] - - // [START dialogflow_delete_knowledge_base] - - /** - * @param knowledgeBaseName Knowledge base id. - */ - public static void deleteKnowledgeBase(String knowledgeBaseName) throws Exception { - // Instantiates a client - try (KnowledgeBasesClient knowledgeBasesClient = KnowledgeBasesClient.create()) { - knowledgeBasesClient.deleteKnowledgeBase(knowledgeBaseName); - System.out.format("KnowledgeBase has been deleted.\n"); - } - } - // [END dialogflow_delete_knowledge_base] } diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateDocumentTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateDocumentTest.java new file mode 100644 index 00000000000..e75405a0b6f --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateDocumentTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBase; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient; +import com.google.cloud.dialogflow.v2beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateDocumentTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String KNOWLEDGE_DISPLAY_NAME = UUID.randomUUID().toString(); + private static String DOCUMENT_DISPLAY_NAME = UUID.randomUUID().toString(); + private ByteArrayOutputStream bout; + private PrintStream out; + private String knowledgeBaseName; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + // Create a knowledge base for the document + try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) { + KnowledgeBase knowledgeBase = + KnowledgeBase.newBuilder().setDisplayName(KNOWLEDGE_DISPLAY_NAME).build(); + ProjectName projectName = ProjectName.of(PROJECT_ID); + KnowledgeBase response = client.createKnowledgeBase(projectName, knowledgeBase); + // Save the full name for deletion + knowledgeBaseName = response.getName(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Delete the created knowledge base + try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) { + DeleteKnowledgeBaseRequest request = + DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build(); + client.deleteKnowledgeBase(request); + } + + System.setOut(null); + } + + @Test + public void testCreateDocument() throws Exception { + DocumentManagement.createDocument( + knowledgeBaseName, + DOCUMENT_DISPLAY_NAME, + "text/html", + "FAQ", + "https://cloud.google.com/storage/docs/faq"); + String got = bout.toString(); + assertThat(got).contains(DOCUMENT_DISPLAY_NAME); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateEntityTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateEntityTest.java new file mode 100644 index 00000000000..0bfed56507b --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateEntityTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2.EntityType; +import com.google.cloud.dialogflow.v2.EntityType.Kind; +import com.google.cloud.dialogflow.v2.EntityTypesClient; +import com.google.cloud.dialogflow.v2.ProjectAgentName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateEntityTest { + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String ENTITY_TYPE_DISPLAY_NAME = + "entity_" + UUID.randomUUID().toString().substring(0, 23); + private static String ENTITY_VALUE = UUID.randomUUID().toString(); + private static List SYNONYMS = + Arrays.asList("fake_synonym_for_testing_1", "fake_synonym_for_testing_2"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String entityTypeName; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + // Create a entity type to hold an entity + try (EntityTypesClient client = EntityTypesClient.create()) { + ProjectAgentName parent = ProjectAgentName.of(PROJECT_ID); + EntityType entityType = + EntityType.newBuilder() + .setDisplayName(ENTITY_TYPE_DISPLAY_NAME) + .setKind(Kind.valueOf("KIND_MAP")) + .build(); + EntityType response = client.createEntityType(parent, entityType); + entityTypeName = response.getName(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Delete the created entity type + try (EntityTypesClient client = EntityTypesClient.create()) { + client.deleteEntityType(entityTypeName); + } + + System.setOut(null); + } + + @Test + public void testCreateEntity() throws Exception { + String entityTypeId = entityTypeName.split("/")[entityTypeName.split("/").length - 1]; + EntityManagement.createEntity( + PROJECT_ID, entityTypeId, ENTITY_VALUE, Collections.singletonList("")); + String got = bout.toString(); + assertThat(got).contains("Entity created"); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateKnowledgeBaseTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateKnowledgeBaseTest.java new file mode 100644 index 00000000000..5581868f1a7 --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/CreateKnowledgeBaseTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBase; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateKnowledgeBaseTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String KNOWLEDGE_DISPLAY_NAME = UUID.randomUUID().toString(); + private ByteArrayOutputStream bout; + private PrintStream out; + private String knowledgeBaseName; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Delete the created knowledge base + try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) { + DeleteKnowledgeBaseRequest request = + DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build(); + client.deleteKnowledgeBase(request); + } + System.setOut(null); + } + + @Test + public void testCreateKnowledgeBase() throws Exception { + KnowledgeBase knowledgeBase = + KnowledgeBaseManagement.createKnowledgeBase(PROJECT_ID, KNOWLEDGE_DISPLAY_NAME); + knowledgeBaseName = knowledgeBase.getName(); + String got = bout.toString(); + assertThat(got).contains(KNOWLEDGE_DISPLAY_NAME); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteDocumentTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteDocumentTest.java new file mode 100644 index 00000000000..d46eede2319 --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteDocumentTest.java @@ -0,0 +1,125 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.dialogflow.v2beta1.CreateDocumentRequest; +import com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest; +import com.google.cloud.dialogflow.v2beta1.Document; +import com.google.cloud.dialogflow.v2beta1.Document.KnowledgeType; +import com.google.cloud.dialogflow.v2beta1.DocumentsClient; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBase; +import com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient; +import com.google.cloud.dialogflow.v2beta1.KnowledgeOperationMetadata; +import com.google.cloud.dialogflow.v2beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteDocumentTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String KNOWLEDGE_DISPLAY_NAME = UUID.randomUUID().toString(); + private static String DOCUMENT_DISPLAY_NAME = UUID.randomUUID().toString(); + private ByteArrayOutputStream bout; + private PrintStream out; + private String knowledgeBaseName; + private String documentName; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // Create a knowledge base for the document + try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) { + KnowledgeBase knowledgeBase = + KnowledgeBase.newBuilder().setDisplayName(KNOWLEDGE_DISPLAY_NAME).build(); + ProjectName projectName = ProjectName.of(PROJECT_ID); + KnowledgeBase response = client.createKnowledgeBase(projectName, knowledgeBase); + // Save the full name for deletion + knowledgeBaseName = response.getName(); + } + + // Create the Document to delete + try (DocumentsClient documentsClient = DocumentsClient.create()) { + Document document = + Document.newBuilder() + .setDisplayName(DOCUMENT_DISPLAY_NAME) + .setContentUri("https://cloud.google.com/storage/docs/faq") + .setMimeType("text/html") + .addKnowledgeTypes(KnowledgeType.valueOf("FAQ")) + .build(); + CreateDocumentRequest createDocumentRequest = + CreateDocumentRequest.newBuilder() + .setDocument(document) + .setParent(knowledgeBaseName) + .build(); + OperationFuture response = + documentsClient.createDocumentAsync(createDocumentRequest); + Document createdDocument = response.get(180, TimeUnit.SECONDS); + documentName = createdDocument.getName(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Delete the created knowledge base + try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) { + DeleteKnowledgeBaseRequest request = + DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build(); + client.deleteKnowledgeBase(request); + } + System.setOut(null); + } + + @Test + public void testCreateDocument() throws Exception { + DocumentManagement.deleteDocument(documentName); + String got = bout.toString(); + assertThat(got).contains("The document has been deleted"); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteEntityTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteEntityTest.java new file mode 100644 index 00000000000..5b1853f6ea5 --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DeleteEntityTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2.EntityType; +import com.google.cloud.dialogflow.v2.EntityType.Entity; +import com.google.cloud.dialogflow.v2.EntityType.Kind; +import com.google.cloud.dialogflow.v2.EntityTypesClient; +import com.google.cloud.dialogflow.v2.ProjectAgentName; +import com.google.protobuf.Empty; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteEntityTest { + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String ENTITY_TYPE_DISPLAY_NAME = + "entity_" + UUID.randomUUID().toString().substring(0, 23); + private static String ENTITY_VALUE = UUID.randomUUID().toString(); + private static List SYNONYMS = + Arrays.asList("fake_synonym_for_testing_1", "fake_synonym_for_testing_2"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String entityTypeName; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Create a entity type to hold an entity + try (EntityTypesClient client = EntityTypesClient.create()) { + ProjectAgentName parent = ProjectAgentName.of(PROJECT_ID); + EntityType entityType = + EntityType.newBuilder() + .setDisplayName(ENTITY_TYPE_DISPLAY_NAME) + .setKind(Kind.valueOf("KIND_MAP")) + .build(); + EntityType response = client.createEntityType(parent, entityType); + entityTypeName = response.getName(); + + // Create an entity to delete + Entity entity = Entity.newBuilder().setValue(ENTITY_VALUE).addAllSynonyms(SYNONYMS).build(); + Empty empty = + client.batchCreateEntitiesAsync(entityTypeName, Collections.singletonList(entity)).get(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Delete the created entity type + try (EntityTypesClient client = EntityTypesClient.create()) { + client.deleteEntityType(entityTypeName); + } + + System.setOut(null); + } + + @Test + public void testDeleteEntity() throws Exception { + String entityTypeId = entityTypeName.split("/")[entityTypeName.split("/").length - 1]; + EntityManagement.deleteEntity(PROJECT_ID, entityTypeId, ENTITY_VALUE); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentKnowledgeTest.java similarity index 62% rename from dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java rename to dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentKnowledgeTest.java index 2f8415b4792..943ab89c8d4 100644 --- a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentKnowledgeTest.java @@ -42,7 +42,7 @@ */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class KnowledgeBaseManagementIT { +public class DetectIntentKnowledgeTest { private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); private static String TEST_KNOWLEDGE_BASE_ID = "MTA4MzE0ODY5NTczMTQzNzU2ODA"; @@ -67,65 +67,6 @@ public void tearDown() { System.setOut(null); } - @Test - public void testKnowledgeBase() throws Exception { - // Create a Knowledge Base - KnowledgeBase knowledgeBase = - KnowledgeBaseManagement.createKnowledgeBase(PROJECT_ID, KNOWLEDGE_BASE_NAME); - assertThat(knowledgeBase.getDisplayName()).contains(KNOWLEDGE_BASE_NAME); - - // Get KnowledgeBase - knowledgeBase = KnowledgeBaseManagement.getKnowledgeBase(knowledgeBase.getName()); - assertThat(knowledgeBase.getDisplayName()).contains(KNOWLEDGE_BASE_NAME); - - // List Knowledge Bases - List knowledgeBases = KnowledgeBaseManagement.listKnowledgeBases(PROJECT_ID); - assertThat(knowledgeBases.size()).isAtLeast(2); - - int found = 0; - for (KnowledgeBase knowledgeBase1 : knowledgeBases) { - if (knowledgeBase1.getDisplayName().equals(KNOWLEDGE_BASE_NAME)) { - found += 1; - } - } - assertThat(found).isEqualTo(1); - - // Delete the Knowledge Base - KnowledgeBaseManagement.deleteKnowledgeBase(knowledgeBase.getName()); - } - - @Test - public void testDocumentManagement() throws Exception { - // Create a Knowledge Base - KnowledgeBase knowledgeBase = - KnowledgeBaseManagement.createKnowledgeBase(PROJECT_ID, KNOWLEDGE_BASE_NAME); - String knowledgeBaseName = knowledgeBase.getName(); - - // Create a Document - Document document = DocumentManagement.createDocument( - knowledgeBaseName, - DOCUMENT_BASE_NAME, - "text/html", - "FAQ", - "https://cloud.google.com/storage/docs/faq"); - assertThat(document.getDisplayName()).contains(DOCUMENT_BASE_NAME); - - // List the Documents - List documents = DocumentManagement.listDocuments(knowledgeBaseName); - assertThat(documents.size()).isEqualTo(1); - assertThat(documents.get(0).getDisplayName()).contains(DOCUMENT_BASE_NAME); - - // Get the Document - document = DocumentManagement.getDocument(document.getName()); - assertThat(document.getDisplayName()).contains(DOCUMENT_BASE_NAME); - - // Delete the Document - DocumentManagement.deleteDocument(document.getName()); - - // Delete the Knowledge Base - KnowledgeBaseManagement.deleteKnowledgeBase(knowledgeBase.getName()); - } - @Test public void testDetectIntentKnowledge() throws Exception { KnowledgeBaseName knowledgeBaseName = KnowledgeBaseName.newBuilder() diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioAndModelSelectionIT.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioIT.java similarity index 75% rename from dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioAndModelSelectionIT.java rename to dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioIT.java index 3f70a08d192..b351ac067a6 100644 --- a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioAndModelSelectionIT.java +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentWithAudioIT.java @@ -35,9 +35,6 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** - * Integration (system) tests for {@link DetectIntentWithModelSelection}. - */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class DetectIntentWithAudioAndModelSelectionIT { @@ -87,24 +84,4 @@ public void testDetectIntentAudio() throws Exception { assertTrue(result.getAllRequiredParamsPresent()); assertEquals("Choose a room please.", fulfillmentText); } - - @Test - public void testDetectIntentWithModelSelection() throws Exception { - List askedQuestions = Lists.newArrayList(); - QueryResult result = DetectIntentWithModelSelection.detectIntentWithModelSelection( - PROJECT_ID, "resources/book_a_room.wav", SESSION_ID, LANGUAGE_CODE); - String fulfillmentText = result.getFulfillmentText(); - while (!result.getAllRequiredParamsPresent() - && ANSWERS.containsKey(fulfillmentText) - && !askedQuestions.contains(fulfillmentText)) { - askedQuestions.add(result.getFulfillmentText()); - assertEquals("room.reservation", result.getAction()); - assertThat(QUESTIONS).contains(fulfillmentText); - result = DetectIntentWithModelSelection.detectIntentWithModelSelection( - PROJECT_ID, ANSWERS.get(fulfillmentText), SESSION_ID, LANGUAGE_CODE); - fulfillmentText = result.getFulfillmentText(); - } - assertTrue(result.getAllRequiredParamsPresent()); - assertEquals("Choose a room please.", fulfillmentText); - } } diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/GetDocumentTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/GetDocumentTest.java new file mode 100644 index 00000000000..cac09729842 --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/GetDocumentTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2beta1.DocumentName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class GetDocumentTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String TEST_KNOWLEDGE_BASE_ID = "MTA4MzE0ODY5NTczMTQzNzU2ODA"; + private static String TEST_DOCUMENT_ID = "MTUwNjk0ODg1NTU4NzkzMDExMg"; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testGetDocument() throws Exception { + DocumentName documentName = + DocumentName.of(PROJECT_ID, TEST_KNOWLEDGE_BASE_ID, TEST_DOCUMENT_ID); + DocumentManagement.getDocument(documentName.toString()); + String got = bout.toString(); + assertThat(got).contains(TEST_DOCUMENT_ID); + } +} diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/ListDocumentsTest.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/ListDocumentsTest.java new file mode 100644 index 00000000000..e977d6c8c82 --- /dev/null +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/ListDocumentsTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.dialogflow; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ListDocumentsTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String TEST_KNOWLEDGE_BASE_ID = "MTA4MzE0ODY5NTczMTQzNzU2ODA"; + private static String TEST_DOCUMENT_ID = "MTUwNjk0ODg1NTU4NzkzMDExMg"; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testGetDocument() throws Exception { + KnowledgeBaseName knowledgeBaseName = + KnowledgeBaseName.of(PROJECT_ID, TEST_KNOWLEDGE_BASE_ID); + DocumentManagement.listDocuments(knowledgeBaseName.toString()); + String got = bout.toString(); + assertThat(got).contains(TEST_DOCUMENT_ID); + } +}