19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static junit .framework .TestCase .assertNotNull ;
21
21
22
- import com .google .cloud .dialogflow .v2beta1 .DeleteKnowledgeBaseRequest ;
23
- import com .google .cloud .dialogflow .v2beta1 .KnowledgeBase ;
24
- import com .google .cloud .dialogflow .v2beta1 .KnowledgeBasesClient ;
25
- import com .google .cloud .dialogflow .v2beta1 . ProjectName ;
22
+ import com .google .cloud .dialogflow .v2 .DeleteKnowledgeBaseRequest ;
23
+ import com .google .cloud .dialogflow .v2 .KnowledgeBase ;
24
+ import com .google .cloud .dialogflow .v2 .KnowledgeBasesClient ;
25
+ import com .google .cloud .dialogflow .v2 . LocationName ;
26
26
import com .google .cloud .testing .junit4 .MultipleAttemptsRule ;
27
27
import java .io .ByteArrayOutputStream ;
28
28
import java .io .IOException ;
41
41
public class CreateDocumentTest {
42
42
43
43
private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
44
+ private static final String LOCATION = "global" ;
44
45
private static String KNOWLEDGE_DISPLAY_NAME = UUID .randomUUID ().toString ();
45
46
private static String DOCUMENT_DISPLAY_NAME = UUID .randomUUID ().toString ();
46
- private ByteArrayOutputStream bout ;
47
- private PrintStream out ;
48
47
private String knowledgeBaseName ;
48
+ private ByteArrayOutputStream bout ;
49
+ private PrintStream newOutputStream ;
50
+ private PrintStream originalOutputStream ;
49
51
50
52
private static void requireEnvVar (String varName ) {
51
- assertNotNull (String .format (varName ), String . format ( varName ) );
53
+ assertNotNull (String .format (varName ));
52
54
}
53
55
54
56
@ BeforeClass
@@ -59,31 +61,36 @@ public static void checkRequirements() {
59
61
60
62
@ Before
61
63
public void setUp () throws IOException {
64
+ originalOutputStream = System .out ;
65
+ bout = new ByteArrayOutputStream ();
66
+ newOutputStream = new PrintStream (bout );
67
+ System .setOut (newOutputStream );
68
+
62
69
// Create a knowledge base for the document
63
70
try (KnowledgeBasesClient client = KnowledgeBasesClient .create ()) {
64
71
KnowledgeBase knowledgeBase =
65
72
KnowledgeBase .newBuilder ().setDisplayName (KNOWLEDGE_DISPLAY_NAME ).build ();
66
- ProjectName projectName = ProjectName .of (PROJECT_ID );
67
- KnowledgeBase response = client .createKnowledgeBase (projectName , knowledgeBase );
73
+ LocationName parent = LocationName .of (PROJECT_ID , LOCATION );
74
+ KnowledgeBase response = client .createKnowledgeBase (parent , knowledgeBase );
68
75
// Save the full name for deletion
69
76
knowledgeBaseName = response .getName ();
70
77
}
71
-
72
- bout = new ByteArrayOutputStream ();
73
- out = new PrintStream (bout );
74
- System .setOut (out );
75
78
}
76
79
77
80
@ After
78
81
public void tearDown () throws IOException {
82
+ if (knowledgeBaseName == null ) {
83
+ return ;
84
+ }
85
+
79
86
// Delete the created knowledge base
80
87
try (KnowledgeBasesClient client = KnowledgeBasesClient .create ()) {
81
88
DeleteKnowledgeBaseRequest request =
82
89
DeleteKnowledgeBaseRequest .newBuilder ().setName (knowledgeBaseName ).setForce (true ).build ();
83
90
client .deleteKnowledgeBase (request );
84
91
}
85
92
86
- System .setOut (null );
93
+ System .setOut (originalOutputStream );
87
94
}
88
95
89
96
@ Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule (3 );
0 commit comments