From a9b5c7e65b001584bd6e73834412646566195c3e Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Tue, 22 Oct 2019 17:59:45 -0300 Subject: [PATCH 01/10] Add Data Catalog createEntry samples and tests --- datacatalog/cloud-client/build.gradle | 2 +- datacatalog/cloud-client/pom.xml | 2 +- .../datacatalog/CreateFilesetEntry.java | 137 ++++++++++++++++++ .../datacatalog/CreateFilesetEntryTests.java | 87 +++++++++++ 4 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java create mode 100644 datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java diff --git a/datacatalog/cloud-client/build.gradle b/datacatalog/cloud-client/build.gradle index cb6066b14b0..a8b2ab32667 100644 --- a/datacatalog/cloud-client/build.gradle +++ b/datacatalog/cloud-client/build.gradle @@ -19,7 +19,7 @@ repositories { } dependencies { - compile group: 'com.google.cloud', name: 'google-cloud-datacatalog', version:'0.4.0-alpha' + compile group: 'com.google.cloud', name: 'google-cloud-datacatalog', version:'0.28.0-alpha' testCompile group: 'com.google.truth', name: 'truth', version:'0.42' testCompile group: 'junit', name: 'junit', version:'4.13-beta-2' diff --git a/datacatalog/cloud-client/pom.xml b/datacatalog/cloud-client/pom.xml index 25d04d1d386..762f119bb21 100644 --- a/datacatalog/cloud-client/pom.xml +++ b/datacatalog/cloud-client/pom.xml @@ -39,7 +39,7 @@ com.google.cloud google-cloud-datacatalog - 0.14.0-alpha + 0.28.0-alpha diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java new file mode 100644 index 00000000000..4dde5630543 --- /dev/null +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -0,0 +1,137 @@ +/* + * Copyright 2019 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START create_fileset_entry] + +import com.google.cloud.datacatalog.ColumnSchema; +import com.google.cloud.datacatalog.CreateEntryGroupRequest; +import com.google.cloud.datacatalog.CreateEntryRequest; +import com.google.cloud.datacatalog.Entry; +import com.google.cloud.datacatalog.EntryGroup; +import com.google.cloud.datacatalog.EntryGroupName; +import com.google.cloud.datacatalog.EntryName; +import com.google.cloud.datacatalog.EntryType; +import com.google.cloud.datacatalog.GcsFilesetSpec; +import com.google.cloud.datacatalog.LocationName; +import com.google.cloud.datacatalog.Schema; + +import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; + +public class CreateFilesetEntry { + + /** + * Create Fileset Entry + * + * @param projectId The project ID to which the fileset belongs, e.g. 'my-project' + * @param entryGroupId The Entry Group ID to which the fileset belongs, + * e.g. 'fileset_entry_group' + * @param entryId The Entry ID for the fileset, e.g. 'fileset_entry_id' + */ + public static void createEntry(String projectId, String entryGroupId, String entryId) { + + // ------------------------------- + // Currently, Data Catalog stores metadata in the + // us-central1 region. + // ------------------------------- + String location = "us-central1"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + + // ------------------------------- + // 1. Environment cleanup: delete pre-existing data. + // ------------------------------- + // Delete any pre-existing Entry with the same name + // that will be used in step 3. + try { + dataCatalogClient.deleteEntry( + EntryName.of(projectId, location, entryGroupId, entryId).toString()); + } catch (Exception e) { + System.out.println("Entry does not exist."); + } + + // Delete any pre-existing Entry Group with the same name + // that will be used in step 2. + try { + dataCatalogClient.deleteEntryGroup( + EntryGroupName.of(projectId, location, entryGroupId).toString()); + } catch (Exception e) { + System.out.println("Entry Group does not exist."); + } + + // ------------------------------- + // 2. Create an Entry Group. + // ------------------------------- + // Construct the EntryGroup for the EntryGroup request. + EntryGroup entryGroup = EntryGroup.newBuilder().build(); + + // Construct the EntryGroup request to be sent by the client. + CreateEntryGroupRequest entryGroupRequest = CreateEntryGroupRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setEntryGroupId(entryGroupId) + .setEntryGroup(entryGroup) + .build(); + + // Use the client to send the API request. + EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest); + + System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName()); + + // ------------------------------- + // 3. Create a Fileset Entry. + // ------------------------------- + // Construct the Entry for the Entry request. + Entry entry = Entry.newBuilder() + .setDisplayName("My Fileset") + .setDescription("This fileset consists of ....") + .setGcsFilesetSpec(GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*") + .build()) + .setSchema(Schema.newBuilder() + .addColumns(ColumnSchema.newBuilder() + .setColumn("first_column") + .setType("STRING") + .setDescription("This columns consists of ....").build()) + .addColumns(ColumnSchema.newBuilder() + .setColumn("first_column") + .setType("STRING") + .setDescription("This columns consists of ....").build()) + .build()) + .setType(EntryType.FILESET) + .build(); + + // Construct the Entry request to be sent by the client. + CreateEntryRequest entryRequest = CreateEntryRequest.newBuilder() + .setParent(entryGroupResponse.getName()) + .setEntryId(entryId) + .setEntry(entry) + .build(); + + // Use the client to send the API request. + Entry entryResponse = dataCatalogClient.createEntry(entryRequest); + + System.out.printf("\nEntry created with name: %s\n", entryResponse.getName()); + + + } catch (Exception e) { + System.out.println("Error in create entry process:\n" + e.toString()); + } + } +} +// [END create_fileset_entry] \ No newline at end of file diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java new file mode 100644 index 00000000000..0cce34ea10e --- /dev/null +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datacatalog.EntryGroupName; +import com.google.cloud.datacatalog.EntryName; +import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link CreateFilesetEntry}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateFilesetEntryTests { + + private ByteArrayOutputStream bout; + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String LOCATION = "us-central1"; + private static String ENTRY_GROUP_ID = "fileset_entry_group"; + private static String ENTRY_ID = "fileset_entry_id"; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + dataCatalogClient.deleteEntry( + EntryName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID, ENTRY_ID).toString()); + dataCatalogClient.deleteEntryGroup( + EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); + } catch (Exception e) { + System.out.println("Error in cleaning up test data:\n" + e.toString()); + } + + } + + @Test + public void testCreateFilesetEntry() { + CreateFilesetEntry.createEntry(PROJECT_ID, "fileset_entry_group", "fileset_entry_id"); + + String output = bout.toString(); + + String entryGroupTemplate = + "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; + assertThat(output, CoreMatchers.containsString( + String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID))); + + String entryTemplate = + "Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s"; + assertThat(output, CoreMatchers.containsString( + String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); + } +} \ No newline at end of file From 3842dd657bf675a612d6dae3215e90b1344c84d2 Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Tue, 22 Oct 2019 18:04:48 -0300 Subject: [PATCH 02/10] Change second column name --- .../main/java/com/example/datacatalog/CreateFilesetEntry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 4dde5630543..a4526bb065c 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -109,7 +109,7 @@ public static void createEntry(String projectId, String entryGroupId, String ent .setType("STRING") .setDescription("This columns consists of ....").build()) .addColumns(ColumnSchema.newBuilder() - .setColumn("first_column") + .setColumn("second_column") .setType("STRING") .setDescription("This columns consists of ....").build()) .build()) From 8b121490459755d10b7b134b37185dc5d1f7c424 Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Wed, 23 Oct 2019 17:09:00 -0300 Subject: [PATCH 03/10] Code review changes --- .../datacatalog/CreateFilesetEntry.java | 124 ++++++++---------- .../datacatalog/CreateFilesetEntryTests.java | 30 +++-- 2 files changed, 69 insertions(+), 85 deletions(-) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index a4526bb065c..7b1a99983f3 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -16,122 +16,104 @@ package com.example.datacatalog; -// [START create_fileset_entry] +// [START datacatalog_create_fileset_entry] +import com.google.api.gax.rpc.AlreadyExistsException; import com.google.cloud.datacatalog.ColumnSchema; import com.google.cloud.datacatalog.CreateEntryGroupRequest; import com.google.cloud.datacatalog.CreateEntryRequest; import com.google.cloud.datacatalog.Entry; import com.google.cloud.datacatalog.EntryGroup; -import com.google.cloud.datacatalog.EntryGroupName; -import com.google.cloud.datacatalog.EntryName; import com.google.cloud.datacatalog.EntryType; import com.google.cloud.datacatalog.GcsFilesetSpec; import com.google.cloud.datacatalog.LocationName; import com.google.cloud.datacatalog.Schema; - import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; +import java.io.IOException; + public class CreateFilesetEntry { /** - * Create Fileset Entry + * Create Fileset Entry. * - * @param projectId The project ID to which the fileset belongs, e.g. 'my-project' - * @param entryGroupId The Entry Group ID to which the fileset belongs, - * e.g. 'fileset_entry_group' - * @param entryId The Entry ID for the fileset, e.g. 'fileset_entry_id' + * @param projectId e.g. 'my-project' + * @param entryGroupId e.g. 'fileset_entry_group' + * @param entryId e.g. 'fileset_entry_id' */ public static void createEntry(String projectId, String entryGroupId, String entryId) { - - // ------------------------------- // Currently, Data Catalog stores metadata in the // us-central1 region. - // ------------------------------- String location = "us-central1"; // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { - - // ------------------------------- - // 1. Environment cleanup: delete pre-existing data. - // ------------------------------- - // Delete any pre-existing Entry with the same name - // that will be used in step 3. - try { - dataCatalogClient.deleteEntry( - EntryName.of(projectId, location, entryGroupId, entryId).toString()); - } catch (Exception e) { - System.out.println("Entry does not exist."); - } - - // Delete any pre-existing Entry Group with the same name - // that will be used in step 2. - try { - dataCatalogClient.deleteEntryGroup( - EntryGroupName.of(projectId, location, entryGroupId).toString()); - } catch (Exception e) { - System.out.println("Entry Group does not exist."); - } - - // ------------------------------- - // 2. Create an Entry Group. + // 1. Create an Entry Group. // ------------------------------- // Construct the EntryGroup for the EntryGroup request. EntryGroup entryGroup = EntryGroup.newBuilder().build(); // Construct the EntryGroup request to be sent by the client. - CreateEntryGroupRequest entryGroupRequest = CreateEntryGroupRequest.newBuilder() - .setParent(LocationName.of(projectId, location).toString()) - .setEntryGroupId(entryGroupId) - .setEntryGroup(entryGroup) - .build(); + CreateEntryGroupRequest entryGroupRequest = + CreateEntryGroupRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setEntryGroupId(entryGroupId) + .setEntryGroup(entryGroup) + .build(); // Use the client to send the API request. EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest); - System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName()); - // ------------------------------- - // 3. Create a Fileset Entry. + // 2. Create a Fileset Entry. // ------------------------------- // Construct the Entry for the Entry request. - Entry entry = Entry.newBuilder() - .setDisplayName("My Fileset") - .setDescription("This fileset consists of ....") - .setGcsFilesetSpec(GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*") - .build()) - .setSchema(Schema.newBuilder() - .addColumns(ColumnSchema.newBuilder() - .setColumn("first_column") - .setType("STRING") - .setDescription("This columns consists of ....").build()) - .addColumns(ColumnSchema.newBuilder() - .setColumn("second_column") - .setType("STRING") - .setDescription("This columns consists of ....").build()) - .build()) - .setType(EntryType.FILESET) - .build(); + Entry entry = + Entry.newBuilder() + .setDisplayName("My Fileset") + .setDescription("This fileset consists of ....") + .setGcsFilesetSpec( + GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*") + .build()) + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setType("STRING") + .setDescription( + "This columns consists of ....") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setType("STRING") + .setDescription( + "This columns consists of ....") + .build()) + .build()) + .setType(EntryType.FILESET) + .build(); // Construct the Entry request to be sent by the client. - CreateEntryRequest entryRequest = CreateEntryRequest.newBuilder() - .setParent(entryGroupResponse.getName()) - .setEntryId(entryId) - .setEntry(entry) - .build(); + CreateEntryRequest entryRequest = + CreateEntryRequest.newBuilder() + .setParent(entryGroupResponse.getName()) + .setEntryId(entryId) + .setEntry(entry) + .build(); // Use the client to send the API request. Entry entryResponse = dataCatalogClient.createEntry(entryRequest); - System.out.printf("\nEntry created with name: %s\n", entryResponse.getName()); - - - } catch (Exception e) { + } catch (AlreadyExistsException | IOException e) { + // AlreadyExistsException's are thrown if EntryGroup or Entry already exists. + // IOException's are thrown when unable to create the DataCatalogClient, + // for example an invalid Service Account path. System.out.println("Error in create entry process:\n" + e.toString()); } } } -// [END create_fileset_entry] \ No newline at end of file +// [END datacatalog_create_fileset_entry] \ No newline at end of file diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java index 0cce34ea10e..ec991b686bc 100644 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java @@ -21,10 +21,9 @@ import com.google.cloud.datacatalog.EntryGroupName; import com.google.cloud.datacatalog.EntryName; import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; - import java.io.ByteArrayOutputStream; import java.io.PrintStream; - +import java.util.UUID; import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Before; @@ -32,19 +31,19 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** - * Integration (system) tests for {@link CreateFilesetEntry}. - */ +/** Integration (system) tests for {@link CreateFilesetEntry}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class CreateFilesetEntryTests { private ByteArrayOutputStream bout; - private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String ENTRY_GROUP_ID = "fileset_entry_group_" + + UUID.randomUUID().toString().substring(0, 8); + private static String ENTRY_ID = "fileset_entry_id_" + + UUID.randomUUID().toString().substring(0, 8); private static String LOCATION = "us-central1"; - private static String ENTRY_GROUP_ID = "fileset_entry_group"; - private static String ENTRY_ID = "fileset_entry_id"; + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); @Before public void setUp() { @@ -65,23 +64,26 @@ public void tearDown() { } catch (Exception e) { System.out.println("Error in cleaning up test data:\n" + e.toString()); } - } @Test public void testCreateFilesetEntry() { - CreateFilesetEntry.createEntry(PROJECT_ID, "fileset_entry_group", "fileset_entry_id"); + CreateFilesetEntry.createEntry(PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID); String output = bout.toString(); String entryGroupTemplate = "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; - assertThat(output, CoreMatchers.containsString( - String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID))); + assertThat( + output, + CoreMatchers.containsString(String.format(entryGroupTemplate, PROJECT_ID, + ENTRY_GROUP_ID))); String entryTemplate = "Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s"; - assertThat(output, CoreMatchers.containsString( - String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); + assertThat( + output, + CoreMatchers.containsString( + String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); } } \ No newline at end of file From daf17bdaffa37626dad54cba9bd73c17e8f4d1bd Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Wed, 23 Oct 2019 18:04:21 -0300 Subject: [PATCH 04/10] Change region tag for the correct one --- .../main/java/com/example/datacatalog/CreateFilesetEntry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 7b1a99983f3..3fb436ed388 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -16,7 +16,7 @@ package com.example.datacatalog; -// [START datacatalog_create_fileset_entry] +// [START datacatalog_create_fileset_tag] import com.google.api.gax.rpc.AlreadyExistsException; import com.google.cloud.datacatalog.ColumnSchema; @@ -116,4 +116,4 @@ public static void createEntry(String projectId, String entryGroupId, String ent } } } -// [END datacatalog_create_fileset_entry] \ No newline at end of file +// [END datacatalog_create_fileset_tag] \ No newline at end of file From 6f7d7099363a5291d15d2762e3bd3f733fd6b6d9 Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Wed, 23 Oct 2019 18:43:41 -0300 Subject: [PATCH 05/10] Removed tag from region tag since this sample does not tag the entry --- .../main/java/com/example/datacatalog/CreateFilesetEntry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 3fb436ed388..85d94c634ff 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -16,7 +16,7 @@ package com.example.datacatalog; -// [START datacatalog_create_fileset_tag] +// [START datacatalog_create_fileset] import com.google.api.gax.rpc.AlreadyExistsException; import com.google.cloud.datacatalog.ColumnSchema; @@ -116,4 +116,4 @@ public static void createEntry(String projectId, String entryGroupId, String ent } } } -// [END datacatalog_create_fileset_tag] \ No newline at end of file +// [END datacatalog_create_fileset] \ No newline at end of file From 804612d6ccde86a57f37f65cc40ae6fe3b08bb2e Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Wed, 23 Oct 2019 18:57:52 -0300 Subject: [PATCH 06/10] ADD method overload to show parameter values examples --- .../example/datacatalog/CreateFilesetEntry.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 85d94c634ff..6504101a423 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -34,13 +34,15 @@ public class CreateFilesetEntry { - /** - * Create Fileset Entry. - * - * @param projectId e.g. 'my-project' - * @param entryGroupId e.g. 'fileset_entry_group' - * @param entryId e.g. 'fileset_entry_id' - */ + public static void createEntry() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String entryGroupId = "fileset_entry_group"; + String entryId = "fileset_entry_id"; + createEntry(projectId, entryGroupId, entryId); + } + + // Create Fileset Entry. public static void createEntry(String projectId, String entryGroupId, String entryId) { // Currently, Data Catalog stores metadata in the // us-central1 region. From 6cb3466bdcacb1d9370c11358a6958d0d7d1aaef Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Thu, 24 Oct 2019 16:36:29 -0300 Subject: [PATCH 07/10] Code review changes --- .../example/datacatalog/CreateEntryGroup.java | 70 +++++++++++ .../datacatalog/CreateFilesetEntry.java | 113 ++++++++---------- .../datacatalog/CreateEntryGroupTests.java | 76 ++++++++++++ .../datacatalog/CreateFilesetEntryTests.java | 28 ++--- 4 files changed, 209 insertions(+), 78 deletions(-) create mode 100644 datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java create mode 100644 datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java new file mode 100644 index 00000000000..701f438e7bc --- /dev/null +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java @@ -0,0 +1,70 @@ +/* + * Copyright 2019 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START datacatalog_create_entry_group_tag] + +import com.google.api.gax.rpc.AlreadyExistsException; +import com.google.cloud.datacatalog.*; +import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; +import java.io.IOException; + +public class CreateEntryGroup { + + public static void createEntryGroup() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String entryGroupId = "fileset_entry_group"; + createEntryGroup(projectId, entryGroupId); + } + + // Create Entry Group. + public static void createEntryGroup(String projectId, String entryGroupId) { + // Currently, Data Catalog stores metadata in the us-central1 region. + String location = "us-central1"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + // Construct the EntryGroup for the EntryGroup request. + EntryGroup entryGroup = + EntryGroup.newBuilder() + .setDisplayName("My Fileset Entry Group") + .setDescription("This Entry Group consists of ....") + .build(); + + // Construct the EntryGroup request to be sent by the client. + CreateEntryGroupRequest entryGroupRequest = + CreateEntryGroupRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setEntryGroupId(entryGroupId) + .setEntryGroup(entryGroup) + .build(); + + // Use the client to send the API request. + EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest); + System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName()); + } catch (AlreadyExistsException | IOException e) { + // AlreadyExistsException's are thrown if EntryGroup or Entry already exists. + // IOException's are thrown when unable to create the DataCatalogClient, + // for example an invalid Service Account path. + System.out.println("Error in create entry process:\n" + e.toString()); + } + } +} +// [END datacatalog_create_entry_group_tag] \ No newline at end of file diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 6504101a423..dc301507cfc 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -16,20 +16,11 @@ package com.example.datacatalog; -// [START datacatalog_create_fileset] +// [START datacatalog_create_fileset_tag] import com.google.api.gax.rpc.AlreadyExistsException; -import com.google.cloud.datacatalog.ColumnSchema; -import com.google.cloud.datacatalog.CreateEntryGroupRequest; -import com.google.cloud.datacatalog.CreateEntryRequest; -import com.google.cloud.datacatalog.Entry; -import com.google.cloud.datacatalog.EntryGroup; -import com.google.cloud.datacatalog.EntryType; -import com.google.cloud.datacatalog.GcsFilesetSpec; -import com.google.cloud.datacatalog.LocationName; -import com.google.cloud.datacatalog.Schema; +import com.google.cloud.datacatalog.*; import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; - import java.io.IOException; public class CreateFilesetEntry { @@ -44,68 +35,68 @@ public static void createEntry() { // Create Fileset Entry. public static void createEntry(String projectId, String entryGroupId, String entryId) { - // Currently, Data Catalog stores metadata in the - // us-central1 region. + // Currently, Data Catalog stores metadata in the us-central1 region. String location = "us-central1"; // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { - // 1. Create an Entry Group. - // ------------------------------- - // Construct the EntryGroup for the EntryGroup request. - EntryGroup entryGroup = EntryGroup.newBuilder().build(); - - // Construct the EntryGroup request to be sent by the client. - CreateEntryGroupRequest entryGroupRequest = - CreateEntryGroupRequest.newBuilder() - .setParent(LocationName.of(projectId, location).toString()) - .setEntryGroupId(entryGroupId) - .setEntryGroup(entryGroup) - .build(); - - // Use the client to send the API request. - EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest); - System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName()); - - // 2. Create a Fileset Entry. - // ------------------------------- // Construct the Entry for the Entry request. Entry entry = - Entry.newBuilder() - .setDisplayName("My Fileset") - .setDescription("This fileset consists of ....") - .setGcsFilesetSpec( - GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*") + Entry.newBuilder() + .setDisplayName("My Fileset") + .setDescription("This fileset consists of ....") + .setGcsFilesetSpec( + GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*").build()) + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_name") + .setDescription("First name") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("last_name") + .setDescription("Last name") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("addresses") + .setDescription("Addresses") + .setMode("REPEATED") + .setType("RECORD") + .addSubcolumns( + ColumnSchema.newBuilder() + .setColumn("city") + .setDescription("City") + .setMode("NULLABLE") + .setType("STRING") .build()) - .setSchema( - Schema.newBuilder() - .addColumns( - ColumnSchema.newBuilder() - .setColumn("first_column") - .setType("STRING") - .setDescription( - "This columns consists of ....") - .build()) - .addColumns( - ColumnSchema.newBuilder() - .setColumn("second_column") - .setType("STRING") - .setDescription( - "This columns consists of ....") - .build()) + .addSubcolumns( + ColumnSchema.newBuilder() + .setColumn("state") + .setDescription("State") + .setMode("NULLABLE") + .setType("STRING") .build()) - .setType(EntryType.FILESET) - .build(); + .build()) + .build()) + .setType(EntryType.FILESET) + .build(); // Construct the Entry request to be sent by the client. CreateEntryRequest entryRequest = - CreateEntryRequest.newBuilder() - .setParent(entryGroupResponse.getName()) - .setEntryId(entryId) - .setEntry(entry) - .build(); + CreateEntryRequest.newBuilder() + .setParent(EntryGroupName.of(projectId, location, entryGroupId).toString()) + .setEntryId(entryId) + .setEntry(entry) + .build(); // Use the client to send the API request. Entry entryResponse = dataCatalogClient.createEntry(entryRequest); @@ -118,4 +109,4 @@ public static void createEntry(String projectId, String entryGroupId, String ent } } } -// [END datacatalog_create_fileset] \ No newline at end of file +// [END datacatalog_create_fileset_tag] \ No newline at end of file diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java new file mode 100644 index 00000000000..4e5c36b880f --- /dev/null +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datacatalog.EntryGroupName; +import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link CreateEntryGroup}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateEntryGroupTests { + + private ByteArrayOutputStream bout; + + private static String ENTRY_GROUP_ID = + "fileset_entry_group_" + UUID.randomUUID().toString().substring(0, 8); + private static String LOCATION = "us-central1"; + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + dataCatalogClient.deleteEntryGroup( + EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); + } catch (Exception e) { + System.out.println("Error in cleaning up test data:\n" + e.toString()); + } + } + + @Test + public void testCreateEntryGroup() { + CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID); + + String output = bout.toString(); + + String entryGroupTemplate = + "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; + assertThat( + output, + CoreMatchers.containsString(String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID))); + } +} \ No newline at end of file diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java index ec991b686bc..1e638969c5b 100644 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java @@ -38,10 +38,10 @@ public class CreateFilesetEntryTests { private ByteArrayOutputStream bout; - private static String ENTRY_GROUP_ID = "fileset_entry_group_" - + UUID.randomUUID().toString().substring(0, 8); - private static String ENTRY_ID = "fileset_entry_id_" - + UUID.randomUUID().toString().substring(0, 8); + private static String ENTRY_GROUP_ID = + "fileset_entry_group_" + UUID.randomUUID().toString().substring(0, 8); + private static String ENTRY_ID = + "fileset_entry_id_" + UUID.randomUUID().toString().substring(0, 8); private static String LOCATION = "us-central1"; private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); @@ -49,6 +49,7 @@ public class CreateFilesetEntryTests { public void setUp() { bout = new ByteArrayOutputStream(); System.setOut(new PrintStream(bout)); + CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID); } @After @@ -58,9 +59,9 @@ public void tearDown() { try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { dataCatalogClient.deleteEntry( - EntryName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID, ENTRY_ID).toString()); + EntryName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID, ENTRY_ID).toString()); dataCatalogClient.deleteEntryGroup( - EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); + EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); } catch (Exception e) { System.out.println("Error in cleaning up test data:\n" + e.toString()); } @@ -72,18 +73,11 @@ public void testCreateFilesetEntry() { String output = bout.toString(); - String entryGroupTemplate = - "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; - assertThat( - output, - CoreMatchers.containsString(String.format(entryGroupTemplate, PROJECT_ID, - ENTRY_GROUP_ID))); - String entryTemplate = - "Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s"; + "Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s"; assertThat( - output, - CoreMatchers.containsString( - String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); + output, + CoreMatchers.containsString( + String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); } } \ No newline at end of file From 1cb15cedcec7db43537afef4c04355ee252b306d Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Fri, 25 Oct 2019 15:23:48 -0300 Subject: [PATCH 08/10] Code review changes --- .../datacatalog/CreateEntryGroupTests.java | 76 ------------------- .../datacatalog/CreateFilesetEntryTests.java | 1 - 2 files changed, 77 deletions(-) delete mode 100644 datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java deleted file mode 100644 index 4e5c36b880f..00000000000 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryGroupTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.datacatalog; - -import static org.junit.Assert.assertThat; - -import com.google.cloud.datacatalog.EntryGroupName; -import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link CreateEntryGroup}. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class CreateEntryGroupTests { - - private ByteArrayOutputStream bout; - - private static String ENTRY_GROUP_ID = - "fileset_entry_group_" + UUID.randomUUID().toString().substring(0, 8); - private static String LOCATION = "us-central1"; - private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); - - try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { - dataCatalogClient.deleteEntryGroup( - EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); - } catch (Exception e) { - System.out.println("Error in cleaning up test data:\n" + e.toString()); - } - } - - @Test - public void testCreateEntryGroup() { - CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID); - - String output = bout.toString(); - - String entryGroupTemplate = - "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; - assertThat( - output, - CoreMatchers.containsString(String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID))); - } -} \ No newline at end of file diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java index 1e638969c5b..835f5d96b4c 100644 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java @@ -33,7 +33,6 @@ /** Integration (system) tests for {@link CreateFilesetEntry}. */ @RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") public class CreateFilesetEntryTests { private ByteArrayOutputStream bout; From 8df1a03a113843398bbe06ada0818e0d9627e8ab Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Fri, 25 Oct 2019 18:45:31 -0300 Subject: [PATCH 09/10] Code review and checkstyle changes --- .../example/datacatalog/CreateEntryGroup.java | 4 +- .../datacatalog/CreateFilesetEntry.java | 8 +++- ...tEntryTests.java => CreateEntryTests.java} | 42 +++++++++++++++---- 3 files changed, 43 insertions(+), 11 deletions(-) rename datacatalog/cloud-client/src/test/java/com/example/datacatalog/{CreateFilesetEntryTests.java => CreateEntryTests.java} (61%) diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java index 701f438e7bc..4e1e4e2519b 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java @@ -19,7 +19,9 @@ // [START datacatalog_create_entry_group_tag] import com.google.api.gax.rpc.AlreadyExistsException; -import com.google.cloud.datacatalog.*; +import com.google.cloud.datacatalog.CreateEntryGroupRequest; +import com.google.cloud.datacatalog.EntryGroup; +import com.google.cloud.datacatalog.LocationName; import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; import java.io.IOException; diff --git a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index dc301507cfc..b8c59214005 100644 --- a/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -19,7 +19,13 @@ // [START datacatalog_create_fileset_tag] import com.google.api.gax.rpc.AlreadyExistsException; -import com.google.cloud.datacatalog.*; +import com.google.cloud.datacatalog.ColumnSchema; +import com.google.cloud.datacatalog.CreateEntryRequest; +import com.google.cloud.datacatalog.Entry; +import com.google.cloud.datacatalog.EntryGroupName; +import com.google.cloud.datacatalog.EntryType; +import com.google.cloud.datacatalog.GcsFilesetSpec; +import com.google.cloud.datacatalog.Schema; import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; import java.io.IOException; diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java similarity index 61% rename from datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java rename to datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java index 835f5d96b4c..6e78ef3cc45 100644 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateFilesetEntryTests.java +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java @@ -26,19 +26,22 @@ import java.util.UUID; import org.hamcrest.CoreMatchers; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Integration (system) tests for {@link CreateFilesetEntry}. */ +/** Integration (system) tests for {@link CreateFilesetEntry} and {@link CreateEntryGroup}. */ @RunWith(JUnit4.class) -public class CreateFilesetEntryTests { +public class CreateEntryTests { private ByteArrayOutputStream bout; - private static String ENTRY_GROUP_ID = - "fileset_entry_group_" + UUID.randomUUID().toString().substring(0, 8); + private static String ENTRY_GROUP_ID_NO_CHILDREN = + "entry_group_no_children_" + UUID.randomUUID().toString().substring(0, 8); + private static String PARENT_ENTRY_GROUP_ID = + "fileset_entry_group_parent_" + UUID.randomUUID().toString().substring(0, 8); private static String ENTRY_ID = "fileset_entry_id_" + UUID.randomUUID().toString().substring(0, 8); private static String LOCATION = "us-central1"; @@ -48,19 +51,24 @@ public class CreateFilesetEntryTests { public void setUp() { bout = new ByteArrayOutputStream(); System.setOut(new PrintStream(bout)); - CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID); } @After public void tearDown() { System.setOut(null); bout.reset(); + } + @AfterClass + public static void tearDownClass() { try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { dataCatalogClient.deleteEntry( - EntryName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID, ENTRY_ID).toString()); + EntryName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID, ENTRY_ID).toString()); + dataCatalogClient.deleteEntryGroup( + EntryGroupName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID).toString()); + dataCatalogClient.deleteEntryGroup( - EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID).toString()); + EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString()); } catch (Exception e) { System.out.println("Error in cleaning up test data:\n" + e.toString()); } @@ -68,7 +76,9 @@ public void tearDown() { @Test public void testCreateFilesetEntry() { - CreateFilesetEntry.createEntry(PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID); + // Must create a Entry Group before creating the entry. + CreateEntryGroup.createEntryGroup(PROJECT_ID, PARENT_ENTRY_GROUP_ID); + CreateFilesetEntry.createEntry(PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID); String output = bout.toString(); @@ -77,6 +87,20 @@ public void testCreateFilesetEntry() { assertThat( output, CoreMatchers.containsString( - String.format(entryTemplate, PROJECT_ID, ENTRY_GROUP_ID, ENTRY_ID))); + String.format(entryTemplate, PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID))); + } + + @Test + public void testCreateEntryGroup() { + CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN); + + String output = bout.toString(); + + String entryGroupTemplate = + "Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; + assertThat( + output, + CoreMatchers.containsString( + String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN))); } } \ No newline at end of file From 0b12da2575ac0f4ea073ee1fc2e5bbc473ba8b2a Mon Sep 17 00:00:00 2001 From: mesmacosta Date: Fri, 25 Oct 2019 18:48:37 -0300 Subject: [PATCH 10/10] Clean up in a safer order --- .../test/java/com/example/datacatalog/CreateEntryTests.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java index 6e78ef3cc45..2651606c89c 100644 --- a/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java +++ b/datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java @@ -62,13 +62,13 @@ public void tearDown() { @AfterClass public static void tearDownClass() { try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + dataCatalogClient.deleteEntryGroup( + EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString()); + dataCatalogClient.deleteEntry( EntryName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID, ENTRY_ID).toString()); dataCatalogClient.deleteEntryGroup( EntryGroupName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID).toString()); - - dataCatalogClient.deleteEntryGroup( - EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString()); } catch (Exception e) { System.out.println("Error in cleaning up test data:\n" + e.toString()); }