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.orchestration.airflow.service.v1; 18 19 import static com.google.cloud.orchestration.airflow.service.v1.ImageVersionsClient.ListImageVersionsPagedResponse; 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.cloud.orchestration.airflow.service.v1.stub.HttpJsonImageVersionsStub; 31 import com.google.common.collect.Lists; 32 import java.io.IOException; 33 import java.util.Arrays; 34 import java.util.List; 35 import javax.annotation.Generated; 36 import org.junit.After; 37 import org.junit.AfterClass; 38 import org.junit.Assert; 39 import org.junit.Before; 40 import org.junit.BeforeClass; 41 import org.junit.Test; 42 43 @Generated("by gapic-generator-java") 44 public class ImageVersionsClientHttpJsonTest { 45 private static MockHttpService mockService; 46 private static ImageVersionsClient client; 47 48 @BeforeClass startStaticServer()49 public static void startStaticServer() throws IOException { 50 mockService = 51 new MockHttpService( 52 HttpJsonImageVersionsStub.getMethodDescriptors(), 53 ImageVersionsSettings.getDefaultEndpoint()); 54 ImageVersionsSettings settings = 55 ImageVersionsSettings.newHttpJsonBuilder() 56 .setTransportChannelProvider( 57 ImageVersionsSettings.defaultHttpJsonTransportProviderBuilder() 58 .setHttpTransport(mockService) 59 .build()) 60 .setCredentialsProvider(NoCredentialsProvider.create()) 61 .build(); 62 client = ImageVersionsClient.create(settings); 63 } 64 65 @AfterClass stopServer()66 public static void stopServer() { 67 client.close(); 68 } 69 70 @Before setUp()71 public void setUp() {} 72 73 @After tearDown()74 public void tearDown() throws Exception { 75 mockService.reset(); 76 } 77 78 @Test listImageVersionsTest()79 public void listImageVersionsTest() throws Exception { 80 ImageVersion responsesElement = ImageVersion.newBuilder().build(); 81 ListImageVersionsResponse expectedResponse = 82 ListImageVersionsResponse.newBuilder() 83 .setNextPageToken("") 84 .addAllImageVersions(Arrays.asList(responsesElement)) 85 .build(); 86 mockService.addResponse(expectedResponse); 87 88 String parent = "projects/project-5833/locations/location-5833"; 89 90 ListImageVersionsPagedResponse pagedListResponse = client.listImageVersions(parent); 91 92 List<ImageVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 93 94 Assert.assertEquals(1, resources.size()); 95 Assert.assertEquals(expectedResponse.getImageVersionsList().get(0), resources.get(0)); 96 97 List<String> actualRequests = mockService.getRequestPaths(); 98 Assert.assertEquals(1, actualRequests.size()); 99 100 String apiClientHeaderKey = 101 mockService 102 .getRequestHeaders() 103 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 104 .iterator() 105 .next(); 106 Assert.assertTrue( 107 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 108 .matcher(apiClientHeaderKey) 109 .matches()); 110 } 111 112 @Test listImageVersionsExceptionTest()113 public void listImageVersionsExceptionTest() throws Exception { 114 ApiException exception = 115 ApiExceptionFactory.createException( 116 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 117 mockService.addException(exception); 118 119 try { 120 String parent = "projects/project-5833/locations/location-5833"; 121 client.listImageVersions(parent); 122 Assert.fail("No exception raised"); 123 } catch (InvalidArgumentException e) { 124 // Expected exception. 125 } 126 } 127 } 128