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.httpjson.GaxHttpJsonProperties; 21 import com.google.api.gax.httpjson.testing.MockHttpService; 22 import com.google.api.gax.rpc.ApiClientHeaderProvider; 23 import com.google.api.gax.rpc.ApiException; 24 import com.google.api.gax.rpc.ApiExceptionFactory; 25 import com.google.api.gax.rpc.InvalidArgumentException; 26 import com.google.api.gax.rpc.StatusCode; 27 import com.google.api.gax.rpc.testing.FakeStatusCode; 28 import com.google.protos.google.cloud.binaryauthorization.v1.stub.HttpJsonValidationHelperV1Stub; 29 import io.grafeas.v1.AttestationOccurrence; 30 import java.io.IOException; 31 import java.util.List; 32 import javax.annotation.Generated; 33 import org.junit.After; 34 import org.junit.AfterClass; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.BeforeClass; 38 import org.junit.Test; 39 40 @Generated("by gapic-generator-java") 41 public class ValidationHelperV1ClientHttpJsonTest { 42 private static MockHttpService mockService; 43 private static ValidationHelperV1Client client; 44 45 @BeforeClass startStaticServer()46 public static void startStaticServer() throws IOException { 47 mockService = 48 new MockHttpService( 49 HttpJsonValidationHelperV1Stub.getMethodDescriptors(), 50 ValidationHelperV1Settings.getDefaultEndpoint()); 51 ValidationHelperV1Settings settings = 52 ValidationHelperV1Settings.newHttpJsonBuilder() 53 .setTransportChannelProvider( 54 ValidationHelperV1Settings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport(mockService) 56 .build()) 57 .setCredentialsProvider(NoCredentialsProvider.create()) 58 .build(); 59 client = ValidationHelperV1Client.create(settings); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 client.close(); 65 } 66 67 @Before setUp()68 public void setUp() {} 69 70 @After tearDown()71 public void tearDown() throws Exception { 72 mockService.reset(); 73 } 74 75 @Test validateAttestationOccurrenceTest()76 public void validateAttestationOccurrenceTest() throws Exception { 77 Service.ValidateAttestationOccurrenceResponse expectedResponse = 78 Service.ValidateAttestationOccurrenceResponse.newBuilder() 79 .setDenialReason("denialReason-966999989") 80 .build(); 81 mockService.addResponse(expectedResponse); 82 83 Service.ValidateAttestationOccurrenceRequest request = 84 Service.ValidateAttestationOccurrenceRequest.newBuilder() 85 .setAttestor("projects/project-380/attestors/attestor-380") 86 .setAttestation(AttestationOccurrence.newBuilder().build()) 87 .setOccurrenceNote("occurrenceNote1722072419") 88 .setOccurrenceResourceUri("occurrenceResourceUri1001424877") 89 .build(); 90 91 Service.ValidateAttestationOccurrenceResponse actualResponse = 92 client.validateAttestationOccurrence(request); 93 Assert.assertEquals(expectedResponse, actualResponse); 94 95 List<String> actualRequests = mockService.getRequestPaths(); 96 Assert.assertEquals(1, actualRequests.size()); 97 98 String apiClientHeaderKey = 99 mockService 100 .getRequestHeaders() 101 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 102 .iterator() 103 .next(); 104 Assert.assertTrue( 105 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 106 .matcher(apiClientHeaderKey) 107 .matches()); 108 } 109 110 @Test validateAttestationOccurrenceExceptionTest()111 public void validateAttestationOccurrenceExceptionTest() throws Exception { 112 ApiException exception = 113 ApiExceptionFactory.createException( 114 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 115 mockService.addException(exception); 116 117 try { 118 Service.ValidateAttestationOccurrenceRequest request = 119 Service.ValidateAttestationOccurrenceRequest.newBuilder() 120 .setAttestor("projects/project-380/attestors/attestor-380") 121 .setAttestation(AttestationOccurrence.newBuilder().build()) 122 .setOccurrenceNote("occurrenceNote1722072419") 123 .setOccurrenceResourceUri("occurrenceResourceUri1001424877") 124 .build(); 125 client.validateAttestationOccurrence(request); 126 Assert.fail("No exception raised"); 127 } catch (InvalidArgumentException e) { 128 // Expected exception. 129 } 130 } 131 } 132