Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8c1edfd
remove unused list_* samples
Strykrol Mar 24, 2020
3caa81a
Merge branch 'master' into snippetCleanup
lesv Mar 25, 2020
96e34ca
dialogflow: delete unused context management samples
Strykrol Apr 3, 2020
be7df0d
dialogflow: delete unused detect intent with model selection samples
Strykrol Apr 3, 2020
a921583
dialogflow: delete unused entity type management samples
Strykrol Apr 3, 2020
acef492
dialogflow: delete unused knowledgebase management samples
Strykrol Apr 3, 2020
ca5f614
dialogflow: delete unused session entity type management samples
Strykrol Apr 3, 2020
5c68ccd
Merge branch 'snippetCleanup' of https://github.com/Strykrol/java-doc…
Strykrol Apr 3, 2020
cf6b2f6
Create CreateDocumentTest
nnegrey Apr 3, 2020
b31ade6
Create CreateEntityTest
nnegrey Apr 3, 2020
a3e2baa
Delete ContextManagementIT.java
nnegrey Apr 3, 2020
88d4cc0
Create CreateKnowledgeBaseTest
nnegrey Apr 3, 2020
9529431
Create DeleteDocumentTest
nnegrey Apr 3, 2020
dbb58c5
Create DeleteEntityTest
nnegrey Apr 3, 2020
8157b04
Update and rename KnowledgeBaseManagementIT.java to DetectIntentKnowl…
nnegrey Apr 3, 2020
7ed17e0
Update and rename DetectIntentWithAudioAndModelSelectionIT.java to De…
nnegrey Apr 3, 2020
5c01b08
Create GetDocumentTest
nnegrey Apr 3, 2020
84f6873
Create ListDocumentsTest
nnegrey Apr 3, 2020
b27cbf7
Delete SessionEntityTypeManagementIT.java
nnegrey Apr 3, 2020
d732880
Delete EntityManagementIT.java
nnegrey Apr 3, 2020
58c41a0
Update KnowledgeBaseManagement.java
nnegrey Apr 3, 2020
3eca251
Update DocumentManagement.java
nnegrey Apr 3, 2020
21c6ef8
Update DetectIntentWithTextToSpeechResponse.java
nnegrey Apr 3, 2020
485ce50
Update DetectIntentWithSentimentAnalysis.java
nnegrey Apr 3, 2020
3aef9be
Update DetectIntentTexts.java
nnegrey Apr 3, 2020
2c439b1
Update DetectIntentKnowledge.java
nnegrey Apr 3, 2020
527a987
Rename CreateDocumentTest to CreateDocumentTest.java
nnegrey Apr 3, 2020
539ceff
Rename CreateEntityTest to CreateEntityTest.java
nnegrey Apr 3, 2020
c516e9a
Rename CreateKnowledgeBaseTest to CreateKnowledgeBaseTest.java
nnegrey Apr 3, 2020
c8f2664
Rename DeleteDocumentTest to DeleteDocumentTest.java
nnegrey Apr 3, 2020
08b9cd1
Rename DeleteEntityTest to DeleteEntityTest.java
nnegrey Apr 3, 2020
8867e88
Rename ListDocumentsTest to ListDocumentsTest.java
nnegrey Apr 3, 2020
fecc2ed
Rename GetDocumentTest to GetDocumentTest.java
nnegrey Apr 3, 2020
53feaef
Update DeleteDocumentTest.java
nnegrey Apr 3, 2020
d12cee0
Update DeleteDocumentTest.java
nnegrey Apr 3, 2020
01ad5c0
merge master to branch
Strykrol Apr 7, 2020
3acc1bf
Merge branch 'master' into snippetCleanup
nnegrey Apr 7, 2020
693efc4
Merge branch 'master' into snippetCleanup
nnegrey Apr 7, 2020
f6ddb01
Update DeleteDocumentTest.java
nnegrey Apr 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +66,8 @@ public static Map<String, KnowledgeAnswers> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +60,8 @@ public static Map<String, QueryResult> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +58,8 @@ public static Map<String, QueryResult> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +58,8 @@ public static Map<String, QueryResult> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,6 @@ public static void deleteDocument(String documentName) throws Exception {
System.out.format("The document has been deleted.");
}
}

// [END dialogflow_delete_document]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> 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<Entity> 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]

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KnowledgeBase> listKnowledgeBases(String projectId) throws Exception {
List<KnowledgeBase> 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.
*/
Expand All @@ -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]
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
Loading