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