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.protobuf.Timestamp; 29 import com.google.protos.google.cloud.binaryauthorization.v1.stub.HttpJsonSystemPolicyV1Stub; 30 import java.io.IOException; 31 import java.util.ArrayList; 32 import java.util.HashMap; 33 import java.util.List; 34 import javax.annotation.Generated; 35 import org.junit.After; 36 import org.junit.AfterClass; 37 import org.junit.Assert; 38 import org.junit.Before; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 42 @Generated("by gapic-generator-java") 43 public class SystemPolicyV1ClientHttpJsonTest { 44 private static MockHttpService mockService; 45 private static SystemPolicyV1Client client; 46 47 @BeforeClass startStaticServer()48 public static void startStaticServer() throws IOException { 49 mockService = 50 new MockHttpService( 51 HttpJsonSystemPolicyV1Stub.getMethodDescriptors(), 52 SystemPolicyV1Settings.getDefaultEndpoint()); 53 SystemPolicyV1Settings settings = 54 SystemPolicyV1Settings.newHttpJsonBuilder() 55 .setTransportChannelProvider( 56 SystemPolicyV1Settings.defaultHttpJsonTransportProviderBuilder() 57 .setHttpTransport(mockService) 58 .build()) 59 .setCredentialsProvider(NoCredentialsProvider.create()) 60 .build(); 61 client = SystemPolicyV1Client.create(settings); 62 } 63 64 @AfterClass stopServer()65 public static void stopServer() { 66 client.close(); 67 } 68 69 @Before setUp()70 public void setUp() {} 71 72 @After tearDown()73 public void tearDown() throws Exception { 74 mockService.reset(); 75 } 76 77 @Test getSystemPolicyTest()78 public void getSystemPolicyTest() throws Exception { 79 Resources.Policy expectedResponse = 80 Resources.Policy.newBuilder() 81 .setName(PolicyName.ofLocationName("[LOCATION]").toString()) 82 .setDescription("description-1724546052") 83 .addAllAdmissionWhitelistPatterns(new ArrayList<Resources.AdmissionWhitelistPattern>()) 84 .putAllClusterAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 85 .putAllKubernetesNamespaceAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 86 .putAllKubernetesServiceAccountAdmissionRules( 87 new HashMap<String, Resources.AdmissionRule>()) 88 .putAllIstioServiceIdentityAdmissionRules( 89 new HashMap<String, Resources.AdmissionRule>()) 90 .setDefaultAdmissionRule(Resources.AdmissionRule.newBuilder().build()) 91 .setUpdateTime(Timestamp.newBuilder().build()) 92 .build(); 93 mockService.addResponse(expectedResponse); 94 95 PolicyName name = PolicyName.ofLocationName("[LOCATION]"); 96 97 Resources.Policy actualResponse = client.getSystemPolicy(name); 98 Assert.assertEquals(expectedResponse, actualResponse); 99 100 List<String> actualRequests = mockService.getRequestPaths(); 101 Assert.assertEquals(1, actualRequests.size()); 102 103 String apiClientHeaderKey = 104 mockService 105 .getRequestHeaders() 106 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 107 .iterator() 108 .next(); 109 Assert.assertTrue( 110 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 111 .matcher(apiClientHeaderKey) 112 .matches()); 113 } 114 115 @Test getSystemPolicyExceptionTest()116 public void getSystemPolicyExceptionTest() throws Exception { 117 ApiException exception = 118 ApiExceptionFactory.createException( 119 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 120 mockService.addException(exception); 121 122 try { 123 PolicyName name = PolicyName.ofLocationName("[LOCATION]"); 124 client.getSystemPolicy(name); 125 Assert.fail("No exception raised"); 126 } catch (InvalidArgumentException e) { 127 // Expected exception. 128 } 129 } 130 131 @Test getSystemPolicyTest2()132 public void getSystemPolicyTest2() throws Exception { 133 Resources.Policy expectedResponse = 134 Resources.Policy.newBuilder() 135 .setName(PolicyName.ofLocationName("[LOCATION]").toString()) 136 .setDescription("description-1724546052") 137 .addAllAdmissionWhitelistPatterns(new ArrayList<Resources.AdmissionWhitelistPattern>()) 138 .putAllClusterAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 139 .putAllKubernetesNamespaceAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 140 .putAllKubernetesServiceAccountAdmissionRules( 141 new HashMap<String, Resources.AdmissionRule>()) 142 .putAllIstioServiceIdentityAdmissionRules( 143 new HashMap<String, Resources.AdmissionRule>()) 144 .setDefaultAdmissionRule(Resources.AdmissionRule.newBuilder().build()) 145 .setUpdateTime(Timestamp.newBuilder().build()) 146 .build(); 147 mockService.addResponse(expectedResponse); 148 149 String name = "locations/location-9731/policy"; 150 151 Resources.Policy actualResponse = client.getSystemPolicy(name); 152 Assert.assertEquals(expectedResponse, actualResponse); 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 getSystemPolicyExceptionTest2()170 public void getSystemPolicyExceptionTest2() 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 name = "locations/location-9731/policy"; 178 client.getSystemPolicy(name); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 } 185