• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.grpc.GaxGrpcProperties;
24 import com.google.api.gax.grpc.testing.LocalChannelProvider;
25 import com.google.api.gax.grpc.testing.MockGrpcService;
26 import com.google.api.gax.grpc.testing.MockServiceHelper;
27 import com.google.api.gax.rpc.ApiClientHeaderProvider;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.common.collect.Lists;
30 import com.google.protobuf.AbstractMessage;
31 import io.grpc.StatusRuntimeException;
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.UUID;
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 CloudCatalogClientTest {
46   private static MockCloudCatalog mockCloudCatalog;
47   private static MockServiceHelper mockServiceHelper;
48   private LocalChannelProvider channelProvider;
49   private CloudCatalogClient client;
50 
51   @BeforeClass
startStaticServer()52   public static void startStaticServer() {
53     mockCloudCatalog = new MockCloudCatalog();
54     mockServiceHelper =
55         new MockServiceHelper(
56             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockCloudCatalog));
57     mockServiceHelper.start();
58   }
59 
60   @AfterClass
stopServer()61   public static void stopServer() {
62     mockServiceHelper.stop();
63   }
64 
65   @Before
setUp()66   public void setUp() throws IOException {
67     mockServiceHelper.reset();
68     channelProvider = mockServiceHelper.createChannelProvider();
69     CloudCatalogSettings settings =
70         CloudCatalogSettings.newBuilder()
71             .setTransportChannelProvider(channelProvider)
72             .setCredentialsProvider(NoCredentialsProvider.create())
73             .build();
74     client = CloudCatalogClient.create(settings);
75   }
76 
77   @After
tearDown()78   public void tearDown() throws Exception {
79     client.close();
80   }
81 
82   @Test
listServicesTest()83   public void listServicesTest() throws Exception {
84     Service responsesElement = Service.newBuilder().build();
85     ListServicesResponse expectedResponse =
86         ListServicesResponse.newBuilder()
87             .setNextPageToken("")
88             .addAllServices(Arrays.asList(responsesElement))
89             .build();
90     mockCloudCatalog.addResponse(expectedResponse);
91 
92     ListServicesPagedResponse pagedListResponse = client.listServices();
93 
94     List<Service> resources = Lists.newArrayList(pagedListResponse.iterateAll());
95 
96     Assert.assertEquals(1, resources.size());
97     Assert.assertEquals(expectedResponse.getServicesList().get(0), resources.get(0));
98 
99     List<AbstractMessage> actualRequests = mockCloudCatalog.getRequests();
100     Assert.assertEquals(1, actualRequests.size());
101     ListServicesRequest actualRequest = ((ListServicesRequest) actualRequests.get(0));
102 
103     Assert.assertTrue(
104         channelProvider.isHeaderSent(
105             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
106             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
107   }
108 
109   @Test
listServicesExceptionTest()110   public void listServicesExceptionTest() throws Exception {
111     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
112     mockCloudCatalog.addException(exception);
113 
114     try {
115       ListServicesRequest request =
116           ListServicesRequest.newBuilder()
117               .setPageSize(883849137)
118               .setPageToken("pageToken873572522")
119               .build();
120       client.listServices(request);
121       Assert.fail("No exception raised");
122     } catch (InvalidArgumentException e) {
123       // Expected exception.
124     }
125   }
126 
127   @Test
listSkusTest()128   public void listSkusTest() throws Exception {
129     Sku responsesElement = Sku.newBuilder().build();
130     ListSkusResponse expectedResponse =
131         ListSkusResponse.newBuilder()
132             .setNextPageToken("")
133             .addAllSkus(Arrays.asList(responsesElement))
134             .build();
135     mockCloudCatalog.addResponse(expectedResponse);
136 
137     ServiceName parent = ServiceName.of("[SERVICE]");
138 
139     ListSkusPagedResponse pagedListResponse = client.listSkus(parent);
140 
141     List<Sku> resources = Lists.newArrayList(pagedListResponse.iterateAll());
142 
143     Assert.assertEquals(1, resources.size());
144     Assert.assertEquals(expectedResponse.getSkusList().get(0), resources.get(0));
145 
146     List<AbstractMessage> actualRequests = mockCloudCatalog.getRequests();
147     Assert.assertEquals(1, actualRequests.size());
148     ListSkusRequest actualRequest = ((ListSkusRequest) actualRequests.get(0));
149 
150     Assert.assertEquals(parent.toString(), actualRequest.getParent());
151     Assert.assertTrue(
152         channelProvider.isHeaderSent(
153             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
154             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
155   }
156 
157   @Test
listSkusExceptionTest()158   public void listSkusExceptionTest() throws Exception {
159     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
160     mockCloudCatalog.addException(exception);
161 
162     try {
163       ServiceName parent = ServiceName.of("[SERVICE]");
164       client.listSkus(parent);
165       Assert.fail("No exception raised");
166     } catch (InvalidArgumentException e) {
167       // Expected exception.
168     }
169   }
170 
171   @Test
listSkusTest2()172   public void listSkusTest2() throws Exception {
173     Sku responsesElement = Sku.newBuilder().build();
174     ListSkusResponse expectedResponse =
175         ListSkusResponse.newBuilder()
176             .setNextPageToken("")
177             .addAllSkus(Arrays.asList(responsesElement))
178             .build();
179     mockCloudCatalog.addResponse(expectedResponse);
180 
181     String parent = "parent-995424086";
182 
183     ListSkusPagedResponse pagedListResponse = client.listSkus(parent);
184 
185     List<Sku> resources = Lists.newArrayList(pagedListResponse.iterateAll());
186 
187     Assert.assertEquals(1, resources.size());
188     Assert.assertEquals(expectedResponse.getSkusList().get(0), resources.get(0));
189 
190     List<AbstractMessage> actualRequests = mockCloudCatalog.getRequests();
191     Assert.assertEquals(1, actualRequests.size());
192     ListSkusRequest actualRequest = ((ListSkusRequest) actualRequests.get(0));
193 
194     Assert.assertEquals(parent, actualRequest.getParent());
195     Assert.assertTrue(
196         channelProvider.isHeaderSent(
197             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
198             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
199   }
200 
201   @Test
listSkusExceptionTest2()202   public void listSkusExceptionTest2() throws Exception {
203     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
204     mockCloudCatalog.addException(exception);
205 
206     try {
207       String parent = "parent-995424086";
208       client.listSkus(parent);
209       Assert.fail("No exception raised");
210     } catch (InvalidArgumentException e) {
211       // Expected exception.
212     }
213   }
214 }
215