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.appengine.v1; 18 19 import static com.google.appengine.v1.FirewallClient.ListIngressRulesPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.appengine.v1.firewall.FirewallRule; 31 import com.google.appengine.v1.stub.HttpJsonFirewallStub; 32 import com.google.common.collect.Lists; 33 import com.google.protobuf.Empty; 34 import com.google.protobuf.FieldMask; 35 import java.io.IOException; 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.List; 39 import javax.annotation.Generated; 40 import org.junit.After; 41 import org.junit.AfterClass; 42 import org.junit.Assert; 43 import org.junit.Before; 44 import org.junit.BeforeClass; 45 import org.junit.Test; 46 47 @Generated("by gapic-generator-java") 48 public class FirewallClientHttpJsonTest { 49 private static MockHttpService mockService; 50 private static FirewallClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() throws IOException { 54 mockService = 55 new MockHttpService( 56 HttpJsonFirewallStub.getMethodDescriptors(), FirewallSettings.getDefaultEndpoint()); 57 FirewallSettings settings = 58 FirewallSettings.newHttpJsonBuilder() 59 .setTransportChannelProvider( 60 FirewallSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = FirewallClient.create(settings); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 client.close(); 71 } 72 73 @Before setUp()74 public void setUp() {} 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 mockService.reset(); 79 } 80 81 @Test listIngressRulesTest()82 public void listIngressRulesTest() throws Exception { 83 FirewallRule responsesElement = FirewallRule.newBuilder().build(); 84 ListIngressRulesResponse expectedResponse = 85 ListIngressRulesResponse.newBuilder() 86 .setNextPageToken("") 87 .addAllIngressRules(Arrays.asList(responsesElement)) 88 .build(); 89 mockService.addResponse(expectedResponse); 90 91 ListIngressRulesRequest request = 92 ListIngressRulesRequest.newBuilder() 93 .setParent("apps/app-3559") 94 .setPageSize(883849137) 95 .setPageToken("pageToken873572522") 96 .setMatchingAddress("matchingAddress861962551") 97 .build(); 98 99 ListIngressRulesPagedResponse pagedListResponse = client.listIngressRules(request); 100 101 List<FirewallRule> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 102 103 Assert.assertEquals(1, resources.size()); 104 Assert.assertEquals(expectedResponse.getIngressRulesList().get(0), resources.get(0)); 105 106 List<String> actualRequests = mockService.getRequestPaths(); 107 Assert.assertEquals(1, actualRequests.size()); 108 109 String apiClientHeaderKey = 110 mockService 111 .getRequestHeaders() 112 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 113 .iterator() 114 .next(); 115 Assert.assertTrue( 116 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 117 .matcher(apiClientHeaderKey) 118 .matches()); 119 } 120 121 @Test listIngressRulesExceptionTest()122 public void listIngressRulesExceptionTest() throws Exception { 123 ApiException exception = 124 ApiExceptionFactory.createException( 125 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 126 mockService.addException(exception); 127 128 try { 129 ListIngressRulesRequest request = 130 ListIngressRulesRequest.newBuilder() 131 .setParent("apps/app-3559") 132 .setPageSize(883849137) 133 .setPageToken("pageToken873572522") 134 .setMatchingAddress("matchingAddress861962551") 135 .build(); 136 client.listIngressRules(request); 137 Assert.fail("No exception raised"); 138 } catch (InvalidArgumentException e) { 139 // Expected exception. 140 } 141 } 142 143 @Test batchUpdateIngressRulesTest()144 public void batchUpdateIngressRulesTest() throws Exception { 145 BatchUpdateIngressRulesResponse expectedResponse = 146 BatchUpdateIngressRulesResponse.newBuilder() 147 .addAllIngressRules(new ArrayList<FirewallRule>()) 148 .build(); 149 mockService.addResponse(expectedResponse); 150 151 BatchUpdateIngressRulesRequest request = 152 BatchUpdateIngressRulesRequest.newBuilder() 153 .setName("apps/app-4902/firewall/ingressRules") 154 .addAllIngressRules(new ArrayList<FirewallRule>()) 155 .build(); 156 157 BatchUpdateIngressRulesResponse actualResponse = client.batchUpdateIngressRules(request); 158 Assert.assertEquals(expectedResponse, actualResponse); 159 160 List<String> actualRequests = mockService.getRequestPaths(); 161 Assert.assertEquals(1, actualRequests.size()); 162 163 String apiClientHeaderKey = 164 mockService 165 .getRequestHeaders() 166 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 167 .iterator() 168 .next(); 169 Assert.assertTrue( 170 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 171 .matcher(apiClientHeaderKey) 172 .matches()); 173 } 174 175 @Test batchUpdateIngressRulesExceptionTest()176 public void batchUpdateIngressRulesExceptionTest() throws Exception { 177 ApiException exception = 178 ApiExceptionFactory.createException( 179 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 180 mockService.addException(exception); 181 182 try { 183 BatchUpdateIngressRulesRequest request = 184 BatchUpdateIngressRulesRequest.newBuilder() 185 .setName("apps/app-4902/firewall/ingressRules") 186 .addAllIngressRules(new ArrayList<FirewallRule>()) 187 .build(); 188 client.batchUpdateIngressRules(request); 189 Assert.fail("No exception raised"); 190 } catch (InvalidArgumentException e) { 191 // Expected exception. 192 } 193 } 194 195 @Test createIngressRuleTest()196 public void createIngressRuleTest() throws Exception { 197 FirewallRule expectedResponse = 198 FirewallRule.newBuilder() 199 .setPriority(-1165461084) 200 .setSourceRange("sourceRange-97207838") 201 .setDescription("description-1724546052") 202 .build(); 203 mockService.addResponse(expectedResponse); 204 205 CreateIngressRuleRequest request = 206 CreateIngressRuleRequest.newBuilder() 207 .setParent("apps/app-3559") 208 .setRule(FirewallRule.newBuilder().build()) 209 .build(); 210 211 FirewallRule actualResponse = client.createIngressRule(request); 212 Assert.assertEquals(expectedResponse, actualResponse); 213 214 List<String> actualRequests = mockService.getRequestPaths(); 215 Assert.assertEquals(1, actualRequests.size()); 216 217 String apiClientHeaderKey = 218 mockService 219 .getRequestHeaders() 220 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 221 .iterator() 222 .next(); 223 Assert.assertTrue( 224 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 225 .matcher(apiClientHeaderKey) 226 .matches()); 227 } 228 229 @Test createIngressRuleExceptionTest()230 public void createIngressRuleExceptionTest() throws Exception { 231 ApiException exception = 232 ApiExceptionFactory.createException( 233 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 234 mockService.addException(exception); 235 236 try { 237 CreateIngressRuleRequest request = 238 CreateIngressRuleRequest.newBuilder() 239 .setParent("apps/app-3559") 240 .setRule(FirewallRule.newBuilder().build()) 241 .build(); 242 client.createIngressRule(request); 243 Assert.fail("No exception raised"); 244 } catch (InvalidArgumentException e) { 245 // Expected exception. 246 } 247 } 248 249 @Test getIngressRuleTest()250 public void getIngressRuleTest() throws Exception { 251 FirewallRule expectedResponse = 252 FirewallRule.newBuilder() 253 .setPriority(-1165461084) 254 .setSourceRange("sourceRange-97207838") 255 .setDescription("description-1724546052") 256 .build(); 257 mockService.addResponse(expectedResponse); 258 259 GetIngressRuleRequest request = 260 GetIngressRuleRequest.newBuilder() 261 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 262 .build(); 263 264 FirewallRule actualResponse = client.getIngressRule(request); 265 Assert.assertEquals(expectedResponse, actualResponse); 266 267 List<String> actualRequests = mockService.getRequestPaths(); 268 Assert.assertEquals(1, actualRequests.size()); 269 270 String apiClientHeaderKey = 271 mockService 272 .getRequestHeaders() 273 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 274 .iterator() 275 .next(); 276 Assert.assertTrue( 277 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 278 .matcher(apiClientHeaderKey) 279 .matches()); 280 } 281 282 @Test getIngressRuleExceptionTest()283 public void getIngressRuleExceptionTest() throws Exception { 284 ApiException exception = 285 ApiExceptionFactory.createException( 286 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 287 mockService.addException(exception); 288 289 try { 290 GetIngressRuleRequest request = 291 GetIngressRuleRequest.newBuilder() 292 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 293 .build(); 294 client.getIngressRule(request); 295 Assert.fail("No exception raised"); 296 } catch (InvalidArgumentException e) { 297 // Expected exception. 298 } 299 } 300 301 @Test updateIngressRuleTest()302 public void updateIngressRuleTest() throws Exception { 303 FirewallRule expectedResponse = 304 FirewallRule.newBuilder() 305 .setPriority(-1165461084) 306 .setSourceRange("sourceRange-97207838") 307 .setDescription("description-1724546052") 308 .build(); 309 mockService.addResponse(expectedResponse); 310 311 UpdateIngressRuleRequest request = 312 UpdateIngressRuleRequest.newBuilder() 313 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 314 .setRule(FirewallRule.newBuilder().build()) 315 .setUpdateMask(FieldMask.newBuilder().build()) 316 .build(); 317 318 FirewallRule actualResponse = client.updateIngressRule(request); 319 Assert.assertEquals(expectedResponse, actualResponse); 320 321 List<String> actualRequests = mockService.getRequestPaths(); 322 Assert.assertEquals(1, actualRequests.size()); 323 324 String apiClientHeaderKey = 325 mockService 326 .getRequestHeaders() 327 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 328 .iterator() 329 .next(); 330 Assert.assertTrue( 331 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 332 .matcher(apiClientHeaderKey) 333 .matches()); 334 } 335 336 @Test updateIngressRuleExceptionTest()337 public void updateIngressRuleExceptionTest() throws Exception { 338 ApiException exception = 339 ApiExceptionFactory.createException( 340 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 341 mockService.addException(exception); 342 343 try { 344 UpdateIngressRuleRequest request = 345 UpdateIngressRuleRequest.newBuilder() 346 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 347 .setRule(FirewallRule.newBuilder().build()) 348 .setUpdateMask(FieldMask.newBuilder().build()) 349 .build(); 350 client.updateIngressRule(request); 351 Assert.fail("No exception raised"); 352 } catch (InvalidArgumentException e) { 353 // Expected exception. 354 } 355 } 356 357 @Test deleteIngressRuleTest()358 public void deleteIngressRuleTest() throws Exception { 359 Empty expectedResponse = Empty.newBuilder().build(); 360 mockService.addResponse(expectedResponse); 361 362 DeleteIngressRuleRequest request = 363 DeleteIngressRuleRequest.newBuilder() 364 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 365 .build(); 366 367 client.deleteIngressRule(request); 368 369 List<String> actualRequests = mockService.getRequestPaths(); 370 Assert.assertEquals(1, actualRequests.size()); 371 372 String apiClientHeaderKey = 373 mockService 374 .getRequestHeaders() 375 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 376 .iterator() 377 .next(); 378 Assert.assertTrue( 379 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 380 .matcher(apiClientHeaderKey) 381 .matches()); 382 } 383 384 @Test deleteIngressRuleExceptionTest()385 public void deleteIngressRuleExceptionTest() throws Exception { 386 ApiException exception = 387 ApiExceptionFactory.createException( 388 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 389 mockService.addException(exception); 390 391 try { 392 DeleteIngressRuleRequest request = 393 DeleteIngressRuleRequest.newBuilder() 394 .setName("apps/app-389/firewall/ingressRules/ingressRule-389") 395 .build(); 396 client.deleteIngressRule(request); 397 Assert.fail("No exception raised"); 398 } catch (InvalidArgumentException e) { 399 // Expected exception. 400 } 401 } 402 } 403