• 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 com.google.api.gax.core.NoCredentialsProvider;
20 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
21 import com.google.api.gax.httpjson.testing.MockHttpService;
22 import com.google.api.gax.rpc.ApiClientHeaderProvider;
23 import com.google.api.gax.rpc.ApiException;
24 import com.google.api.gax.rpc.ApiExceptionFactory;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.api.gax.rpc.StatusCode;
27 import com.google.api.gax.rpc.testing.FakeStatusCode;
28 import com.google.devtools.clouderrorreporting.v1beta1.stub.HttpJsonReportErrorsServiceStub;
29 import java.io.IOException;
30 import java.util.List;
31 import javax.annotation.Generated;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 
39 @Generated("by gapic-generator-java")
40 public class ReportErrorsServiceClientHttpJsonTest {
41   private static MockHttpService mockService;
42   private static ReportErrorsServiceClient client;
43 
44   @BeforeClass
startStaticServer()45   public static void startStaticServer() throws IOException {
46     mockService =
47         new MockHttpService(
48             HttpJsonReportErrorsServiceStub.getMethodDescriptors(),
49             ReportErrorsServiceSettings.getDefaultEndpoint());
50     ReportErrorsServiceSettings settings =
51         ReportErrorsServiceSettings.newHttpJsonBuilder()
52             .setTransportChannelProvider(
53                 ReportErrorsServiceSettings.defaultHttpJsonTransportProviderBuilder()
54                     .setHttpTransport(mockService)
55                     .build())
56             .setCredentialsProvider(NoCredentialsProvider.create())
57             .build();
58     client = ReportErrorsServiceClient.create(settings);
59   }
60 
61   @AfterClass
stopServer()62   public static void stopServer() {
63     client.close();
64   }
65 
66   @Before
setUp()67   public void setUp() {}
68 
69   @After
tearDown()70   public void tearDown() throws Exception {
71     mockService.reset();
72   }
73 
74   @Test
reportErrorEventTest()75   public void reportErrorEventTest() throws Exception {
76     ReportErrorEventResponse expectedResponse = ReportErrorEventResponse.newBuilder().build();
77     mockService.addResponse(expectedResponse);
78 
79     ProjectName projectName = ProjectName.of("[PROJECT]");
80     ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build();
81 
82     ReportErrorEventResponse actualResponse = client.reportErrorEvent(projectName, event);
83     Assert.assertEquals(expectedResponse, actualResponse);
84 
85     List<String> actualRequests = mockService.getRequestPaths();
86     Assert.assertEquals(1, actualRequests.size());
87 
88     String apiClientHeaderKey =
89         mockService
90             .getRequestHeaders()
91             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
92             .iterator()
93             .next();
94     Assert.assertTrue(
95         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
96             .matcher(apiClientHeaderKey)
97             .matches());
98   }
99 
100   @Test
reportErrorEventExceptionTest()101   public void reportErrorEventExceptionTest() throws Exception {
102     ApiException exception =
103         ApiExceptionFactory.createException(
104             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
105     mockService.addException(exception);
106 
107     try {
108       ProjectName projectName = ProjectName.of("[PROJECT]");
109       ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build();
110       client.reportErrorEvent(projectName, event);
111       Assert.fail("No exception raised");
112     } catch (InvalidArgumentException e) {
113       // Expected exception.
114     }
115   }
116 
117   @Test
reportErrorEventTest2()118   public void reportErrorEventTest2() throws Exception {
119     ReportErrorEventResponse expectedResponse = ReportErrorEventResponse.newBuilder().build();
120     mockService.addResponse(expectedResponse);
121 
122     String projectName = "projects/project-3287";
123     ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build();
124 
125     ReportErrorEventResponse actualResponse = client.reportErrorEvent(projectName, event);
126     Assert.assertEquals(expectedResponse, actualResponse);
127 
128     List<String> actualRequests = mockService.getRequestPaths();
129     Assert.assertEquals(1, actualRequests.size());
130 
131     String apiClientHeaderKey =
132         mockService
133             .getRequestHeaders()
134             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
135             .iterator()
136             .next();
137     Assert.assertTrue(
138         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
139             .matcher(apiClientHeaderKey)
140             .matches());
141   }
142 
143   @Test
reportErrorEventExceptionTest2()144   public void reportErrorEventExceptionTest2() throws Exception {
145     ApiException exception =
146         ApiExceptionFactory.createException(
147             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
148     mockService.addException(exception);
149 
150     try {
151       String projectName = "projects/project-3287";
152       ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build();
153       client.reportErrorEvent(projectName, event);
154       Assert.fail("No exception raised");
155     } catch (InvalidArgumentException e) {
156       // Expected exception.
157     }
158   }
159 }
160