Skip to content

Commit a2f7111

Browse files
authored
Add handwritten integration test for Compute GAPIC (#3660)
1 parent 0e58b7d commit a2f7111

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

google-cloud-clients/google-cloud-compute/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,10 @@
7070
<classifier>testlib</classifier>
7171
<scope>test</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>com.google.truth</groupId>
75+
<artifactId>truth</artifactId>
76+
<scope>test</scope>
77+
</dependency>
7378
</dependencies>
7479
</project>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2018 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+
* https://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+
package com.google.cloud.compute.v1.it;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.api.gax.core.FixedCredentialsProvider;
21+
import com.google.api.gax.paging.Page;
22+
import com.google.auth.Credentials;
23+
import com.google.auth.oauth2.GoogleCredentials;
24+
import com.google.cloud.ServiceOptions;
25+
import com.google.cloud.compute.v1.DiskType;
26+
import com.google.cloud.compute.v1.DiskTypeClient;
27+
import com.google.cloud.compute.v1.DiskTypeClient.AggregatedListDiskTypesPagedResponse;
28+
import com.google.cloud.compute.v1.DiskTypeSettings;
29+
import com.google.cloud.compute.v1.DiskTypesScopedList;
30+
import com.google.cloud.compute.v1.ListDiskTypesHttpRequest;
31+
import com.google.cloud.compute.v1.ProjectName;
32+
import com.google.cloud.compute.v1.ProjectZoneDiskTypeName;
33+
import com.google.cloud.compute.v1.ProjectZoneName;
34+
import com.google.common.collect.Lists;
35+
import java.io.IOException;
36+
import java.util.Iterator;
37+
import java.util.LinkedList;
38+
import java.util.List;
39+
import org.junit.AfterClass;
40+
import org.junit.BeforeClass;
41+
import org.junit.Rule;
42+
import org.junit.Test;
43+
import org.junit.rules.Timeout;
44+
45+
public class ITComputeTest {
46+
47+
private static final String ZONE = "us-central1-a";
48+
private static final String DISK_TYPE = "local-ssd";
49+
private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId();
50+
51+
private static DiskTypeClient diskTypeClient;
52+
private static DiskTypeSettings diskTypeSettings;
53+
54+
55+
@Rule
56+
public Timeout globalTimeout = Timeout.seconds(300);
57+
58+
@BeforeClass
59+
public static void beforeClass() throws IOException{
60+
Credentials credentials = GoogleCredentials.getApplicationDefault();
61+
62+
diskTypeSettings =
63+
DiskTypeSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
64+
diskTypeClient =
65+
DiskTypeClient.create(diskTypeSettings);
66+
}
67+
68+
@AfterClass
69+
public static void tearDown() throws Exception {
70+
diskTypeClient.close();
71+
}
72+
73+
@Test
74+
public void testGetDiskType() {
75+
DiskType diskType = diskTypeClient.getDiskType(ProjectZoneDiskTypeName
76+
.of(DISK_TYPE, DEFAULT_PROJECT, ZONE));
77+
ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink()));
78+
assertThat(returnDiskName.getZone()).isEqualTo(ZONE);
79+
assertThat(returnDiskName.getDiskType()).isEqualTo(DISK_TYPE);
80+
assertThat(diskType.getCreationTimestamp()).isNotNull();
81+
assertThat(diskType.getDescription()).isNotNull();
82+
assertThat(diskType.getValidDiskSize()).isNotNull();
83+
assertThat(diskType.getDefaultDiskSizeGb()).isNotNull();
84+
}
85+
86+
@Test
87+
public void testListDiskTypes() {
88+
Page<DiskType> diskPage = diskTypeClient.listDiskTypes(ProjectZoneName.of(DEFAULT_PROJECT, ZONE)).getPage();
89+
Iterator<DiskType> diskTypeIterator = diskPage.iterateAll().iterator();
90+
assertThat(diskTypeIterator.hasNext()).isTrue();
91+
while (diskTypeIterator.hasNext()) {
92+
DiskType diskType = diskTypeIterator.next();
93+
assertThat(diskType.getSelfLink()).isNotNull();
94+
ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink()));
95+
assertThat(returnDiskName.getZone()).isEqualTo(ZONE);
96+
assertThat(diskType.getCreationTimestamp()).isNotNull();
97+
assertThat(diskType.getDescription()).isNotNull();
98+
assertThat(diskType.getValidDiskSize()).isNotNull();
99+
assertThat(diskType.getDefaultDiskSizeGb()).isNotNull();
100+
}
101+
}
102+
103+
@Test
104+
public void testListDiskTypesWithFilter() {
105+
ListDiskTypesHttpRequest request = ListDiskTypesHttpRequest.newBuilder()
106+
.setZone(ProjectZoneName.of(DEFAULT_PROJECT, ZONE).toString())
107+
.setFilter("(defaultDiskSizeGb = 375)")
108+
.build();
109+
Page<DiskType> diskPage = diskTypeClient.listDiskTypes(request).getPage();
110+
Iterator<DiskType> diskTypeIterator = diskPage.iterateAll().iterator();
111+
assertThat(diskTypeIterator.hasNext()).isTrue();
112+
while (diskTypeIterator.hasNext()) {
113+
DiskType diskType = diskTypeIterator.next();
114+
assertThat(diskType.getZone()).isNotNull();
115+
ProjectZoneName zoneName = ProjectZoneName.parse(trimUrl(diskType.getZone()));
116+
assertThat(zoneName.getZone()).isEqualTo(ZONE);
117+
assertThat(diskType.getCreationTimestamp()).isNotNull();
118+
assertThat(diskType.getDescription()).isNotNull();
119+
assertThat(diskType.getValidDiskSize()).isNotNull();
120+
assertThat(diskType.getDefaultDiskSizeGb()).isNotNull();
121+
}
122+
}
123+
124+
@Test
125+
public void testAggregatedListDiskTypes() {
126+
AggregatedListDiskTypesPagedResponse pagedListResponse = diskTypeClient.aggregatedListDiskTypes(ProjectName.of(DEFAULT_PROJECT));
127+
List<DiskTypesScopedList> diskTypeScopedListIterator = Lists.newArrayList(pagedListResponse.iterateAll());
128+
List<DiskType> diskTypeIterator = new LinkedList<>();
129+
for (DiskTypesScopedList scopedList : diskTypeScopedListIterator) {
130+
diskTypeIterator.addAll(scopedList.getDiskTypesList());
131+
}
132+
assertThat(diskTypeIterator.size()).isGreaterThan(0);
133+
for (DiskType diskType : diskTypeIterator) {
134+
assertThat(diskType.getZone()).isNotNull();
135+
ProjectZoneDiskTypeName zoneName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink()));
136+
assertThat(zoneName.getDiskType()).isNotNull();
137+
assertThat(zoneName.getZone()).isNotNull();
138+
assertThat(diskType.getCreationTimestamp()).isNotNull();
139+
assertThat(diskType.getDescription()).isNotNull();
140+
assertThat(diskType.getValidDiskSize()).isNotNull();
141+
assertThat(diskType.getDefaultDiskSizeGb()).isNotNull();
142+
}
143+
}
144+
145+
/** For a given resource's URI, trim the path until it contains only the PathTemplate string. */
146+
private static String trimUrl(String url) {
147+
return url.replaceFirst("^https://www.googleapis.com/compute/v1/", "");
148+
}
149+
}

0 commit comments

Comments
 (0)