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.v1p2beta1; 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.cloud.asset.v1p2beta1.stub.HttpJsonAssetServiceStub; 29 import com.google.protobuf.Empty; 30 import java.io.IOException; 31 import java.util.ArrayList; 32 import java.util.List; 33 import javax.annotation.Generated; 34 import org.junit.After; 35 import org.junit.AfterClass; 36 import org.junit.Assert; 37 import org.junit.Before; 38 import org.junit.BeforeClass; 39 import org.junit.Test; 40 41 @Generated("by gapic-generator-java") 42 public class AssetServiceClientHttpJsonTest { 43 private static MockHttpService mockService; 44 private static AssetServiceClient client; 45 46 @BeforeClass startStaticServer()47 public static void startStaticServer() throws IOException { 48 mockService = 49 new MockHttpService( 50 HttpJsonAssetServiceStub.getMethodDescriptors(), 51 AssetServiceSettings.getDefaultEndpoint()); 52 AssetServiceSettings settings = 53 AssetServiceSettings.newHttpJsonBuilder() 54 .setTransportChannelProvider( 55 AssetServiceSettings.defaultHttpJsonTransportProviderBuilder() 56 .setHttpTransport(mockService) 57 .build()) 58 .setCredentialsProvider(NoCredentialsProvider.create()) 59 .build(); 60 client = AssetServiceClient.create(settings); 61 } 62 63 @AfterClass stopServer()64 public static void stopServer() { 65 client.close(); 66 } 67 68 @Before setUp()69 public void setUp() {} 70 71 @After tearDown()72 public void tearDown() throws Exception { 73 mockService.reset(); 74 } 75 76 @Test createFeedTest()77 public void createFeedTest() throws Exception { 78 Feed expectedResponse = 79 Feed.newBuilder() 80 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 81 .addAllAssetNames(new ArrayList<String>()) 82 .addAllAssetTypes(new ArrayList<String>()) 83 .setContentType(ContentType.forNumber(0)) 84 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 85 .build(); 86 mockService.addResponse(expectedResponse); 87 88 String parent = "parent-4715/parent-4715"; 89 90 Feed actualResponse = client.createFeed(parent); 91 Assert.assertEquals(expectedResponse, actualResponse); 92 93 List<String> actualRequests = mockService.getRequestPaths(); 94 Assert.assertEquals(1, actualRequests.size()); 95 96 String apiClientHeaderKey = 97 mockService 98 .getRequestHeaders() 99 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 100 .iterator() 101 .next(); 102 Assert.assertTrue( 103 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 104 .matcher(apiClientHeaderKey) 105 .matches()); 106 } 107 108 @Test createFeedExceptionTest()109 public void createFeedExceptionTest() throws Exception { 110 ApiException exception = 111 ApiExceptionFactory.createException( 112 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 113 mockService.addException(exception); 114 115 try { 116 String parent = "parent-4715/parent-4715"; 117 client.createFeed(parent); 118 Assert.fail("No exception raised"); 119 } catch (InvalidArgumentException e) { 120 // Expected exception. 121 } 122 } 123 124 @Test getFeedTest()125 public void getFeedTest() throws Exception { 126 Feed expectedResponse = 127 Feed.newBuilder() 128 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 129 .addAllAssetNames(new ArrayList<String>()) 130 .addAllAssetTypes(new ArrayList<String>()) 131 .setContentType(ContentType.forNumber(0)) 132 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 133 .build(); 134 mockService.addResponse(expectedResponse); 135 136 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 137 138 Feed actualResponse = client.getFeed(name); 139 Assert.assertEquals(expectedResponse, actualResponse); 140 141 List<String> actualRequests = mockService.getRequestPaths(); 142 Assert.assertEquals(1, actualRequests.size()); 143 144 String apiClientHeaderKey = 145 mockService 146 .getRequestHeaders() 147 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 148 .iterator() 149 .next(); 150 Assert.assertTrue( 151 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 152 .matcher(apiClientHeaderKey) 153 .matches()); 154 } 155 156 @Test getFeedExceptionTest()157 public void getFeedExceptionTest() throws Exception { 158 ApiException exception = 159 ApiExceptionFactory.createException( 160 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 161 mockService.addException(exception); 162 163 try { 164 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 165 client.getFeed(name); 166 Assert.fail("No exception raised"); 167 } catch (InvalidArgumentException e) { 168 // Expected exception. 169 } 170 } 171 172 @Test getFeedTest2()173 public void getFeedTest2() throws Exception { 174 Feed expectedResponse = 175 Feed.newBuilder() 176 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 177 .addAllAssetNames(new ArrayList<String>()) 178 .addAllAssetTypes(new ArrayList<String>()) 179 .setContentType(ContentType.forNumber(0)) 180 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 181 .build(); 182 mockService.addResponse(expectedResponse); 183 184 String name = "name-2459/name-2459/feeds/feed-2459"; 185 186 Feed actualResponse = client.getFeed(name); 187 Assert.assertEquals(expectedResponse, actualResponse); 188 189 List<String> actualRequests = mockService.getRequestPaths(); 190 Assert.assertEquals(1, actualRequests.size()); 191 192 String apiClientHeaderKey = 193 mockService 194 .getRequestHeaders() 195 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 196 .iterator() 197 .next(); 198 Assert.assertTrue( 199 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 200 .matcher(apiClientHeaderKey) 201 .matches()); 202 } 203 204 @Test getFeedExceptionTest2()205 public void getFeedExceptionTest2() throws Exception { 206 ApiException exception = 207 ApiExceptionFactory.createException( 208 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 209 mockService.addException(exception); 210 211 try { 212 String name = "name-2459/name-2459/feeds/feed-2459"; 213 client.getFeed(name); 214 Assert.fail("No exception raised"); 215 } catch (InvalidArgumentException e) { 216 // Expected exception. 217 } 218 } 219 220 @Test listFeedsTest()221 public void listFeedsTest() throws Exception { 222 ListFeedsResponse expectedResponse = 223 ListFeedsResponse.newBuilder().addAllFeeds(new ArrayList<Feed>()).build(); 224 mockService.addResponse(expectedResponse); 225 226 String parent = "parent-4715/parent-4715"; 227 228 ListFeedsResponse actualResponse = client.listFeeds(parent); 229 Assert.assertEquals(expectedResponse, actualResponse); 230 231 List<String> actualRequests = mockService.getRequestPaths(); 232 Assert.assertEquals(1, actualRequests.size()); 233 234 String apiClientHeaderKey = 235 mockService 236 .getRequestHeaders() 237 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 238 .iterator() 239 .next(); 240 Assert.assertTrue( 241 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 242 .matcher(apiClientHeaderKey) 243 .matches()); 244 } 245 246 @Test listFeedsExceptionTest()247 public void listFeedsExceptionTest() throws Exception { 248 ApiException exception = 249 ApiExceptionFactory.createException( 250 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 251 mockService.addException(exception); 252 253 try { 254 String parent = "parent-4715/parent-4715"; 255 client.listFeeds(parent); 256 Assert.fail("No exception raised"); 257 } catch (InvalidArgumentException e) { 258 // Expected exception. 259 } 260 } 261 262 @Test updateFeedTest()263 public void updateFeedTest() throws Exception { 264 Feed expectedResponse = 265 Feed.newBuilder() 266 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 267 .addAllAssetNames(new ArrayList<String>()) 268 .addAllAssetTypes(new ArrayList<String>()) 269 .setContentType(ContentType.forNumber(0)) 270 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 271 .build(); 272 mockService.addResponse(expectedResponse); 273 274 Feed feed = 275 Feed.newBuilder() 276 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 277 .addAllAssetNames(new ArrayList<String>()) 278 .addAllAssetTypes(new ArrayList<String>()) 279 .setContentType(ContentType.forNumber(0)) 280 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 281 .build(); 282 283 Feed actualResponse = client.updateFeed(feed); 284 Assert.assertEquals(expectedResponse, actualResponse); 285 286 List<String> actualRequests = mockService.getRequestPaths(); 287 Assert.assertEquals(1, actualRequests.size()); 288 289 String apiClientHeaderKey = 290 mockService 291 .getRequestHeaders() 292 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 293 .iterator() 294 .next(); 295 Assert.assertTrue( 296 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 297 .matcher(apiClientHeaderKey) 298 .matches()); 299 } 300 301 @Test updateFeedExceptionTest()302 public void updateFeedExceptionTest() throws Exception { 303 ApiException exception = 304 ApiExceptionFactory.createException( 305 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 306 mockService.addException(exception); 307 308 try { 309 Feed feed = 310 Feed.newBuilder() 311 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 312 .addAllAssetNames(new ArrayList<String>()) 313 .addAllAssetTypes(new ArrayList<String>()) 314 .setContentType(ContentType.forNumber(0)) 315 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 316 .build(); 317 client.updateFeed(feed); 318 Assert.fail("No exception raised"); 319 } catch (InvalidArgumentException e) { 320 // Expected exception. 321 } 322 } 323 324 @Test deleteFeedTest()325 public void deleteFeedTest() throws Exception { 326 Empty expectedResponse = Empty.newBuilder().build(); 327 mockService.addResponse(expectedResponse); 328 329 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 330 331 client.deleteFeed(name); 332 333 List<String> actualRequests = mockService.getRequestPaths(); 334 Assert.assertEquals(1, actualRequests.size()); 335 336 String apiClientHeaderKey = 337 mockService 338 .getRequestHeaders() 339 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 340 .iterator() 341 .next(); 342 Assert.assertTrue( 343 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 344 .matcher(apiClientHeaderKey) 345 .matches()); 346 } 347 348 @Test deleteFeedExceptionTest()349 public void deleteFeedExceptionTest() throws Exception { 350 ApiException exception = 351 ApiExceptionFactory.createException( 352 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 353 mockService.addException(exception); 354 355 try { 356 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 357 client.deleteFeed(name); 358 Assert.fail("No exception raised"); 359 } catch (InvalidArgumentException e) { 360 // Expected exception. 361 } 362 } 363 364 @Test deleteFeedTest2()365 public void deleteFeedTest2() throws Exception { 366 Empty expectedResponse = Empty.newBuilder().build(); 367 mockService.addResponse(expectedResponse); 368 369 String name = "name-2459/name-2459/feeds/feed-2459"; 370 371 client.deleteFeed(name); 372 373 List<String> actualRequests = mockService.getRequestPaths(); 374 Assert.assertEquals(1, actualRequests.size()); 375 376 String apiClientHeaderKey = 377 mockService 378 .getRequestHeaders() 379 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 380 .iterator() 381 .next(); 382 Assert.assertTrue( 383 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 384 .matcher(apiClientHeaderKey) 385 .matches()); 386 } 387 388 @Test deleteFeedExceptionTest2()389 public void deleteFeedExceptionTest2() throws Exception { 390 ApiException exception = 391 ApiExceptionFactory.createException( 392 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 393 mockService.addException(exception); 394 395 try { 396 String name = "name-2459/name-2459/feeds/feed-2459"; 397 client.deleteFeed(name); 398 Assert.fail("No exception raised"); 399 } catch (InvalidArgumentException e) { 400 // Expected exception. 401 } 402 } 403 } 404