diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index 9c461408..12b027f3 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -33,6 +33,11 @@ + + com.google.protobuf + protobuf-java-util + 3.14.0 + junit junit diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 9feda839..2e0726b4 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -31,6 +31,11 @@ 1.4.1 + + com.google.protobuf + protobuf-java-util + 3.14.0 + junit junit diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 85baff7a..110949c3 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -44,6 +44,11 @@ + + com.google.protobuf + protobuf-java-util + 3.14.0 + junit junit diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateEntry.java new file mode 100644 index 00000000..2cbc7e40 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateEntry.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_create_entry] +import com.google.cloud.datacatalog.v1.CreateEntryRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to create an entry +public class CreateEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + Entry entry = Entry.newBuilder().build(); + createEntry(entryGroupName, entryId, entry); + } + + public static void createEntry(EntryGroupName entryGroupName, String entryId, Entry entry) + throws IOException { + // 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 client = DataCatalogClient.create()) { + CreateEntryRequest request = + CreateEntryRequest.newBuilder() + .setParent(entryGroupName.toString()) + .setEntryId(entryId) + .setEntry(entry) + .build(); + client.createEntry(request); + System.out.println("Entry created successfully"); + } + } +} +// [END data_catalog_create_entry] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java index db833573..64133657 100644 --- a/samples/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google Inc. + * Copyright 2020 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,41 +16,37 @@ package com.example.datacatalog; -// [START datacatalog_create_entry_group_tag] - -import com.google.api.gax.rpc.AlreadyExistsException; +// [START data_catalog_create_entry_group] import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest; import com.google.cloud.datacatalog.v1.DataCatalogClient; import com.google.cloud.datacatalog.v1.EntryGroup; import com.google.cloud.datacatalog.v1.LocationName; import java.io.IOException; +// Sample to create an entry group public class CreateEntryGroup { - public static void createEntryGroup() { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String entryGroupId = "fileset_entry_group"; - createEntryGroup(projectId, entryGroupId); + String projectId = "MY_PROJECT_ID"; + String location = "us-central1"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + createEntryGroup(projectId, location, 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"; - + public static void createEntryGroup(String projectId, String location, String entryGroupId) + throws IOException { // 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") + .setDisplayName("MY 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()) @@ -58,15 +54,9 @@ public static void createEntryGroup(String projectId, String 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()); + dataCatalogClient.createEntryGroup(entryGroupRequest); + System.out.println("Entry Group created successfully"); } } } -// [END datacatalog_create_entry_group_tag] \ No newline at end of file +// [END data_catalog_create_entry_group] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index c1ca167a..579c18bd 100644 --- a/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -115,4 +115,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_tag] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java new file mode 100644 index 00000000..8528185c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java @@ -0,0 +1,68 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_create_tag_template] +import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import java.io.IOException; + +// Sample to create tag template +public class CreateTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + LocationName locationName = LocationName.of(projectId, location); + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + public static void createTagTemplate( + LocationName name, String tagTemplateId, TagTemplate template) throws IOException { + // 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 client = DataCatalogClient.create()) { + CreateTagTemplateRequest request = + CreateTagTemplateRequest.newBuilder() + .setParent(name.toString()) + .setTagTemplateId(tagTemplateId) + .setTagTemplate(template) + .build(); + client.createTagTemplate(request); + System.out.println("Tag template created successfully"); + } + } +} +// [END data_catalog_create_tag_template] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java new file mode 100644 index 00000000..8658f7f9 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_delete_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryRequest; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.IOException; + +// Sample to delete a entry +public class DeleteEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + deleteEntry(entryName); + } + + public static void deleteEntry(EntryName entryName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteEntryRequest request = + DeleteEntryRequest.newBuilder().setName(entryName.toString()).build(); + client.deleteEntry(request); + System.out.println("Entry deleted successfully"); + } + } +} +// [END data_catalog_delete_entry] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java b/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java new file mode 100644 index 00000000..07460d3f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 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 data_catalog_delete_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to delete a entry group +public class DeleteEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + deleteEntryGroup(entryGroupName); + } + + public static void deleteEntryGroup(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteEntryGroupRequest request = + DeleteEntryGroupRequest.newBuilder().setName(entryGroupName.toString()).build(); + client.deleteEntryGroup(request); + System.out.println("Entry group deleted successfully"); + } + } +} +// [END data_catalog_delete_entry_group] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java b/samples/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java new file mode 100644 index 00000000..fc1a2a04 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_delete_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.IOException; + +// Sample to delete tag template +public class DeleteTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + deleteTagTemplate(tagTemplate); + } + + public static void deleteTagTemplate(TagTemplateName template) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteTagTemplateRequest request = + DeleteTagTemplateRequest.newBuilder().setName(template.toString()).setForce(true).build(); + client.deleteTagTemplate(request); + System.out.println("Tag template deleted successfully"); + } + } +} +// [END data_catalog_delete_tag_template] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/GetEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/GetEntry.java new file mode 100644 index 00000000..572af94e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/GetEntry.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_get_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.IOException; + +// Sample to get an entity +public class GetEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + getEntry(entryName); + } + + public static void getEntry(EntryName entryName) throws IOException { + // 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 client = DataCatalogClient.create()) { + Entry entry = client.getEntry(entryName); + System.out.println("Entry retrieved successfully: " + entry.getName()); + } + } +} +// [END data_catalog_get_entry] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java b/samples/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java new file mode 100644 index 00000000..ee73efff --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_get_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to get an entity group +public class GetEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + getEntryGroup(entryGroupName); + } + + public static void getEntryGroup(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + EntryGroup entryGroup = client.getEntryGroup(entryGroupName); + System.out.println("Entry group retrieved successfully: " + entryGroup.getName()); + } + } +} +// [END data_catalog_get_entry_group] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java b/samples/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java new file mode 100644 index 00000000..8d7e76ca --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_get_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.GetTagTemplateRequest; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.IOException; + +// Sample to get tag template +public class GetTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + getTagTemplate(tagTemplate); + } + + public static void getTagTemplate(TagTemplateName template) throws IOException { + // 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 client = DataCatalogClient.create()) { + GetTagTemplateRequest request = + GetTagTemplateRequest.newBuilder().setName(template.toString()).build(); + TagTemplate tagTemplate = client.getTagTemplate(request); + System.out.println("Tag template retrieved successfully :" + tagTemplate.getName()); + } + } +} +// [END data_catalog_get_tag_template] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/ListEntries.java b/samples/snippets/src/main/java/com/example/datacatalog/ListEntries.java new file mode 100644 index 00000000..c635e591 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/ListEntries.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_list_entries] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to get list of entries +public class ListEntries { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + listEntries(entryGroupName); + } + + public static void listEntries(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DataCatalogClient.ListEntriesPagedResponse listEntries = client.listEntries(entryGroupName); + listEntries + .iterateAll() + .forEach( + entry -> { + System.out.println("Entry name : " + entry.getName()); + }); + } + } +} +// [END data_catalog_list_entries] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java b/samples/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java new file mode 100644 index 00000000..aa6194e5 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java @@ -0,0 +1,55 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_list_entry_groups] + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.ListEntryGroupsRequest; +import com.google.cloud.datacatalog.v1.LocationName; +import java.io.IOException; + +// Sample to get list of entry group +public class ListEntryGroups { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + LocationName name = LocationName.of(projectId, location); + listEntryGroups(name); + } + + public static void listEntryGroups(LocationName name) throws IOException { + // 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 client = DataCatalogClient.create()) { + ListEntryGroupsRequest request = + ListEntryGroupsRequest.newBuilder().setParent(name.toString()).build(); + DataCatalogClient.ListEntryGroupsPagedResponse listEntryGroups = + client.listEntryGroups(request); + listEntryGroups + .iterateAll() + .forEach( + entryGroup -> { + System.out.println("Entry group name : " + entryGroup.getName()); + }); + } + } +} +// [END data_catalog_list_entry_groups] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java new file mode 100644 index 00000000..ea965215 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_update_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.UpdateEntryRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update an entity +public class UpdateEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + String description = "MY_DESCRIPTION"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + Entry entry = + Entry.newBuilder().setName(entryName.toString()).setDescription(description).build(); + updateEntry(entry); + } + + public static void updateEntry(Entry entry) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("description"); + UpdateEntryRequest request = + UpdateEntryRequest.newBuilder().setEntry(entry).setUpdateMask(fieldMask).build(); + Entry entryUpdate = client.updateEntry(request); + System.out.println("Entry updated successfully : " + entryUpdate.getDescription()); + } + } +} +// [END data_catalog_update_entry] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java b/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java new file mode 100644 index 00000000..5c8bf3f3 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_update_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.UpdateEntryGroupRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update an entity group +public class UpdateEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String description = "MY_DESCRIPTION"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + EntryGroup entryGroup = + EntryGroup.newBuilder() + .setName(entryGroupName.toString()) + .setDescription(description) + .build(); + updateEntryGroup(entryGroup); + } + + public static void updateEntryGroup(EntryGroup entryGroup) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("description"); + UpdateEntryGroupRequest request = + UpdateEntryGroupRequest.newBuilder() + .setEntryGroup(entryGroup) + .setUpdateMask(fieldMask) + .build(); + EntryGroup entryGroupUpdate = client.updateEntryGroup(request); + System.out.println("Entry group updated successfully : " + entryGroupUpdate.getDescription()); + } + } +} +// [END data_catalog_update_entry_group] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java b/samples/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java new file mode 100644 index 00000000..ebef7ea1 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 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; + +// [START data_catalog_update_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import com.google.cloud.datacatalog.v1.UpdateTagTemplateRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update tag template +public class UpdateTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + String displayName = "MY_DISPLAY_NAME"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + TagTemplate template = + TagTemplate.newBuilder() + .setName(tagTemplate.toString()) + .setDisplayName(displayName) + .build(); + updateTagTemplate(template); + } + + public static void updateTagTemplate(TagTemplate template) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("display_name"); + UpdateTagTemplateRequest request = + UpdateTagTemplateRequest.newBuilder() + .setTagTemplate(template) + .setUpdateMask(fieldMask) + .build(); + TagTemplate tagTemplateUpdate = client.updateTagTemplate(request); + System.out.println( + "Tag template updated successfully : " + tagTemplateUpdate.getDisplayName()); + } + } +} +// [END data_catalog_update_tag_template] diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java new file mode 100644 index 00000000..2b55edeb --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateEntryGroupIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + entryGroup = "CREATE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateEntryGroup() throws IOException { + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + assertThat(bout.toString()).contains("Entry Group created"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java new file mode 100644 index 00000000..9260584e --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java @@ -0,0 +1,119 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryId; + private String entryGroupId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryId = "CREATE_ENTRY_TEST_" + ID; + entryGroupId = "CREATE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateEntry() throws IOException { + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + assertThat(bout.toString()).contains("Entry created successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java index dd8555ef..b09cca57 100644 --- a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java @@ -23,6 +23,7 @@ import com.google.cloud.datacatalog.v1.EntryGroupName; import com.google.cloud.datacatalog.v1.EntryName; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; @@ -80,12 +81,12 @@ public static void tearDownClass() { } @Test - public void testCreateFilesetEntry() { + public void testCreateFilesetEntry() throws IOException { String entryGroupId = "fileset_entry_group_parent_" + getUuid8Chars(); String entryId = "fileset_entry_id_" + getUuid8Chars(); // Must create a Entry Group before creating the entry. - CreateEntryGroup.createEntryGroup(PROJECT_ID, entryGroupId); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); CreateFilesetEntry.createEntry(PROJECT_ID, entryGroupId, entryId); // Store names for clean up on teardown @@ -104,10 +105,10 @@ public void testCreateFilesetEntry() { } @Test - public void testCreateEntryGroup() { + public void testCreateEntryGroup() throws IOException { String entryGroupId = "entry_group_no_children_" + getUuid8Chars(); - CreateEntryGroup.createEntryGroup(PROJECT_ID, entryGroupId); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); // Store names for clean up on teardown String expectedEntryGroupName = @@ -116,7 +117,7 @@ public void testCreateEntryGroup() { String output = bout.toString(); - String entryGroupTemplate = "Entry Group created with name: %s"; + String entryGroupTemplate = "Entry Group created successfully"; assertThat( output, CoreMatchers.containsString(String.format(entryGroupTemplate, expectedEntryGroupName))); diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java new file mode 100644 index 00000000..037440d1 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 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 com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateTagTemplateIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + tagTemplateId = "create_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateTagTemplate() throws IOException { + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + assertThat(bout.toString()).contains("Tag template created successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java b/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java new file mode 100644 index 00000000..6f5fac2c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteEntryGroupIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "DELETE_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testDeleteEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + assertThat(bout.toString()).contains("Entry group deleted successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java new file mode 100644 index 00000000..124c2989 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java @@ -0,0 +1,120 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryId; + private String entryGroupId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryId = "DELETE_ENTRY_TEST_" + ID; + entryGroupId = "DELETE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a temporary entry group and entry + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testDeleteEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + assertThat(bout.toString()).contains("Entry deleted successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java b/samples/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java new file mode 100644 index 00000000..d979845f --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 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 com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteTagTemplateIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + tagTemplateId = "delete_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testDeleteTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + assertThat(bout.toString()).contains("Tag template deleted successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java b/samples/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java new file mode 100644 index 00000000..0034e00a --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetEntryGroupIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "GET_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + GetEntryGroup.getEntryGroup(name); + assertThat(bout.toString()).contains("Entry group retrieved successfully:"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java new file mode 100644 index 00000000..7693ade9 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java @@ -0,0 +1,121 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryId; + private String entryGroupId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryId = "GET_ENTRY_TEST_" + ID; + entryGroupId = "GET_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + GetEntry.getEntry(entryName); + assertThat(bout.toString()).contains("Entry retrieved successfully:"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java b/samples/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java new file mode 100644 index 00000000..374b896f --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java @@ -0,0 +1,103 @@ +/* + * Copyright 2020 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 com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetTagTemplateIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + tagTemplateId = "get_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @After + public void tearDown() throws IOException { + // clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + GetTagTemplate.getTagTemplate(name); + assertThat(bout.toString()).contains("Tag template retrieved successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java b/samples/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java new file mode 100644 index 00000000..1e85fee2 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java @@ -0,0 +1,121 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListEntriesIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryId; + private String entryGroupId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryId = "LIST_ENTRIES_TEST_" + ID; + entryGroupId = "LIST_ENTRIES_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testListEntries() throws IOException { + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + ListEntries.listEntries(entryGroupName); + assertThat(bout.toString()).contains("Entry name :"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java b/samples/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java new file mode 100644 index 00000000..9426878a --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.LocationName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListEntryGroupsIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "LIST_ENTRY_GROUPS_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testListEntryGroups() throws IOException { + ListEntryGroups.listEntryGroups(LocationName.of(PROJECT_ID, LOCATION)); + assertThat(bout.toString()).contains("Entry group name :"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java b/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java new file mode 100644 index 00000000..aac12d51 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UpdateEntryGroupIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "UPDATE_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + EntryGroup entryGroup = + EntryGroup.newBuilder().setName(name.toString()).setDescription("test-description").build(); + UpdateEntryGroup.updateEntryGroup(entryGroup); + assertThat(bout.toString()).contains("Entry group updated successfully :"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java new file mode 100644 index 00000000..cc289bb2 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java @@ -0,0 +1,123 @@ +/* + * Copyright 2020 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; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UpdateEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryId; + private String entryGroupId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryId = "UPDATE_ENTRY_TEST_" + ID; + entryGroupId = "UPDATE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + Entry entry = + Entry.newBuilder().setName(entryName.toString()).setDescription("test_description").build(); + UpdateEntry.updateEntry(entry); + assertThat(bout.toString()).contains("Entry updated successfully :"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java b/samples/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java new file mode 100644 index 00000000..5f351ac1 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java @@ -0,0 +1,108 @@ +/* + * Copyright 2020 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 com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UpdateTagTemplateIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + tagTemplateId = "update_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @After + public void tearDown() throws IOException { + // clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + TagTemplate template = + TagTemplate.newBuilder() + .setName(name.toString()) + .setDisplayName("test_display_name") + .build(); + UpdateTagTemplate.updateTagTemplate(template); + assertThat(bout.toString()).contains("Tag template updated successfully :"); + } +}