Skip to content

Commit

Permalink
samples: update sample code for document (#938)
Browse files Browse the repository at this point in the history
* update dialogflow to V2 for document sample code

* update code style

* add location for document sample code
  • Loading branch information
deliaqi committed Jun 18, 2022
1 parent 9847007 commit b632548
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.v2beta1.CreateDocumentRequest;
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.KnowledgeOperationMetadata;
import com.google.cloud.dialogflow.v2.CreateDocumentRequest;
import com.google.cloud.dialogflow.v2.Document;
import com.google.cloud.dialogflow.v2.Document.KnowledgeType;
import com.google.cloud.dialogflow.v2.DocumentsClient;
import com.google.cloud.dialogflow.v2.KnowledgeOperationMetadata;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DocumentManagement {

public static Document createDocument(
public static void createDocument(
String knowledgeBaseName,
String displayName,
String mimeType,
Expand All @@ -58,14 +58,13 @@ public static Document createDocument(
Document createdDocument = response.get(180, TimeUnit.SECONDS);
System.out.format("Created Document:\n");
System.out.format(" - Display Name: %s\n", createdDocument.getDisplayName());
System.out.format(" - Knowledge ID: %s\n", createdDocument.getName());
System.out.format(" - Document Name: %s\n", createdDocument.getName());
System.out.format(" - MIME Type: %s\n", createdDocument.getMimeType());
System.out.format(" - Knowledge Types:\n");
for (KnowledgeType knowledgeTypeId : document.getKnowledgeTypesList()) {
System.out.format(" - %s \n", knowledgeTypeId.getValueDescriptor());
}
System.out.format(" - Source: %s \n", document.getContentUri());
return createdDocument;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
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 com.google.cloud.dialogflow.v2.DeleteKnowledgeBaseRequest;
import com.google.cloud.dialogflow.v2.KnowledgeBase;
import com.google.cloud.dialogflow.v2.KnowledgeBasesClient;
import com.google.cloud.dialogflow.v2.LocationName;
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -41,14 +41,16 @@
public class CreateDocumentTest {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION = "global";
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 ByteArrayOutputStream bout;
private PrintStream newOutputStream;
private PrintStream originalOutputStream;

private static void requireEnvVar(String varName) {
assertNotNull(String.format(varName), String.format(varName));
assertNotNull(String.format(varName));
}

@BeforeClass
Expand All @@ -59,31 +61,36 @@ public static void checkRequirements() {

@Before
public void setUp() throws IOException {
originalOutputStream = System.out;
bout = new ByteArrayOutputStream();
newOutputStream = new PrintStream(bout);
System.setOut(newOutputStream);

// 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);
LocationName parent = LocationName.of(PROJECT_ID, LOCATION);
KnowledgeBase response = client.createKnowledgeBase(parent, 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 {
if (knowledgeBaseName == null) {
return;
}

// 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);
System.setOut(originalOutputStream);
}

@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
Expand Down

0 comments on commit b632548

Please sign in to comment.