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.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 ChangelogsClientTest { 53 private static MockChangelogs mockChangelogs; 54 private static MockLocations mockLocations; 55 private static MockServiceHelper mockServiceHelper; 56 private LocalChannelProvider channelProvider; 57 private ChangelogsClient client; 58 59 @BeforeClass startStaticServer()60 public static void startStaticServer() { 61 mockChangelogs = new MockChangelogs(); 62 mockLocations = new MockLocations(); 63 mockServiceHelper = 64 new MockServiceHelper( 65 UUID.randomUUID().toString(), 66 Arrays.<MockGrpcService>asList(mockChangelogs, 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 ChangelogsSettings settings = 80 ChangelogsSettings.newBuilder() 81 .setTransportChannelProvider(channelProvider) 82 .setCredentialsProvider(NoCredentialsProvider.create()) 83 .build(); 84 client = ChangelogsClient.create(settings); 85 } 86 87 @After tearDown()88 public void tearDown() throws Exception { 89 client.close(); 90 } 91 92 @Test listChangelogsTest()93 public void listChangelogsTest() throws Exception { 94 Changelog responsesElement = Changelog.newBuilder().build(); 95 ListChangelogsResponse expectedResponse = 96 ListChangelogsResponse.newBuilder() 97 .setNextPageToken("") 98 .addAllChangelogs(Arrays.asList(responsesElement)) 99 .build(); 100 mockChangelogs.addResponse(expectedResponse); 101 102 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 103 104 ListChangelogsPagedResponse pagedListResponse = client.listChangelogs(parent); 105 106 List<Changelog> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 107 108 Assert.assertEquals(1, resources.size()); 109 Assert.assertEquals(expectedResponse.getChangelogsList().get(0), resources.get(0)); 110 111 List<AbstractMessage> actualRequests = mockChangelogs.getRequests(); 112 Assert.assertEquals(1, actualRequests.size()); 113 ListChangelogsRequest actualRequest = ((ListChangelogsRequest) actualRequests.get(0)); 114 115 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 116 Assert.assertTrue( 117 channelProvider.isHeaderSent( 118 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 119 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 120 } 121 122 @Test listChangelogsExceptionTest()123 public void listChangelogsExceptionTest() throws Exception { 124 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 125 mockChangelogs.addException(exception); 126 127 try { 128 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 129 client.listChangelogs(parent); 130 Assert.fail("No exception raised"); 131 } catch (InvalidArgumentException e) { 132 // Expected exception. 133 } 134 } 135 136 @Test listChangelogsTest2()137 public void listChangelogsTest2() throws Exception { 138 Changelog responsesElement = Changelog.newBuilder().build(); 139 ListChangelogsResponse expectedResponse = 140 ListChangelogsResponse.newBuilder() 141 .setNextPageToken("") 142 .addAllChangelogs(Arrays.asList(responsesElement)) 143 .build(); 144 mockChangelogs.addResponse(expectedResponse); 145 146 String parent = "parent-995424086"; 147 148 ListChangelogsPagedResponse pagedListResponse = client.listChangelogs(parent); 149 150 List<Changelog> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 151 152 Assert.assertEquals(1, resources.size()); 153 Assert.assertEquals(expectedResponse.getChangelogsList().get(0), resources.get(0)); 154 155 List<AbstractMessage> actualRequests = mockChangelogs.getRequests(); 156 Assert.assertEquals(1, actualRequests.size()); 157 ListChangelogsRequest actualRequest = ((ListChangelogsRequest) actualRequests.get(0)); 158 159 Assert.assertEquals(parent, actualRequest.getParent()); 160 Assert.assertTrue( 161 channelProvider.isHeaderSent( 162 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 163 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 164 } 165 166 @Test listChangelogsExceptionTest2()167 public void listChangelogsExceptionTest2() throws Exception { 168 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 169 mockChangelogs.addException(exception); 170 171 try { 172 String parent = "parent-995424086"; 173 client.listChangelogs(parent); 174 Assert.fail("No exception raised"); 175 } catch (InvalidArgumentException e) { 176 // Expected exception. 177 } 178 } 179 180 @Test getChangelogTest()181 public void getChangelogTest() throws Exception { 182 Changelog expectedResponse = 183 Changelog.newBuilder() 184 .setName( 185 ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]").toString()) 186 .setUserEmail("userEmail315299473") 187 .setDisplayName("displayName1714148973") 188 .setAction("action-1422950858") 189 .setType("type3575610") 190 .setResource("resource-341064690") 191 .setCreateTime(Timestamp.newBuilder().build()) 192 .build(); 193 mockChangelogs.addResponse(expectedResponse); 194 195 ChangelogName name = ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]"); 196 197 Changelog actualResponse = client.getChangelog(name); 198 Assert.assertEquals(expectedResponse, actualResponse); 199 200 List<AbstractMessage> actualRequests = mockChangelogs.getRequests(); 201 Assert.assertEquals(1, actualRequests.size()); 202 GetChangelogRequest actualRequest = ((GetChangelogRequest) actualRequests.get(0)); 203 204 Assert.assertEquals(name.toString(), actualRequest.getName()); 205 Assert.assertTrue( 206 channelProvider.isHeaderSent( 207 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 208 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 209 } 210 211 @Test getChangelogExceptionTest()212 public void getChangelogExceptionTest() throws Exception { 213 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 214 mockChangelogs.addException(exception); 215 216 try { 217 ChangelogName name = ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]"); 218 client.getChangelog(name); 219 Assert.fail("No exception raised"); 220 } catch (InvalidArgumentException e) { 221 // Expected exception. 222 } 223 } 224 225 @Test getChangelogTest2()226 public void getChangelogTest2() throws Exception { 227 Changelog expectedResponse = 228 Changelog.newBuilder() 229 .setName( 230 ChangelogName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[CHANGELOG]").toString()) 231 .setUserEmail("userEmail315299473") 232 .setDisplayName("displayName1714148973") 233 .setAction("action-1422950858") 234 .setType("type3575610") 235 .setResource("resource-341064690") 236 .setCreateTime(Timestamp.newBuilder().build()) 237 .build(); 238 mockChangelogs.addResponse(expectedResponse); 239 240 String name = "name3373707"; 241 242 Changelog actualResponse = client.getChangelog(name); 243 Assert.assertEquals(expectedResponse, actualResponse); 244 245 List<AbstractMessage> actualRequests = mockChangelogs.getRequests(); 246 Assert.assertEquals(1, actualRequests.size()); 247 GetChangelogRequest actualRequest = ((GetChangelogRequest) actualRequests.get(0)); 248 249 Assert.assertEquals(name, actualRequest.getName()); 250 Assert.assertTrue( 251 channelProvider.isHeaderSent( 252 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 253 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 254 } 255 256 @Test getChangelogExceptionTest2()257 public void getChangelogExceptionTest2() throws Exception { 258 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 259 mockChangelogs.addException(exception); 260 261 try { 262 String name = "name3373707"; 263 client.getChangelog(name); 264 Assert.fail("No exception raised"); 265 } catch (InvalidArgumentException e) { 266 // Expected exception. 267 } 268 } 269 270 @Test listLocationsTest()271 public void listLocationsTest() throws Exception { 272 Location responsesElement = Location.newBuilder().build(); 273 ListLocationsResponse expectedResponse = 274 ListLocationsResponse.newBuilder() 275 .setNextPageToken("") 276 .addAllLocations(Arrays.asList(responsesElement)) 277 .build(); 278 mockLocations.addResponse(expectedResponse); 279 280 ListLocationsRequest request = 281 ListLocationsRequest.newBuilder() 282 .setName("name3373707") 283 .setFilter("filter-1274492040") 284 .setPageSize(883849137) 285 .setPageToken("pageToken873572522") 286 .build(); 287 288 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 289 290 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 291 292 Assert.assertEquals(1, resources.size()); 293 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 294 295 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 296 Assert.assertEquals(1, actualRequests.size()); 297 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 298 299 Assert.assertEquals(request.getName(), actualRequest.getName()); 300 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 301 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 302 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 303 Assert.assertTrue( 304 channelProvider.isHeaderSent( 305 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 306 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 307 } 308 309 @Test listLocationsExceptionTest()310 public void listLocationsExceptionTest() throws Exception { 311 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 312 mockLocations.addException(exception); 313 314 try { 315 ListLocationsRequest request = 316 ListLocationsRequest.newBuilder() 317 .setName("name3373707") 318 .setFilter("filter-1274492040") 319 .setPageSize(883849137) 320 .setPageToken("pageToken873572522") 321 .build(); 322 client.listLocations(request); 323 Assert.fail("No exception raised"); 324 } catch (InvalidArgumentException e) { 325 // Expected exception. 326 } 327 } 328 329 @Test getLocationTest()330 public void getLocationTest() throws Exception { 331 Location expectedResponse = 332 Location.newBuilder() 333 .setName("name3373707") 334 .setLocationId("locationId1541836720") 335 .setDisplayName("displayName1714148973") 336 .putAllLabels(new HashMap<String, String>()) 337 .setMetadata(Any.newBuilder().build()) 338 .build(); 339 mockLocations.addResponse(expectedResponse); 340 341 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 342 343 Location actualResponse = client.getLocation(request); 344 Assert.assertEquals(expectedResponse, actualResponse); 345 346 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 347 Assert.assertEquals(1, actualRequests.size()); 348 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 349 350 Assert.assertEquals(request.getName(), actualRequest.getName()); 351 Assert.assertTrue( 352 channelProvider.isHeaderSent( 353 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 354 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 355 } 356 357 @Test getLocationExceptionTest()358 public void getLocationExceptionTest() throws Exception { 359 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 360 mockLocations.addException(exception); 361 362 try { 363 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 364 client.getLocation(request); 365 Assert.fail("No exception raised"); 366 } catch (InvalidArgumentException e) { 367 // Expected exception. 368 } 369 } 370 } 371