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.v1beta1; 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.v1beta1.stub.HttpJsonWebRiskServiceV1Beta1Stub; 29 import com.google.protobuf.ByteString; 30 import com.google.protobuf.Timestamp; 31 import com.google.webrisk.v1beta1.ComputeThreatListDiffRequest; 32 import com.google.webrisk.v1beta1.ComputeThreatListDiffResponse; 33 import com.google.webrisk.v1beta1.SearchHashesResponse; 34 import com.google.webrisk.v1beta1.SearchUrisResponse; 35 import com.google.webrisk.v1beta1.ThreatEntryAdditions; 36 import com.google.webrisk.v1beta1.ThreatEntryRemovals; 37 import com.google.webrisk.v1beta1.ThreatType; 38 import java.io.IOException; 39 import java.util.ArrayList; 40 import java.util.List; 41 import javax.annotation.Generated; 42 import org.junit.After; 43 import org.junit.AfterClass; 44 import org.junit.Assert; 45 import org.junit.Before; 46 import org.junit.BeforeClass; 47 import org.junit.Test; 48 49 @Generated("by gapic-generator-java") 50 public class WebRiskServiceV1Beta1ClientHttpJsonTest { 51 private static MockHttpService mockService; 52 private static WebRiskServiceV1Beta1Client client; 53 54 @BeforeClass startStaticServer()55 public static void startStaticServer() throws IOException { 56 mockService = 57 new MockHttpService( 58 HttpJsonWebRiskServiceV1Beta1Stub.getMethodDescriptors(), 59 WebRiskServiceV1Beta1Settings.getDefaultEndpoint()); 60 WebRiskServiceV1Beta1Settings settings = 61 WebRiskServiceV1Beta1Settings.newHttpJsonBuilder() 62 .setTransportChannelProvider( 63 WebRiskServiceV1Beta1Settings.defaultHttpJsonTransportProviderBuilder() 64 .setHttpTransport(mockService) 65 .build()) 66 .setCredentialsProvider(NoCredentialsProvider.create()) 67 .build(); 68 client = WebRiskServiceV1Beta1Client.create(settings); 69 } 70 71 @AfterClass stopServer()72 public static void stopServer() { 73 client.close(); 74 } 75 76 @Before setUp()77 public void setUp() {} 78 79 @After tearDown()80 public void tearDown() throws Exception { 81 mockService.reset(); 82 } 83 84 @Test computeThreatListDiffTest()85 public void computeThreatListDiffTest() throws Exception { 86 ComputeThreatListDiffResponse expectedResponse = 87 ComputeThreatListDiffResponse.newBuilder() 88 .setAdditions(ThreatEntryAdditions.newBuilder().build()) 89 .setRemovals(ThreatEntryRemovals.newBuilder().build()) 90 .setNewVersionToken(ByteString.EMPTY) 91 .setChecksum(ComputeThreatListDiffResponse.Checksum.newBuilder().build()) 92 .setRecommendedNextDiff(Timestamp.newBuilder().build()) 93 .build(); 94 mockService.addResponse(expectedResponse); 95 96 ThreatType threatType = ThreatType.forNumber(0); 97 ByteString versionToken = ByteString.EMPTY; 98 ComputeThreatListDiffRequest.Constraints constraints = 99 ComputeThreatListDiffRequest.Constraints.newBuilder().build(); 100 101 ComputeThreatListDiffResponse actualResponse = 102 client.computeThreatListDiff(threatType, versionToken, constraints); 103 Assert.assertEquals(expectedResponse, actualResponse); 104 105 List<String> actualRequests = mockService.getRequestPaths(); 106 Assert.assertEquals(1, actualRequests.size()); 107 108 String apiClientHeaderKey = 109 mockService 110 .getRequestHeaders() 111 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 112 .iterator() 113 .next(); 114 Assert.assertTrue( 115 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 116 .matcher(apiClientHeaderKey) 117 .matches()); 118 } 119 120 @Test computeThreatListDiffExceptionTest()121 public void computeThreatListDiffExceptionTest() throws Exception { 122 ApiException exception = 123 ApiExceptionFactory.createException( 124 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 125 mockService.addException(exception); 126 127 try { 128 ThreatType threatType = ThreatType.forNumber(0); 129 ByteString versionToken = ByteString.EMPTY; 130 ComputeThreatListDiffRequest.Constraints constraints = 131 ComputeThreatListDiffRequest.Constraints.newBuilder().build(); 132 client.computeThreatListDiff(threatType, versionToken, constraints); 133 Assert.fail("No exception raised"); 134 } catch (InvalidArgumentException e) { 135 // Expected exception. 136 } 137 } 138 139 @Test searchUrisTest()140 public void searchUrisTest() throws Exception { 141 SearchUrisResponse expectedResponse = 142 SearchUrisResponse.newBuilder() 143 .setThreat(SearchUrisResponse.ThreatUri.newBuilder().build()) 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String uri = "uri116076"; 148 List<ThreatType> threatTypes = new ArrayList<>(); 149 150 SearchUrisResponse actualResponse = client.searchUris(uri, threatTypes); 151 Assert.assertEquals(expectedResponse, actualResponse); 152 153 List<String> actualRequests = mockService.getRequestPaths(); 154 Assert.assertEquals(1, actualRequests.size()); 155 156 String apiClientHeaderKey = 157 mockService 158 .getRequestHeaders() 159 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 160 .iterator() 161 .next(); 162 Assert.assertTrue( 163 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 164 .matcher(apiClientHeaderKey) 165 .matches()); 166 } 167 168 @Test searchUrisExceptionTest()169 public void searchUrisExceptionTest() throws Exception { 170 ApiException exception = 171 ApiExceptionFactory.createException( 172 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 173 mockService.addException(exception); 174 175 try { 176 String uri = "uri116076"; 177 List<ThreatType> threatTypes = new ArrayList<>(); 178 client.searchUris(uri, threatTypes); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test searchHashesTest()186 public void searchHashesTest() throws Exception { 187 SearchHashesResponse expectedResponse = 188 SearchHashesResponse.newBuilder() 189 .addAllThreats(new ArrayList<SearchHashesResponse.ThreatHash>()) 190 .setNegativeExpireTime(Timestamp.newBuilder().build()) 191 .build(); 192 mockService.addResponse(expectedResponse); 193 194 ByteString hashPrefix = ByteString.EMPTY; 195 List<ThreatType> threatTypes = new ArrayList<>(); 196 197 SearchHashesResponse actualResponse = client.searchHashes(hashPrefix, threatTypes); 198 Assert.assertEquals(expectedResponse, actualResponse); 199 200 List<String> actualRequests = mockService.getRequestPaths(); 201 Assert.assertEquals(1, actualRequests.size()); 202 203 String apiClientHeaderKey = 204 mockService 205 .getRequestHeaders() 206 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 207 .iterator() 208 .next(); 209 Assert.assertTrue( 210 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 211 .matcher(apiClientHeaderKey) 212 .matches()); 213 } 214 215 @Test searchHashesExceptionTest()216 public void searchHashesExceptionTest() throws Exception { 217 ApiException exception = 218 ApiExceptionFactory.createException( 219 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 220 mockService.addException(exception); 221 222 try { 223 ByteString hashPrefix = ByteString.EMPTY; 224 List<ThreatType> threatTypes = new ArrayList<>(); 225 client.searchHashes(hashPrefix, threatTypes); 226 Assert.fail("No exception raised"); 227 } catch (InvalidArgumentException e) { 228 // Expected exception. 229 } 230 } 231 } 232