• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.resourcenames.ResourceName;
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.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
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 AssetServiceClientTest {
48   private static MockAssetService mockAssetService;
49   private static MockServiceHelper mockServiceHelper;
50   private LocalChannelProvider channelProvider;
51   private AssetServiceClient client;
52 
53   @BeforeClass
startStaticServer()54   public static void startStaticServer() {
55     mockAssetService = new MockAssetService();
56     mockServiceHelper =
57         new MockServiceHelper(
58             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockAssetService));
59     mockServiceHelper.start();
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     mockServiceHelper.stop();
65   }
66 
67   @Before
setUp()68   public void setUp() throws IOException {
69     mockServiceHelper.reset();
70     channelProvider = mockServiceHelper.createChannelProvider();
71     AssetServiceSettings settings =
72         AssetServiceSettings.newBuilder()
73             .setTransportChannelProvider(channelProvider)
74             .setCredentialsProvider(NoCredentialsProvider.create())
75             .build();
76     client = AssetServiceClient.create(settings);
77   }
78 
79   @After
tearDown()80   public void tearDown() throws Exception {
81     client.close();
82   }
83 
84   @Test
listAssetsTest()85   public void listAssetsTest() throws Exception {
86     Asset responsesElement = Asset.newBuilder().build();
87     ListAssetsResponse expectedResponse =
88         ListAssetsResponse.newBuilder()
89             .setNextPageToken("")
90             .addAllAssets(Arrays.asList(responsesElement))
91             .build();
92     mockAssetService.addResponse(expectedResponse);
93 
94     ResourceName parent =
95         new ResourceName() {
96           @Override
97           public Map<String, String> getFieldValuesMap() {
98             Map<String, String> fieldValuesMap = new HashMap<>();
99             fieldValuesMap.put("parent", "parent-4715/parent-4715");
100             return fieldValuesMap;
101           }
102 
103           @Override
104           public String getFieldValue(String fieldName) {
105             return getFieldValuesMap().get(fieldName);
106           }
107 
108           @Override
109           public String toString() {
110             return "parent-4715/parent-4715";
111           }
112         };
113 
114     ListAssetsPagedResponse pagedListResponse = client.listAssets(parent);
115 
116     List<Asset> resources = Lists.newArrayList(pagedListResponse.iterateAll());
117 
118     Assert.assertEquals(1, resources.size());
119     Assert.assertEquals(expectedResponse.getAssetsList().get(0), resources.get(0));
120 
121     List<AbstractMessage> actualRequests = mockAssetService.getRequests();
122     Assert.assertEquals(1, actualRequests.size());
123     ListAssetsRequest actualRequest = ((ListAssetsRequest) actualRequests.get(0));
124 
125     Assert.assertEquals(parent.toString(), actualRequest.getParent());
126     Assert.assertTrue(
127         channelProvider.isHeaderSent(
128             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
129             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
130   }
131 
132   @Test
listAssetsExceptionTest()133   public void listAssetsExceptionTest() throws Exception {
134     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
135     mockAssetService.addException(exception);
136 
137     try {
138       ResourceName parent =
139           new ResourceName() {
140             @Override
141             public Map<String, String> getFieldValuesMap() {
142               Map<String, String> fieldValuesMap = new HashMap<>();
143               fieldValuesMap.put("parent", "parent-4715/parent-4715");
144               return fieldValuesMap;
145             }
146 
147             @Override
148             public String getFieldValue(String fieldName) {
149               return getFieldValuesMap().get(fieldName);
150             }
151 
152             @Override
153             public String toString() {
154               return "parent-4715/parent-4715";
155             }
156           };
157       client.listAssets(parent);
158       Assert.fail("No exception raised");
159     } catch (InvalidArgumentException e) {
160       // Expected exception.
161     }
162   }
163 
164   @Test
listAssetsTest2()165   public void listAssetsTest2() throws Exception {
166     Asset responsesElement = Asset.newBuilder().build();
167     ListAssetsResponse expectedResponse =
168         ListAssetsResponse.newBuilder()
169             .setNextPageToken("")
170             .addAllAssets(Arrays.asList(responsesElement))
171             .build();
172     mockAssetService.addResponse(expectedResponse);
173 
174     String parent = "parent-995424086";
175 
176     ListAssetsPagedResponse pagedListResponse = client.listAssets(parent);
177 
178     List<Asset> resources = Lists.newArrayList(pagedListResponse.iterateAll());
179 
180     Assert.assertEquals(1, resources.size());
181     Assert.assertEquals(expectedResponse.getAssetsList().get(0), resources.get(0));
182 
183     List<AbstractMessage> actualRequests = mockAssetService.getRequests();
184     Assert.assertEquals(1, actualRequests.size());
185     ListAssetsRequest actualRequest = ((ListAssetsRequest) actualRequests.get(0));
186 
187     Assert.assertEquals(parent, actualRequest.getParent());
188     Assert.assertTrue(
189         channelProvider.isHeaderSent(
190             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
191             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
192   }
193 
194   @Test
listAssetsExceptionTest2()195   public void listAssetsExceptionTest2() throws Exception {
196     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
197     mockAssetService.addException(exception);
198 
199     try {
200       String parent = "parent-995424086";
201       client.listAssets(parent);
202       Assert.fail("No exception raised");
203     } catch (InvalidArgumentException e) {
204       // Expected exception.
205     }
206   }
207 }
208