• 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.api.servicecontrol.v1;
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.api.servicecontrol.v1.stub.HttpJsonServiceControllerStub;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import javax.annotation.Generated;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 
40 @Generated("by gapic-generator-java")
41 public class ServiceControllerClientHttpJsonTest {
42   private static MockHttpService mockService;
43   private static ServiceControllerClient client;
44 
45   @BeforeClass
startStaticServer()46   public static void startStaticServer() throws IOException {
47     mockService =
48         new MockHttpService(
49             HttpJsonServiceControllerStub.getMethodDescriptors(),
50             ServiceControllerSettings.getDefaultEndpoint());
51     ServiceControllerSettings settings =
52         ServiceControllerSettings.newHttpJsonBuilder()
53             .setTransportChannelProvider(
54                 ServiceControllerSettings.defaultHttpJsonTransportProviderBuilder()
55                     .setHttpTransport(mockService)
56                     .build())
57             .setCredentialsProvider(NoCredentialsProvider.create())
58             .build();
59     client = ServiceControllerClient.create(settings);
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     client.close();
65   }
66 
67   @Before
setUp()68   public void setUp() {}
69 
70   @After
tearDown()71   public void tearDown() throws Exception {
72     mockService.reset();
73   }
74 
75   @Test
checkTest()76   public void checkTest() throws Exception {
77     CheckResponse expectedResponse =
78         CheckResponse.newBuilder()
79             .setOperationId("operationId129704162")
80             .addAllCheckErrors(new ArrayList<CheckError>())
81             .setServiceConfigId("serviceConfigId650537426")
82             .setServiceRolloutId("serviceRolloutId47544183")
83             .setCheckInfo(CheckResponse.CheckInfo.newBuilder().build())
84             .build();
85     mockService.addResponse(expectedResponse);
86 
87     CheckRequest request =
88         CheckRequest.newBuilder()
89             .setServiceName("serviceName-4234")
90             .setOperation(Operation.newBuilder().build())
91             .setServiceConfigId("serviceConfigId650537426")
92             .build();
93 
94     CheckResponse actualResponse = client.check(request);
95     Assert.assertEquals(expectedResponse, actualResponse);
96 
97     List<String> actualRequests = mockService.getRequestPaths();
98     Assert.assertEquals(1, actualRequests.size());
99 
100     String apiClientHeaderKey =
101         mockService
102             .getRequestHeaders()
103             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
104             .iterator()
105             .next();
106     Assert.assertTrue(
107         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
108             .matcher(apiClientHeaderKey)
109             .matches());
110   }
111 
112   @Test
checkExceptionTest()113   public void checkExceptionTest() throws Exception {
114     ApiException exception =
115         ApiExceptionFactory.createException(
116             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
117     mockService.addException(exception);
118 
119     try {
120       CheckRequest request =
121           CheckRequest.newBuilder()
122               .setServiceName("serviceName-4234")
123               .setOperation(Operation.newBuilder().build())
124               .setServiceConfigId("serviceConfigId650537426")
125               .build();
126       client.check(request);
127       Assert.fail("No exception raised");
128     } catch (InvalidArgumentException e) {
129       // Expected exception.
130     }
131   }
132 
133   @Test
reportTest()134   public void reportTest() throws Exception {
135     ReportResponse expectedResponse =
136         ReportResponse.newBuilder()
137             .addAllReportErrors(new ArrayList<ReportResponse.ReportError>())
138             .setServiceConfigId("serviceConfigId650537426")
139             .setServiceRolloutId("serviceRolloutId47544183")
140             .build();
141     mockService.addResponse(expectedResponse);
142 
143     ReportRequest request =
144         ReportRequest.newBuilder()
145             .setServiceName("serviceName-4234")
146             .addAllOperations(new ArrayList<Operation>())
147             .setServiceConfigId("serviceConfigId650537426")
148             .build();
149 
150     ReportResponse actualResponse = client.report(request);
151     Assert.assertEquals(expectedResponse, actualResponse);
152 
153     List<String> actualRequests = mockService.getRequestPaths();
154     Assert.assertEquals(1, actualRequests.size());
155 
156     String apiClientHeaderKey =
157         mockService
158             .getRequestHeaders()
159             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
160             .iterator()
161             .next();
162     Assert.assertTrue(
163         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
164             .matcher(apiClientHeaderKey)
165             .matches());
166   }
167 
168   @Test
reportExceptionTest()169   public void reportExceptionTest() throws Exception {
170     ApiException exception =
171         ApiExceptionFactory.createException(
172             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
173     mockService.addException(exception);
174 
175     try {
176       ReportRequest request =
177           ReportRequest.newBuilder()
178               .setServiceName("serviceName-4234")
179               .addAllOperations(new ArrayList<Operation>())
180               .setServiceConfigId("serviceConfigId650537426")
181               .build();
182       client.report(request);
183       Assert.fail("No exception raised");
184     } catch (InvalidArgumentException e) {
185       // Expected exception.
186     }
187   }
188 }
189