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.RegionDiskTypesClient.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.HttpJsonRegionDiskTypesStub; 31 import com.google.common.collect.Lists; 32 import java.io.IOException; 33 import java.util.Arrays; 34 import java.util.List; 35 import javax.annotation.Generated; 36 import org.junit.After; 37 import org.junit.AfterClass; 38 import org.junit.Assert; 39 import org.junit.Before; 40 import org.junit.BeforeClass; 41 import org.junit.Test; 42 43 @Generated("by gapic-generator-java") 44 public class RegionDiskTypesClientTest { 45 private static MockHttpService mockService; 46 private static RegionDiskTypesClient client; 47 48 @BeforeClass startStaticServer()49 public static void startStaticServer() throws IOException { 50 mockService = 51 new MockHttpService( 52 HttpJsonRegionDiskTypesStub.getMethodDescriptors(), 53 RegionDiskTypesSettings.getDefaultEndpoint()); 54 RegionDiskTypesSettings settings = 55 RegionDiskTypesSettings.newBuilder() 56 .setTransportChannelProvider( 57 RegionDiskTypesSettings.defaultHttpJsonTransportProviderBuilder() 58 .setHttpTransport(mockService) 59 .build()) 60 .setCredentialsProvider(NoCredentialsProvider.create()) 61 .build(); 62 client = RegionDiskTypesClient.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 DiskType expectedResponse = 81 DiskType.newBuilder() 82 .setCreationTimestamp("creationTimestamp-370203401") 83 .setDefaultDiskSizeGb(807490165) 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 .setValidDiskSize("validDiskSize-594443430") 92 .setZone("zone3744684") 93 .build(); 94 mockService.addResponse(expectedResponse); 95 96 String project = "project-6911"; 97 String region = "region-9622"; 98 String diskType = "diskType-227"; 99 100 DiskType actualResponse = client.get(project, region, diskType); 101 Assert.assertEquals(expectedResponse, actualResponse); 102 103 List<String> actualRequests = mockService.getRequestPaths(); 104 Assert.assertEquals(1, actualRequests.size()); 105 106 String apiClientHeaderKey = 107 mockService 108 .getRequestHeaders() 109 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 110 .iterator() 111 .next(); 112 Assert.assertTrue( 113 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 114 .matcher(apiClientHeaderKey) 115 .matches()); 116 } 117 118 @Test getExceptionTest()119 public void getExceptionTest() throws Exception { 120 ApiException exception = 121 ApiExceptionFactory.createException( 122 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 123 mockService.addException(exception); 124 125 try { 126 String project = "project-6911"; 127 String region = "region-9622"; 128 String diskType = "diskType-227"; 129 client.get(project, region, diskType); 130 Assert.fail("No exception raised"); 131 } catch (InvalidArgumentException e) { 132 // Expected exception. 133 } 134 } 135 136 @Test listTest()137 public void listTest() throws Exception { 138 DiskType responsesElement = DiskType.newBuilder().build(); 139 RegionDiskTypeList expectedResponse = 140 RegionDiskTypeList.newBuilder() 141 .setNextPageToken("") 142 .addAllItems(Arrays.asList(responsesElement)) 143 .build(); 144 mockService.addResponse(expectedResponse); 145 146 String project = "project-6911"; 147 String region = "region-9622"; 148 149 ListPagedResponse pagedListResponse = client.list(project, region); 150 151 List<DiskType> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 155 156 List<String> actualRequests = mockService.getRequestPaths(); 157 Assert.assertEquals(1, actualRequests.size()); 158 159 String apiClientHeaderKey = 160 mockService 161 .getRequestHeaders() 162 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 163 .iterator() 164 .next(); 165 Assert.assertTrue( 166 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 167 .matcher(apiClientHeaderKey) 168 .matches()); 169 } 170 171 @Test listExceptionTest()172 public void listExceptionTest() throws Exception { 173 ApiException exception = 174 ApiExceptionFactory.createException( 175 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 176 mockService.addException(exception); 177 178 try { 179 String project = "project-6911"; 180 String region = "region-9622"; 181 client.list(project, region); 182 Assert.fail("No exception raised"); 183 } catch (InvalidArgumentException e) { 184 // Expected exception. 185 } 186 } 187 } 188