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.protos.google.cloud.binaryauthorization.v1; 18 19 import com.google.api.gax.core.NoCredentialsProvider; 20 import com.google.api.gax.grpc.GaxGrpcProperties; 21 import com.google.api.gax.grpc.testing.LocalChannelProvider; 22 import com.google.api.gax.grpc.testing.MockGrpcService; 23 import com.google.api.gax.grpc.testing.MockServiceHelper; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.InvalidArgumentException; 26 import com.google.protobuf.AbstractMessage; 27 import io.grafeas.v1.AttestationOccurrence; 28 import io.grpc.StatusRuntimeException; 29 import java.io.IOException; 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.UUID; 33 import javax.annotation.Generated; 34 import org.junit.After; 35 import org.junit.AfterClass; 36 import org.junit.Assert; 37 import org.junit.Before; 38 import org.junit.BeforeClass; 39 import org.junit.Test; 40 41 @Generated("by gapic-generator-java") 42 public class ValidationHelperV1ClientTest { 43 private static MockServiceHelper mockServiceHelper; 44 private static MockValidationHelperV1 mockValidationHelperV1; 45 private LocalChannelProvider channelProvider; 46 private ValidationHelperV1Client client; 47 48 @BeforeClass startStaticServer()49 public static void startStaticServer() { 50 mockValidationHelperV1 = new MockValidationHelperV1(); 51 mockServiceHelper = 52 new MockServiceHelper( 53 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockValidationHelperV1)); 54 mockServiceHelper.start(); 55 } 56 57 @AfterClass stopServer()58 public static void stopServer() { 59 mockServiceHelper.stop(); 60 } 61 62 @Before setUp()63 public void setUp() throws IOException { 64 mockServiceHelper.reset(); 65 channelProvider = mockServiceHelper.createChannelProvider(); 66 ValidationHelperV1Settings settings = 67 ValidationHelperV1Settings.newBuilder() 68 .setTransportChannelProvider(channelProvider) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = ValidationHelperV1Client.create(settings); 72 } 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 client.close(); 77 } 78 79 @Test validateAttestationOccurrenceTest()80 public void validateAttestationOccurrenceTest() throws Exception { 81 Service.ValidateAttestationOccurrenceResponse expectedResponse = 82 Service.ValidateAttestationOccurrenceResponse.newBuilder() 83 .setDenialReason("denialReason-966999989") 84 .build(); 85 mockValidationHelperV1.addResponse(expectedResponse); 86 87 Service.ValidateAttestationOccurrenceRequest request = 88 Service.ValidateAttestationOccurrenceRequest.newBuilder() 89 .setAttestor("attestor542920680") 90 .setAttestation(AttestationOccurrence.newBuilder().build()) 91 .setOccurrenceNote("occurrenceNote1722072419") 92 .setOccurrenceResourceUri("occurrenceResourceUri1001424877") 93 .build(); 94 95 Service.ValidateAttestationOccurrenceResponse actualResponse = 96 client.validateAttestationOccurrence(request); 97 Assert.assertEquals(expectedResponse, actualResponse); 98 99 List<AbstractMessage> actualRequests = mockValidationHelperV1.getRequests(); 100 Assert.assertEquals(1, actualRequests.size()); 101 Service.ValidateAttestationOccurrenceRequest actualRequest = 102 ((Service.ValidateAttestationOccurrenceRequest) actualRequests.get(0)); 103 104 Assert.assertEquals(request.getAttestor(), actualRequest.getAttestor()); 105 Assert.assertEquals(request.getAttestation(), actualRequest.getAttestation()); 106 Assert.assertEquals(request.getOccurrenceNote(), actualRequest.getOccurrenceNote()); 107 Assert.assertEquals( 108 request.getOccurrenceResourceUri(), actualRequest.getOccurrenceResourceUri()); 109 Assert.assertTrue( 110 channelProvider.isHeaderSent( 111 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 112 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 113 } 114 115 @Test validateAttestationOccurrenceExceptionTest()116 public void validateAttestationOccurrenceExceptionTest() throws Exception { 117 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 118 mockValidationHelperV1.addException(exception); 119 120 try { 121 Service.ValidateAttestationOccurrenceRequest request = 122 Service.ValidateAttestationOccurrenceRequest.newBuilder() 123 .setAttestor("attestor542920680") 124 .setAttestation(AttestationOccurrence.newBuilder().build()) 125 .setOccurrenceNote("occurrenceNote1722072419") 126 .setOccurrenceResourceUri("occurrenceResourceUri1001424877") 127 .build(); 128 client.validateAttestationOccurrence(request); 129 Assert.fail("No exception raised"); 130 } catch (InvalidArgumentException e) { 131 // Expected exception. 132 } 133 } 134 } 135