|
15 | 15 | */ |
16 | 16 | package com.google.cloud.recaptchaenterprise.v1beta1; |
17 | 17 |
|
| 18 | +import static com.google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client.ListKeysPagedResponse; |
| 19 | + |
18 | 20 | import com.google.api.gax.core.NoCredentialsProvider; |
19 | 21 | import com.google.api.gax.grpc.GaxGrpcProperties; |
20 | 22 | import com.google.api.gax.grpc.testing.LocalChannelProvider; |
21 | 23 | import com.google.api.gax.grpc.testing.MockGrpcService; |
22 | 24 | import com.google.api.gax.grpc.testing.MockServiceHelper; |
23 | 25 | import com.google.api.gax.rpc.ApiClientHeaderProvider; |
24 | 26 | import com.google.api.gax.rpc.InvalidArgumentException; |
| 27 | +import com.google.common.collect.Lists; |
25 | 28 | import com.google.protobuf.AbstractMessage; |
| 29 | +import com.google.protobuf.Empty; |
26 | 30 | import com.google.recaptchaenterprise.v1beta1.AnnotateAssessmentRequest; |
27 | 31 | import com.google.recaptchaenterprise.v1beta1.AnnotateAssessmentResponse; |
28 | 32 | import com.google.recaptchaenterprise.v1beta1.Assessment; |
29 | 33 | import com.google.recaptchaenterprise.v1beta1.AssessmentName; |
30 | 34 | import com.google.recaptchaenterprise.v1beta1.CreateAssessmentRequest; |
| 35 | +import com.google.recaptchaenterprise.v1beta1.CreateKeyRequest; |
| 36 | +import com.google.recaptchaenterprise.v1beta1.DeleteKeyRequest; |
| 37 | +import com.google.recaptchaenterprise.v1beta1.GetKeyRequest; |
| 38 | +import com.google.recaptchaenterprise.v1beta1.Key; |
| 39 | +import com.google.recaptchaenterprise.v1beta1.KeyName; |
| 40 | +import com.google.recaptchaenterprise.v1beta1.ListKeysRequest; |
| 41 | +import com.google.recaptchaenterprise.v1beta1.ListKeysResponse; |
31 | 42 | import com.google.recaptchaenterprise.v1beta1.ProjectName; |
| 43 | +import com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest; |
32 | 44 | import io.grpc.Status; |
33 | 45 | import io.grpc.StatusRuntimeException; |
34 | 46 | import java.io.IOException; |
@@ -167,4 +179,224 @@ public void annotateAssessmentExceptionTest() throws Exception { |
167 | 179 | // Expected exception |
168 | 180 | } |
169 | 181 | } |
| 182 | + |
| 183 | + @Test |
| 184 | + @SuppressWarnings("all") |
| 185 | + public void createKeyTest() { |
| 186 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 187 | + String displayName = "displayName1615086568"; |
| 188 | + Key expectedResponse = |
| 189 | + Key.newBuilder().setName(name.toString()).setDisplayName(displayName).build(); |
| 190 | + mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse); |
| 191 | + |
| 192 | + ProjectName parent = ProjectName.of("[PROJECT]"); |
| 193 | + Key key = Key.newBuilder().build(); |
| 194 | + CreateKeyRequest request = |
| 195 | + CreateKeyRequest.newBuilder().setParent(parent.toString()).setKey(key).build(); |
| 196 | + |
| 197 | + Key actualResponse = client.createKey(request); |
| 198 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 199 | + |
| 200 | + List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests(); |
| 201 | + Assert.assertEquals(1, actualRequests.size()); |
| 202 | + CreateKeyRequest actualRequest = (CreateKeyRequest) actualRequests.get(0); |
| 203 | + |
| 204 | + Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent())); |
| 205 | + Assert.assertEquals(key, actualRequest.getKey()); |
| 206 | + Assert.assertTrue( |
| 207 | + channelProvider.isHeaderSent( |
| 208 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 209 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + @SuppressWarnings("all") |
| 214 | + public void createKeyExceptionTest() throws Exception { |
| 215 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 216 | + mockRecaptchaEnterpriseServiceV1Beta1.addException(exception); |
| 217 | + |
| 218 | + try { |
| 219 | + ProjectName parent = ProjectName.of("[PROJECT]"); |
| 220 | + Key key = Key.newBuilder().build(); |
| 221 | + CreateKeyRequest request = |
| 222 | + CreateKeyRequest.newBuilder().setParent(parent.toString()).setKey(key).build(); |
| 223 | + |
| 224 | + client.createKey(request); |
| 225 | + Assert.fail("No exception raised"); |
| 226 | + } catch (InvalidArgumentException e) { |
| 227 | + // Expected exception |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + @SuppressWarnings("all") |
| 233 | + public void listKeysTest() { |
| 234 | + String nextPageToken = ""; |
| 235 | + Key keysElement = Key.newBuilder().build(); |
| 236 | + List<Key> keys = Arrays.asList(keysElement); |
| 237 | + ListKeysResponse expectedResponse = |
| 238 | + ListKeysResponse.newBuilder().setNextPageToken(nextPageToken).addAllKeys(keys).build(); |
| 239 | + mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse); |
| 240 | + |
| 241 | + ProjectName parent = ProjectName.of("[PROJECT]"); |
| 242 | + ListKeysRequest request = ListKeysRequest.newBuilder().setParent(parent.toString()).build(); |
| 243 | + |
| 244 | + ListKeysPagedResponse pagedListResponse = client.listKeys(request); |
| 245 | + |
| 246 | + List<Key> resources = Lists.newArrayList(pagedListResponse.iterateAll()); |
| 247 | + Assert.assertEquals(1, resources.size()); |
| 248 | + Assert.assertEquals(expectedResponse.getKeysList().get(0), resources.get(0)); |
| 249 | + |
| 250 | + List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests(); |
| 251 | + Assert.assertEquals(1, actualRequests.size()); |
| 252 | + ListKeysRequest actualRequest = (ListKeysRequest) actualRequests.get(0); |
| 253 | + |
| 254 | + Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent())); |
| 255 | + Assert.assertTrue( |
| 256 | + channelProvider.isHeaderSent( |
| 257 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 258 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + @SuppressWarnings("all") |
| 263 | + public void listKeysExceptionTest() throws Exception { |
| 264 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 265 | + mockRecaptchaEnterpriseServiceV1Beta1.addException(exception); |
| 266 | + |
| 267 | + try { |
| 268 | + ProjectName parent = ProjectName.of("[PROJECT]"); |
| 269 | + ListKeysRequest request = ListKeysRequest.newBuilder().setParent(parent.toString()).build(); |
| 270 | + |
| 271 | + client.listKeys(request); |
| 272 | + Assert.fail("No exception raised"); |
| 273 | + } catch (InvalidArgumentException e) { |
| 274 | + // Expected exception |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + @SuppressWarnings("all") |
| 280 | + public void getKeyTest() { |
| 281 | + KeyName name2 = KeyName.of("[PROJECT]", "[KEY]"); |
| 282 | + String displayName = "displayName1615086568"; |
| 283 | + Key expectedResponse = |
| 284 | + Key.newBuilder().setName(name2.toString()).setDisplayName(displayName).build(); |
| 285 | + mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse); |
| 286 | + |
| 287 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 288 | + GetKeyRequest request = GetKeyRequest.newBuilder().setName(name.toString()).build(); |
| 289 | + |
| 290 | + Key actualResponse = client.getKey(request); |
| 291 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 292 | + |
| 293 | + List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests(); |
| 294 | + Assert.assertEquals(1, actualRequests.size()); |
| 295 | + GetKeyRequest actualRequest = (GetKeyRequest) actualRequests.get(0); |
| 296 | + |
| 297 | + Assert.assertEquals(name, KeyName.parse(actualRequest.getName())); |
| 298 | + Assert.assertTrue( |
| 299 | + channelProvider.isHeaderSent( |
| 300 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 301 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 302 | + } |
| 303 | + |
| 304 | + @Test |
| 305 | + @SuppressWarnings("all") |
| 306 | + public void getKeyExceptionTest() throws Exception { |
| 307 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 308 | + mockRecaptchaEnterpriseServiceV1Beta1.addException(exception); |
| 309 | + |
| 310 | + try { |
| 311 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 312 | + GetKeyRequest request = GetKeyRequest.newBuilder().setName(name.toString()).build(); |
| 313 | + |
| 314 | + client.getKey(request); |
| 315 | + Assert.fail("No exception raised"); |
| 316 | + } catch (InvalidArgumentException e) { |
| 317 | + // Expected exception |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + @Test |
| 322 | + @SuppressWarnings("all") |
| 323 | + public void updateKeyTest() { |
| 324 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 325 | + String displayName = "displayName1615086568"; |
| 326 | + Key expectedResponse = |
| 327 | + Key.newBuilder().setName(name.toString()).setDisplayName(displayName).build(); |
| 328 | + mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse); |
| 329 | + |
| 330 | + Key key = Key.newBuilder().build(); |
| 331 | + UpdateKeyRequest request = UpdateKeyRequest.newBuilder().setKey(key).build(); |
| 332 | + |
| 333 | + Key actualResponse = client.updateKey(request); |
| 334 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 335 | + |
| 336 | + List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests(); |
| 337 | + Assert.assertEquals(1, actualRequests.size()); |
| 338 | + UpdateKeyRequest actualRequest = (UpdateKeyRequest) actualRequests.get(0); |
| 339 | + |
| 340 | + Assert.assertEquals(key, actualRequest.getKey()); |
| 341 | + Assert.assertTrue( |
| 342 | + channelProvider.isHeaderSent( |
| 343 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 344 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + @SuppressWarnings("all") |
| 349 | + public void updateKeyExceptionTest() throws Exception { |
| 350 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 351 | + mockRecaptchaEnterpriseServiceV1Beta1.addException(exception); |
| 352 | + |
| 353 | + try { |
| 354 | + Key key = Key.newBuilder().build(); |
| 355 | + UpdateKeyRequest request = UpdateKeyRequest.newBuilder().setKey(key).build(); |
| 356 | + |
| 357 | + client.updateKey(request); |
| 358 | + Assert.fail("No exception raised"); |
| 359 | + } catch (InvalidArgumentException e) { |
| 360 | + // Expected exception |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + @Test |
| 365 | + @SuppressWarnings("all") |
| 366 | + public void deleteKeyTest() { |
| 367 | + Empty expectedResponse = Empty.newBuilder().build(); |
| 368 | + mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse); |
| 369 | + |
| 370 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 371 | + DeleteKeyRequest request = DeleteKeyRequest.newBuilder().setName(name.toString()).build(); |
| 372 | + |
| 373 | + client.deleteKey(request); |
| 374 | + |
| 375 | + List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests(); |
| 376 | + Assert.assertEquals(1, actualRequests.size()); |
| 377 | + DeleteKeyRequest actualRequest = (DeleteKeyRequest) actualRequests.get(0); |
| 378 | + |
| 379 | + Assert.assertEquals(name, KeyName.parse(actualRequest.getName())); |
| 380 | + Assert.assertTrue( |
| 381 | + channelProvider.isHeaderSent( |
| 382 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 383 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 384 | + } |
| 385 | + |
| 386 | + @Test |
| 387 | + @SuppressWarnings("all") |
| 388 | + public void deleteKeyExceptionTest() throws Exception { |
| 389 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 390 | + mockRecaptchaEnterpriseServiceV1Beta1.addException(exception); |
| 391 | + |
| 392 | + try { |
| 393 | + KeyName name = KeyName.of("[PROJECT]", "[KEY]"); |
| 394 | + DeleteKeyRequest request = DeleteKeyRequest.newBuilder().setName(name.toString()).build(); |
| 395 | + |
| 396 | + client.deleteKey(request); |
| 397 | + Assert.fail("No exception raised"); |
| 398 | + } catch (InvalidArgumentException e) { |
| 399 | + // Expected exception |
| 400 | + } |
| 401 | + } |
170 | 402 | } |
0 commit comments