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.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.Empty; 28 import io.grpc.StatusRuntimeException; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.UUID; 34 import javax.annotation.Generated; 35 import org.junit.After; 36 import org.junit.AfterClass; 37 import org.junit.Assert; 38 import org.junit.Before; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 42 @Generated("by gapic-generator-java") 43 public class AssetServiceClientTest { 44 private static MockAssetService mockAssetService; 45 private static MockServiceHelper mockServiceHelper; 46 private LocalChannelProvider channelProvider; 47 private AssetServiceClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() { 51 mockAssetService = new MockAssetService(); 52 mockServiceHelper = 53 new MockServiceHelper( 54 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockAssetService)); 55 mockServiceHelper.start(); 56 } 57 58 @AfterClass stopServer()59 public static void stopServer() { 60 mockServiceHelper.stop(); 61 } 62 63 @Before setUp()64 public void setUp() throws IOException { 65 mockServiceHelper.reset(); 66 channelProvider = mockServiceHelper.createChannelProvider(); 67 AssetServiceSettings settings = 68 AssetServiceSettings.newBuilder() 69 .setTransportChannelProvider(channelProvider) 70 .setCredentialsProvider(NoCredentialsProvider.create()) 71 .build(); 72 client = AssetServiceClient.create(settings); 73 } 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 client.close(); 78 } 79 80 @Test createFeedTest()81 public void createFeedTest() throws Exception { 82 Feed expectedResponse = 83 Feed.newBuilder() 84 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 85 .addAllAssetNames(new ArrayList<String>()) 86 .addAllAssetTypes(new ArrayList<String>()) 87 .setContentType(ContentType.forNumber(0)) 88 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 89 .build(); 90 mockAssetService.addResponse(expectedResponse); 91 92 String parent = "parent-995424086"; 93 94 Feed actualResponse = client.createFeed(parent); 95 Assert.assertEquals(expectedResponse, actualResponse); 96 97 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 98 Assert.assertEquals(1, actualRequests.size()); 99 CreateFeedRequest actualRequest = ((CreateFeedRequest) actualRequests.get(0)); 100 101 Assert.assertEquals(parent, actualRequest.getParent()); 102 Assert.assertTrue( 103 channelProvider.isHeaderSent( 104 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 105 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 106 } 107 108 @Test createFeedExceptionTest()109 public void createFeedExceptionTest() throws Exception { 110 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 111 mockAssetService.addException(exception); 112 113 try { 114 String parent = "parent-995424086"; 115 client.createFeed(parent); 116 Assert.fail("No exception raised"); 117 } catch (InvalidArgumentException e) { 118 // Expected exception. 119 } 120 } 121 122 @Test getFeedTest()123 public void getFeedTest() throws Exception { 124 Feed expectedResponse = 125 Feed.newBuilder() 126 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 127 .addAllAssetNames(new ArrayList<String>()) 128 .addAllAssetTypes(new ArrayList<String>()) 129 .setContentType(ContentType.forNumber(0)) 130 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 131 .build(); 132 mockAssetService.addResponse(expectedResponse); 133 134 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 135 136 Feed actualResponse = client.getFeed(name); 137 Assert.assertEquals(expectedResponse, actualResponse); 138 139 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 140 Assert.assertEquals(1, actualRequests.size()); 141 GetFeedRequest actualRequest = ((GetFeedRequest) actualRequests.get(0)); 142 143 Assert.assertEquals(name.toString(), actualRequest.getName()); 144 Assert.assertTrue( 145 channelProvider.isHeaderSent( 146 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 147 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 148 } 149 150 @Test getFeedExceptionTest()151 public void getFeedExceptionTest() throws Exception { 152 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 153 mockAssetService.addException(exception); 154 155 try { 156 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 157 client.getFeed(name); 158 Assert.fail("No exception raised"); 159 } catch (InvalidArgumentException e) { 160 // Expected exception. 161 } 162 } 163 164 @Test getFeedTest2()165 public void getFeedTest2() throws Exception { 166 Feed expectedResponse = 167 Feed.newBuilder() 168 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 169 .addAllAssetNames(new ArrayList<String>()) 170 .addAllAssetTypes(new ArrayList<String>()) 171 .setContentType(ContentType.forNumber(0)) 172 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 173 .build(); 174 mockAssetService.addResponse(expectedResponse); 175 176 String name = "name3373707"; 177 178 Feed actualResponse = client.getFeed(name); 179 Assert.assertEquals(expectedResponse, actualResponse); 180 181 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 182 Assert.assertEquals(1, actualRequests.size()); 183 GetFeedRequest actualRequest = ((GetFeedRequest) actualRequests.get(0)); 184 185 Assert.assertEquals(name, actualRequest.getName()); 186 Assert.assertTrue( 187 channelProvider.isHeaderSent( 188 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 189 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 190 } 191 192 @Test getFeedExceptionTest2()193 public void getFeedExceptionTest2() throws Exception { 194 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 195 mockAssetService.addException(exception); 196 197 try { 198 String name = "name3373707"; 199 client.getFeed(name); 200 Assert.fail("No exception raised"); 201 } catch (InvalidArgumentException e) { 202 // Expected exception. 203 } 204 } 205 206 @Test listFeedsTest()207 public void listFeedsTest() throws Exception { 208 ListFeedsResponse expectedResponse = 209 ListFeedsResponse.newBuilder().addAllFeeds(new ArrayList<Feed>()).build(); 210 mockAssetService.addResponse(expectedResponse); 211 212 String parent = "parent-995424086"; 213 214 ListFeedsResponse actualResponse = client.listFeeds(parent); 215 Assert.assertEquals(expectedResponse, actualResponse); 216 217 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 218 Assert.assertEquals(1, actualRequests.size()); 219 ListFeedsRequest actualRequest = ((ListFeedsRequest) actualRequests.get(0)); 220 221 Assert.assertEquals(parent, actualRequest.getParent()); 222 Assert.assertTrue( 223 channelProvider.isHeaderSent( 224 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 225 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 226 } 227 228 @Test listFeedsExceptionTest()229 public void listFeedsExceptionTest() throws Exception { 230 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 231 mockAssetService.addException(exception); 232 233 try { 234 String parent = "parent-995424086"; 235 client.listFeeds(parent); 236 Assert.fail("No exception raised"); 237 } catch (InvalidArgumentException e) { 238 // Expected exception. 239 } 240 } 241 242 @Test updateFeedTest()243 public void updateFeedTest() throws Exception { 244 Feed expectedResponse = 245 Feed.newBuilder() 246 .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString()) 247 .addAllAssetNames(new ArrayList<String>()) 248 .addAllAssetTypes(new ArrayList<String>()) 249 .setContentType(ContentType.forNumber(0)) 250 .setFeedOutputConfig(FeedOutputConfig.newBuilder().build()) 251 .build(); 252 mockAssetService.addResponse(expectedResponse); 253 254 Feed feed = Feed.newBuilder().build(); 255 256 Feed actualResponse = client.updateFeed(feed); 257 Assert.assertEquals(expectedResponse, actualResponse); 258 259 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 260 Assert.assertEquals(1, actualRequests.size()); 261 UpdateFeedRequest actualRequest = ((UpdateFeedRequest) actualRequests.get(0)); 262 263 Assert.assertEquals(feed, actualRequest.getFeed()); 264 Assert.assertTrue( 265 channelProvider.isHeaderSent( 266 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 267 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 268 } 269 270 @Test updateFeedExceptionTest()271 public void updateFeedExceptionTest() throws Exception { 272 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 273 mockAssetService.addException(exception); 274 275 try { 276 Feed feed = Feed.newBuilder().build(); 277 client.updateFeed(feed); 278 Assert.fail("No exception raised"); 279 } catch (InvalidArgumentException e) { 280 // Expected exception. 281 } 282 } 283 284 @Test deleteFeedTest()285 public void deleteFeedTest() throws Exception { 286 Empty expectedResponse = Empty.newBuilder().build(); 287 mockAssetService.addResponse(expectedResponse); 288 289 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 290 291 client.deleteFeed(name); 292 293 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 294 Assert.assertEquals(1, actualRequests.size()); 295 DeleteFeedRequest actualRequest = ((DeleteFeedRequest) actualRequests.get(0)); 296 297 Assert.assertEquals(name.toString(), actualRequest.getName()); 298 Assert.assertTrue( 299 channelProvider.isHeaderSent( 300 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 301 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 302 } 303 304 @Test deleteFeedExceptionTest()305 public void deleteFeedExceptionTest() throws Exception { 306 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 307 mockAssetService.addException(exception); 308 309 try { 310 FeedName name = FeedName.ofProjectFeedName("[PROJECT]", "[FEED]"); 311 client.deleteFeed(name); 312 Assert.fail("No exception raised"); 313 } catch (InvalidArgumentException e) { 314 // Expected exception. 315 } 316 } 317 318 @Test deleteFeedTest2()319 public void deleteFeedTest2() throws Exception { 320 Empty expectedResponse = Empty.newBuilder().build(); 321 mockAssetService.addResponse(expectedResponse); 322 323 String name = "name3373707"; 324 325 client.deleteFeed(name); 326 327 List<AbstractMessage> actualRequests = mockAssetService.getRequests(); 328 Assert.assertEquals(1, actualRequests.size()); 329 DeleteFeedRequest actualRequest = ((DeleteFeedRequest) actualRequests.get(0)); 330 331 Assert.assertEquals(name, actualRequest.getName()); 332 Assert.assertTrue( 333 channelProvider.isHeaderSent( 334 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 335 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 336 } 337 338 @Test deleteFeedExceptionTest2()339 public void deleteFeedExceptionTest2() throws Exception { 340 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 341 mockAssetService.addException(exception); 342 343 try { 344 String name = "name3373707"; 345 client.deleteFeed(name); 346 Assert.fail("No exception raised"); 347 } catch (InvalidArgumentException e) { 348 // Expected exception. 349 } 350 } 351 } 352