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.dialogflow.cx.v3; 18 19 import static com.google.cloud.dialogflow.cx.v3.DeploymentsClient.ListDeploymentsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.DeploymentsClient.ListLocationsPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 24 import com.google.api.gax.httpjson.testing.MockHttpService; 25 import com.google.api.gax.rpc.ApiClientHeaderProvider; 26 import com.google.api.gax.rpc.ApiException; 27 import com.google.api.gax.rpc.ApiExceptionFactory; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.StatusCode; 30 import com.google.api.gax.rpc.testing.FakeStatusCode; 31 import com.google.cloud.dialogflow.cx.v3.stub.HttpJsonDeploymentsStub; 32 import com.google.cloud.location.GetLocationRequest; 33 import com.google.cloud.location.ListLocationsRequest; 34 import com.google.cloud.location.ListLocationsResponse; 35 import com.google.cloud.location.Location; 36 import com.google.common.collect.Lists; 37 import com.google.protobuf.Any; 38 import com.google.protobuf.Timestamp; 39 import java.io.IOException; 40 import java.util.Arrays; 41 import java.util.HashMap; 42 import java.util.List; 43 import javax.annotation.Generated; 44 import org.junit.After; 45 import org.junit.AfterClass; 46 import org.junit.Assert; 47 import org.junit.Before; 48 import org.junit.BeforeClass; 49 import org.junit.Test; 50 51 @Generated("by gapic-generator-java") 52 public class DeploymentsClientHttpJsonTest { 53 private static MockHttpService mockService; 54 private static DeploymentsClient client; 55 56 @BeforeClass startStaticServer()57 public static void startStaticServer() throws IOException { 58 mockService = 59 new MockHttpService( 60 HttpJsonDeploymentsStub.getMethodDescriptors(), 61 DeploymentsSettings.getDefaultEndpoint()); 62 DeploymentsSettings settings = 63 DeploymentsSettings.newHttpJsonBuilder() 64 .setTransportChannelProvider( 65 DeploymentsSettings.defaultHttpJsonTransportProviderBuilder() 66 .setHttpTransport(mockService) 67 .build()) 68 .setCredentialsProvider(NoCredentialsProvider.create()) 69 .build(); 70 client = DeploymentsClient.create(settings); 71 } 72 73 @AfterClass stopServer()74 public static void stopServer() { 75 client.close(); 76 } 77 78 @Before setUp()79 public void setUp() {} 80 81 @After tearDown()82 public void tearDown() throws Exception { 83 mockService.reset(); 84 } 85 86 @Test listDeploymentsTest()87 public void listDeploymentsTest() throws Exception { 88 Deployment responsesElement = Deployment.newBuilder().build(); 89 ListDeploymentsResponse expectedResponse = 90 ListDeploymentsResponse.newBuilder() 91 .setNextPageToken("") 92 .addAllDeployments(Arrays.asList(responsesElement)) 93 .build(); 94 mockService.addResponse(expectedResponse); 95 96 EnvironmentName parent = 97 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 98 99 ListDeploymentsPagedResponse pagedListResponse = client.listDeployments(parent); 100 101 List<Deployment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 102 103 Assert.assertEquals(1, resources.size()); 104 Assert.assertEquals(expectedResponse.getDeploymentsList().get(0), resources.get(0)); 105 106 List<String> actualRequests = mockService.getRequestPaths(); 107 Assert.assertEquals(1, actualRequests.size()); 108 109 String apiClientHeaderKey = 110 mockService 111 .getRequestHeaders() 112 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 113 .iterator() 114 .next(); 115 Assert.assertTrue( 116 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 117 .matcher(apiClientHeaderKey) 118 .matches()); 119 } 120 121 @Test listDeploymentsExceptionTest()122 public void listDeploymentsExceptionTest() throws Exception { 123 ApiException exception = 124 ApiExceptionFactory.createException( 125 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 126 mockService.addException(exception); 127 128 try { 129 EnvironmentName parent = 130 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 131 client.listDeployments(parent); 132 Assert.fail("No exception raised"); 133 } catch (InvalidArgumentException e) { 134 // Expected exception. 135 } 136 } 137 138 @Test listDeploymentsTest2()139 public void listDeploymentsTest2() throws Exception { 140 Deployment responsesElement = Deployment.newBuilder().build(); 141 ListDeploymentsResponse expectedResponse = 142 ListDeploymentsResponse.newBuilder() 143 .setNextPageToken("") 144 .addAllDeployments(Arrays.asList(responsesElement)) 145 .build(); 146 mockService.addResponse(expectedResponse); 147 148 String parent = 149 "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835"; 150 151 ListDeploymentsPagedResponse pagedListResponse = client.listDeployments(parent); 152 153 List<Deployment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 154 155 Assert.assertEquals(1, resources.size()); 156 Assert.assertEquals(expectedResponse.getDeploymentsList().get(0), resources.get(0)); 157 158 List<String> actualRequests = mockService.getRequestPaths(); 159 Assert.assertEquals(1, actualRequests.size()); 160 161 String apiClientHeaderKey = 162 mockService 163 .getRequestHeaders() 164 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 165 .iterator() 166 .next(); 167 Assert.assertTrue( 168 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 169 .matcher(apiClientHeaderKey) 170 .matches()); 171 } 172 173 @Test listDeploymentsExceptionTest2()174 public void listDeploymentsExceptionTest2() throws Exception { 175 ApiException exception = 176 ApiExceptionFactory.createException( 177 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 178 mockService.addException(exception); 179 180 try { 181 String parent = 182 "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835"; 183 client.listDeployments(parent); 184 Assert.fail("No exception raised"); 185 } catch (InvalidArgumentException e) { 186 // Expected exception. 187 } 188 } 189 190 @Test getDeploymentTest()191 public void getDeploymentTest() throws Exception { 192 Deployment expectedResponse = 193 Deployment.newBuilder() 194 .setName( 195 DeploymentName.of( 196 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]") 197 .toString()) 198 .setFlowVersion( 199 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 200 .toString()) 201 .setResult(Deployment.Result.newBuilder().build()) 202 .setStartTime(Timestamp.newBuilder().build()) 203 .setEndTime(Timestamp.newBuilder().build()) 204 .build(); 205 mockService.addResponse(expectedResponse); 206 207 DeploymentName name = 208 DeploymentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]"); 209 210 Deployment actualResponse = client.getDeployment(name); 211 Assert.assertEquals(expectedResponse, actualResponse); 212 213 List<String> actualRequests = mockService.getRequestPaths(); 214 Assert.assertEquals(1, actualRequests.size()); 215 216 String apiClientHeaderKey = 217 mockService 218 .getRequestHeaders() 219 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 220 .iterator() 221 .next(); 222 Assert.assertTrue( 223 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 224 .matcher(apiClientHeaderKey) 225 .matches()); 226 } 227 228 @Test getDeploymentExceptionTest()229 public void getDeploymentExceptionTest() throws Exception { 230 ApiException exception = 231 ApiExceptionFactory.createException( 232 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 233 mockService.addException(exception); 234 235 try { 236 DeploymentName name = 237 DeploymentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]"); 238 client.getDeployment(name); 239 Assert.fail("No exception raised"); 240 } catch (InvalidArgumentException e) { 241 // Expected exception. 242 } 243 } 244 245 @Test getDeploymentTest2()246 public void getDeploymentTest2() throws Exception { 247 Deployment expectedResponse = 248 Deployment.newBuilder() 249 .setName( 250 DeploymentName.of( 251 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]") 252 .toString()) 253 .setFlowVersion( 254 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 255 .toString()) 256 .setResult(Deployment.Result.newBuilder().build()) 257 .setStartTime(Timestamp.newBuilder().build()) 258 .setEndTime(Timestamp.newBuilder().build()) 259 .build(); 260 mockService.addResponse(expectedResponse); 261 262 String name = 263 "projects/project-138/locations/location-138/agents/agent-138/environments/environment-138/deployments/deployment-138"; 264 265 Deployment actualResponse = client.getDeployment(name); 266 Assert.assertEquals(expectedResponse, actualResponse); 267 268 List<String> actualRequests = mockService.getRequestPaths(); 269 Assert.assertEquals(1, actualRequests.size()); 270 271 String apiClientHeaderKey = 272 mockService 273 .getRequestHeaders() 274 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 275 .iterator() 276 .next(); 277 Assert.assertTrue( 278 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 279 .matcher(apiClientHeaderKey) 280 .matches()); 281 } 282 283 @Test getDeploymentExceptionTest2()284 public void getDeploymentExceptionTest2() throws Exception { 285 ApiException exception = 286 ApiExceptionFactory.createException( 287 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 288 mockService.addException(exception); 289 290 try { 291 String name = 292 "projects/project-138/locations/location-138/agents/agent-138/environments/environment-138/deployments/deployment-138"; 293 client.getDeployment(name); 294 Assert.fail("No exception raised"); 295 } catch (InvalidArgumentException e) { 296 // Expected exception. 297 } 298 } 299 300 @Test listLocationsTest()301 public void listLocationsTest() throws Exception { 302 Location responsesElement = Location.newBuilder().build(); 303 ListLocationsResponse expectedResponse = 304 ListLocationsResponse.newBuilder() 305 .setNextPageToken("") 306 .addAllLocations(Arrays.asList(responsesElement)) 307 .build(); 308 mockService.addResponse(expectedResponse); 309 310 ListLocationsRequest request = 311 ListLocationsRequest.newBuilder() 312 .setName("projects/project-3664") 313 .setFilter("filter-1274492040") 314 .setPageSize(883849137) 315 .setPageToken("pageToken873572522") 316 .build(); 317 318 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 319 320 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 321 322 Assert.assertEquals(1, resources.size()); 323 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 324 325 List<String> actualRequests = mockService.getRequestPaths(); 326 Assert.assertEquals(1, actualRequests.size()); 327 328 String apiClientHeaderKey = 329 mockService 330 .getRequestHeaders() 331 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 332 .iterator() 333 .next(); 334 Assert.assertTrue( 335 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 336 .matcher(apiClientHeaderKey) 337 .matches()); 338 } 339 340 @Test listLocationsExceptionTest()341 public void listLocationsExceptionTest() throws Exception { 342 ApiException exception = 343 ApiExceptionFactory.createException( 344 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 345 mockService.addException(exception); 346 347 try { 348 ListLocationsRequest request = 349 ListLocationsRequest.newBuilder() 350 .setName("projects/project-3664") 351 .setFilter("filter-1274492040") 352 .setPageSize(883849137) 353 .setPageToken("pageToken873572522") 354 .build(); 355 client.listLocations(request); 356 Assert.fail("No exception raised"); 357 } catch (InvalidArgumentException e) { 358 // Expected exception. 359 } 360 } 361 362 @Test getLocationTest()363 public void getLocationTest() throws Exception { 364 Location expectedResponse = 365 Location.newBuilder() 366 .setName("name3373707") 367 .setLocationId("locationId1541836720") 368 .setDisplayName("displayName1714148973") 369 .putAllLabels(new HashMap<String, String>()) 370 .setMetadata(Any.newBuilder().build()) 371 .build(); 372 mockService.addResponse(expectedResponse); 373 374 GetLocationRequest request = 375 GetLocationRequest.newBuilder() 376 .setName("projects/project-9062/locations/location-9062") 377 .build(); 378 379 Location actualResponse = client.getLocation(request); 380 Assert.assertEquals(expectedResponse, actualResponse); 381 382 List<String> actualRequests = mockService.getRequestPaths(); 383 Assert.assertEquals(1, actualRequests.size()); 384 385 String apiClientHeaderKey = 386 mockService 387 .getRequestHeaders() 388 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 389 .iterator() 390 .next(); 391 Assert.assertTrue( 392 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 393 .matcher(apiClientHeaderKey) 394 .matches()); 395 } 396 397 @Test getLocationExceptionTest()398 public void getLocationExceptionTest() throws Exception { 399 ApiException exception = 400 ApiExceptionFactory.createException( 401 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 402 mockService.addException(exception); 403 404 try { 405 GetLocationRequest request = 406 GetLocationRequest.newBuilder() 407 .setName("projects/project-9062/locations/location-9062") 408 .build(); 409 client.getLocation(request); 410 Assert.fail("No exception raised"); 411 } catch (InvalidArgumentException e) { 412 // Expected exception. 413 } 414 } 415 } 416