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.billing.v1; 18 19 import static com.google.cloud.billing.v1.CloudCatalogClient.ListServicesPagedResponse; 20 import static com.google.cloud.billing.v1.CloudCatalogClient.ListSkusPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 24 import com.google.api.gax.httpjson.testing.MockHttpService; 25 import com.google.api.gax.rpc.ApiClientHeaderProvider; 26 import com.google.api.gax.rpc.ApiException; 27 import com.google.api.gax.rpc.ApiExceptionFactory; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.StatusCode; 30 import com.google.api.gax.rpc.testing.FakeStatusCode; 31 import com.google.cloud.billing.v1.stub.HttpJsonCloudCatalogStub; 32 import com.google.common.collect.Lists; 33 import java.io.IOException; 34 import java.util.Arrays; 35 import java.util.List; 36 import javax.annotation.Generated; 37 import org.junit.After; 38 import org.junit.AfterClass; 39 import org.junit.Assert; 40 import org.junit.Before; 41 import org.junit.BeforeClass; 42 import org.junit.Test; 43 44 @Generated("by gapic-generator-java") 45 public class CloudCatalogClientHttpJsonTest { 46 private static MockHttpService mockService; 47 private static CloudCatalogClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonCloudCatalogStub.getMethodDescriptors(), 54 CloudCatalogSettings.getDefaultEndpoint()); 55 CloudCatalogSettings settings = 56 CloudCatalogSettings.newHttpJsonBuilder() 57 .setTransportChannelProvider( 58 CloudCatalogSettings.defaultHttpJsonTransportProviderBuilder() 59 .setHttpTransport(mockService) 60 .build()) 61 .setCredentialsProvider(NoCredentialsProvider.create()) 62 .build(); 63 client = CloudCatalogClient.create(settings); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 client.close(); 69 } 70 71 @Before setUp()72 public void setUp() {} 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 mockService.reset(); 77 } 78 79 @Test listServicesTest()80 public void listServicesTest() throws Exception { 81 Service responsesElement = Service.newBuilder().build(); 82 ListServicesResponse expectedResponse = 83 ListServicesResponse.newBuilder() 84 .setNextPageToken("") 85 .addAllServices(Arrays.asList(responsesElement)) 86 .build(); 87 mockService.addResponse(expectedResponse); 88 89 ListServicesPagedResponse pagedListResponse = client.listServices(); 90 91 List<Service> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 92 93 Assert.assertEquals(1, resources.size()); 94 Assert.assertEquals(expectedResponse.getServicesList().get(0), resources.get(0)); 95 96 List<String> actualRequests = mockService.getRequestPaths(); 97 Assert.assertEquals(1, actualRequests.size()); 98 99 String apiClientHeaderKey = 100 mockService 101 .getRequestHeaders() 102 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 103 .iterator() 104 .next(); 105 Assert.assertTrue( 106 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 107 .matcher(apiClientHeaderKey) 108 .matches()); 109 } 110 111 @Test listServicesExceptionTest()112 public void listServicesExceptionTest() throws Exception { 113 ApiException exception = 114 ApiExceptionFactory.createException( 115 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 116 mockService.addException(exception); 117 118 try { 119 ListServicesRequest request = 120 ListServicesRequest.newBuilder() 121 .setPageSize(883849137) 122 .setPageToken("pageToken873572522") 123 .build(); 124 client.listServices(request); 125 Assert.fail("No exception raised"); 126 } catch (InvalidArgumentException e) { 127 // Expected exception. 128 } 129 } 130 131 @Test listSkusTest()132 public void listSkusTest() throws Exception { 133 Sku responsesElement = Sku.newBuilder().build(); 134 ListSkusResponse expectedResponse = 135 ListSkusResponse.newBuilder() 136 .setNextPageToken("") 137 .addAllSkus(Arrays.asList(responsesElement)) 138 .build(); 139 mockService.addResponse(expectedResponse); 140 141 ServiceName parent = ServiceName.of("[SERVICE]"); 142 143 ListSkusPagedResponse pagedListResponse = client.listSkus(parent); 144 145 List<Sku> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 146 147 Assert.assertEquals(1, resources.size()); 148 Assert.assertEquals(expectedResponse.getSkusList().get(0), resources.get(0)); 149 150 List<String> actualRequests = mockService.getRequestPaths(); 151 Assert.assertEquals(1, actualRequests.size()); 152 153 String apiClientHeaderKey = 154 mockService 155 .getRequestHeaders() 156 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 157 .iterator() 158 .next(); 159 Assert.assertTrue( 160 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 161 .matcher(apiClientHeaderKey) 162 .matches()); 163 } 164 165 @Test listSkusExceptionTest()166 public void listSkusExceptionTest() throws Exception { 167 ApiException exception = 168 ApiExceptionFactory.createException( 169 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 170 mockService.addException(exception); 171 172 try { 173 ServiceName parent = ServiceName.of("[SERVICE]"); 174 client.listSkus(parent); 175 Assert.fail("No exception raised"); 176 } catch (InvalidArgumentException e) { 177 // Expected exception. 178 } 179 } 180 181 @Test listSkusTest2()182 public void listSkusTest2() throws Exception { 183 Sku responsesElement = Sku.newBuilder().build(); 184 ListSkusResponse expectedResponse = 185 ListSkusResponse.newBuilder() 186 .setNextPageToken("") 187 .addAllSkus(Arrays.asList(responsesElement)) 188 .build(); 189 mockService.addResponse(expectedResponse); 190 191 String parent = "services/service-5597"; 192 193 ListSkusPagedResponse pagedListResponse = client.listSkus(parent); 194 195 List<Sku> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 196 197 Assert.assertEquals(1, resources.size()); 198 Assert.assertEquals(expectedResponse.getSkusList().get(0), resources.get(0)); 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 listSkusExceptionTest2()216 public void listSkusExceptionTest2() 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 String parent = "services/service-5597"; 224 client.listSkus(parent); 225 Assert.fail("No exception raised"); 226 } catch (InvalidArgumentException e) { 227 // Expected exception. 228 } 229 } 230 } 231