• 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.cloudprofiler.v2;
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.cloudprofiler.v2.stub.HttpJsonProfilerServiceStub;
29 import com.google.protobuf.ByteString;
30 import com.google.protobuf.Duration;
31 import com.google.protobuf.FieldMask;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
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 ProfilerServiceClientHttpJsonTest {
46   private static MockHttpService mockService;
47   private static ProfilerServiceClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() throws IOException {
51     mockService =
52         new MockHttpService(
53             HttpJsonProfilerServiceStub.getMethodDescriptors(),
54             ProfilerServiceSettings.getDefaultEndpoint());
55     ProfilerServiceSettings settings =
56         ProfilerServiceSettings.newHttpJsonBuilder()
57             .setTransportChannelProvider(
58                 ProfilerServiceSettings.defaultHttpJsonTransportProviderBuilder()
59                     .setHttpTransport(mockService)
60                     .build())
61             .setCredentialsProvider(NoCredentialsProvider.create())
62             .build();
63     client = ProfilerServiceClient.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
createProfileTest()80   public void createProfileTest() throws Exception {
81     Profile expectedResponse =
82         Profile.newBuilder()
83             .setName("name3373707")
84             .setProfileType(ProfileType.forNumber(0))
85             .setDeployment(Deployment.newBuilder().build())
86             .setDuration(Duration.newBuilder().build())
87             .setProfileBytes(ByteString.EMPTY)
88             .putAllLabels(new HashMap<String, String>())
89             .build();
90     mockService.addResponse(expectedResponse);
91 
92     CreateProfileRequest request =
93         CreateProfileRequest.newBuilder()
94             .setParent(ProjectName.of("[PROJECT]").toString())
95             .setDeployment(Deployment.newBuilder().build())
96             .addAllProfileType(new ArrayList<ProfileType>())
97             .build();
98 
99     Profile actualResponse = client.createProfile(request);
100     Assert.assertEquals(expectedResponse, actualResponse);
101 
102     List<String> actualRequests = mockService.getRequestPaths();
103     Assert.assertEquals(1, actualRequests.size());
104 
105     String apiClientHeaderKey =
106         mockService
107             .getRequestHeaders()
108             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
109             .iterator()
110             .next();
111     Assert.assertTrue(
112         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
113             .matcher(apiClientHeaderKey)
114             .matches());
115   }
116 
117   @Test
createProfileExceptionTest()118   public void createProfileExceptionTest() throws Exception {
119     ApiException exception =
120         ApiExceptionFactory.createException(
121             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
122     mockService.addException(exception);
123 
124     try {
125       CreateProfileRequest request =
126           CreateProfileRequest.newBuilder()
127               .setParent(ProjectName.of("[PROJECT]").toString())
128               .setDeployment(Deployment.newBuilder().build())
129               .addAllProfileType(new ArrayList<ProfileType>())
130               .build();
131       client.createProfile(request);
132       Assert.fail("No exception raised");
133     } catch (InvalidArgumentException e) {
134       // Expected exception.
135     }
136   }
137 
138   @Test
createOfflineProfileTest()139   public void createOfflineProfileTest() throws Exception {
140     Profile expectedResponse =
141         Profile.newBuilder()
142             .setName("name3373707")
143             .setProfileType(ProfileType.forNumber(0))
144             .setDeployment(Deployment.newBuilder().build())
145             .setDuration(Duration.newBuilder().build())
146             .setProfileBytes(ByteString.EMPTY)
147             .putAllLabels(new HashMap<String, String>())
148             .build();
149     mockService.addResponse(expectedResponse);
150 
151     ProjectName parent = ProjectName.of("[PROJECT]");
152     Profile profile = Profile.newBuilder().build();
153 
154     Profile actualResponse = client.createOfflineProfile(parent, profile);
155     Assert.assertEquals(expectedResponse, actualResponse);
156 
157     List<String> actualRequests = mockService.getRequestPaths();
158     Assert.assertEquals(1, actualRequests.size());
159 
160     String apiClientHeaderKey =
161         mockService
162             .getRequestHeaders()
163             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
164             .iterator()
165             .next();
166     Assert.assertTrue(
167         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
168             .matcher(apiClientHeaderKey)
169             .matches());
170   }
171 
172   @Test
createOfflineProfileExceptionTest()173   public void createOfflineProfileExceptionTest() throws Exception {
174     ApiException exception =
175         ApiExceptionFactory.createException(
176             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
177     mockService.addException(exception);
178 
179     try {
180       ProjectName parent = ProjectName.of("[PROJECT]");
181       Profile profile = Profile.newBuilder().build();
182       client.createOfflineProfile(parent, profile);
183       Assert.fail("No exception raised");
184     } catch (InvalidArgumentException e) {
185       // Expected exception.
186     }
187   }
188 
189   @Test
createOfflineProfileTest2()190   public void createOfflineProfileTest2() throws Exception {
191     Profile expectedResponse =
192         Profile.newBuilder()
193             .setName("name3373707")
194             .setProfileType(ProfileType.forNumber(0))
195             .setDeployment(Deployment.newBuilder().build())
196             .setDuration(Duration.newBuilder().build())
197             .setProfileBytes(ByteString.EMPTY)
198             .putAllLabels(new HashMap<String, String>())
199             .build();
200     mockService.addResponse(expectedResponse);
201 
202     String parent = "projects/project-2353";
203     Profile profile = Profile.newBuilder().build();
204 
205     Profile actualResponse = client.createOfflineProfile(parent, profile);
206     Assert.assertEquals(expectedResponse, actualResponse);
207 
208     List<String> actualRequests = mockService.getRequestPaths();
209     Assert.assertEquals(1, actualRequests.size());
210 
211     String apiClientHeaderKey =
212         mockService
213             .getRequestHeaders()
214             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
215             .iterator()
216             .next();
217     Assert.assertTrue(
218         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
219             .matcher(apiClientHeaderKey)
220             .matches());
221   }
222 
223   @Test
createOfflineProfileExceptionTest2()224   public void createOfflineProfileExceptionTest2() throws Exception {
225     ApiException exception =
226         ApiExceptionFactory.createException(
227             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
228     mockService.addException(exception);
229 
230     try {
231       String parent = "projects/project-2353";
232       Profile profile = Profile.newBuilder().build();
233       client.createOfflineProfile(parent, profile);
234       Assert.fail("No exception raised");
235     } catch (InvalidArgumentException e) {
236       // Expected exception.
237     }
238   }
239 
240   @Test
updateProfileTest()241   public void updateProfileTest() throws Exception {
242     Profile expectedResponse =
243         Profile.newBuilder()
244             .setName("name3373707")
245             .setProfileType(ProfileType.forNumber(0))
246             .setDeployment(Deployment.newBuilder().build())
247             .setDuration(Duration.newBuilder().build())
248             .setProfileBytes(ByteString.EMPTY)
249             .putAllLabels(new HashMap<String, String>())
250             .build();
251     mockService.addResponse(expectedResponse);
252 
253     Profile profile =
254         Profile.newBuilder()
255             .setName("projects/project-4284/profiles/profile-4284")
256             .setProfileType(ProfileType.forNumber(0))
257             .setDeployment(Deployment.newBuilder().build())
258             .setDuration(Duration.newBuilder().build())
259             .setProfileBytes(ByteString.EMPTY)
260             .putAllLabels(new HashMap<String, String>())
261             .build();
262     FieldMask updateMask = FieldMask.newBuilder().build();
263 
264     Profile actualResponse = client.updateProfile(profile, updateMask);
265     Assert.assertEquals(expectedResponse, actualResponse);
266 
267     List<String> actualRequests = mockService.getRequestPaths();
268     Assert.assertEquals(1, actualRequests.size());
269 
270     String apiClientHeaderKey =
271         mockService
272             .getRequestHeaders()
273             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
274             .iterator()
275             .next();
276     Assert.assertTrue(
277         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
278             .matcher(apiClientHeaderKey)
279             .matches());
280   }
281 
282   @Test
updateProfileExceptionTest()283   public void updateProfileExceptionTest() throws Exception {
284     ApiException exception =
285         ApiExceptionFactory.createException(
286             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
287     mockService.addException(exception);
288 
289     try {
290       Profile profile =
291           Profile.newBuilder()
292               .setName("projects/project-4284/profiles/profile-4284")
293               .setProfileType(ProfileType.forNumber(0))
294               .setDeployment(Deployment.newBuilder().build())
295               .setDuration(Duration.newBuilder().build())
296               .setProfileBytes(ByteString.EMPTY)
297               .putAllLabels(new HashMap<String, String>())
298               .build();
299       FieldMask updateMask = FieldMask.newBuilder().build();
300       client.updateProfile(profile, updateMask);
301       Assert.fail("No exception raised");
302     } catch (InvalidArgumentException e) {
303       // Expected exception.
304     }
305   }
306 }
307