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 com.google.protobuf.Timestamp; 28 import io.grpc.StatusRuntimeException; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.HashMap; 33 import java.util.List; 34 import java.util.UUID; 35 import javax.annotation.Generated; 36 import org.junit.After; 37 import org.junit.AfterClass; 38 import org.junit.Assert; 39 import org.junit.Before; 40 import org.junit.BeforeClass; 41 import org.junit.Test; 42 43 @Generated("by gapic-generator-java") 44 public class SystemPolicyV1ClientTest { 45 private static MockServiceHelper mockServiceHelper; 46 private static MockSystemPolicyV1 mockSystemPolicyV1; 47 private LocalChannelProvider channelProvider; 48 private SystemPolicyV1Client client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() { 52 mockSystemPolicyV1 = new MockSystemPolicyV1(); 53 mockServiceHelper = 54 new MockServiceHelper( 55 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockSystemPolicyV1)); 56 mockServiceHelper.start(); 57 } 58 59 @AfterClass stopServer()60 public static void stopServer() { 61 mockServiceHelper.stop(); 62 } 63 64 @Before setUp()65 public void setUp() throws IOException { 66 mockServiceHelper.reset(); 67 channelProvider = mockServiceHelper.createChannelProvider(); 68 SystemPolicyV1Settings settings = 69 SystemPolicyV1Settings.newBuilder() 70 .setTransportChannelProvider(channelProvider) 71 .setCredentialsProvider(NoCredentialsProvider.create()) 72 .build(); 73 client = SystemPolicyV1Client.create(settings); 74 } 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 client.close(); 79 } 80 81 @Test getSystemPolicyTest()82 public void getSystemPolicyTest() throws Exception { 83 Resources.Policy expectedResponse = 84 Resources.Policy.newBuilder() 85 .setName(PolicyName.ofLocationName("[LOCATION]").toString()) 86 .setDescription("description-1724546052") 87 .addAllAdmissionWhitelistPatterns(new ArrayList<Resources.AdmissionWhitelistPattern>()) 88 .putAllClusterAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 89 .putAllKubernetesNamespaceAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 90 .putAllKubernetesServiceAccountAdmissionRules( 91 new HashMap<String, Resources.AdmissionRule>()) 92 .putAllIstioServiceIdentityAdmissionRules( 93 new HashMap<String, Resources.AdmissionRule>()) 94 .setDefaultAdmissionRule(Resources.AdmissionRule.newBuilder().build()) 95 .setUpdateTime(Timestamp.newBuilder().build()) 96 .build(); 97 mockSystemPolicyV1.addResponse(expectedResponse); 98 99 PolicyName name = PolicyName.ofLocationName("[LOCATION]"); 100 101 Resources.Policy actualResponse = client.getSystemPolicy(name); 102 Assert.assertEquals(expectedResponse, actualResponse); 103 104 List<AbstractMessage> actualRequests = mockSystemPolicyV1.getRequests(); 105 Assert.assertEquals(1, actualRequests.size()); 106 Service.GetSystemPolicyRequest actualRequest = 107 ((Service.GetSystemPolicyRequest) actualRequests.get(0)); 108 109 Assert.assertEquals(name.toString(), actualRequest.getName()); 110 Assert.assertTrue( 111 channelProvider.isHeaderSent( 112 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 113 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 114 } 115 116 @Test getSystemPolicyExceptionTest()117 public void getSystemPolicyExceptionTest() throws Exception { 118 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 119 mockSystemPolicyV1.addException(exception); 120 121 try { 122 PolicyName name = PolicyName.ofLocationName("[LOCATION]"); 123 client.getSystemPolicy(name); 124 Assert.fail("No exception raised"); 125 } catch (InvalidArgumentException e) { 126 // Expected exception. 127 } 128 } 129 130 @Test getSystemPolicyTest2()131 public void getSystemPolicyTest2() throws Exception { 132 Resources.Policy expectedResponse = 133 Resources.Policy.newBuilder() 134 .setName(PolicyName.ofLocationName("[LOCATION]").toString()) 135 .setDescription("description-1724546052") 136 .addAllAdmissionWhitelistPatterns(new ArrayList<Resources.AdmissionWhitelistPattern>()) 137 .putAllClusterAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 138 .putAllKubernetesNamespaceAdmissionRules(new HashMap<String, Resources.AdmissionRule>()) 139 .putAllKubernetesServiceAccountAdmissionRules( 140 new HashMap<String, Resources.AdmissionRule>()) 141 .putAllIstioServiceIdentityAdmissionRules( 142 new HashMap<String, Resources.AdmissionRule>()) 143 .setDefaultAdmissionRule(Resources.AdmissionRule.newBuilder().build()) 144 .setUpdateTime(Timestamp.newBuilder().build()) 145 .build(); 146 mockSystemPolicyV1.addResponse(expectedResponse); 147 148 String name = "name3373707"; 149 150 Resources.Policy actualResponse = client.getSystemPolicy(name); 151 Assert.assertEquals(expectedResponse, actualResponse); 152 153 List<AbstractMessage> actualRequests = mockSystemPolicyV1.getRequests(); 154 Assert.assertEquals(1, actualRequests.size()); 155 Service.GetSystemPolicyRequest actualRequest = 156 ((Service.GetSystemPolicyRequest) actualRequests.get(0)); 157 158 Assert.assertEquals(name, actualRequest.getName()); 159 Assert.assertTrue( 160 channelProvider.isHeaderSent( 161 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 162 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 163 } 164 165 @Test getSystemPolicyExceptionTest2()166 public void getSystemPolicyExceptionTest2() throws Exception { 167 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 168 mockSystemPolicyV1.addException(exception); 169 170 try { 171 String name = "name3373707"; 172 client.getSystemPolicy(name); 173 Assert.fail("No exception raised"); 174 } catch (InvalidArgumentException e) { 175 // Expected exception. 176 } 177 } 178 } 179