Skip to content

Commit 43764f8

Browse files
mesmacostatswast
authored andcommitted
Add Data Catalog createEntry quickstart samples and tests. (GoogleCloudPlatform#1731)
* ADD sample for create fileset entry quickstart * RAN google java format * CHANGE exception comment * FIX lint issues * Split quickstart into a new folder * Rename files since class name will appear on samples * Ran java formatter * revert region tags change * remove extra space * change to real bucket name
1 parent b21fae0 commit 43764f8

File tree

7 files changed

+462
-27
lines changed

7 files changed

+462
-27
lines changed

datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void createEntry(String projectId, String entryGroupId, String ent
5454
.setDisplayName("My Fileset")
5555
.setDescription("This fileset consists of ....")
5656
.setGcsFilesetSpec(
57-
GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*").build())
57+
GcsFilesetSpec.newBuilder().addFilePatterns("gs://cloud-samples-data/*").build())
5858
.setSchema(
5959
Schema.newBuilder()
6060
.addColumns(

datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package com.example.datacatalog;
1818

1919
import static org.junit.Assert.assertThat;
20+
import static org.junit.Assert.fail;
2021

2122
import com.google.cloud.datacatalog.EntryGroupName;
2223
import com.google.cloud.datacatalog.EntryName;
2324
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
2425
import java.io.ByteArrayOutputStream;
2526
import java.io.PrintStream;
27+
import java.util.ArrayList;
28+
import java.util.List;
2629
import java.util.UUID;
2730
import org.hamcrest.CoreMatchers;
2831
import org.junit.After;
@@ -38,15 +41,12 @@ public class CreateEntryTests {
3841

3942
private ByteArrayOutputStream bout;
4043

41-
private static String ENTRY_GROUP_ID_NO_CHILDREN =
42-
"entry_group_no_children_" + UUID.randomUUID().toString().substring(0, 8);
43-
private static String PARENT_ENTRY_GROUP_ID =
44-
"fileset_entry_group_parent_" + UUID.randomUUID().toString().substring(0, 8);
45-
private static String ENTRY_ID =
46-
"fileset_entry_id_" + UUID.randomUUID().toString().substring(0, 8);
4744
private static String LOCATION = "us-central1";
4845
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
4946

47+
private static List<String> entryGroupsPendingDeletion = new ArrayList<>();
48+
private static List<String> entriesPendingDeletion = new ArrayList<>();
49+
5050
@Before
5151
public void setUp() {
5252
bout = new ByteArrayOutputStream();
@@ -62,45 +62,67 @@ public void tearDown() {
6262
@AfterClass
6363
public static void tearDownClass() {
6464
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
65-
dataCatalogClient.deleteEntryGroup(
66-
EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString());
67-
68-
dataCatalogClient.deleteEntry(
69-
EntryName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID, ENTRY_ID).toString());
70-
dataCatalogClient.deleteEntryGroup(
71-
EntryGroupName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID).toString());
65+
// Must delete Entries before deleting the Entry Group.
66+
if (entriesPendingDeletion.isEmpty() || entryGroupsPendingDeletion.isEmpty()) {
67+
fail("Something went wrong, no entries were generated");
68+
}
69+
70+
for (String entryName : entriesPendingDeletion) {
71+
dataCatalogClient.deleteEntry(entryName);
72+
}
73+
74+
for (String entryGroupName : entryGroupsPendingDeletion) {
75+
dataCatalogClient.deleteEntryGroup(entryGroupName);
76+
}
7277
} catch (Exception e) {
7378
System.out.println("Error in cleaning up test data:\n" + e.toString());
7479
}
7580
}
7681

7782
@Test
7883
public void testCreateFilesetEntry() {
84+
String entryGroupId = "fileset_entry_group_parent_" + getUuid8Chars();
85+
String entryId = "fileset_entry_id_" + getUuid8Chars();
86+
7987
// Must create a Entry Group before creating the entry.
80-
CreateEntryGroup.createEntryGroup(PROJECT_ID, PARENT_ENTRY_GROUP_ID);
81-
CreateFilesetEntry.createEntry(PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID);
88+
CreateEntryGroup.createEntryGroup(PROJECT_ID, entryGroupId);
89+
CreateFilesetEntry.createEntry(PROJECT_ID, entryGroupId, entryId);
90+
91+
// Store names for clean up on teardown
92+
String expectedEntryGroupName =
93+
EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId).toString();
94+
entryGroupsPendingDeletion.add(expectedEntryGroupName);
95+
96+
String expectedEntryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId).toString();
97+
entriesPendingDeletion.add(expectedEntryName);
8298

8399
String output = bout.toString();
84100

85-
String entryTemplate =
86-
"Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s";
101+
String entryTemplate = "Entry created with name: %s";
87102
assertThat(
88-
output,
89-
CoreMatchers.containsString(
90-
String.format(entryTemplate, PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID)));
103+
output, CoreMatchers.containsString(String.format(entryTemplate, expectedEntryName)));
91104
}
92105

93106
@Test
94107
public void testCreateEntryGroup() {
95-
CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN);
108+
String entryGroupId = "entry_group_no_children_" + getUuid8Chars();
109+
110+
CreateEntryGroup.createEntryGroup(PROJECT_ID, entryGroupId);
111+
112+
// Store names for clean up on teardown
113+
String expectedEntryGroupName =
114+
EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId).toString();
115+
entryGroupsPendingDeletion.add(expectedEntryGroupName);
96116

97117
String output = bout.toString();
98118

99-
String entryGroupTemplate =
100-
"Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s";
119+
String entryGroupTemplate = "Entry Group created with name: %s";
101120
assertThat(
102121
output,
103-
CoreMatchers.containsString(
104-
String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN)));
122+
CoreMatchers.containsString(String.format(entryGroupTemplate, expectedEntryGroupName)));
123+
}
124+
125+
private String getUuid8Chars() {
126+
return UUID.randomUUID().toString().substring(0, 8);
105127
}
106-
}
128+
}

datacatalog/quickstart/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Getting Started with Data Catalog and the Google Cloud Client libraries
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=datacatalog/cloud-client/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Data Catalog][datacatalog] is a fully managed and scalable metadata management service that empowers organizations
7+
to quickly discover, manage, and understand all their data in Google Cloud.
8+
This sample Java application demonstrates how to access the Data Catalog API using
9+
the [Google Cloud Client Library for Java][google-cloud-java].
10+
11+
[datacatalog]: https://cloud.google.com/data-catalog/
12+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
13+
14+
## Quickstart
15+
16+
#### Setup
17+
- Install [Maven](http://maven.apache.org/).
18+
- [Enable](https://console.cloud.google.com/apis/api/datacatalog.googleapis.com/overview) Data Catalog API.
19+
- Set up [authentication](https://cloud.google.com/docs/authentication/getting-started).
20+
21+
#### Build
22+
- Build your project with:
23+
```
24+
mvn clean package -DskipTests
25+
```
26+
27+
#### Testing
28+
Run the test with Maven.
29+
```
30+
mvn verify
31+
```

datacatalog/quickstart/build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2019 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
apply plugin: 'java'
16+
17+
repositories {
18+
mavenCentral()
19+
}
20+
21+
dependencies {
22+
compile group: 'com.google.cloud', name: 'google-cloud-datacatalog-quickstart', version:'0.28.0-alpha'
23+
24+
testCompile group: 'com.google.truth', name: 'truth', version:'0.42'
25+
testCompile group: 'junit', name: 'junit', version:'4.13-beta-2'
26+
}
27+
28+
test {
29+
useJUnit()
30+
testLogging.showStandardStreams = true
31+
beforeTest { descriptor ->
32+
logger.lifecycle("test: " + descriptor + " Running")
33+
}
34+
35+
onOutput { descriptor, event ->
36+
logger.lifecycle("test: " + descriptor + ": " + event.message )
37+
}
38+
afterTest { descriptor, result ->
39+
logger.lifecycle("test: " + descriptor + ": " + result )
40+
}
41+
}

datacatalog/quickstart/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!--
2+
Copyright 2019 Google LLC.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.datacatalog</groupId>
20+
<artifactId>datacatalog-google-cloud-quickstart</artifactId>
21+
<packaging>jar</packaging>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.11</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.cloud</groupId>
41+
<artifactId>google-cloud-datacatalog</artifactId>
42+
<version>0.29.0-alpha</version>
43+
</dependency>
44+
45+
<!-- Test dependencies -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.13-beta-3</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>1.0</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>

0 commit comments

Comments
 (0)