• 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.devtools.clouderrorreporting.v1beta1;
18 
19 import static com.google.devtools.clouderrorreporting.v1beta1.ErrorStatsServiceClient.ListEventsPagedResponse;
20 import static com.google.devtools.clouderrorreporting.v1beta1.ErrorStatsServiceClient.ListGroupStatsPagedResponse;
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 ErrorStatsServiceClientTest {
46   private static MockErrorStatsService mockErrorStatsService;
47   private static MockServiceHelper mockServiceHelper;
48   private LocalChannelProvider channelProvider;
49   private ErrorStatsServiceClient client;
50 
51   @BeforeClass
startStaticServer()52   public static void startStaticServer() {
53     mockErrorStatsService = new MockErrorStatsService();
54     mockServiceHelper =
55         new MockServiceHelper(
56             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockErrorStatsService));
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     ErrorStatsServiceSettings settings =
70         ErrorStatsServiceSettings.newBuilder()
71             .setTransportChannelProvider(channelProvider)
72             .setCredentialsProvider(NoCredentialsProvider.create())
73             .build();
74     client = ErrorStatsServiceClient.create(settings);
75   }
76 
77   @After
tearDown()78   public void tearDown() throws Exception {
79     client.close();
80   }
81 
82   @Test
listGroupStatsTest()83   public void listGroupStatsTest() throws Exception {
84     ErrorGroupStats responsesElement = ErrorGroupStats.newBuilder().build();
85     ListGroupStatsResponse expectedResponse =
86         ListGroupStatsResponse.newBuilder()
87             .setNextPageToken("")
88             .addAllErrorGroupStats(Arrays.asList(responsesElement))
89             .build();
90     mockErrorStatsService.addResponse(expectedResponse);
91 
92     ProjectName projectName = ProjectName.of("[PROJECT]");
93     QueryTimeRange timeRange = QueryTimeRange.newBuilder().build();
94 
95     ListGroupStatsPagedResponse pagedListResponse = client.listGroupStats(projectName, timeRange);
96 
97     List<ErrorGroupStats> resources = Lists.newArrayList(pagedListResponse.iterateAll());
98 
99     Assert.assertEquals(1, resources.size());
100     Assert.assertEquals(expectedResponse.getErrorGroupStatsList().get(0), resources.get(0));
101 
102     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
103     Assert.assertEquals(1, actualRequests.size());
104     ListGroupStatsRequest actualRequest = ((ListGroupStatsRequest) actualRequests.get(0));
105 
106     Assert.assertEquals(projectName.toString(), actualRequest.getProjectName());
107     Assert.assertEquals(timeRange, actualRequest.getTimeRange());
108     Assert.assertTrue(
109         channelProvider.isHeaderSent(
110             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
111             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
112   }
113 
114   @Test
listGroupStatsExceptionTest()115   public void listGroupStatsExceptionTest() throws Exception {
116     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
117     mockErrorStatsService.addException(exception);
118 
119     try {
120       ProjectName projectName = ProjectName.of("[PROJECT]");
121       QueryTimeRange timeRange = QueryTimeRange.newBuilder().build();
122       client.listGroupStats(projectName, timeRange);
123       Assert.fail("No exception raised");
124     } catch (InvalidArgumentException e) {
125       // Expected exception.
126     }
127   }
128 
129   @Test
listGroupStatsTest2()130   public void listGroupStatsTest2() throws Exception {
131     ErrorGroupStats responsesElement = ErrorGroupStats.newBuilder().build();
132     ListGroupStatsResponse expectedResponse =
133         ListGroupStatsResponse.newBuilder()
134             .setNextPageToken("")
135             .addAllErrorGroupStats(Arrays.asList(responsesElement))
136             .build();
137     mockErrorStatsService.addResponse(expectedResponse);
138 
139     String projectName = "projectName-940047036";
140     QueryTimeRange timeRange = QueryTimeRange.newBuilder().build();
141 
142     ListGroupStatsPagedResponse pagedListResponse = client.listGroupStats(projectName, timeRange);
143 
144     List<ErrorGroupStats> resources = Lists.newArrayList(pagedListResponse.iterateAll());
145 
146     Assert.assertEquals(1, resources.size());
147     Assert.assertEquals(expectedResponse.getErrorGroupStatsList().get(0), resources.get(0));
148 
149     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
150     Assert.assertEquals(1, actualRequests.size());
151     ListGroupStatsRequest actualRequest = ((ListGroupStatsRequest) actualRequests.get(0));
152 
153     Assert.assertEquals(projectName, actualRequest.getProjectName());
154     Assert.assertEquals(timeRange, actualRequest.getTimeRange());
155     Assert.assertTrue(
156         channelProvider.isHeaderSent(
157             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
158             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
159   }
160 
161   @Test
listGroupStatsExceptionTest2()162   public void listGroupStatsExceptionTest2() throws Exception {
163     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
164     mockErrorStatsService.addException(exception);
165 
166     try {
167       String projectName = "projectName-940047036";
168       QueryTimeRange timeRange = QueryTimeRange.newBuilder().build();
169       client.listGroupStats(projectName, timeRange);
170       Assert.fail("No exception raised");
171     } catch (InvalidArgumentException e) {
172       // Expected exception.
173     }
174   }
175 
176   @Test
listEventsTest()177   public void listEventsTest() throws Exception {
178     ErrorEvent responsesElement = ErrorEvent.newBuilder().build();
179     ListEventsResponse expectedResponse =
180         ListEventsResponse.newBuilder()
181             .setNextPageToken("")
182             .addAllErrorEvents(Arrays.asList(responsesElement))
183             .build();
184     mockErrorStatsService.addResponse(expectedResponse);
185 
186     ProjectName projectName = ProjectName.of("[PROJECT]");
187     String groupId = "groupId293428218";
188 
189     ListEventsPagedResponse pagedListResponse = client.listEvents(projectName, groupId);
190 
191     List<ErrorEvent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
192 
193     Assert.assertEquals(1, resources.size());
194     Assert.assertEquals(expectedResponse.getErrorEventsList().get(0), resources.get(0));
195 
196     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
197     Assert.assertEquals(1, actualRequests.size());
198     ListEventsRequest actualRequest = ((ListEventsRequest) actualRequests.get(0));
199 
200     Assert.assertEquals(projectName.toString(), actualRequest.getProjectName());
201     Assert.assertEquals(groupId, actualRequest.getGroupId());
202     Assert.assertTrue(
203         channelProvider.isHeaderSent(
204             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
205             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
206   }
207 
208   @Test
listEventsExceptionTest()209   public void listEventsExceptionTest() throws Exception {
210     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
211     mockErrorStatsService.addException(exception);
212 
213     try {
214       ProjectName projectName = ProjectName.of("[PROJECT]");
215       String groupId = "groupId293428218";
216       client.listEvents(projectName, groupId);
217       Assert.fail("No exception raised");
218     } catch (InvalidArgumentException e) {
219       // Expected exception.
220     }
221   }
222 
223   @Test
listEventsTest2()224   public void listEventsTest2() throws Exception {
225     ErrorEvent responsesElement = ErrorEvent.newBuilder().build();
226     ListEventsResponse expectedResponse =
227         ListEventsResponse.newBuilder()
228             .setNextPageToken("")
229             .addAllErrorEvents(Arrays.asList(responsesElement))
230             .build();
231     mockErrorStatsService.addResponse(expectedResponse);
232 
233     String projectName = "projectName-940047036";
234     String groupId = "groupId293428218";
235 
236     ListEventsPagedResponse pagedListResponse = client.listEvents(projectName, groupId);
237 
238     List<ErrorEvent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
239 
240     Assert.assertEquals(1, resources.size());
241     Assert.assertEquals(expectedResponse.getErrorEventsList().get(0), resources.get(0));
242 
243     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
244     Assert.assertEquals(1, actualRequests.size());
245     ListEventsRequest actualRequest = ((ListEventsRequest) actualRequests.get(0));
246 
247     Assert.assertEquals(projectName, actualRequest.getProjectName());
248     Assert.assertEquals(groupId, actualRequest.getGroupId());
249     Assert.assertTrue(
250         channelProvider.isHeaderSent(
251             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
252             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
253   }
254 
255   @Test
listEventsExceptionTest2()256   public void listEventsExceptionTest2() throws Exception {
257     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
258     mockErrorStatsService.addException(exception);
259 
260     try {
261       String projectName = "projectName-940047036";
262       String groupId = "groupId293428218";
263       client.listEvents(projectName, groupId);
264       Assert.fail("No exception raised");
265     } catch (InvalidArgumentException e) {
266       // Expected exception.
267     }
268   }
269 
270   @Test
deleteEventsTest()271   public void deleteEventsTest() throws Exception {
272     DeleteEventsResponse expectedResponse = DeleteEventsResponse.newBuilder().build();
273     mockErrorStatsService.addResponse(expectedResponse);
274 
275     ProjectName projectName = ProjectName.of("[PROJECT]");
276 
277     DeleteEventsResponse actualResponse = client.deleteEvents(projectName);
278     Assert.assertEquals(expectedResponse, actualResponse);
279 
280     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
281     Assert.assertEquals(1, actualRequests.size());
282     DeleteEventsRequest actualRequest = ((DeleteEventsRequest) actualRequests.get(0));
283 
284     Assert.assertEquals(projectName.toString(), actualRequest.getProjectName());
285     Assert.assertTrue(
286         channelProvider.isHeaderSent(
287             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
288             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
289   }
290 
291   @Test
deleteEventsExceptionTest()292   public void deleteEventsExceptionTest() throws Exception {
293     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
294     mockErrorStatsService.addException(exception);
295 
296     try {
297       ProjectName projectName = ProjectName.of("[PROJECT]");
298       client.deleteEvents(projectName);
299       Assert.fail("No exception raised");
300     } catch (InvalidArgumentException e) {
301       // Expected exception.
302     }
303   }
304 
305   @Test
deleteEventsTest2()306   public void deleteEventsTest2() throws Exception {
307     DeleteEventsResponse expectedResponse = DeleteEventsResponse.newBuilder().build();
308     mockErrorStatsService.addResponse(expectedResponse);
309 
310     String projectName = "projectName-940047036";
311 
312     DeleteEventsResponse actualResponse = client.deleteEvents(projectName);
313     Assert.assertEquals(expectedResponse, actualResponse);
314 
315     List<AbstractMessage> actualRequests = mockErrorStatsService.getRequests();
316     Assert.assertEquals(1, actualRequests.size());
317     DeleteEventsRequest actualRequest = ((DeleteEventsRequest) actualRequests.get(0));
318 
319     Assert.assertEquals(projectName, actualRequest.getProjectName());
320     Assert.assertTrue(
321         channelProvider.isHeaderSent(
322             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
323             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
324   }
325 
326   @Test
deleteEventsExceptionTest2()327   public void deleteEventsExceptionTest2() throws Exception {
328     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
329     mockErrorStatsService.addException(exception);
330 
331     try {
332       String projectName = "projectName-940047036";
333       client.deleteEvents(projectName);
334       Assert.fail("No exception raised");
335     } catch (InvalidArgumentException e) {
336       // Expected exception.
337     }
338   }
339 }
340