Skip to content

Commit 5a184db

Browse files
jacspa96Jacek Spalinski
andauthored
feat(dataplex): adjust code samples for Aspect Type (#9575)
* feat(dataplex): explicitly pass arguments to the functions * feat(dataplex): adjust integration tests to changed functions' signatures * feat(dataplex): remove snake case from test methods' names --------- Co-authored-by: Jacek Spalinski <[email protected]>
1 parent d1cf7a2 commit 5a184db

File tree

6 files changed

+46
-41
lines changed

6 files changed

+46
-41
lines changed

dataplex/snippets/src/main/java/dataplex/CreateAspectType.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public static void main(String[] args) throws Exception {
3232
String location = "MY_LOCATION";
3333
String aspectTypeId = "MY_ASPECT_TYPE_ID";
3434

35-
LocationName locationName = LocationName.of(projectId, location);
3635
AspectType.MetadataTemplate aspectField =
3736
AspectType.MetadataTemplate.newBuilder()
3837
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
@@ -55,15 +54,18 @@ public static void main(String[] args) throws Exception {
5554
.build())
5655
.build();
5756
List<AspectType.MetadataTemplate> aspectFields = List.of(aspectField);
58-
AspectType createdAspectType = createAspectType(locationName, aspectTypeId, aspectFields);
57+
AspectType createdAspectType =
58+
createAspectType(projectId, location, aspectTypeId, aspectFields);
5959
System.out.println("Successfully created aspect type: " + createdAspectType.getName());
6060
}
6161

6262
public static AspectType createAspectType(
63-
LocationName locationName,
63+
String projectId,
64+
String location,
6465
String aspectTypeId,
6566
List<AspectType.MetadataTemplate> aspectFields)
6667
throws Exception {
68+
LocationName locationName = LocationName.of(projectId, location);
6769
AspectType aspectType =
6870
AspectType.newBuilder()
6971
.setDescription("description of the aspect type")

dataplex/snippets/src/main/java/dataplex/DeleteAspectType.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public static void main(String[] args) throws Exception {
3030
String location = "MY_LOCATION";
3131
String aspectTypeId = "MY_ASPECT_TYPE_ID";
3232

33-
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
34-
deleteAspectType(aspectTypeName);
33+
deleteAspectType(projectId, location, aspectTypeId);
3534
System.out.println("Successfully deleted aspect type");
3635
}
3736

38-
public static void deleteAspectType(AspectTypeName aspectTypeName) throws Exception {
37+
public static void deleteAspectType(String projectId, String location, String aspectTypeId)
38+
throws Exception {
39+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
40+
3941
// Initialize client that will be used to send requests. This client only needs to be created
4042
// once, and can be reused for multiple requests. After completing all of your requests, call
4143
// the "close" method on the client to safely clean up any remaining background resources,

dataplex/snippets/src/main/java/dataplex/GetAspectType.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public static void main(String[] args) throws IOException {
3232
String location = "MY_LOCATION";
3333
String aspectTypeId = "MY_ASPECT_TYPE_ID";
3434

35-
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
36-
AspectType aspectType = getAspectType(aspectTypeName);
35+
AspectType aspectType = getAspectType(projectId, location, aspectTypeId);
3736
System.out.println("Aspect type retrieved successfully: " + aspectType.getName());
3837
}
3938

40-
public static AspectType getAspectType(AspectTypeName aspectTypeName) throws IOException {
39+
public static AspectType getAspectType(String projectId, String location, String aspectTypeId)
40+
throws IOException {
41+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
42+
4143
// Initialize client that will be used to send requests. This client only needs to be created
4244
// once, and can be reused for multiple requests. After completing all of your requests, call
4345
// the "close" method on the client to safely clean up any remaining background resources,

dataplex/snippets/src/main/java/dataplex/ListAspectTypes.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ public static void main(String[] args) throws IOException {
3333
// Available locations: https://cloud.google.com/dataplex/docs/locations
3434
String location = "MY_LOCATION";
3535

36-
LocationName locationName = LocationName.of(projectId, location);
37-
List<AspectType> aspectTypes = listAspectTypes(locationName);
36+
List<AspectType> aspectTypes = listAspectTypes(projectId, location);
3837
aspectTypes.forEach(
3938
aspectType -> System.out.println("Aspect type name: " + aspectType.getName()));
4039
}
4140

42-
public static List<AspectType> listAspectTypes(LocationName locationName) throws IOException {
41+
public static List<AspectType> listAspectTypes(String projectId, String location)
42+
throws IOException {
43+
LocationName locationName = LocationName.of(projectId, location);
44+
4345
// Initialize client that will be used to send requests. This client only needs to be created
4446
// once, and can be reused for multiple requests. After completing all of your requests, call
4547
// the "close" method on the client to safely clean up any remaining background resources,
4648
// or use "try-with-close" statement to do this automatically.
4749
try (CatalogServiceClient client = CatalogServiceClient.create()) {
4850
CatalogServiceClient.ListAspectTypesPagedResponse listAspectTypesResponse =
4951
client.listAspectTypes(locationName);
52+
// Paging is implicitly handled by .iterateAll(), all results will be returned
5053
return ImmutableList.copyOf(listAspectTypesResponse.iterateAll());
5154
}
5255
}

dataplex/snippets/src/main/java/dataplex/UpdateAspectType.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static void main(String[] args) throws Exception {
3333
String location = "MY_LOCATION";
3434
String aspectTypeId = "MY_ASPECT_TYPE_ID";
3535

36-
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
3736
AspectType.MetadataTemplate aspectField =
3837
AspectType.MetadataTemplate.newBuilder()
3938
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
@@ -56,16 +55,20 @@ public static void main(String[] args) throws Exception {
5655
.build())
5756
.build();
5857
List<AspectType.MetadataTemplate> aspectFields = List.of(aspectField);
59-
AspectType updatedAspectType = updateAspectType(aspectTypeName, aspectFields);
58+
AspectType updatedAspectType =
59+
updateAspectType(projectId, location, aspectTypeId, aspectFields);
6060
System.out.println("Successfully updated aspect type: " + updatedAspectType.getName());
6161
}
6262

6363
public static AspectType updateAspectType(
64-
AspectTypeName aspectTypeName, List<AspectType.MetadataTemplate> aspectFields)
64+
String projectId,
65+
String location,
66+
String aspectTypeId,
67+
List<AspectType.MetadataTemplate> aspectFields)
6568
throws Exception {
6669
AspectType aspectType =
6770
AspectType.newBuilder()
68-
.setName(aspectTypeName.toString())
71+
.setName(AspectTypeName.of(projectId, location, aspectTypeId).toString())
6972
.setDescription("updated description of the aspect type")
7073
.setMetadataTemplate(
7174
AspectType.MetadataTemplate.newBuilder()

dataplex/snippets/src/test/java/dataplex/AspectTypeIT.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static junit.framework.TestCase.assertNotNull;
2121

2222
import com.google.cloud.dataplex.v1.AspectType;
23-
import com.google.cloud.dataplex.v1.AspectTypeName;
24-
import com.google.cloud.dataplex.v1.LocationName;
2523
import java.io.IOException;
2624
import java.util.ArrayList;
2725
import java.util.List;
@@ -33,8 +31,7 @@
3331
public class AspectTypeIT {
3432
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
3533
private static final String LOCATION = "us-central1";
36-
private static LocationName locationName;
37-
private static AspectTypeName aspectTypeName;
34+
private static final String aspectTypeId = "test-aspect-type" + ID;
3835
private static String expectedAspectType;
3936

4037
private static final String PROJECT_ID = requireProjectIdEnvVar();
@@ -54,67 +51,63 @@ public static void checkRequirements() {
5451
@BeforeClass
5552
// Set-up code that will be executed before all tests
5653
public static void setUp() throws Exception {
57-
String aspectTypeId = "test-aspect-type" + ID;
58-
locationName = LocationName.of(PROJECT_ID, LOCATION);
59-
aspectTypeName = AspectTypeName.of(PROJECT_ID, LOCATION, aspectTypeId);
6054
expectedAspectType =
6155
String.format(
6256
"projects/%s/locations/%s/aspectTypes/%s", PROJECT_ID, LOCATION, aspectTypeId);
6357
// Create Aspect Type resource that will be used in tests for "get", "list" and "update" methods
64-
CreateAspectType.createAspectType(locationName, aspectTypeId, new ArrayList<>());
58+
CreateAspectType.createAspectType(PROJECT_ID, LOCATION, aspectTypeId, new ArrayList<>());
6559
}
6660

6761
@Test
68-
public void listAspectTypes_returnsListContainingAspectTypeCreatedInSetUp() throws IOException {
69-
List<AspectType> aspectTypes = ListAspectTypes.listAspectTypes(locationName);
62+
public void testListAspectTypes() throws IOException {
63+
List<AspectType> aspectTypes = ListAspectTypes.listAspectTypes(PROJECT_ID, LOCATION);
7064
assertThat(aspectTypes.stream().map(AspectType::getName)).contains(expectedAspectType);
7165
}
7266

7367
@Test
74-
public void getAspectType_returnsAspectTypeCreatedInSetUp() throws IOException {
75-
AspectType aspectType = GetAspectType.getAspectType(aspectTypeName);
68+
public void testGetAspectType() throws IOException {
69+
AspectType aspectType = GetAspectType.getAspectType(PROJECT_ID, LOCATION, aspectTypeId);
7670
assertThat(aspectType.getName()).isEqualTo(expectedAspectType);
7771
}
7872

7973
@Test
80-
public void updateAspectType_returnsUpdatedAspectType() throws Exception {
81-
AspectType aspectType = UpdateAspectType.updateAspectType(aspectTypeName, new ArrayList<>());
74+
public void testUpdateAspectType() throws Exception {
75+
AspectType aspectType =
76+
UpdateAspectType.updateAspectType(PROJECT_ID, LOCATION, aspectTypeId, new ArrayList<>());
8277
assertThat(aspectType.getName()).isEqualTo(expectedAspectType);
8378
}
8479

8580
@Test
86-
public void createAspectType_returnsCreatedAspectType() throws Exception {
81+
public void testCreateAspectType() throws Exception {
8782
String aspectTypeIdToCreate = "test-aspect-type" + UUID.randomUUID().toString().substring(0, 8);
88-
AspectTypeName aspectTypeNameToCreate =
89-
AspectTypeName.of(PROJECT_ID, LOCATION, aspectTypeIdToCreate);
9083
String expectedAspectTypeToCreate =
9184
String.format(
9285
"projects/%s/locations/%s/aspectTypes/%s", PROJECT_ID, LOCATION, aspectTypeIdToCreate);
9386

9487
AspectType aspectType =
95-
CreateAspectType.createAspectType(locationName, aspectTypeIdToCreate, new ArrayList<>());
88+
CreateAspectType.createAspectType(
89+
PROJECT_ID, LOCATION, aspectTypeIdToCreate, new ArrayList<>());
9690
// Clean-up created Aspect Type
97-
DeleteAspectType.deleteAspectType(aspectTypeNameToCreate);
91+
DeleteAspectType.deleteAspectType(PROJECT_ID, LOCATION, aspectTypeIdToCreate);
9892

9993
assertThat(aspectType.getName()).isEqualTo(expectedAspectTypeToCreate);
10094
}
10195

10296
@Test
103-
public void deleteAspectType_executesTheCallWithoutException() throws Exception {
97+
public void testDeleteAspectType() throws Exception {
10498
String aspectTypeIdToDelete = "test-aspect-type" + UUID.randomUUID().toString().substring(0, 8);
105-
AspectTypeName aspectTypeNameToDelete =
106-
AspectTypeName.of(PROJECT_ID, LOCATION, aspectTypeIdToDelete);
10799
// Create Aspect Type to be deleted
108-
CreateAspectType.createAspectType(locationName, aspectTypeIdToDelete, new ArrayList<>());
100+
CreateAspectType.createAspectType(
101+
PROJECT_ID, LOCATION, aspectTypeIdToDelete, new ArrayList<>());
109102

110103
// No exception means successful call
111-
DeleteAspectType.deleteAspectType(aspectTypeNameToDelete);
104+
DeleteAspectType.deleteAspectType(PROJECT_ID, LOCATION, aspectTypeIdToDelete);
112105
}
113106

114107
@AfterClass
115108
// Clean-up code that will be executed after all tests
116109
public static void tearDown() throws Exception {
117110
// Clean-up Aspect Type resource created in setUp()
118-
DeleteAspectType.deleteAspectType(aspectTypeName);
111+
DeleteAspectType.deleteAspectType(PROJECT_ID, LOCATION, aspectTypeId);
119112
}
120113
}

0 commit comments

Comments
 (0)