PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/internal/ToIteratorOfListPageExpectTest.java

http://github.com/jclouds/jclouds
Java | 184 lines | 127 code | 41 blank | 16 comment | 3 complexity | 256b9798b056d40fd9f2f259eabf2e07 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.googlecomputeengine.internal;
  18. import static org.jclouds.googlecomputeengine.options.ListOptions.Builder.maxResults;
  19. import static org.testng.Assert.assertEquals;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.jclouds.googlecloud.domain.ForwardingListPage;
  23. import org.jclouds.googlecloud.domain.ListPage;
  24. import org.jclouds.googlecomputeengine.GoogleComputeEngineApi;
  25. import org.jclouds.googlecomputeengine.config.GoogleComputeEngineParserModule;
  26. import org.jclouds.googlecomputeengine.domain.Address;
  27. import org.jclouds.googlecomputeengine.domain.Image;
  28. import org.jclouds.googlecomputeengine.domain.Instance;
  29. import org.jclouds.googlecomputeengine.features.AddressApi;
  30. import org.jclouds.googlecomputeengine.features.ImageApi;
  31. import org.jclouds.googlecomputeengine.features.InstanceApi;
  32. import org.jclouds.googlecomputeengine.parse.ParseAddressTest;
  33. import org.jclouds.googlecomputeengine.parse.ParseImageTest;
  34. import org.jclouds.googlecomputeengine.parse.ParseInstanceTest;
  35. import org.jclouds.http.HttpRequest;
  36. import org.jclouds.http.HttpResponse;
  37. import org.jclouds.json.Json;
  38. import org.jclouds.json.config.GsonModule;
  39. import org.testng.annotations.Test;
  40. import com.google.common.collect.ImmutableList;
  41. import com.google.inject.Guice;
  42. @Test(groups = "unit", testName = "ToIteratorOfListPageTest")
  43. public class ToIteratorOfListPageExpectTest extends BaseGoogleComputeEngineExpectTest<GoogleComputeEngineApi> {
  44. private final Json json = Guice.createInjector(new GsonModule(), new GoogleComputeEngineParserModule())
  45. .getInstance(Json.class);
  46. public void globalScope() {
  47. HttpRequest list = HttpRequest
  48. .builder()
  49. .method("GET")
  50. .endpoint(BASE_URL + "/party/global/images")
  51. .addHeader("Accept", "application/json")
  52. .addHeader("Authorization", "Bearer " + TOKEN).build();
  53. HttpResponse operationResponse = HttpResponse.builder().statusCode(200)
  54. .payload(payloadFromResource("/image_list_single_page.json")).build();
  55. ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
  56. TOKEN_RESPONSE, list, operationResponse).images();
  57. Iterator<ListPage<Image>> images = imageApi.list();
  58. assertEquals(images.next().size(), 3);
  59. }
  60. public void multiplePagesProjectScoped() {
  61. HttpRequest list1 = HttpRequest
  62. .builder()
  63. .method("GET")
  64. .endpoint(BASE_URL + "/party/global/images?maxResults=1")
  65. .addHeader("Accept", "application/json")
  66. .addHeader("Authorization", "Bearer " + TOKEN).build();
  67. HttpRequest list2 = list1.toBuilder()
  68. .endpoint(BASE_URL + "/party/global/images?pageToken=token1&maxResults=1").build();
  69. HttpRequest list3 = list1.toBuilder()
  70. .endpoint(BASE_URL + "/party/global/images?pageToken=token2&maxResults=1").build();
  71. List<Image> items = ImmutableList.of(new ParseImageTest().expected());
  72. HttpResponse list1Response = HttpResponse.builder().statusCode(200)
  73. .payload(json.toJson(ForwardingListPage.create(items, "token1"))).build();
  74. HttpResponse list2Response = HttpResponse.builder().statusCode(200)
  75. .payload(json.toJson(ForwardingListPage.create(items, "token2"))).build();
  76. HttpResponse list3Response = HttpResponse.builder().statusCode(200)
  77. .payload(json.toJson(ForwardingListPage.create(items, null))).build();
  78. ImageApi imageApi = orderedRequestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
  79. TOKEN_RESPONSE, list1, list1Response, list2, list2Response, list3, list3Response).images();
  80. Iterator<ListPage<Image>> images = imageApi.list(maxResults(1));
  81. int imageCounter = 0;
  82. while (images.hasNext()) {
  83. imageCounter += images.next().size();
  84. }
  85. assertEquals(imageCounter, 3);
  86. }
  87. public void multiplePagesRegionScoped() {
  88. HttpRequest list1 = HttpRequest
  89. .builder()
  90. .method("GET")
  91. .endpoint(BASE_URL + "/party/regions/us-central1/addresses?maxResults=1")
  92. .addHeader("Accept", "application/json")
  93. .addHeader("Authorization", "Bearer " + TOKEN).build();
  94. HttpRequest list2 = list1.toBuilder()
  95. .endpoint(BASE_URL + "/party/regions/us-central1/addresses?pageToken=token1&maxResults=1").build();
  96. HttpRequest list3 = list1.toBuilder()
  97. .endpoint(BASE_URL + "/party/regions/us-central1/addresses?pageToken=token2&maxResults=1").build();
  98. List<Address> items = ImmutableList.of(new ParseAddressTest().expected());
  99. HttpResponse list1Response = HttpResponse.builder().statusCode(200)
  100. .payload(json.toJson(ForwardingListPage.create(items, "token1"))).build();
  101. HttpResponse list2Response = HttpResponse.builder().statusCode(200)
  102. .payload(json.toJson(ForwardingListPage.create(items, "token2"))).build();
  103. HttpResponse list3Response = HttpResponse.builder().statusCode(200)
  104. .payload(json.toJson(ForwardingListPage.create(items, null))).build();
  105. AddressApi addressApi = orderedRequestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
  106. TOKEN_RESPONSE, list1, list1Response, list2, list2Response, list3, list3Response)
  107. .addressesInRegion("us-central1");
  108. Iterator<ListPage<Address>> addresses = addressApi.list(maxResults(1));
  109. int addressCounter = 0;
  110. while (addresses.hasNext()) {
  111. addressCounter += addresses.next().size();
  112. }
  113. assertEquals(addressCounter, 3);
  114. }
  115. public void multiplePagesZoneScoped() {
  116. HttpRequest list1 = HttpRequest
  117. .builder()
  118. .method("GET")
  119. .endpoint(BASE_URL + "/party/zones/us-central1-a/instances?maxResults=1")
  120. .addHeader("Accept", "application/json")
  121. .addHeader("Authorization", "Bearer " + TOKEN).build();
  122. HttpRequest list2 = list1.toBuilder()
  123. .endpoint(BASE_URL + "/party/zones/us-central1-a/instances?pageToken=token1&maxResults=1").build();
  124. HttpRequest list3 = list1.toBuilder()
  125. .endpoint(BASE_URL + "/party/zones/us-central1-a/instances?pageToken=token2&maxResults=1").build();
  126. List<Instance> items = ImmutableList.of(new ParseInstanceTest().expected());
  127. HttpResponse list1Response = HttpResponse.builder().statusCode(200)
  128. .payload(json.toJson(ForwardingListPage.create(items, "token1"))).build();
  129. HttpResponse list2Response = HttpResponse.builder().statusCode(200)
  130. .payload(json.toJson(ForwardingListPage.create(items, "token2"))).build();
  131. HttpResponse list3Response = HttpResponse.builder().statusCode(200)
  132. .payload(json.toJson(ForwardingListPage.create(items, null))).build();
  133. InstanceApi instanceApi = orderedRequestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE),
  134. TOKEN_RESPONSE, list1, list1Response, list2, list2Response, list3, list3Response)
  135. .instancesInZone("us-central1-a");
  136. Iterator<ListPage<Instance>> instances = instanceApi.list(maxResults(1));
  137. int instanceCounter = 0;
  138. while (instances.hasNext()) {
  139. instanceCounter += instances.next().size();
  140. }
  141. assertEquals(instanceCounter, 3);
  142. }
  143. }