1919import static com .google .common .truth .Truth .assertThat ;
2020import static org .junit .Assert .assertEquals ;
2121
22- import com .google .cloud .dialogflow .v2beta1 .DeleteDocumentRequest ;
2322import com .google .cloud .dialogflow .v2beta1 .Document ;
2423import com .google .cloud .dialogflow .v2beta1 .DocumentName ;
25- import com .google .cloud .dialogflow .v2beta1 .DocumentsClient ;
2624import com .google .cloud .dialogflow .v2beta1 .KnowledgeAnswers ;
2725import com .google .cloud .dialogflow .v2beta1 .KnowledgeAnswers .Answer ;
2826import com .google .cloud .dialogflow .v2beta1 .KnowledgeBase ;
2927import com .google .cloud .dialogflow .v2beta1 .KnowledgeBaseName ;
30- import com .google .cloud .dialogflow .v2beta1 .KnowledgeBasesClient ;
31- import com .google .cloud .dialogflow .v2beta1 .ProjectName ;
3228import com .google .common .collect .ImmutableList ;
3329import java .io .ByteArrayOutputStream ;
3430import java .io .PrintStream ;
3531import java .util .List ;
3632import java .util .Map ;
33+ import java .util .UUID ;
3734
3835import org .junit .After ;
3936import org .junit .Before ;
@@ -51,114 +48,83 @@ public class KnowledgeBaseManagementIT {
5148 private static String PROJECT_ID = System .getenv ().get ("GOOGLE_CLOUD_PROJECT" );
5249 private static String TEST_KNOWLEDGE_BASE_ID = "MTA4MzE0ODY5NTczMTQzNzU2ODA" ;
5350 private static String TEST_DOCUMENT_ID = "MTUwNjk0ODg1NTU4NzkzMDExMg" ;
54- private static String SESSION_ID = "fake_session_for_testing" ;
51+ private static String SESSION_ID = UUID . randomUUID (). toString () ;
5552 private static String LANGUAGE_CODE = "en-US" ;
56- private static String KNOWLEDGE_BASE_NAME = "fake_knowledge_base_name" ;
57- private static String DOCUMENT_BASE_NAME = "fake_document_name" ;
53+ private static String KNOWLEDGE_BASE_NAME = UUID . randomUUID (). toString () ;
54+ private static String DOCUMENT_BASE_NAME = UUID . randomUUID (). toString () ;
5855
5956 private static List <String > TEXTS = ImmutableList
60- .of ("How do I sign up?" , "Is my data redundant?" , "Where can I find pricing information?" ,
61- "Where is my data stored?" , "What are my support options?" ,
62- "How can I maximize the availability of my data?" );
57+ .of ("How do I sign up?" , "Is my data redundant?" , "Where can I find pricing information?" ,
58+ "Where is my data stored?" , "What are my support options?" ,
59+ "How can I maximize the availability of my data?" );
6360
6461 @ Before
6562 public void setUp () {
6663 System .setOut (new PrintStream (new ByteArrayOutputStream ()));
6764 }
6865
69- // If any knowledge base/documents remain after test complete, delete them.
7066 @ After
71- public void tearDown () throws Exception {
72- try (KnowledgeBasesClient knowledgeBasesClient = KnowledgeBasesClient .create ()) {
73- try (DocumentsClient documentsClient = DocumentsClient .create ()) {
74- ProjectName projectName = ProjectName .of (PROJECT_ID );
75- for (KnowledgeBase knowledgeBase :
76- knowledgeBasesClient .listKnowledgeBases (projectName ).iterateAll ()) {
77- // DO NOT DELETE THE TEST KNOWLEDGE BASE
78- if (!knowledgeBase .getName ().contains (TEST_KNOWLEDGE_BASE_ID )) {
79- // Delete any documents in the knowledge base.
80- for (Document document : documentsClient .listDocuments (
81- knowledgeBase .getName ()).iterateAll ()) {
82- // DO NOT DELETE THE TEST DOCUMENT
83- if (!document .getName ().contains (TEST_DOCUMENT_ID )) {
84- documentsClient .deleteDocumentCallable ().call (
85- DeleteDocumentRequest .newBuilder ().setName (document .getName ()).build ());
86- }
87- }
88- knowledgeBasesClient .deleteKnowledgeBase (knowledgeBase .getName ());
89- }
90- }
91- }
92- }
67+ public void tearDown () {
9368 System .setOut (null );
9469 }
9570
9671 @ Test
9772 public void testKnowledgeBase () throws Exception {
98- // Check the knowledge base does not yet exist
99- List <KnowledgeBase > knowledgeBases = KnowledgeBaseManagement .listKnowledgeBases (PROJECT_ID );
100- assertEquals (1 , knowledgeBases .size ());
101-
10273 // Create a Knowledge Base
10374 KnowledgeBase knowledgeBase =
104- KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , KNOWLEDGE_BASE_NAME );
105- assertEquals (knowledgeBase .getDisplayName (), KNOWLEDGE_BASE_NAME );
75+ KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , KNOWLEDGE_BASE_NAME );
76+ assertThat (knowledgeBase .getDisplayName ()). contains ( KNOWLEDGE_BASE_NAME );
10677
10778 // Get KnowledgeBase
10879 knowledgeBase = KnowledgeBaseManagement .getKnowledgeBase (knowledgeBase .getName ());
109- assertEquals (knowledgeBase .getDisplayName (), KNOWLEDGE_BASE_NAME );
80+ assertThat (knowledgeBase .getDisplayName ()). contains ( KNOWLEDGE_BASE_NAME );
11081
11182 // List Knowledge Bases
112- knowledgeBases = KnowledgeBaseManagement .listKnowledgeBases (PROJECT_ID );
113- assertEquals ( 2 , knowledgeBases .size ());
83+ List < KnowledgeBase > knowledgeBases = KnowledgeBaseManagement .listKnowledgeBases (PROJECT_ID );
84+ assertThat ( knowledgeBases .size ()). isAtLeast ( 2 );
11485
11586 int found = 0 ;
11687 for (KnowledgeBase knowledgeBase1 : knowledgeBases ) {
11788 if (knowledgeBase1 .getDisplayName ().equals (KNOWLEDGE_BASE_NAME )) {
11889 found += 1 ;
11990 }
12091 }
121- assertEquals ( 1 , found );
92+ assertThat ( found ). isEqualTo ( 1 );
12293
12394 // Delete the Knowledge Base
12495 KnowledgeBaseManagement .deleteKnowledgeBase (knowledgeBase .getName ());
125-
126- // List Knowledge Bases (ensure delete success)
127- knowledgeBases = KnowledgeBaseManagement .listKnowledgeBases (PROJECT_ID );
128- assertEquals (1 , knowledgeBases .size ());
12996 }
13097
13198 @ Test
13299 public void testDocumentManagement () throws Exception {
133100 // Create a Knowledge Base
134101 KnowledgeBase knowledgeBase =
135- KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , KNOWLEDGE_BASE_NAME );
102+ KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , KNOWLEDGE_BASE_NAME );
136103 String knowledgeBaseName = knowledgeBase .getName ();
137104
138105 // Create a Document
139106 Document document = DocumentManagement .createDocument (
140- knowledgeBaseName ,
141- DOCUMENT_BASE_NAME ,
142- "text/html" ,
143- "FAQ" ,
144- "https://cloud.google.com/storage/docs/faq" );
145- assertEquals ( DOCUMENT_BASE_NAME , document .getDisplayName ());
107+ knowledgeBaseName ,
108+ DOCUMENT_BASE_NAME ,
109+ "text/html" ,
110+ "FAQ" ,
111+ "https://cloud.google.com/storage/docs/faq" );
112+ assertThat ( document .getDisplayName ()). contains ( DOCUMENT_BASE_NAME );
146113
147114 // List the Documents
148115 List <Document > documents = DocumentManagement .listDocuments (knowledgeBaseName );
149- assertEquals ( 1 , documents .size ());
150- assertEquals ( DOCUMENT_BASE_NAME , documents .get (0 ).getDisplayName ());
116+ assertThat ( documents .size ()). isEqualTo ( 1 );
117+ assertThat ( documents .get (0 ).getDisplayName ()). contains ( DOCUMENT_BASE_NAME );
151118
152119 // Get the Document
153120 document = DocumentManagement .getDocument (document .getName ());
154- assertEquals ( DOCUMENT_BASE_NAME , document .getDisplayName ());
121+ assertThat ( document .getDisplayName ()). contains ( DOCUMENT_BASE_NAME );
155122
156123 // Delete the Document
157124 DocumentManagement .deleteDocument (document .getName ());
158125
159- // List the Document
160- documents = DocumentManagement .listDocuments (knowledgeBaseName );
161- assertEquals (0 , documents .size ());
126+ // Delete the Knowledge Base
127+ KnowledgeBaseManagement .deleteKnowledgeBase (knowledgeBase .getName ());
162128 }
163129
164130 @ Test
0 commit comments