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