From 6d28eda01a2a34ffb1f68bbe265a2e2f11514cb6 Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Mon, 10 Sep 2018 11:41:25 -0400 Subject: [PATCH 1/4] adding ITComputeTest.java --- .../cloud/compute/v1/it/ITComputeTest.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java new file mode 100644 index 000000000000..e6ea12304865 --- /dev/null +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java @@ -0,0 +1,150 @@ +/* + * Copyright 2018 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 + * + * https://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.google.cloud.compute.v1.it; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.api.gax.paging.Page; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.ServiceOptions; +import com.google.cloud.compute.v1.DiskType; +import com.google.cloud.compute.v1.DiskTypeClient; +import com.google.cloud.compute.v1.DiskTypeClient.AggregatedListDiskTypesPagedResponse; +import com.google.cloud.compute.v1.DiskTypeName; +import com.google.cloud.compute.v1.DiskTypeSettings; +import com.google.cloud.compute.v1.DiskTypesScopedList; +import com.google.cloud.compute.v1.ListDiskTypesHttpRequest; +import com.google.cloud.compute.v1.ProjectName; +import com.google.cloud.compute.v1.ZoneName; +import com.google.common.collect.Lists; +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class ITComputeTest { + + private static final String ZONE = "us-central1-a"; + private static final String DISK_TYPE = "local-ssd"; + private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); + + private static DiskTypeClient diskTypeClient; + private static DiskTypeSettings diskTypeSettings; + + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + @BeforeClass + public static void beforeClass() throws IOException{ + Credentials credentials = GoogleCredentials.getApplicationDefault(); + + diskTypeSettings = + DiskTypeSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build(); + diskTypeClient = + DiskTypeClient.create(diskTypeSettings); + } + + @AfterClass + public static void tearDown() throws Exception { + diskTypeClient.close(); + } + + @Test + public void testGetDiskType() { + DiskType diskType = diskTypeClient.getDiskType(DiskTypeName.of(DISK_TYPE, DEFAULT_PROJECT, ZONE)); + DiskTypeName returnDiskName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertEquals(ZONE, returnDiskName.getZone()); + assertEquals(DISK_TYPE, returnDiskName.getDiskType()); + assertNotNull(diskType.getCreationTimestamp()); + assertNotNull(diskType.getDescription()); + assertNotNull(diskType.getValidDiskSize()); + assertNotNull(diskType.getDefaultDiskSizeGb()); + } + + @Test + public void testListDiskTypes() { + Page diskPage = diskTypeClient.listDiskTypes(ZoneName.of(DEFAULT_PROJECT, ZONE)).getPage(); + Iterator diskTypeIterator = diskPage.iterateAll().iterator(); + assertTrue(diskTypeIterator.hasNext()); + while (diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertNotNull(diskType.getSelfLink()); + DiskTypeName returnDiskName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertEquals(ZONE, returnDiskName.getZone()); + assertNotNull(diskType.getCreationTimestamp()); + assertNotNull(diskType.getDescription()); + assertNotNull(diskType.getValidDiskSize()); + assertNotNull(diskType.getDefaultDiskSizeGb()); + } + } + + @Test + public void testListDiskTypesWithFilter() { + ListDiskTypesHttpRequest request = ListDiskTypesHttpRequest.newBuilder() + .setZone(ZoneName.of(DEFAULT_PROJECT, ZONE).toString()) + .setFilter("(defaultDiskSizeGb = 375)") + .build(); + Page diskPage = diskTypeClient.listDiskTypes(request).getPage(); + Iterator diskTypeIterator = diskPage.iterateAll().iterator(); + assertTrue(diskTypeIterator.hasNext()); + while (diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertNotNull(diskType.getZone()); + ZoneName zoneName = ZoneName.parse(trimUrl(diskType.getZone())); + assertEquals(ZONE, zoneName.getZone()); + assertNotNull(diskType.getCreationTimestamp()); + assertNotNull(diskType.getDescription()); + assertNotNull(diskType.getValidDiskSize()); + assertNotNull(diskType.getDefaultDiskSizeGb()); + } + } + + @Test + public void testAggregatedListDiskTypes() { + AggregatedListDiskTypesPagedResponse pagedListResponse = diskTypeClient.aggregatedListDiskTypes(ProjectName.of(DEFAULT_PROJECT)); + List diskTypeScopedListIterator = Lists.newArrayList(pagedListResponse.iterateAll()); + List diskTypeIterator = new LinkedList<>(); + for (DiskTypesScopedList scopedList : diskTypeScopedListIterator) { + diskTypeIterator.addAll(scopedList.getDiskTypesList()); + } + assertTrue(diskTypeIterator.size() > 0); + for (DiskType diskType : diskTypeIterator) { + assertNotNull(diskType.getZone()); + DiskTypeName zoneName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertNotNull(zoneName.getDiskType()); + assertNotNull(zoneName.getZone()); + assertNotNull(diskType.getCreationTimestamp()); + assertNotNull(diskType.getDescription()); + assertNotNull(diskType.getValidDiskSize()); + assertNotNull(diskType.getDefaultDiskSizeGb()); + } + } + + /** For a given resource's URI, trim the path until it contains only the PathTemplate string. */ + private String trimUrl(String url) { + return url.replaceFirst("^https://www.googleapis.com/compute/v1/", ""); + } +} \ No newline at end of file From fdb8a3f711cfb8bc9b2778b7477232d2ad28f04e Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Mon, 10 Sep 2018 10:40:59 -0700 Subject: [PATCH 2/4] fix ResourceNames in ITComputeTest --- .../cloud/compute/v1/it/ITComputeTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java index e6ea12304865..ab56ef8bb1c4 100644 --- a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java @@ -27,12 +27,12 @@ import com.google.cloud.compute.v1.DiskType; import com.google.cloud.compute.v1.DiskTypeClient; import com.google.cloud.compute.v1.DiskTypeClient.AggregatedListDiskTypesPagedResponse; -import com.google.cloud.compute.v1.DiskTypeName; import com.google.cloud.compute.v1.DiskTypeSettings; import com.google.cloud.compute.v1.DiskTypesScopedList; import com.google.cloud.compute.v1.ListDiskTypesHttpRequest; import com.google.cloud.compute.v1.ProjectName; -import com.google.cloud.compute.v1.ZoneName; +import com.google.cloud.compute.v1.ProjectZoneDiskTypeName; +import com.google.cloud.compute.v1.ProjectZoneName; import com.google.common.collect.Lists; import java.io.IOException; import java.util.Iterator; @@ -74,8 +74,9 @@ public static void tearDown() throws Exception { @Test public void testGetDiskType() { - DiskType diskType = diskTypeClient.getDiskType(DiskTypeName.of(DISK_TYPE, DEFAULT_PROJECT, ZONE)); - DiskTypeName returnDiskName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + DiskType diskType = diskTypeClient.getDiskType(ProjectZoneDiskTypeName + .of(DISK_TYPE, DEFAULT_PROJECT, ZONE)); + ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); assertEquals(ZONE, returnDiskName.getZone()); assertEquals(DISK_TYPE, returnDiskName.getDiskType()); assertNotNull(diskType.getCreationTimestamp()); @@ -86,13 +87,13 @@ public void testGetDiskType() { @Test public void testListDiskTypes() { - Page diskPage = diskTypeClient.listDiskTypes(ZoneName.of(DEFAULT_PROJECT, ZONE)).getPage(); + Page diskPage = diskTypeClient.listDiskTypes(ProjectZoneName.of(DEFAULT_PROJECT, ZONE)).getPage(); Iterator diskTypeIterator = diskPage.iterateAll().iterator(); assertTrue(diskTypeIterator.hasNext()); while (diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); assertNotNull(diskType.getSelfLink()); - DiskTypeName returnDiskName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); assertEquals(ZONE, returnDiskName.getZone()); assertNotNull(diskType.getCreationTimestamp()); assertNotNull(diskType.getDescription()); @@ -104,7 +105,7 @@ public void testListDiskTypes() { @Test public void testListDiskTypesWithFilter() { ListDiskTypesHttpRequest request = ListDiskTypesHttpRequest.newBuilder() - .setZone(ZoneName.of(DEFAULT_PROJECT, ZONE).toString()) + .setZone(ProjectZoneName.of(DEFAULT_PROJECT, ZONE).toString()) .setFilter("(defaultDiskSizeGb = 375)") .build(); Page diskPage = diskTypeClient.listDiskTypes(request).getPage(); @@ -113,7 +114,7 @@ public void testListDiskTypesWithFilter() { while (diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); assertNotNull(diskType.getZone()); - ZoneName zoneName = ZoneName.parse(trimUrl(diskType.getZone())); + ProjectZoneName zoneName = ProjectZoneName.parse(trimUrl(diskType.getZone())); assertEquals(ZONE, zoneName.getZone()); assertNotNull(diskType.getCreationTimestamp()); assertNotNull(diskType.getDescription()); @@ -133,7 +134,7 @@ public void testAggregatedListDiskTypes() { assertTrue(diskTypeIterator.size() > 0); for (DiskType diskType : diskTypeIterator) { assertNotNull(diskType.getZone()); - DiskTypeName zoneName = DiskTypeName.parse(trimUrl(diskType.getSelfLink())); + ProjectZoneDiskTypeName zoneName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); assertNotNull(zoneName.getDiskType()); assertNotNull(zoneName.getZone()); assertNotNull(diskType.getCreationTimestamp()); From ba29b8a32176a7f92e6adfa8c1cc1e496a75f9d4 Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 11 Sep 2018 09:40:29 -0700 Subject: [PATCH 3/4] formatting, use google.Truth --- .../google-cloud-compute/pom.xml | 5 ++ .../cloud/compute/v1/it/ITComputeTest.java | 64 +++++++++---------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/google-cloud-clients/google-cloud-compute/pom.xml b/google-cloud-clients/google-cloud-compute/pom.xml index cf6a49bbb945..b7bbeb282127 100644 --- a/google-cloud-clients/google-cloud-compute/pom.xml +++ b/google-cloud-clients/google-cloud-compute/pom.xml @@ -70,5 +70,10 @@ testlib test + + com.google.truth + truth + test + diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java index ab56ef8bb1c4..bc152f9f8444 100644 --- a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java @@ -15,9 +15,7 @@ */ package com.google.cloud.compute.v1.it; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; import com.google.api.gax.core.FixedCredentialsProvider; import com.google.api.gax.paging.Page; @@ -48,7 +46,7 @@ public class ITComputeTest { private static final String ZONE = "us-central1-a"; private static final String DISK_TYPE = "local-ssd"; - private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); + private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); private static DiskTypeClient diskTypeClient; private static DiskTypeSettings diskTypeSettings; @@ -77,28 +75,28 @@ public void testGetDiskType() { DiskType diskType = diskTypeClient.getDiskType(ProjectZoneDiskTypeName .of(DISK_TYPE, DEFAULT_PROJECT, ZONE)); ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); - assertEquals(ZONE, returnDiskName.getZone()); - assertEquals(DISK_TYPE, returnDiskName.getDiskType()); - assertNotNull(diskType.getCreationTimestamp()); - assertNotNull(diskType.getDescription()); - assertNotNull(diskType.getValidDiskSize()); - assertNotNull(diskType.getDefaultDiskSizeGb()); + assertThat(returnDiskName.getZone()).isEqualTo(ZONE); + assertThat(returnDiskName.getDiskType()).isEqualTo(DISK_TYPE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); } @Test public void testListDiskTypes() { Page diskPage = diskTypeClient.listDiskTypes(ProjectZoneName.of(DEFAULT_PROJECT, ZONE)).getPage(); Iterator diskTypeIterator = diskPage.iterateAll().iterator(); - assertTrue(diskTypeIterator.hasNext()); + assertThat(diskTypeIterator.hasNext()).isTrue(); while (diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); - assertNotNull(diskType.getSelfLink()); + assertThat(diskType.getSelfLink()).isNotNull(); ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); - assertEquals(ZONE, returnDiskName.getZone()); - assertNotNull(diskType.getCreationTimestamp()); - assertNotNull(diskType.getDescription()); - assertNotNull(diskType.getValidDiskSize()); - assertNotNull(diskType.getDefaultDiskSizeGb()); + assertThat(returnDiskName.getZone()).isEqualTo(ZONE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); } } @@ -110,16 +108,16 @@ public void testListDiskTypesWithFilter() { .build(); Page diskPage = diskTypeClient.listDiskTypes(request).getPage(); Iterator diskTypeIterator = diskPage.iterateAll().iterator(); - assertTrue(diskTypeIterator.hasNext()); + assertThat(diskTypeIterator.hasNext()).isTrue(); while (diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); - assertNotNull(diskType.getZone()); + assertThat(diskType.getZone()).isNotNull(); ProjectZoneName zoneName = ProjectZoneName.parse(trimUrl(diskType.getZone())); - assertEquals(ZONE, zoneName.getZone()); - assertNotNull(diskType.getCreationTimestamp()); - assertNotNull(diskType.getDescription()); - assertNotNull(diskType.getValidDiskSize()); - assertNotNull(diskType.getDefaultDiskSizeGb()); + assertThat(zoneName.getZone()).isEqualTo(ZONE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); } } @@ -131,21 +129,21 @@ public void testAggregatedListDiskTypes() { for (DiskTypesScopedList scopedList : diskTypeScopedListIterator) { diskTypeIterator.addAll(scopedList.getDiskTypesList()); } - assertTrue(diskTypeIterator.size() > 0); + assertThat(diskTypeIterator.size()).isGreaterThan(0); for (DiskType diskType : diskTypeIterator) { - assertNotNull(diskType.getZone()); + assertThat(diskType.getZone()).isNotNull(); ProjectZoneDiskTypeName zoneName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); - assertNotNull(zoneName.getDiskType()); - assertNotNull(zoneName.getZone()); - assertNotNull(diskType.getCreationTimestamp()); - assertNotNull(diskType.getDescription()); - assertNotNull(diskType.getValidDiskSize()); - assertNotNull(diskType.getDefaultDiskSizeGb()); + assertThat(zoneName.getDiskType()).isNotNull(); + assertThat(zoneName.getZone()).isNotNull(); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); } } /** For a given resource's URI, trim the path until it contains only the PathTemplate string. */ - private String trimUrl(String url) { + private static String trimUrl(String url) { return url.replaceFirst("^https://www.googleapis.com/compute/v1/", ""); } } \ No newline at end of file From 3895bdb681024cf2952b9f81ddfbf6c1c32d81b0 Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 11 Sep 2018 09:40:52 -0700 Subject: [PATCH 4/4] newline --- .../test/java/com/google/cloud/compute/v1/it/ITComputeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java index bc152f9f8444..e5da6b092c1f 100644 --- a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java @@ -146,4 +146,4 @@ public void testAggregatedListDiskTypes() { private static String trimUrl(String url) { return url.replaceFirst("^https://www.googleapis.com/compute/v1/", ""); } -} \ No newline at end of file +}