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 ;
22
+ import com .google .cloud .dialogflow .v2 .DeleteKnowledgeBaseRequest ;
23
+ import com .google .cloud .dialogflow .v2 .KnowledgeBasesClient ;
25
24
import java .io .ByteArrayOutputStream ;
26
25
import java .io .IOException ;
27
26
import java .io .PrintStream ;
38
37
public class CreateKnowledgeBaseTest {
39
38
40
39
private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
40
+ private static final String LOCATION = "global" ;
41
+ private static final String ID_PREFIX_IN_OUTPUT = "Name: " ;
41
42
private static String KNOWLEDGE_DISPLAY_NAME = UUID .randomUUID ().toString ();
42
- private ByteArrayOutputStream bout ;
43
- private PrintStream out ;
44
43
private String knowledgeBaseName ;
44
+ private ByteArrayOutputStream bout ;
45
+ private PrintStream newOutputStream ;
46
+ private PrintStream originalOutputStream ;
45
47
46
48
private static void requireEnvVar (String varName ) {
47
- assertNotNull (String .format (varName ), String .format (varName ));
49
+ assertNotNull (System .getenv (varName ));
50
+ }
51
+
52
+ // Extract the name of created resource from "Name: %s\n" in sample code output
53
+ private static String getResourceNameFromOutputString (String output ) {
54
+ return output .substring (
55
+ output .lastIndexOf (ID_PREFIX_IN_OUTPUT ) + ID_PREFIX_IN_OUTPUT .length (),
56
+ output .length () - 1 );
48
57
}
49
58
50
59
@ BeforeClass
@@ -56,27 +65,30 @@ public static void checkRequirements() {
56
65
@ Before
57
66
public void setUp () {
58
67
bout = new ByteArrayOutputStream ();
59
- out = new PrintStream (bout );
60
- System .setOut (out );
68
+ newOutputStream = new PrintStream (bout );
69
+ System .setOut (newOutputStream );
61
70
}
62
71
63
72
@ After
64
73
public void tearDown () throws IOException {
74
+ if (knowledgeBaseName == null ) {
75
+ return ;
76
+ }
77
+
65
78
// Delete the created knowledge base
66
79
try (KnowledgeBasesClient client = KnowledgeBasesClient .create ()) {
67
80
DeleteKnowledgeBaseRequest request =
68
81
DeleteKnowledgeBaseRequest .newBuilder ().setName (knowledgeBaseName ).setForce (true ).build ();
69
82
client .deleteKnowledgeBase (request );
70
83
}
71
- System .setOut (null );
84
+ System .setOut (originalOutputStream );
72
85
}
73
86
74
87
@ Test
75
88
public void testCreateKnowledgeBase () throws Exception {
76
- KnowledgeBase knowledgeBase =
77
- KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , KNOWLEDGE_DISPLAY_NAME );
78
- knowledgeBaseName = knowledgeBase .getName ();
79
- String got = bout .toString ();
80
- assertThat (got ).contains (KNOWLEDGE_DISPLAY_NAME );
89
+ KnowledgeBaseManagement .createKnowledgeBase (PROJECT_ID , LOCATION , KNOWLEDGE_DISPLAY_NAME );
90
+ String output = bout .toString ();
91
+ assertThat (output ).contains (KNOWLEDGE_DISPLAY_NAME );
92
+ knowledgeBaseName = getResourceNameFromOutputString (output );
81
93
}
82
94
}
0 commit comments