1 /* 2 * Copyright 2022 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 17 package com.google.cloud.compute.v1; 18 19 import static com.google.cloud.compute.v1.ZonesClient.ListPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.cloud.compute.v1.stub.HttpJsonZonesStub; 31 import com.google.common.collect.Lists; 32 import java.io.IOException; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.List; 36 import javax.annotation.Generated; 37 import org.junit.After; 38 import org.junit.AfterClass; 39 import org.junit.Assert; 40 import org.junit.Before; 41 import org.junit.BeforeClass; 42 import org.junit.Test; 43 44 @Generated("by gapic-generator-java") 45 public class ZonesClientTest { 46 private static MockHttpService mockService; 47 private static ZonesClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonZonesStub.getMethodDescriptors(), ZonesSettings.getDefaultEndpoint()); 54 ZonesSettings settings = 55 ZonesSettings.newBuilder() 56 .setTransportChannelProvider( 57 ZonesSettings.defaultHttpJsonTransportProviderBuilder() 58 .setHttpTransport(mockService) 59 .build()) 60 .setCredentialsProvider(NoCredentialsProvider.create()) 61 .build(); 62 client = ZonesClient.create(settings); 63 } 64 65 @AfterClass stopServer()66 public static void stopServer() { 67 client.close(); 68 } 69 70 @Before setUp()71 public void setUp() {} 72 73 @After tearDown()74 public void tearDown() throws Exception { 75 mockService.reset(); 76 } 77 78 @Test getTest()79 public void getTest() throws Exception { 80 Zone expectedResponse = 81 Zone.newBuilder() 82 .addAllAvailableCpuPlatforms(new ArrayList<String>()) 83 .setCreationTimestamp("creationTimestamp-370203401") 84 .setDeprecated(DeprecationStatus.newBuilder().build()) 85 .setDescription("description-1724546052") 86 .setId(3355) 87 .setKind("kind3292052") 88 .setName("name3373707") 89 .setRegion("region-934795532") 90 .setSelfLink("selfLink1191800166") 91 .setStatus("status-892481550") 92 .setSupportsPzs(true) 93 .build(); 94 mockService.addResponse(expectedResponse); 95 96 String project = "project-6911"; 97 String zone = "zone-5246"; 98 99 Zone actualResponse = client.get(project, zone); 100 Assert.assertEquals(expectedResponse, actualResponse); 101 102 List<String> actualRequests = mockService.getRequestPaths(); 103 Assert.assertEquals(1, actualRequests.size()); 104 105 String apiClientHeaderKey = 106 mockService 107 .getRequestHeaders() 108 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 109 .iterator() 110 .next(); 111 Assert.assertTrue( 112 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 113 .matcher(apiClientHeaderKey) 114 .matches()); 115 } 116 117 @Test getExceptionTest()118 public void getExceptionTest() throws Exception { 119 ApiException exception = 120 ApiExceptionFactory.createException( 121 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 122 mockService.addException(exception); 123 124 try { 125 String project = "project-6911"; 126 String zone = "zone-5246"; 127 client.get(project, zone); 128 Assert.fail("No exception raised"); 129 } catch (InvalidArgumentException e) { 130 // Expected exception. 131 } 132 } 133 134 @Test listTest()135 public void listTest() throws Exception { 136 Zone responsesElement = Zone.newBuilder().build(); 137 ZoneList expectedResponse = 138 ZoneList.newBuilder() 139 .setNextPageToken("") 140 .addAllItems(Arrays.asList(responsesElement)) 141 .build(); 142 mockService.addResponse(expectedResponse); 143 144 String project = "project-6911"; 145 146 ListPagedResponse pagedListResponse = client.list(project); 147 148 List<Zone> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 149 150 Assert.assertEquals(1, resources.size()); 151 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 152 153 List<String> actualRequests = mockService.getRequestPaths(); 154 Assert.assertEquals(1, actualRequests.size()); 155 156 String apiClientHeaderKey = 157 mockService 158 .getRequestHeaders() 159 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 160 .iterator() 161 .next(); 162 Assert.assertTrue( 163 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 164 .matcher(apiClientHeaderKey) 165 .matches()); 166 } 167 168 @Test listExceptionTest()169 public void listExceptionTest() throws Exception { 170 ApiException exception = 171 ApiExceptionFactory.createException( 172 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 173 mockService.addException(exception); 174 175 try { 176 String project = "project-6911"; 177 client.list(project); 178 Assert.fail("No exception raised"); 179 } catch (InvalidArgumentException e) { 180 // Expected exception. 181 } 182 } 183 } 184