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.httpjson.GaxHttpJsonProperties; 24 import com.google.api.gax.httpjson.testing.MockHttpService; 25 import com.google.api.gax.rpc.ApiClientHeaderProvider; 26 import com.google.api.gax.rpc.ApiException; 27 import com.google.api.gax.rpc.ApiExceptionFactory; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.StatusCode; 30 import com.google.api.gax.rpc.testing.FakeStatusCode; 31 import com.google.cloud.asset.v1p1beta1.stub.HttpJsonAssetServiceStub; 32 import com.google.common.collect.Lists; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 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 AssetServiceClientHttpJsonTest { 47 private static MockHttpService mockService; 48 private static AssetServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() throws IOException { 52 mockService = 53 new MockHttpService( 54 HttpJsonAssetServiceStub.getMethodDescriptors(), 55 AssetServiceSettings.getDefaultEndpoint()); 56 AssetServiceSettings settings = 57 AssetServiceSettings.newHttpJsonBuilder() 58 .setTransportChannelProvider( 59 AssetServiceSettings.defaultHttpJsonTransportProviderBuilder() 60 .setHttpTransport(mockService) 61 .build()) 62 .setCredentialsProvider(NoCredentialsProvider.create()) 63 .build(); 64 client = AssetServiceClient.create(settings); 65 } 66 67 @AfterClass stopServer()68 public static void stopServer() { 69 client.close(); 70 } 71 72 @Before setUp()73 public void setUp() {} 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 mockService.reset(); 78 } 79 80 @Test searchAllResourcesTest()81 public void searchAllResourcesTest() throws Exception { 82 StandardResourceMetadata responsesElement = StandardResourceMetadata.newBuilder().build(); 83 SearchAllResourcesResponse expectedResponse = 84 SearchAllResourcesResponse.newBuilder() 85 .setNextPageToken("") 86 .addAllResults(Arrays.asList(responsesElement)) 87 .build(); 88 mockService.addResponse(expectedResponse); 89 90 String scope = "scope-2319/scope-2319"; 91 String query = "query107944136"; 92 List<String> assetTypes = new ArrayList<>(); 93 94 SearchAllResourcesPagedResponse pagedListResponse = 95 client.searchAllResources(scope, query, assetTypes); 96 97 List<StandardResourceMetadata> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 98 99 Assert.assertEquals(1, resources.size()); 100 Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0)); 101 102 List<String> actualRequests = mockService.getRequestPaths(); 103 Assert.assertEquals(1, actualRequests.size()); 104 105 String apiClientHeaderKey = 106 mockService 107 .getRequestHeaders() 108 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 109 .iterator() 110 .next(); 111 Assert.assertTrue( 112 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 113 .matcher(apiClientHeaderKey) 114 .matches()); 115 } 116 117 @Test searchAllResourcesExceptionTest()118 public void searchAllResourcesExceptionTest() throws Exception { 119 ApiException exception = 120 ApiExceptionFactory.createException( 121 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 122 mockService.addException(exception); 123 124 try { 125 String scope = "scope-2319/scope-2319"; 126 String query = "query107944136"; 127 List<String> assetTypes = new ArrayList<>(); 128 client.searchAllResources(scope, query, assetTypes); 129 Assert.fail("No exception raised"); 130 } catch (InvalidArgumentException e) { 131 // Expected exception. 132 } 133 } 134 135 @Test searchAllIamPoliciesTest()136 public void searchAllIamPoliciesTest() throws Exception { 137 IamPolicySearchResult responsesElement = IamPolicySearchResult.newBuilder().build(); 138 SearchAllIamPoliciesResponse expectedResponse = 139 SearchAllIamPoliciesResponse.newBuilder() 140 .setNextPageToken("") 141 .addAllResults(Arrays.asList(responsesElement)) 142 .build(); 143 mockService.addResponse(expectedResponse); 144 145 String scope = "scope-2319/scope-2319"; 146 String query = "query107944136"; 147 148 SearchAllIamPoliciesPagedResponse pagedListResponse = client.searchAllIamPolicies(scope, query); 149 150 List<IamPolicySearchResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 151 152 Assert.assertEquals(1, resources.size()); 153 Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0)); 154 155 List<String> actualRequests = mockService.getRequestPaths(); 156 Assert.assertEquals(1, actualRequests.size()); 157 158 String apiClientHeaderKey = 159 mockService 160 .getRequestHeaders() 161 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 162 .iterator() 163 .next(); 164 Assert.assertTrue( 165 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 166 .matcher(apiClientHeaderKey) 167 .matches()); 168 } 169 170 @Test searchAllIamPoliciesExceptionTest()171 public void searchAllIamPoliciesExceptionTest() throws Exception { 172 ApiException exception = 173 ApiExceptionFactory.createException( 174 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 175 mockService.addException(exception); 176 177 try { 178 String scope = "scope-2319/scope-2319"; 179 String query = "query107944136"; 180 client.searchAllIamPolicies(scope, query); 181 Assert.fail("No exception raised"); 182 } catch (InvalidArgumentException e) { 183 // Expected exception. 184 } 185 } 186 } 187