17
17
package com .example .datacatalog ;
18
18
19
19
import static org .junit .Assert .assertThat ;
20
+ import static org .junit .Assert .fail ;
20
21
21
22
import com .google .cloud .datacatalog .EntryGroupName ;
22
23
import com .google .cloud .datacatalog .EntryName ;
23
24
import com .google .cloud .datacatalog .v1beta1 .DataCatalogClient ;
24
25
import java .io .ByteArrayOutputStream ;
25
26
import java .io .PrintStream ;
27
+ import java .util .ArrayList ;
28
+ import java .util .List ;
26
29
import java .util .UUID ;
27
30
import org .hamcrest .CoreMatchers ;
28
31
import org .junit .After ;
@@ -38,15 +41,12 @@ public class CreateEntryTests {
38
41
39
42
private ByteArrayOutputStream bout ;
40
43
41
- private static String ENTRY_GROUP_ID_NO_CHILDREN =
42
- "entry_group_no_children_" + UUID .randomUUID ().toString ().substring (0 , 8 );
43
- private static String PARENT_ENTRY_GROUP_ID =
44
- "fileset_entry_group_parent_" + UUID .randomUUID ().toString ().substring (0 , 8 );
45
- private static String ENTRY_ID =
46
- "fileset_entry_id_" + UUID .randomUUID ().toString ().substring (0 , 8 );
47
44
private static String LOCATION = "us-central1" ;
48
45
private static String PROJECT_ID = System .getenv ().get ("GOOGLE_CLOUD_PROJECT" );
49
46
47
+ private static List <String > entryGroupsPendingDeletion = new ArrayList <>();
48
+ private static List <String > entriesPendingDeletion = new ArrayList <>();
49
+
50
50
@ Before
51
51
public void setUp () {
52
52
bout = new ByteArrayOutputStream ();
@@ -62,45 +62,67 @@ public void tearDown() {
62
62
@ AfterClass
63
63
public static void tearDownClass () {
64
64
try (DataCatalogClient dataCatalogClient = DataCatalogClient .create ()) {
65
- dataCatalogClient .deleteEntryGroup (
66
- EntryGroupName .of (PROJECT_ID , LOCATION , ENTRY_GROUP_ID_NO_CHILDREN ).toString ());
67
-
68
- dataCatalogClient .deleteEntry (
69
- EntryName .of (PROJECT_ID , LOCATION , PARENT_ENTRY_GROUP_ID , ENTRY_ID ).toString ());
70
- dataCatalogClient .deleteEntryGroup (
71
- EntryGroupName .of (PROJECT_ID , LOCATION , PARENT_ENTRY_GROUP_ID ).toString ());
65
+ // Must delete Entries before deleting the Entry Group.
66
+ if (entriesPendingDeletion .isEmpty () || entryGroupsPendingDeletion .isEmpty ()) {
67
+ fail ("Something went wrong, no entries were generated" );
68
+ }
69
+
70
+ for (String entryName : entriesPendingDeletion ) {
71
+ dataCatalogClient .deleteEntry (entryName );
72
+ }
73
+
74
+ for (String entryGroupName : entryGroupsPendingDeletion ) {
75
+ dataCatalogClient .deleteEntryGroup (entryGroupName );
76
+ }
72
77
} catch (Exception e ) {
73
78
System .out .println ("Error in cleaning up test data:\n " + e .toString ());
74
79
}
75
80
}
76
81
77
82
@ Test
78
83
public void testCreateFilesetEntry () {
84
+ String entryGroupId = "fileset_entry_group_parent_" + getUuid8Chars ();
85
+ String entryId = "fileset_entry_id_" + getUuid8Chars ();
86
+
79
87
// Must create a Entry Group before creating the entry.
80
- CreateEntryGroup .createEntryGroup (PROJECT_ID , PARENT_ENTRY_GROUP_ID );
81
- CreateFilesetEntry .createEntry (PROJECT_ID , PARENT_ENTRY_GROUP_ID , ENTRY_ID );
88
+ CreateEntryGroup .createEntryGroup (PROJECT_ID , entryGroupId );
89
+ CreateFilesetEntry .createEntry (PROJECT_ID , entryGroupId , entryId );
90
+
91
+ // Store names for clean up on teardown
92
+ String expectedEntryGroupName =
93
+ EntryGroupName .of (PROJECT_ID , LOCATION , entryGroupId ).toString ();
94
+ entryGroupsPendingDeletion .add (expectedEntryGroupName );
95
+
96
+ String expectedEntryName = EntryName .of (PROJECT_ID , LOCATION , entryGroupId , entryId ).toString ();
97
+ entriesPendingDeletion .add (expectedEntryName );
82
98
83
99
String output = bout .toString ();
84
100
85
- String entryTemplate =
86
- "Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s" ;
101
+ String entryTemplate = "Entry created with name: %s" ;
87
102
assertThat (
88
- output ,
89
- CoreMatchers .containsString (
90
- String .format (entryTemplate , PROJECT_ID , PARENT_ENTRY_GROUP_ID , ENTRY_ID )));
103
+ output , CoreMatchers .containsString (String .format (entryTemplate , expectedEntryName )));
91
104
}
92
105
93
106
@ Test
94
107
public void testCreateEntryGroup () {
95
- CreateEntryGroup .createEntryGroup (PROJECT_ID , ENTRY_GROUP_ID_NO_CHILDREN );
108
+ String entryGroupId = "entry_group_no_children_" + getUuid8Chars ();
109
+
110
+ CreateEntryGroup .createEntryGroup (PROJECT_ID , entryGroupId );
111
+
112
+ // Store names for clean up on teardown
113
+ String expectedEntryGroupName =
114
+ EntryGroupName .of (PROJECT_ID , LOCATION , entryGroupId ).toString ();
115
+ entryGroupsPendingDeletion .add (expectedEntryGroupName );
96
116
97
117
String output = bout .toString ();
98
118
99
- String entryGroupTemplate =
100
- "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s" ;
119
+ String entryGroupTemplate = "Entry Group created with name: %s" ;
101
120
assertThat (
102
121
output ,
103
- CoreMatchers .containsString (
104
- String .format (entryGroupTemplate , PROJECT_ID , ENTRY_GROUP_ID_NO_CHILDREN )));
122
+ CoreMatchers .containsString (String .format (entryGroupTemplate , expectedEntryGroupName )));
123
+ }
124
+
125
+ private String getUuid8Chars () {
126
+ return UUID .randomUUID ().toString ().substring (0 , 8 );
105
127
}
106
- }
128
+ }
0 commit comments