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.v1p5beta1; 18 19 import static com.google.cloud.asset.v1p5beta1.AssetServiceClient.ListAssetsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.api.resourcenames.ResourceName; 31 import com.google.cloud.asset.v1p5beta1.stub.HttpJsonAssetServiceStub; 32 import com.google.common.collect.Lists; 33 import java.io.IOException; 34 import java.util.Arrays; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class AssetServiceClientHttpJsonTest { 48 private static MockHttpService mockService; 49 private static AssetServiceClient client; 50 51 @BeforeClass startStaticServer()52 public static void startStaticServer() throws IOException { 53 mockService = 54 new MockHttpService( 55 HttpJsonAssetServiceStub.getMethodDescriptors(), 56 AssetServiceSettings.getDefaultEndpoint()); 57 AssetServiceSettings settings = 58 AssetServiceSettings.newHttpJsonBuilder() 59 .setTransportChannelProvider( 60 AssetServiceSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = AssetServiceClient.create(settings); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 client.close(); 71 } 72 73 @Before setUp()74 public void setUp() {} 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 mockService.reset(); 79 } 80 81 @Test listAssetsTest()82 public void listAssetsTest() throws Exception { 83 Asset responsesElement = Asset.newBuilder().build(); 84 ListAssetsResponse expectedResponse = 85 ListAssetsResponse.newBuilder() 86 .setNextPageToken("") 87 .addAllAssets(Arrays.asList(responsesElement)) 88 .build(); 89 mockService.addResponse(expectedResponse); 90 91 ResourceName parent = 92 new ResourceName() { 93 @Override 94 public Map<String, String> getFieldValuesMap() { 95 Map<String, String> fieldValuesMap = new HashMap<>(); 96 fieldValuesMap.put("parent", "parent-4715/parent-4715"); 97 return fieldValuesMap; 98 } 99 100 @Override 101 public String getFieldValue(String fieldName) { 102 return getFieldValuesMap().get(fieldName); 103 } 104 105 @Override 106 public String toString() { 107 return "parent-4715/parent-4715"; 108 } 109 }; 110 111 ListAssetsPagedResponse pagedListResponse = client.listAssets(parent); 112 113 List<Asset> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 114 115 Assert.assertEquals(1, resources.size()); 116 Assert.assertEquals(expectedResponse.getAssetsList().get(0), resources.get(0)); 117 118 List<String> actualRequests = mockService.getRequestPaths(); 119 Assert.assertEquals(1, actualRequests.size()); 120 121 String apiClientHeaderKey = 122 mockService 123 .getRequestHeaders() 124 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 125 .iterator() 126 .next(); 127 Assert.assertTrue( 128 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 129 .matcher(apiClientHeaderKey) 130 .matches()); 131 } 132 133 @Test listAssetsExceptionTest()134 public void listAssetsExceptionTest() throws Exception { 135 ApiException exception = 136 ApiExceptionFactory.createException( 137 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 138 mockService.addException(exception); 139 140 try { 141 ResourceName parent = 142 new ResourceName() { 143 @Override 144 public Map<String, String> getFieldValuesMap() { 145 Map<String, String> fieldValuesMap = new HashMap<>(); 146 fieldValuesMap.put("parent", "parent-4715/parent-4715"); 147 return fieldValuesMap; 148 } 149 150 @Override 151 public String getFieldValue(String fieldName) { 152 return getFieldValuesMap().get(fieldName); 153 } 154 155 @Override 156 public String toString() { 157 return "parent-4715/parent-4715"; 158 } 159 }; 160 client.listAssets(parent); 161 Assert.fail("No exception raised"); 162 } catch (InvalidArgumentException e) { 163 // Expected exception. 164 } 165 } 166 167 @Test listAssetsTest2()168 public void listAssetsTest2() throws Exception { 169 Asset responsesElement = Asset.newBuilder().build(); 170 ListAssetsResponse expectedResponse = 171 ListAssetsResponse.newBuilder() 172 .setNextPageToken("") 173 .addAllAssets(Arrays.asList(responsesElement)) 174 .build(); 175 mockService.addResponse(expectedResponse); 176 177 String parent = "parent-4715/parent-4715"; 178 179 ListAssetsPagedResponse pagedListResponse = client.listAssets(parent); 180 181 List<Asset> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 182 183 Assert.assertEquals(1, resources.size()); 184 Assert.assertEquals(expectedResponse.getAssetsList().get(0), resources.get(0)); 185 186 List<String> actualRequests = mockService.getRequestPaths(); 187 Assert.assertEquals(1, actualRequests.size()); 188 189 String apiClientHeaderKey = 190 mockService 191 .getRequestHeaders() 192 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 193 .iterator() 194 .next(); 195 Assert.assertTrue( 196 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 197 .matcher(apiClientHeaderKey) 198 .matches()); 199 } 200 201 @Test listAssetsExceptionTest2()202 public void listAssetsExceptionTest2() throws Exception { 203 ApiException exception = 204 ApiExceptionFactory.createException( 205 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 206 mockService.addException(exception); 207 208 try { 209 String parent = "parent-4715/parent-4715"; 210 client.listAssets(parent); 211 Assert.fail("No exception raised"); 212 } catch (InvalidArgumentException e) { 213 // Expected exception. 214 } 215 } 216 } 217