1 /* 2 * Copyright 2021 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.integration; 17 18 import com.google.cloud.compute.v1.AcceleratorType; 19 import com.google.cloud.compute.v1.AcceleratorTypesClient; 20 import com.google.cloud.compute.v1.AcceleratorTypesScopedList; 21 import com.google.cloud.compute.v1.AggregatedListAcceleratorTypesRequest; 22 import com.google.cloud.compute.v1.ListZonesRequest; 23 import com.google.cloud.compute.v1.Zone; 24 import com.google.cloud.compute.v1.ZonesClient; 25 import com.google.cloud.compute.v1.ZonesSettings; 26 import com.google.common.collect.Lists; 27 import java.io.IOException; 28 import java.util.Map; 29 import org.junit.AfterClass; 30 import org.junit.Assert; 31 import org.junit.BeforeClass; 32 import org.junit.Test; 33 34 public class ITPaginationTest extends BaseTest { 35 private static ZonesClient zonesClient; 36 37 @BeforeClass setUp()38 public static void setUp() throws IOException { 39 ZonesSettings zoneSettings = ZonesSettings.newBuilder().build(); 40 zonesClient = ZonesClient.create(zoneSettings); 41 } 42 43 @AfterClass tearDown()44 public static void tearDown() { 45 zonesClient.close(); 46 } 47 48 @Test testList()49 public void testList() { 50 ZonesClient.ListPagedResponse response = zonesClient.list(DEFAULT_PROJECT); 51 boolean presented = false; 52 for (Zone element : response.iterateAll()) { 53 if (element.getName().equals(DEFAULT_ZONE)) { 54 presented = true; 55 } 56 } 57 Assert.assertTrue(presented); 58 } 59 60 @Test testPaginationMaxResults()61 public void testPaginationMaxResults() { 62 ListZonesRequest listZonesRequest = 63 ListZonesRequest.newBuilder().setProject(DEFAULT_PROJECT).setMaxResults(15).build(); 64 ZonesClient.ListPagedResponse response = zonesClient.list(listZonesRequest); 65 ZonesClient.ListPage listPage = response.getPage(); 66 Assert.assertEquals(15, Lists.newArrayList(listPage.getValues()).size()); 67 } 68 69 @Test testPaginationNextPage()70 public void testPaginationNextPage() { 71 ListZonesRequest listZonesRequest = 72 ListZonesRequest.newBuilder().setProject(DEFAULT_PROJECT).setMaxResults(15).build(); 73 ZonesClient.ListPagedResponse response = zonesClient.list(listZonesRequest); 74 ZonesClient.ListPage listPage = response.getPage(); 75 ZonesClient.ListPage nextPage = listPage.getNextPage(); 76 Assert.assertNotEquals(listPage, nextPage); 77 } 78 79 @Test testPaginationNextPageSize()80 public void testPaginationNextPageSize() { 81 ListZonesRequest listZonesRequest = 82 ListZonesRequest.newBuilder().setProject(DEFAULT_PROJECT).setMaxResults(15).build(); 83 ZonesClient.ListPagedResponse response = zonesClient.list(listZonesRequest); 84 ZonesClient.ListPage listPage = response.getPage(); 85 ZonesClient.ListPage nextPage = listPage.getNextPage(20); 86 Assert.assertEquals(20, Lists.newArrayList(nextPage.getValues()).size()); 87 } 88 89 @Test testPaginationNextToken()90 public void testPaginationNextToken() { 91 ListZonesRequest listZonesRequest = 92 ListZonesRequest.newBuilder().setProject(DEFAULT_PROJECT).setMaxResults(15).build(); 93 ZonesClient.ListPagedResponse response = zonesClient.list(listZonesRequest); 94 ZonesClient.ListPage listPage = response.getPage(); 95 String nextPageToken = listPage.getNextPageToken(); 96 ZonesClient.ListPage nextPage = listPage.getNextPage(); 97 98 ListZonesRequest listZonesRequestToken = 99 ListZonesRequest.newBuilder() 100 .setProject(DEFAULT_PROJECT) 101 .setMaxResults(15) 102 .setPageToken(nextPageToken) 103 .build(); 104 ZonesClient.ListPagedResponse responseToken = zonesClient.list(listZonesRequestToken); 105 ZonesClient.ListPage nextPageWithToken = responseToken.getPage(); 106 Assert.assertEquals( 107 Lists.newArrayList(nextPage.getValues()), 108 Lists.newArrayList(nextPageWithToken.getValues())); 109 } 110 111 @Test testPaginationIterating()112 public void testPaginationIterating() { 113 ListZonesRequest listZonesRequest = 114 ListZonesRequest.newBuilder().setProject(DEFAULT_PROJECT).setMaxResults(1).build(); 115 ZonesClient.ListPagedResponse response = zonesClient.list(listZonesRequest); 116 boolean presented = false; 117 int count = 0; 118 for (Zone element : response.iterateAll()) { 119 count++; 120 if (element.getName().equals(DEFAULT_ZONE)) { 121 presented = true; 122 } 123 } 124 Assert.assertTrue( 125 String.format( 126 "Zone %s was not found for %s in zones list (size: %d).", 127 DEFAULT_ZONE, DEFAULT_PROJECT, count), 128 presented); 129 } 130 131 @Test testPaginationAggregatedIterating()132 public void testPaginationAggregatedIterating() throws IOException { 133 AcceleratorTypesClient acceleratorTypesClient = AcceleratorTypesClient.create(); 134 AggregatedListAcceleratorTypesRequest aggregatedListAcceleratorTypesRequest = 135 AggregatedListAcceleratorTypesRequest.newBuilder() 136 .setProject(DEFAULT_PROJECT) 137 .setMaxResults(2) 138 .build(); 139 AcceleratorTypesClient.AggregatedListPagedResponse response = 140 acceleratorTypesClient.aggregatedList(aggregatedListAcceleratorTypesRequest); 141 boolean presented = false; 142 for (Map.Entry<String, AcceleratorTypesScopedList> entry : response.iterateAll()) { 143 if (entry.getKey().equals("zones/" + DEFAULT_ZONE)) { 144 for (AcceleratorType type : entry.getValue().getAcceleratorTypesList()) { 145 if (type.getName().equals("nvidia-tesla-t4")) { 146 presented = true; 147 break; 148 } 149 } 150 } 151 } 152 Assert.assertTrue(presented); 153 } 154 } 155