• 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.webrisk.v1;
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.ByteString;
28 import com.google.protobuf.Timestamp;
29 import com.google.webrisk.v1.ComputeThreatListDiffRequest;
30 import com.google.webrisk.v1.ComputeThreatListDiffResponse;
31 import com.google.webrisk.v1.CreateSubmissionRequest;
32 import com.google.webrisk.v1.ProjectName;
33 import com.google.webrisk.v1.SearchHashesRequest;
34 import com.google.webrisk.v1.SearchHashesResponse;
35 import com.google.webrisk.v1.SearchUrisRequest;
36 import com.google.webrisk.v1.SearchUrisResponse;
37 import com.google.webrisk.v1.Submission;
38 import com.google.webrisk.v1.ThreatEntryAdditions;
39 import com.google.webrisk.v1.ThreatEntryRemovals;
40 import com.google.webrisk.v1.ThreatType;
41 import io.grpc.StatusRuntimeException;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.UUID;
47 import javax.annotation.Generated;
48 import org.junit.After;
49 import org.junit.AfterClass;
50 import org.junit.Assert;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 
55 @Generated("by gapic-generator-java")
56 public class WebRiskServiceClientTest {
57   private static MockServiceHelper mockServiceHelper;
58   private static MockWebRiskService mockWebRiskService;
59   private LocalChannelProvider channelProvider;
60   private WebRiskServiceClient client;
61 
62   @BeforeClass
startStaticServer()63   public static void startStaticServer() {
64     mockWebRiskService = new MockWebRiskService();
65     mockServiceHelper =
66         new MockServiceHelper(
67             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockWebRiskService));
68     mockServiceHelper.start();
69   }
70 
71   @AfterClass
stopServer()72   public static void stopServer() {
73     mockServiceHelper.stop();
74   }
75 
76   @Before
setUp()77   public void setUp() throws IOException {
78     mockServiceHelper.reset();
79     channelProvider = mockServiceHelper.createChannelProvider();
80     WebRiskServiceSettings settings =
81         WebRiskServiceSettings.newBuilder()
82             .setTransportChannelProvider(channelProvider)
83             .setCredentialsProvider(NoCredentialsProvider.create())
84             .build();
85     client = WebRiskServiceClient.create(settings);
86   }
87 
88   @After
tearDown()89   public void tearDown() throws Exception {
90     client.close();
91   }
92 
93   @Test
computeThreatListDiffTest()94   public void computeThreatListDiffTest() throws Exception {
95     ComputeThreatListDiffResponse expectedResponse =
96         ComputeThreatListDiffResponse.newBuilder()
97             .setAdditions(ThreatEntryAdditions.newBuilder().build())
98             .setRemovals(ThreatEntryRemovals.newBuilder().build())
99             .setNewVersionToken(ByteString.EMPTY)
100             .setChecksum(ComputeThreatListDiffResponse.Checksum.newBuilder().build())
101             .setRecommendedNextDiff(Timestamp.newBuilder().build())
102             .build();
103     mockWebRiskService.addResponse(expectedResponse);
104 
105     ThreatType threatType = ThreatType.forNumber(0);
106     ByteString versionToken = ByteString.EMPTY;
107     ComputeThreatListDiffRequest.Constraints constraints =
108         ComputeThreatListDiffRequest.Constraints.newBuilder().build();
109 
110     ComputeThreatListDiffResponse actualResponse =
111         client.computeThreatListDiff(threatType, versionToken, constraints);
112     Assert.assertEquals(expectedResponse, actualResponse);
113 
114     List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
115     Assert.assertEquals(1, actualRequests.size());
116     ComputeThreatListDiffRequest actualRequest =
117         ((ComputeThreatListDiffRequest) actualRequests.get(0));
118 
119     Assert.assertEquals(threatType, actualRequest.getThreatType());
120     Assert.assertEquals(versionToken, actualRequest.getVersionToken());
121     Assert.assertEquals(constraints, actualRequest.getConstraints());
122     Assert.assertTrue(
123         channelProvider.isHeaderSent(
124             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
125             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
126   }
127 
128   @Test
computeThreatListDiffExceptionTest()129   public void computeThreatListDiffExceptionTest() throws Exception {
130     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
131     mockWebRiskService.addException(exception);
132 
133     try {
134       ThreatType threatType = ThreatType.forNumber(0);
135       ByteString versionToken = ByteString.EMPTY;
136       ComputeThreatListDiffRequest.Constraints constraints =
137           ComputeThreatListDiffRequest.Constraints.newBuilder().build();
138       client.computeThreatListDiff(threatType, versionToken, constraints);
139       Assert.fail("No exception raised");
140     } catch (InvalidArgumentException e) {
141       // Expected exception.
142     }
143   }
144 
145   @Test
searchUrisTest()146   public void searchUrisTest() throws Exception {
147     SearchUrisResponse expectedResponse =
148         SearchUrisResponse.newBuilder()
149             .setThreat(SearchUrisResponse.ThreatUri.newBuilder().build())
150             .build();
151     mockWebRiskService.addResponse(expectedResponse);
152 
153     String uri = "uri116076";
154     List<ThreatType> threatTypes = new ArrayList<>();
155 
156     SearchUrisResponse actualResponse = client.searchUris(uri, threatTypes);
157     Assert.assertEquals(expectedResponse, actualResponse);
158 
159     List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
160     Assert.assertEquals(1, actualRequests.size());
161     SearchUrisRequest actualRequest = ((SearchUrisRequest) actualRequests.get(0));
162 
163     Assert.assertEquals(uri, actualRequest.getUri());
164     Assert.assertEquals(threatTypes, actualRequest.getThreatTypesList());
165     Assert.assertTrue(
166         channelProvider.isHeaderSent(
167             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
168             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
169   }
170 
171   @Test
searchUrisExceptionTest()172   public void searchUrisExceptionTest() throws Exception {
173     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
174     mockWebRiskService.addException(exception);
175 
176     try {
177       String uri = "uri116076";
178       List<ThreatType> threatTypes = new ArrayList<>();
179       client.searchUris(uri, threatTypes);
180       Assert.fail("No exception raised");
181     } catch (InvalidArgumentException e) {
182       // Expected exception.
183     }
184   }
185 
186   @Test
searchHashesTest()187   public void searchHashesTest() throws Exception {
188     SearchHashesResponse expectedResponse =
189         SearchHashesResponse.newBuilder()
190             .addAllThreats(new ArrayList<SearchHashesResponse.ThreatHash>())
191             .setNegativeExpireTime(Timestamp.newBuilder().build())
192             .build();
193     mockWebRiskService.addResponse(expectedResponse);
194 
195     ByteString hashPrefix = ByteString.EMPTY;
196     List<ThreatType> threatTypes = new ArrayList<>();
197 
198     SearchHashesResponse actualResponse = client.searchHashes(hashPrefix, threatTypes);
199     Assert.assertEquals(expectedResponse, actualResponse);
200 
201     List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
202     Assert.assertEquals(1, actualRequests.size());
203     SearchHashesRequest actualRequest = ((SearchHashesRequest) actualRequests.get(0));
204 
205     Assert.assertEquals(hashPrefix, actualRequest.getHashPrefix());
206     Assert.assertEquals(threatTypes, actualRequest.getThreatTypesList());
207     Assert.assertTrue(
208         channelProvider.isHeaderSent(
209             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
210             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
211   }
212 
213   @Test
searchHashesExceptionTest()214   public void searchHashesExceptionTest() throws Exception {
215     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
216     mockWebRiskService.addException(exception);
217 
218     try {
219       ByteString hashPrefix = ByteString.EMPTY;
220       List<ThreatType> threatTypes = new ArrayList<>();
221       client.searchHashes(hashPrefix, threatTypes);
222       Assert.fail("No exception raised");
223     } catch (InvalidArgumentException e) {
224       // Expected exception.
225     }
226   }
227 
228   @Test
createSubmissionTest()229   public void createSubmissionTest() throws Exception {
230     Submission expectedResponse = Submission.newBuilder().setUri("uri116076").build();
231     mockWebRiskService.addResponse(expectedResponse);
232 
233     ProjectName parent = ProjectName.of("[PROJECT]");
234     Submission submission = Submission.newBuilder().build();
235 
236     Submission actualResponse = client.createSubmission(parent, submission);
237     Assert.assertEquals(expectedResponse, actualResponse);
238 
239     List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
240     Assert.assertEquals(1, actualRequests.size());
241     CreateSubmissionRequest actualRequest = ((CreateSubmissionRequest) actualRequests.get(0));
242 
243     Assert.assertEquals(parent.toString(), actualRequest.getParent());
244     Assert.assertEquals(submission, actualRequest.getSubmission());
245     Assert.assertTrue(
246         channelProvider.isHeaderSent(
247             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
248             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
249   }
250 
251   @Test
createSubmissionExceptionTest()252   public void createSubmissionExceptionTest() throws Exception {
253     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
254     mockWebRiskService.addException(exception);
255 
256     try {
257       ProjectName parent = ProjectName.of("[PROJECT]");
258       Submission submission = Submission.newBuilder().build();
259       client.createSubmission(parent, submission);
260       Assert.fail("No exception raised");
261     } catch (InvalidArgumentException e) {
262       // Expected exception.
263     }
264   }
265 
266   @Test
createSubmissionTest2()267   public void createSubmissionTest2() throws Exception {
268     Submission expectedResponse = Submission.newBuilder().setUri("uri116076").build();
269     mockWebRiskService.addResponse(expectedResponse);
270 
271     String parent = "parent-995424086";
272     Submission submission = Submission.newBuilder().build();
273 
274     Submission actualResponse = client.createSubmission(parent, submission);
275     Assert.assertEquals(expectedResponse, actualResponse);
276 
277     List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
278     Assert.assertEquals(1, actualRequests.size());
279     CreateSubmissionRequest actualRequest = ((CreateSubmissionRequest) actualRequests.get(0));
280 
281     Assert.assertEquals(parent, actualRequest.getParent());
282     Assert.assertEquals(submission, actualRequest.getSubmission());
283     Assert.assertTrue(
284         channelProvider.isHeaderSent(
285             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
286             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
287   }
288 
289   @Test
createSubmissionExceptionTest2()290   public void createSubmissionExceptionTest2() throws Exception {
291     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
292     mockWebRiskService.addException(exception);
293 
294     try {
295       String parent = "parent-995424086";
296       Submission submission = Submission.newBuilder().build();
297       client.createSubmission(parent, submission);
298       Assert.fail("No exception raised");
299     } catch (InvalidArgumentException e) {
300       // Expected exception.
301     }
302   }
303 }
304