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.grpc.GaxGrpcProperties; 24 import com.google.api.gax.grpc.testing.LocalChannelProvider; 25 import com.google.api.gax.grpc.testing.MockGrpcService; 26 import com.google.api.gax.grpc.testing.MockServiceHelper; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.cloud.location.GetLocationRequest; 30 import com.google.cloud.location.ListLocationsRequest; 31 import com.google.cloud.location.ListLocationsResponse; 32 import com.google.cloud.location.Location; 33 import com.google.common.collect.Lists; 34 import com.google.protobuf.AbstractMessage; 35 import com.google.protobuf.Any; 36 import com.google.protobuf.Timestamp; 37 import io.grpc.StatusRuntimeException; 38 import java.io.IOException; 39 import java.util.Arrays; 40 import java.util.HashMap; 41 import java.util.List; 42 import java.util.UUID; 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 DeploymentsClientTest { 53 private static MockDeployments mockDeployments; 54 private static MockLocations mockLocations; 55 private static MockServiceHelper mockServiceHelper; 56 private LocalChannelProvider channelProvider; 57 private DeploymentsClient client; 58 59 @BeforeClass startStaticServer()60 public static void startStaticServer() { 61 mockDeployments = new MockDeployments(); 62 mockLocations = new MockLocations(); 63 mockServiceHelper = 64 new MockServiceHelper( 65 UUID.randomUUID().toString(), 66 Arrays.<MockGrpcService>asList(mockDeployments, mockLocations)); 67 mockServiceHelper.start(); 68 } 69 70 @AfterClass stopServer()71 public static void stopServer() { 72 mockServiceHelper.stop(); 73 } 74 75 @Before setUp()76 public void setUp() throws IOException { 77 mockServiceHelper.reset(); 78 channelProvider = mockServiceHelper.createChannelProvider(); 79 DeploymentsSettings settings = 80 DeploymentsSettings.newBuilder() 81 .setTransportChannelProvider(channelProvider) 82 .setCredentialsProvider(NoCredentialsProvider.create()) 83 .build(); 84 client = DeploymentsClient.create(settings); 85 } 86 87 @After tearDown()88 public void tearDown() throws Exception { 89 client.close(); 90 } 91 92 @Test listDeploymentsTest()93 public void listDeploymentsTest() throws Exception { 94 Deployment responsesElement = Deployment.newBuilder().build(); 95 ListDeploymentsResponse expectedResponse = 96 ListDeploymentsResponse.newBuilder() 97 .setNextPageToken("") 98 .addAllDeployments(Arrays.asList(responsesElement)) 99 .build(); 100 mockDeployments.addResponse(expectedResponse); 101 102 EnvironmentName parent = 103 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 104 105 ListDeploymentsPagedResponse pagedListResponse = client.listDeployments(parent); 106 107 List<Deployment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 108 109 Assert.assertEquals(1, resources.size()); 110 Assert.assertEquals(expectedResponse.getDeploymentsList().get(0), resources.get(0)); 111 112 List<AbstractMessage> actualRequests = mockDeployments.getRequests(); 113 Assert.assertEquals(1, actualRequests.size()); 114 ListDeploymentsRequest actualRequest = ((ListDeploymentsRequest) actualRequests.get(0)); 115 116 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 117 Assert.assertTrue( 118 channelProvider.isHeaderSent( 119 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 120 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 121 } 122 123 @Test listDeploymentsExceptionTest()124 public void listDeploymentsExceptionTest() throws Exception { 125 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 126 mockDeployments.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 mockDeployments.addResponse(expectedResponse); 147 148 String parent = "parent-995424086"; 149 150 ListDeploymentsPagedResponse pagedListResponse = client.listDeployments(parent); 151 152 List<Deployment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 153 154 Assert.assertEquals(1, resources.size()); 155 Assert.assertEquals(expectedResponse.getDeploymentsList().get(0), resources.get(0)); 156 157 List<AbstractMessage> actualRequests = mockDeployments.getRequests(); 158 Assert.assertEquals(1, actualRequests.size()); 159 ListDeploymentsRequest actualRequest = ((ListDeploymentsRequest) actualRequests.get(0)); 160 161 Assert.assertEquals(parent, actualRequest.getParent()); 162 Assert.assertTrue( 163 channelProvider.isHeaderSent( 164 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 165 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 166 } 167 168 @Test listDeploymentsExceptionTest2()169 public void listDeploymentsExceptionTest2() throws Exception { 170 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 171 mockDeployments.addException(exception); 172 173 try { 174 String parent = "parent-995424086"; 175 client.listDeployments(parent); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 182 @Test getDeploymentTest()183 public void getDeploymentTest() throws Exception { 184 Deployment expectedResponse = 185 Deployment.newBuilder() 186 .setName( 187 DeploymentName.of( 188 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]") 189 .toString()) 190 .setFlowVersion( 191 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 192 .toString()) 193 .setResult(Deployment.Result.newBuilder().build()) 194 .setStartTime(Timestamp.newBuilder().build()) 195 .setEndTime(Timestamp.newBuilder().build()) 196 .build(); 197 mockDeployments.addResponse(expectedResponse); 198 199 DeploymentName name = 200 DeploymentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]"); 201 202 Deployment actualResponse = client.getDeployment(name); 203 Assert.assertEquals(expectedResponse, actualResponse); 204 205 List<AbstractMessage> actualRequests = mockDeployments.getRequests(); 206 Assert.assertEquals(1, actualRequests.size()); 207 GetDeploymentRequest actualRequest = ((GetDeploymentRequest) actualRequests.get(0)); 208 209 Assert.assertEquals(name.toString(), actualRequest.getName()); 210 Assert.assertTrue( 211 channelProvider.isHeaderSent( 212 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 213 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 214 } 215 216 @Test getDeploymentExceptionTest()217 public void getDeploymentExceptionTest() throws Exception { 218 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 219 mockDeployments.addException(exception); 220 221 try { 222 DeploymentName name = 223 DeploymentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]"); 224 client.getDeployment(name); 225 Assert.fail("No exception raised"); 226 } catch (InvalidArgumentException e) { 227 // Expected exception. 228 } 229 } 230 231 @Test getDeploymentTest2()232 public void getDeploymentTest2() throws Exception { 233 Deployment expectedResponse = 234 Deployment.newBuilder() 235 .setName( 236 DeploymentName.of( 237 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[DEPLOYMENT]") 238 .toString()) 239 .setFlowVersion( 240 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 241 .toString()) 242 .setResult(Deployment.Result.newBuilder().build()) 243 .setStartTime(Timestamp.newBuilder().build()) 244 .setEndTime(Timestamp.newBuilder().build()) 245 .build(); 246 mockDeployments.addResponse(expectedResponse); 247 248 String name = "name3373707"; 249 250 Deployment actualResponse = client.getDeployment(name); 251 Assert.assertEquals(expectedResponse, actualResponse); 252 253 List<AbstractMessage> actualRequests = mockDeployments.getRequests(); 254 Assert.assertEquals(1, actualRequests.size()); 255 GetDeploymentRequest actualRequest = ((GetDeploymentRequest) actualRequests.get(0)); 256 257 Assert.assertEquals(name, actualRequest.getName()); 258 Assert.assertTrue( 259 channelProvider.isHeaderSent( 260 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 261 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 262 } 263 264 @Test getDeploymentExceptionTest2()265 public void getDeploymentExceptionTest2() throws Exception { 266 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 267 mockDeployments.addException(exception); 268 269 try { 270 String name = "name3373707"; 271 client.getDeployment(name); 272 Assert.fail("No exception raised"); 273 } catch (InvalidArgumentException e) { 274 // Expected exception. 275 } 276 } 277 278 @Test listLocationsTest()279 public void listLocationsTest() throws Exception { 280 Location responsesElement = Location.newBuilder().build(); 281 ListLocationsResponse expectedResponse = 282 ListLocationsResponse.newBuilder() 283 .setNextPageToken("") 284 .addAllLocations(Arrays.asList(responsesElement)) 285 .build(); 286 mockLocations.addResponse(expectedResponse); 287 288 ListLocationsRequest request = 289 ListLocationsRequest.newBuilder() 290 .setName("name3373707") 291 .setFilter("filter-1274492040") 292 .setPageSize(883849137) 293 .setPageToken("pageToken873572522") 294 .build(); 295 296 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 297 298 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 299 300 Assert.assertEquals(1, resources.size()); 301 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 302 303 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 304 Assert.assertEquals(1, actualRequests.size()); 305 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 306 307 Assert.assertEquals(request.getName(), actualRequest.getName()); 308 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 309 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 310 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 311 Assert.assertTrue( 312 channelProvider.isHeaderSent( 313 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 314 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 315 } 316 317 @Test listLocationsExceptionTest()318 public void listLocationsExceptionTest() throws Exception { 319 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 320 mockLocations.addException(exception); 321 322 try { 323 ListLocationsRequest request = 324 ListLocationsRequest.newBuilder() 325 .setName("name3373707") 326 .setFilter("filter-1274492040") 327 .setPageSize(883849137) 328 .setPageToken("pageToken873572522") 329 .build(); 330 client.listLocations(request); 331 Assert.fail("No exception raised"); 332 } catch (InvalidArgumentException e) { 333 // Expected exception. 334 } 335 } 336 337 @Test getLocationTest()338 public void getLocationTest() throws Exception { 339 Location expectedResponse = 340 Location.newBuilder() 341 .setName("name3373707") 342 .setLocationId("locationId1541836720") 343 .setDisplayName("displayName1714148973") 344 .putAllLabels(new HashMap<String, String>()) 345 .setMetadata(Any.newBuilder().build()) 346 .build(); 347 mockLocations.addResponse(expectedResponse); 348 349 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 350 351 Location actualResponse = client.getLocation(request); 352 Assert.assertEquals(expectedResponse, actualResponse); 353 354 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 355 Assert.assertEquals(1, actualRequests.size()); 356 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 357 358 Assert.assertEquals(request.getName(), actualRequest.getName()); 359 Assert.assertTrue( 360 channelProvider.isHeaderSent( 361 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 362 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 363 } 364 365 @Test getLocationExceptionTest()366 public void getLocationExceptionTest() throws Exception { 367 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 368 mockLocations.addException(exception); 369 370 try { 371 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 372 client.getLocation(request); 373 Assert.fail("No exception raised"); 374 } catch (InvalidArgumentException e) { 375 // Expected exception. 376 } 377 } 378 } 379