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.ChangelogsClient.ListChangelogsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.ChangelogsClient.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.HttpJsonChangelogsStub; 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 ChangelogsClientHttpJsonTest { 53 private static MockHttpService mockService; 54 private static ChangelogsClient client; 55 56 @BeforeClass startStaticServer()57 public static void startStaticServer() throws IOException { 58 mockService = 59 new MockHttpService( 60 HttpJsonChangelogsStub.getMethodDescriptors(), ChangelogsSettings.getDefaultEndpoint()); 61 ChangelogsSettings settings = 62 ChangelogsSettings.newHttpJsonBuilder() 63 .setTransportChannelProvider( 64 ChangelogsSettings.defaultHttpJsonTransportProviderBuilder() 65 .setHttpTransport(mockService) 66 .build()) 67 .setCredentialsProvider(NoCredentialsProvider.create()) 68 .build(); 69 client = ChangelogsClient.create(settings); 70 } 71 72 @AfterClass stopServer()73 public static void stopServer() { 74 client.close(); 75 } 76 77 @Before setUp()78 public void setUp() {} 79 80 @After tearDown()81 public void tearDown() throws Exception { 82 mockService.reset(); 83 } 84 85 @Test listChangelogsTest()86 public void listChangelogsTest() throws Exception { 87 Changelog responsesElement = Changelog.newBuilder().build(); 88 ListChangelogsResponse expectedResponse = 89 ListChangelogsResponse.newBuilder() 90 .setNextPageToken("") 91 .addAllChangelogs(Arrays.asList(responsesElement)) 92 .build(); 93 mockService.addResponse(expectedResponse); 94 95 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 96 97 ListChangelogsPagedResponse pagedListResponse = client.listChangelogs(parent); 98 99 List<Changelog> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 100 101 Assert.assertEquals(1, resources.size()); 102 Assert.assertEquals(expectedResponse.getChangelogsList().get(0), resources.get(0)); 103 104 List<String> actualRequests = mockService.getRequestPaths(); 105 Assert.assertEquals(1, actualRequests.size()); 106 107 String apiClientHeaderKey = 108 mockService 109 .getRequestHeaders() 110 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 111 .iterator() 112 .next(); 113 Assert.assertTrue( 114 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 115 .matcher(apiClientHeaderKey) 116 .matches()); 117 } 118 119 @Test listChangelogsExceptionTest()120 public void listChangelogsExceptionTest() throws Exception { 121 ApiException exception = 122 ApiExceptionFactory.createException( 123 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 124 mockService.addException(exception); 125 126 try { 127 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 128 client.listChangelogs(parent); 129 Assert.fail("No exception raised"); 130 } catch (InvalidArgumentException e) { 131 // Expected exception. 132 } 133 } 134 135 @Test listChangelogsTest2()136 public void listChangelogsTest2() throws Exception { 137 Changelog responsesElement = Changelog.newBuilder().build(); 138 ListChangelogsResponse expectedResponse = 139 ListChangelogsResponse.newBuilder() 140 .setNextPageToken("") 141 .addAllChangelogs(Arrays.asList(responsesElement)) 142 .build(); 143 mockService.addResponse(expectedResponse); 144 145 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 146 147 ListChangelogsPagedResponse pagedListResponse = client.listChangelogs(parent); 148 149 List<Changelog> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 150 151 Assert.assertEquals(1, resources.size()); 152 Assert.assertEquals(expectedResponse.getChangelogsList().get(0), resources.get(0)); 153 154 List<String> actualRequests = mockService.getRequestPaths(); 155 Assert.assertEquals(1, actualRequests.size()); 156 157 String apiClientHeaderKey = 158 mockService 159 .getRequestHeaders() 160 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 161 .iterator() 162 .next(); 163 Assert.assertTrue( 164 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 165 .matcher(apiClientHeaderKey) 166 .matches()); 167 } 168 169 @Test listChangelogsExceptionTest2()170 public void listChangelogsExceptionTest2() throws Exception { 171 ApiException exception = 172 ApiExceptionFactory.createException( 173 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 174 mockService.addException(exception); 175 176 try { 177 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 178 client.listChangelogs(parent); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test getChangelogTest()186 public void getChangelogTest() throws Exception { 187 Changelog expectedResponse = 188 Changelog.newBuilder() 189 .setName( 190 ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]").toString()) 191 .setUserEmail("userEmail315299473") 192 .setDisplayName("displayName1714148973") 193 .setAction("action-1422950858") 194 .setType("type3575610") 195 .setResource("resource-341064690") 196 .setCreateTime(Timestamp.newBuilder().build()) 197 .build(); 198 mockService.addResponse(expectedResponse); 199 200 ChangelogName name = ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]"); 201 202 Changelog actualResponse = client.getChangelog(name); 203 Assert.assertEquals(expectedResponse, actualResponse); 204 205 List<String> actualRequests = mockService.getRequestPaths(); 206 Assert.assertEquals(1, actualRequests.size()); 207 208 String apiClientHeaderKey = 209 mockService 210 .getRequestHeaders() 211 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 212 .iterator() 213 .next(); 214 Assert.assertTrue( 215 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 216 .matcher(apiClientHeaderKey) 217 .matches()); 218 } 219 220 @Test getChangelogExceptionTest()221 public void getChangelogExceptionTest() throws Exception { 222 ApiException exception = 223 ApiExceptionFactory.createException( 224 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 225 mockService.addException(exception); 226 227 try { 228 ChangelogName name = ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]"); 229 client.getChangelog(name); 230 Assert.fail("No exception raised"); 231 } catch (InvalidArgumentException e) { 232 // Expected exception. 233 } 234 } 235 236 @Test getChangelogTest2()237 public void getChangelogTest2() throws Exception { 238 Changelog expectedResponse = 239 Changelog.newBuilder() 240 .setName( 241 ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]").toString()) 242 .setUserEmail("userEmail315299473") 243 .setDisplayName("displayName1714148973") 244 .setAction("action-1422950858") 245 .setType("type3575610") 246 .setResource("resource-341064690") 247 .setCreateTime(Timestamp.newBuilder().build()) 248 .build(); 249 mockService.addResponse(expectedResponse); 250 251 String name = 252 "projects/project-5621/locations/location-5621/agents/agent-5621/changelogs/changelog-5621"; 253 254 Changelog actualResponse = client.getChangelog(name); 255 Assert.assertEquals(expectedResponse, actualResponse); 256 257 List<String> actualRequests = mockService.getRequestPaths(); 258 Assert.assertEquals(1, actualRequests.size()); 259 260 String apiClientHeaderKey = 261 mockService 262 .getRequestHeaders() 263 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 264 .iterator() 265 .next(); 266 Assert.assertTrue( 267 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 268 .matcher(apiClientHeaderKey) 269 .matches()); 270 } 271 272 @Test getChangelogExceptionTest2()273 public void getChangelogExceptionTest2() throws Exception { 274 ApiException exception = 275 ApiExceptionFactory.createException( 276 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 277 mockService.addException(exception); 278 279 try { 280 String name = 281 "projects/project-5621/locations/location-5621/agents/agent-5621/changelogs/changelog-5621"; 282 client.getChangelog(name); 283 Assert.fail("No exception raised"); 284 } catch (InvalidArgumentException e) { 285 // Expected exception. 286 } 287 } 288 289 @Test listLocationsTest()290 public void listLocationsTest() throws Exception { 291 Location responsesElement = Location.newBuilder().build(); 292 ListLocationsResponse expectedResponse = 293 ListLocationsResponse.newBuilder() 294 .setNextPageToken("") 295 .addAllLocations(Arrays.asList(responsesElement)) 296 .build(); 297 mockService.addResponse(expectedResponse); 298 299 ListLocationsRequest request = 300 ListLocationsRequest.newBuilder() 301 .setName("projects/project-3664") 302 .setFilter("filter-1274492040") 303 .setPageSize(883849137) 304 .setPageToken("pageToken873572522") 305 .build(); 306 307 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 308 309 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 310 311 Assert.assertEquals(1, resources.size()); 312 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 313 314 List<String> actualRequests = mockService.getRequestPaths(); 315 Assert.assertEquals(1, actualRequests.size()); 316 317 String apiClientHeaderKey = 318 mockService 319 .getRequestHeaders() 320 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 321 .iterator() 322 .next(); 323 Assert.assertTrue( 324 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 325 .matcher(apiClientHeaderKey) 326 .matches()); 327 } 328 329 @Test listLocationsExceptionTest()330 public void listLocationsExceptionTest() throws Exception { 331 ApiException exception = 332 ApiExceptionFactory.createException( 333 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 334 mockService.addException(exception); 335 336 try { 337 ListLocationsRequest request = 338 ListLocationsRequest.newBuilder() 339 .setName("projects/project-3664") 340 .setFilter("filter-1274492040") 341 .setPageSize(883849137) 342 .setPageToken("pageToken873572522") 343 .build(); 344 client.listLocations(request); 345 Assert.fail("No exception raised"); 346 } catch (InvalidArgumentException e) { 347 // Expected exception. 348 } 349 } 350 351 @Test getLocationTest()352 public void getLocationTest() throws Exception { 353 Location expectedResponse = 354 Location.newBuilder() 355 .setName("name3373707") 356 .setLocationId("locationId1541836720") 357 .setDisplayName("displayName1714148973") 358 .putAllLabels(new HashMap<String, String>()) 359 .setMetadata(Any.newBuilder().build()) 360 .build(); 361 mockService.addResponse(expectedResponse); 362 363 GetLocationRequest request = 364 GetLocationRequest.newBuilder() 365 .setName("projects/project-9062/locations/location-9062") 366 .build(); 367 368 Location actualResponse = client.getLocation(request); 369 Assert.assertEquals(expectedResponse, actualResponse); 370 371 List<String> actualRequests = mockService.getRequestPaths(); 372 Assert.assertEquals(1, actualRequests.size()); 373 374 String apiClientHeaderKey = 375 mockService 376 .getRequestHeaders() 377 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 378 .iterator() 379 .next(); 380 Assert.assertTrue( 381 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 382 .matcher(apiClientHeaderKey) 383 .matches()); 384 } 385 386 @Test getLocationExceptionTest()387 public void getLocationExceptionTest() throws Exception { 388 ApiException exception = 389 ApiExceptionFactory.createException( 390 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 391 mockService.addException(exception); 392 393 try { 394 GetLocationRequest request = 395 GetLocationRequest.newBuilder() 396 .setName("projects/project-9062/locations/location-9062") 397 .build(); 398 client.getLocation(request); 399 Assert.fail("No exception raised"); 400 } catch (InvalidArgumentException e) { 401 // Expected exception. 402 } 403 } 404 } 405