Skip to content

Commit 3701e95

Browse files
committed
Delete dataset after each test in DatasetTests.
1 parent 19bf13b commit 3701e95

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

healthcare/v1/src/test/java/snippets/healthcare/DatasetTests.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.PrintStream;
2727
import java.nio.charset.StandardCharsets;
2828
import java.util.UUID;
29+
2930
import org.junit.After;
3031
import org.junit.Before;
3132
import org.junit.BeforeClass;
@@ -69,8 +70,8 @@ public void beforeTest() throws IOException {
6970
System.setOut(new PrintStream(bout));
7071

7172
String datasetId = "dataset-" + UUID.randomUUID().toString().replaceAll("-", "_");
72-
datasetName =
73-
String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, datasetId);
73+
String parentName = String.format("projects/%s/locations/%s", PROJECT_ID, REGION_ID);
74+
datasetName = String.format("%s/datasets/%s", parentName, datasetId);
7475

7576
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, datasetId);
7677

@@ -79,30 +80,41 @@ public void beforeTest() throws IOException {
7980
}
8081

8182
@After
82-
public void tearDown() {
83+
public void tearDown() throws IOException {
8384
System.setOut(originalOut);
85+
try {
86+
DatasetDelete.datasetDelete(datasetName);
87+
} catch (GoogleJsonResponseException ex) {
88+
// Dataset already deleted, continue.
89+
}
8490
bout.reset();
8591
}
8692

8793
@Test
88-
public void test_DatasetCreate() throws IOException {
89-
String newDatasetName =
90-
String.format("projects/%s/locations/%s/datasets/new-dataset", PROJECT_ID, REGION_ID);
94+
public void test_DatasetCreateDelete() throws IOException {
95+
String newName = "new-dataset";
96+
String newFullName =
97+
String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, newName);
9198
try {
92-
DatasetDelete.datasetDelete(newDatasetName);
99+
DatasetDelete.datasetDelete(newFullName);
93100
} catch (GoogleJsonResponseException gjre) {
94101
// Expected if new-dataset does not exist.
95102
}
96-
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, "new-dataset");
103+
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, newName);
97104

98105
String output = bout.toString(StandardCharsets.UTF_8);
99106
assertThat(output, containsString("Dataset created."));
107+
bout.reset();
108+
109+
DatasetDelete.datasetDelete(newFullName);
110+
111+
output = bout.toString();
112+
assertThat(output, containsString("Dataset deleted."));
100113
}
101114

102115
@Test
103116
public void test_DatasetGet() throws IOException {
104117
DatasetGet.datasetGet(datasetName);
105-
106118
String output = bout.toString();
107119
assertThat(output, containsString("Dataset retrieved:"));
108120
}
@@ -146,12 +158,4 @@ public void test_DatasetSetIamPolicy() throws IOException {
146158
String output = bout.toString();
147159
assertThat(output, containsString("Dataset policy has been updated: "));
148160
}
149-
150-
@Test
151-
public void test_DatasetDelete() throws IOException {
152-
DatasetDelete.datasetDelete(datasetName);
153-
154-
String output = bout.toString();
155-
assertThat(output, containsString("Dataset deleted."));
156-
}
157161
}

0 commit comments

Comments
 (0)