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.InterconnectLocationsClient.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.HttpJsonInterconnectLocationsStub; 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 InterconnectLocationsClientTest { 46 private static MockHttpService mockService; 47 private static InterconnectLocationsClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonInterconnectLocationsStub.getMethodDescriptors(), 54 InterconnectLocationsSettings.getDefaultEndpoint()); 55 InterconnectLocationsSettings settings = 56 InterconnectLocationsSettings.newBuilder() 57 .setTransportChannelProvider( 58 InterconnectLocationsSettings.defaultHttpJsonTransportProviderBuilder() 59 .setHttpTransport(mockService) 60 .build()) 61 .setCredentialsProvider(NoCredentialsProvider.create()) 62 .build(); 63 client = InterconnectLocationsClient.create(settings); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 client.close(); 69 } 70 71 @Before setUp()72 public void setUp() {} 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 mockService.reset(); 77 } 78 79 @Test getTest()80 public void getTest() throws Exception { 81 InterconnectLocation expectedResponse = 82 InterconnectLocation.newBuilder() 83 .setAddress("address-1147692044") 84 .setAvailabilityZone("availabilityZone-1674934361") 85 .setCity("city3053931") 86 .setContinent("continent-403427916") 87 .setCreationTimestamp("creationTimestamp-370203401") 88 .setDescription("description-1724546052") 89 .setFacilityProvider("facilityProvider-132020812") 90 .setFacilityProviderFacilityId("facilityProviderFacilityId459025394") 91 .setId(3355) 92 .setKind("kind3292052") 93 .setName("name3373707") 94 .setPeeringdbFacilityId("peeringdbFacilityId1154838908") 95 .addAllRegionInfos(new ArrayList<InterconnectLocationRegionInfo>()) 96 .setSelfLink("selfLink1191800166") 97 .setStatus("status-892481550") 98 .setSupportsPzs(true) 99 .build(); 100 mockService.addResponse(expectedResponse); 101 102 String project = "project-6911"; 103 String interconnectLocation = "interconnectLocation-8857"; 104 105 InterconnectLocation actualResponse = client.get(project, interconnectLocation); 106 Assert.assertEquals(expectedResponse, actualResponse); 107 108 List<String> actualRequests = mockService.getRequestPaths(); 109 Assert.assertEquals(1, actualRequests.size()); 110 111 String apiClientHeaderKey = 112 mockService 113 .getRequestHeaders() 114 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 115 .iterator() 116 .next(); 117 Assert.assertTrue( 118 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 119 .matcher(apiClientHeaderKey) 120 .matches()); 121 } 122 123 @Test getExceptionTest()124 public void getExceptionTest() throws Exception { 125 ApiException exception = 126 ApiExceptionFactory.createException( 127 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 128 mockService.addException(exception); 129 130 try { 131 String project = "project-6911"; 132 String interconnectLocation = "interconnectLocation-8857"; 133 client.get(project, interconnectLocation); 134 Assert.fail("No exception raised"); 135 } catch (InvalidArgumentException e) { 136 // Expected exception. 137 } 138 } 139 140 @Test listTest()141 public void listTest() throws Exception { 142 InterconnectLocation responsesElement = InterconnectLocation.newBuilder().build(); 143 InterconnectLocationList expectedResponse = 144 InterconnectLocationList.newBuilder() 145 .setNextPageToken("") 146 .addAllItems(Arrays.asList(responsesElement)) 147 .build(); 148 mockService.addResponse(expectedResponse); 149 150 String project = "project-6911"; 151 152 ListPagedResponse pagedListResponse = client.list(project); 153 154 List<InterconnectLocation> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 155 156 Assert.assertEquals(1, resources.size()); 157 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 158 159 List<String> actualRequests = mockService.getRequestPaths(); 160 Assert.assertEquals(1, actualRequests.size()); 161 162 String apiClientHeaderKey = 163 mockService 164 .getRequestHeaders() 165 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 166 .iterator() 167 .next(); 168 Assert.assertTrue( 169 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 170 .matcher(apiClientHeaderKey) 171 .matches()); 172 } 173 174 @Test listExceptionTest()175 public void listExceptionTest() throws Exception { 176 ApiException exception = 177 ApiExceptionFactory.createException( 178 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 179 mockService.addException(exception); 180 181 try { 182 String project = "project-6911"; 183 client.list(project); 184 Assert.fail("No exception raised"); 185 } catch (InvalidArgumentException e) { 186 // Expected exception. 187 } 188 } 189 } 190