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.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.common.collect.Lists; 32 import com.google.devtools.clouderrorreporting.v1beta1.stub.HttpJsonErrorStatsServiceStub; 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 ErrorStatsServiceClientHttpJsonTest { 46 private static MockHttpService mockService; 47 private static ErrorStatsServiceClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonErrorStatsServiceStub.getMethodDescriptors(), 54 ErrorStatsServiceSettings.getDefaultEndpoint()); 55 ErrorStatsServiceSettings settings = 56 ErrorStatsServiceSettings.newHttpJsonBuilder() 57 .setTransportChannelProvider( 58 ErrorStatsServiceSettings.defaultHttpJsonTransportProviderBuilder() 59 .setHttpTransport(mockService) 60 .build()) 61 .setCredentialsProvider(NoCredentialsProvider.create()) 62 .build(); 63 client = ErrorStatsServiceClient.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 listGroupStatsTest()80 public void listGroupStatsTest() throws Exception { 81 ErrorGroupStats responsesElement = ErrorGroupStats.newBuilder().build(); 82 ListGroupStatsResponse expectedResponse = 83 ListGroupStatsResponse.newBuilder() 84 .setNextPageToken("") 85 .addAllErrorGroupStats(Arrays.asList(responsesElement)) 86 .build(); 87 mockService.addResponse(expectedResponse); 88 89 ProjectName projectName = ProjectName.of("[PROJECT]"); 90 QueryTimeRange timeRange = QueryTimeRange.newBuilder().build(); 91 92 ListGroupStatsPagedResponse pagedListResponse = client.listGroupStats(projectName, timeRange); 93 94 List<ErrorGroupStats> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 95 96 Assert.assertEquals(1, resources.size()); 97 Assert.assertEquals(expectedResponse.getErrorGroupStatsList().get(0), resources.get(0)); 98 99 List<String> actualRequests = mockService.getRequestPaths(); 100 Assert.assertEquals(1, actualRequests.size()); 101 102 String apiClientHeaderKey = 103 mockService 104 .getRequestHeaders() 105 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 106 .iterator() 107 .next(); 108 Assert.assertTrue( 109 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 110 .matcher(apiClientHeaderKey) 111 .matches()); 112 } 113 114 @Test listGroupStatsExceptionTest()115 public void listGroupStatsExceptionTest() throws Exception { 116 ApiException exception = 117 ApiExceptionFactory.createException( 118 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 119 mockService.addException(exception); 120 121 try { 122 ProjectName projectName = ProjectName.of("[PROJECT]"); 123 QueryTimeRange timeRange = QueryTimeRange.newBuilder().build(); 124 client.listGroupStats(projectName, timeRange); 125 Assert.fail("No exception raised"); 126 } catch (InvalidArgumentException e) { 127 // Expected exception. 128 } 129 } 130 131 @Test listGroupStatsTest2()132 public void listGroupStatsTest2() throws Exception { 133 ErrorGroupStats responsesElement = ErrorGroupStats.newBuilder().build(); 134 ListGroupStatsResponse expectedResponse = 135 ListGroupStatsResponse.newBuilder() 136 .setNextPageToken("") 137 .addAllErrorGroupStats(Arrays.asList(responsesElement)) 138 .build(); 139 mockService.addResponse(expectedResponse); 140 141 String projectName = "projects/project-3287"; 142 QueryTimeRange timeRange = QueryTimeRange.newBuilder().build(); 143 144 ListGroupStatsPagedResponse pagedListResponse = client.listGroupStats(projectName, timeRange); 145 146 List<ErrorGroupStats> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 147 148 Assert.assertEquals(1, resources.size()); 149 Assert.assertEquals(expectedResponse.getErrorGroupStatsList().get(0), resources.get(0)); 150 151 List<String> actualRequests = mockService.getRequestPaths(); 152 Assert.assertEquals(1, actualRequests.size()); 153 154 String apiClientHeaderKey = 155 mockService 156 .getRequestHeaders() 157 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 158 .iterator() 159 .next(); 160 Assert.assertTrue( 161 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 162 .matcher(apiClientHeaderKey) 163 .matches()); 164 } 165 166 @Test listGroupStatsExceptionTest2()167 public void listGroupStatsExceptionTest2() throws Exception { 168 ApiException exception = 169 ApiExceptionFactory.createException( 170 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 171 mockService.addException(exception); 172 173 try { 174 String projectName = "projects/project-3287"; 175 QueryTimeRange timeRange = QueryTimeRange.newBuilder().build(); 176 client.listGroupStats(projectName, timeRange); 177 Assert.fail("No exception raised"); 178 } catch (InvalidArgumentException e) { 179 // Expected exception. 180 } 181 } 182 183 @Test listEventsTest()184 public void listEventsTest() throws Exception { 185 ErrorEvent responsesElement = ErrorEvent.newBuilder().build(); 186 ListEventsResponse expectedResponse = 187 ListEventsResponse.newBuilder() 188 .setNextPageToken("") 189 .addAllErrorEvents(Arrays.asList(responsesElement)) 190 .build(); 191 mockService.addResponse(expectedResponse); 192 193 ProjectName projectName = ProjectName.of("[PROJECT]"); 194 String groupId = "groupId293428218"; 195 196 ListEventsPagedResponse pagedListResponse = client.listEvents(projectName, groupId); 197 198 List<ErrorEvent> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 199 200 Assert.assertEquals(1, resources.size()); 201 Assert.assertEquals(expectedResponse.getErrorEventsList().get(0), resources.get(0)); 202 203 List<String> actualRequests = mockService.getRequestPaths(); 204 Assert.assertEquals(1, actualRequests.size()); 205 206 String apiClientHeaderKey = 207 mockService 208 .getRequestHeaders() 209 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 210 .iterator() 211 .next(); 212 Assert.assertTrue( 213 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 214 .matcher(apiClientHeaderKey) 215 .matches()); 216 } 217 218 @Test listEventsExceptionTest()219 public void listEventsExceptionTest() throws Exception { 220 ApiException exception = 221 ApiExceptionFactory.createException( 222 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 223 mockService.addException(exception); 224 225 try { 226 ProjectName projectName = ProjectName.of("[PROJECT]"); 227 String groupId = "groupId293428218"; 228 client.listEvents(projectName, groupId); 229 Assert.fail("No exception raised"); 230 } catch (InvalidArgumentException e) { 231 // Expected exception. 232 } 233 } 234 235 @Test listEventsTest2()236 public void listEventsTest2() throws Exception { 237 ErrorEvent responsesElement = ErrorEvent.newBuilder().build(); 238 ListEventsResponse expectedResponse = 239 ListEventsResponse.newBuilder() 240 .setNextPageToken("") 241 .addAllErrorEvents(Arrays.asList(responsesElement)) 242 .build(); 243 mockService.addResponse(expectedResponse); 244 245 String projectName = "projects/project-3287"; 246 String groupId = "groupId293428218"; 247 248 ListEventsPagedResponse pagedListResponse = client.listEvents(projectName, groupId); 249 250 List<ErrorEvent> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 251 252 Assert.assertEquals(1, resources.size()); 253 Assert.assertEquals(expectedResponse.getErrorEventsList().get(0), resources.get(0)); 254 255 List<String> actualRequests = mockService.getRequestPaths(); 256 Assert.assertEquals(1, actualRequests.size()); 257 258 String apiClientHeaderKey = 259 mockService 260 .getRequestHeaders() 261 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 262 .iterator() 263 .next(); 264 Assert.assertTrue( 265 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 266 .matcher(apiClientHeaderKey) 267 .matches()); 268 } 269 270 @Test listEventsExceptionTest2()271 public void listEventsExceptionTest2() throws Exception { 272 ApiException exception = 273 ApiExceptionFactory.createException( 274 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 275 mockService.addException(exception); 276 277 try { 278 String projectName = "projects/project-3287"; 279 String groupId = "groupId293428218"; 280 client.listEvents(projectName, groupId); 281 Assert.fail("No exception raised"); 282 } catch (InvalidArgumentException e) { 283 // Expected exception. 284 } 285 } 286 287 @Test deleteEventsTest()288 public void deleteEventsTest() throws Exception { 289 DeleteEventsResponse expectedResponse = DeleteEventsResponse.newBuilder().build(); 290 mockService.addResponse(expectedResponse); 291 292 ProjectName projectName = ProjectName.of("[PROJECT]"); 293 294 DeleteEventsResponse actualResponse = client.deleteEvents(projectName); 295 Assert.assertEquals(expectedResponse, actualResponse); 296 297 List<String> actualRequests = mockService.getRequestPaths(); 298 Assert.assertEquals(1, actualRequests.size()); 299 300 String apiClientHeaderKey = 301 mockService 302 .getRequestHeaders() 303 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 304 .iterator() 305 .next(); 306 Assert.assertTrue( 307 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 308 .matcher(apiClientHeaderKey) 309 .matches()); 310 } 311 312 @Test deleteEventsExceptionTest()313 public void deleteEventsExceptionTest() throws Exception { 314 ApiException exception = 315 ApiExceptionFactory.createException( 316 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 317 mockService.addException(exception); 318 319 try { 320 ProjectName projectName = ProjectName.of("[PROJECT]"); 321 client.deleteEvents(projectName); 322 Assert.fail("No exception raised"); 323 } catch (InvalidArgumentException e) { 324 // Expected exception. 325 } 326 } 327 328 @Test deleteEventsTest2()329 public void deleteEventsTest2() throws Exception { 330 DeleteEventsResponse expectedResponse = DeleteEventsResponse.newBuilder().build(); 331 mockService.addResponse(expectedResponse); 332 333 String projectName = "projects/project-3287"; 334 335 DeleteEventsResponse actualResponse = client.deleteEvents(projectName); 336 Assert.assertEquals(expectedResponse, actualResponse); 337 338 List<String> actualRequests = mockService.getRequestPaths(); 339 Assert.assertEquals(1, actualRequests.size()); 340 341 String apiClientHeaderKey = 342 mockService 343 .getRequestHeaders() 344 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 345 .iterator() 346 .next(); 347 Assert.assertTrue( 348 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 349 .matcher(apiClientHeaderKey) 350 .matches()); 351 } 352 353 @Test deleteEventsExceptionTest2()354 public void deleteEventsExceptionTest2() throws Exception { 355 ApiException exception = 356 ApiExceptionFactory.createException( 357 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 358 mockService.addException(exception); 359 360 try { 361 String projectName = "projects/project-3287"; 362 client.deleteEvents(projectName); 363 Assert.fail("No exception raised"); 364 } catch (InvalidArgumentException e) { 365 // Expected exception. 366 } 367 } 368 } 369