• 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.v1p7beta1;
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.api.gax.rpc.StatusCode;
27 import com.google.longrunning.Operation;
28 import com.google.protobuf.AbstractMessage;
29 import com.google.protobuf.Any;
30 import com.google.protobuf.Timestamp;
31 import io.grpc.StatusRuntimeException;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.UUID;
37 import java.util.concurrent.ExecutionException;
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
exportAssetsTest()85   public void exportAssetsTest() throws Exception {
86     ExportAssetsResponse expectedResponse =
87         ExportAssetsResponse.newBuilder()
88             .setReadTime(Timestamp.newBuilder().build())
89             .setOutputConfig(OutputConfig.newBuilder().build())
90             .setOutputResult(OutputResult.newBuilder().build())
91             .build();
92     Operation resultOperation =
93         Operation.newBuilder()
94             .setName("exportAssetsTest")
95             .setDone(true)
96             .setResponse(Any.pack(expectedResponse))
97             .build();
98     mockAssetService.addResponse(resultOperation);
99 
100     ExportAssetsRequest request =
101         ExportAssetsRequest.newBuilder()
102             .setParent("ExportAssetsRequest-846449128".toString())
103             .setReadTime(Timestamp.newBuilder().build())
104             .addAllAssetTypes(new ArrayList<String>())
105             .setContentType(ContentType.forNumber(0))
106             .setOutputConfig(OutputConfig.newBuilder().build())
107             .addAllRelationshipTypes(new ArrayList<String>())
108             .build();
109 
110     ExportAssetsResponse actualResponse = client.exportAssetsAsync(request).get();
111     Assert.assertEquals(expectedResponse, actualResponse);
112 
113     List<AbstractMessage> actualRequests = mockAssetService.getRequests();
114     Assert.assertEquals(1, actualRequests.size());
115     ExportAssetsRequest actualRequest = ((ExportAssetsRequest) actualRequests.get(0));
116 
117     Assert.assertEquals(request.getParent(), actualRequest.getParent());
118     Assert.assertEquals(request.getReadTime(), actualRequest.getReadTime());
119     Assert.assertEquals(request.getAssetTypesList(), actualRequest.getAssetTypesList());
120     Assert.assertEquals(request.getContentType(), actualRequest.getContentType());
121     Assert.assertEquals(request.getOutputConfig(), actualRequest.getOutputConfig());
122     Assert.assertEquals(
123         request.getRelationshipTypesList(), actualRequest.getRelationshipTypesList());
124     Assert.assertTrue(
125         channelProvider.isHeaderSent(
126             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
127             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
128   }
129 
130   @Test
exportAssetsExceptionTest()131   public void exportAssetsExceptionTest() throws Exception {
132     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
133     mockAssetService.addException(exception);
134 
135     try {
136       ExportAssetsRequest request =
137           ExportAssetsRequest.newBuilder()
138               .setParent("ExportAssetsRequest-846449128".toString())
139               .setReadTime(Timestamp.newBuilder().build())
140               .addAllAssetTypes(new ArrayList<String>())
141               .setContentType(ContentType.forNumber(0))
142               .setOutputConfig(OutputConfig.newBuilder().build())
143               .addAllRelationshipTypes(new ArrayList<String>())
144               .build();
145       client.exportAssetsAsync(request).get();
146       Assert.fail("No exception raised");
147     } catch (ExecutionException e) {
148       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
149       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
150       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
151     }
152   }
153 }
154