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.cloud.asset.v1p1beta1; 18 19 import static com.google.cloud.asset.v1p1beta1.AssetServiceClient.SearchAllIamPoliciesPagedResponse; 20 import static com.google.cloud.asset.v1p1beta1.AssetServiceClient.SearchAllResourcesPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.grpc.GaxGrpcProperties; 24 import com.google.api.gax.grpc.testing.LocalChannelProvider; 25 import com.google.api.gax.grpc.testing.MockGrpcService; 26 import com.google.api.gax.grpc.testing.MockServiceHelper; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.common.collect.Lists; 30 import com.google.protobuf.AbstractMessage; 31 import io.grpc.StatusRuntimeException; 32 import java.io.IOException; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.List; 36 import java.util.UUID; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class AssetServiceClientTest { 47 private static MockAssetService mockAssetService; 48 private static MockServiceHelper mockServiceHelper; 49 private LocalChannelProvider channelProvider; 50 private AssetServiceClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() { 54 mockAssetService = new MockAssetService(); 55 mockServiceHelper = 56 new MockServiceHelper( 57 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockAssetService)); 58 mockServiceHelper.start(); 59 } 60 61 @AfterClass stopServer()62 public static void stopServer() { 63 mockServiceHelper.stop(); 64 } 65 66 @Before setUp()67 public void setUp() throws IOException { 68 mockServiceHelper.reset(); 69 channelProvider = mockServiceHelper.createChannelProvider(); 70 AssetServiceSettings settings = 71 AssetServiceSettings.newBuilder() 72 .setTransportChannelProvider(channelProvider) 73 .setCredentialsProvider(NoCredentialsProvider.create()) 74 .build(); 75 client = AssetServiceClient.create(settings); 76 } 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 client.close(); 81 } 82 83 @Test searchAllResourcesTest()84 public void searchAllResourcesTest() throws Exception { 85 StandardResourceMetadata responsesElement = StandardResourceMetadata.newBuilder().build(); 86 SearchAllResourcesResponse expectedResponse = 87 SearchAllResourcesResponse.newBuilder() 88 .setNextPageToken("") 89 .addAllResults(Arrays.asList(responsesElement)) 90 .build(); 91 mockAssetService.addResponse(expectedResponse); 92 93 String scope = "scope109264468"; 94 String query = "query107944136"; 95 List<String> assetTypes = new ArrayList<>(); 96 97 SearchAllResourcesPagedResponse pagedListResponse = 98 client.searchAllResources(scope, query, assetTypes); 99 100 List<StandardResourceMetadata> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 101 102 Assert.assertEquals(1, resources.size()); 103 Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0)); 104 105 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 106 Assert.assertEquals(1, actualRequests.size()); 107 SearchAllResourcesRequest actualRequest = ((SearchAllResourcesRequest) actualRequests.get(0)); 108 109 Assert.assertEquals(scope, actualRequest.getScope()); 110 Assert.assertEquals(query, actualRequest.getQuery()); 111 Assert.assertEquals(assetTypes, actualRequest.getAssetTypesList()); 112 Assert.assertTrue( 113 channelProvider.isHeaderSent( 114 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 115 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 116 } 117 118 @Test searchAllResourcesExceptionTest()119 public void searchAllResourcesExceptionTest() throws Exception { 120 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 121 mockAssetService.addException(exception); 122 123 try { 124 String scope = "scope109264468"; 125 String query = "query107944136"; 126 List<String> assetTypes = new ArrayList<>(); 127 client.searchAllResources(scope, query, assetTypes); 128 Assert.fail("No exception raised"); 129 } catch (InvalidArgumentException e) { 130 // Expected exception. 131 } 132 } 133 134 @Test searchAllIamPoliciesTest()135 public void searchAllIamPoliciesTest() throws Exception { 136 IamPolicySearchResult responsesElement = IamPolicySearchResult.newBuilder().build(); 137 SearchAllIamPoliciesResponse expectedResponse = 138 SearchAllIamPoliciesResponse.newBuilder() 139 .setNextPageToken("") 140 .addAllResults(Arrays.asList(responsesElement)) 141 .build(); 142 mockAssetService.addResponse(expectedResponse); 143 144 String scope = "scope109264468"; 145 String query = "query107944136"; 146 147 SearchAllIamPoliciesPagedResponse pagedListResponse = client.searchAllIamPolicies(scope, query); 148 149 List<IamPolicySearchResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 150 151 Assert.assertEquals(1, resources.size()); 152 Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0)); 153 154 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 155 Assert.assertEquals(1, actualRequests.size()); 156 SearchAllIamPoliciesRequest actualRequest = 157 ((SearchAllIamPoliciesRequest) actualRequests.get(0)); 158 159 Assert.assertEquals(scope, actualRequest.getScope()); 160 Assert.assertEquals(query, actualRequest.getQuery()); 161 Assert.assertTrue( 162 channelProvider.isHeaderSent( 163 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 164 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 165 } 166 167 @Test searchAllIamPoliciesExceptionTest()168 public void searchAllIamPoliciesExceptionTest() throws Exception { 169 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 170 mockAssetService.addException(exception); 171 172 try { 173 String scope = "scope109264468"; 174 String query = "query107944136"; 175 client.searchAllIamPolicies(scope, query); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 } 182