• 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.dataflow.v1beta3;
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.dataflow.v1beta3.stub.HttpJsonTemplatesServiceStub;
29 import com.google.protobuf.Timestamp;
30 import com.google.rpc.Status;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import javax.annotation.Generated;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 
43 @Generated("by gapic-generator-java")
44 public class TemplatesServiceClientHttpJsonTest {
45   private static MockHttpService mockService;
46   private static TemplatesServiceClient client;
47 
48   @BeforeClass
startStaticServer()49   public static void startStaticServer() throws IOException {
50     mockService =
51         new MockHttpService(
52             HttpJsonTemplatesServiceStub.getMethodDescriptors(),
53             TemplatesServiceSettings.getDefaultEndpoint());
54     TemplatesServiceSettings settings =
55         TemplatesServiceSettings.newHttpJsonBuilder()
56             .setTransportChannelProvider(
57                 TemplatesServiceSettings.defaultHttpJsonTransportProviderBuilder()
58                     .setHttpTransport(mockService)
59                     .build())
60             .setCredentialsProvider(NoCredentialsProvider.create())
61             .build();
62     client = TemplatesServiceClient.create(settings);
63   }
64 
65   @AfterClass
stopServer()66   public static void stopServer() {
67     client.close();
68   }
69 
70   @Before
setUp()71   public void setUp() {}
72 
73   @After
tearDown()74   public void tearDown() throws Exception {
75     mockService.reset();
76   }
77 
78   @Test
createJobFromTemplateTest()79   public void createJobFromTemplateTest() throws Exception {
80     Job expectedResponse =
81         Job.newBuilder()
82             .setId("id3355")
83             .setProjectId("projectId-894832108")
84             .setName("name3373707")
85             .setType(JobType.forNumber(0))
86             .setEnvironment(Environment.newBuilder().build())
87             .addAllSteps(new ArrayList<Step>())
88             .setStepsLocation("stepsLocation1335803900")
89             .setCurrentState(JobState.forNumber(0))
90             .setCurrentStateTime(Timestamp.newBuilder().build())
91             .setRequestedState(JobState.forNumber(0))
92             .setExecutionInfo(JobExecutionInfo.newBuilder().build())
93             .setCreateTime(Timestamp.newBuilder().build())
94             .setReplaceJobId("replaceJobId441554788")
95             .putAllTransformNameMapping(new HashMap<String, String>())
96             .setClientRequestId("clientRequestId848942207")
97             .setReplacedByJobId("replacedByJobId614115665")
98             .addAllTempFiles(new ArrayList<String>())
99             .putAllLabels(new HashMap<String, String>())
100             .setLocation("location1901043637")
101             .setPipelineDescription(PipelineDescription.newBuilder().build())
102             .addAllStageStates(new ArrayList<ExecutionStageState>())
103             .setJobMetadata(JobMetadata.newBuilder().build())
104             .setStartTime(Timestamp.newBuilder().build())
105             .setCreatedFromSnapshotId("createdFromSnapshotId851802705")
106             .setSatisfiesPzs(true)
107             .build();
108     mockService.addResponse(expectedResponse);
109 
110     CreateJobFromTemplateRequest request =
111         CreateJobFromTemplateRequest.newBuilder()
112             .setProjectId("projectId-1530")
113             .setJobName("jobName-1438096408")
114             .putAllParameters(new HashMap<String, String>())
115             .setEnvironment(RuntimeEnvironment.newBuilder().build())
116             .setLocation("location-9355")
117             .build();
118 
119     Job actualResponse = client.createJobFromTemplate(request);
120     Assert.assertEquals(expectedResponse, actualResponse);
121 
122     List<String> actualRequests = mockService.getRequestPaths();
123     Assert.assertEquals(1, actualRequests.size());
124 
125     String apiClientHeaderKey =
126         mockService
127             .getRequestHeaders()
128             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
129             .iterator()
130             .next();
131     Assert.assertTrue(
132         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
133             .matcher(apiClientHeaderKey)
134             .matches());
135   }
136 
137   @Test
createJobFromTemplateExceptionTest()138   public void createJobFromTemplateExceptionTest() throws Exception {
139     ApiException exception =
140         ApiExceptionFactory.createException(
141             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
142     mockService.addException(exception);
143 
144     try {
145       CreateJobFromTemplateRequest request =
146           CreateJobFromTemplateRequest.newBuilder()
147               .setProjectId("projectId-1530")
148               .setJobName("jobName-1438096408")
149               .putAllParameters(new HashMap<String, String>())
150               .setEnvironment(RuntimeEnvironment.newBuilder().build())
151               .setLocation("location-9355")
152               .build();
153       client.createJobFromTemplate(request);
154       Assert.fail("No exception raised");
155     } catch (InvalidArgumentException e) {
156       // Expected exception.
157     }
158   }
159 
160   @Test
launchTemplateTest()161   public void launchTemplateTest() throws Exception {
162     LaunchTemplateResponse expectedResponse =
163         LaunchTemplateResponse.newBuilder().setJob(Job.newBuilder().build()).build();
164     mockService.addResponse(expectedResponse);
165 
166     LaunchTemplateRequest request =
167         LaunchTemplateRequest.newBuilder()
168             .setProjectId("projectId-1530")
169             .setValidateOnly(true)
170             .setLaunchParameters(LaunchTemplateParameters.newBuilder().build())
171             .setLocation("location-9355")
172             .build();
173 
174     LaunchTemplateResponse actualResponse = client.launchTemplate(request);
175     Assert.assertEquals(expectedResponse, actualResponse);
176 
177     List<String> actualRequests = mockService.getRequestPaths();
178     Assert.assertEquals(1, actualRequests.size());
179 
180     String apiClientHeaderKey =
181         mockService
182             .getRequestHeaders()
183             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
184             .iterator()
185             .next();
186     Assert.assertTrue(
187         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
188             .matcher(apiClientHeaderKey)
189             .matches());
190   }
191 
192   @Test
launchTemplateExceptionTest()193   public void launchTemplateExceptionTest() throws Exception {
194     ApiException exception =
195         ApiExceptionFactory.createException(
196             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
197     mockService.addException(exception);
198 
199     try {
200       LaunchTemplateRequest request =
201           LaunchTemplateRequest.newBuilder()
202               .setProjectId("projectId-1530")
203               .setValidateOnly(true)
204               .setLaunchParameters(LaunchTemplateParameters.newBuilder().build())
205               .setLocation("location-9355")
206               .build();
207       client.launchTemplate(request);
208       Assert.fail("No exception raised");
209     } catch (InvalidArgumentException e) {
210       // Expected exception.
211     }
212   }
213 
214   @Test
getTemplateTest()215   public void getTemplateTest() throws Exception {
216     GetTemplateResponse expectedResponse =
217         GetTemplateResponse.newBuilder()
218             .setStatus(Status.newBuilder().build())
219             .setMetadata(TemplateMetadata.newBuilder().build())
220             .setRuntimeMetadata(RuntimeMetadata.newBuilder().build())
221             .build();
222     mockService.addResponse(expectedResponse);
223 
224     GetTemplateRequest request =
225         GetTemplateRequest.newBuilder()
226             .setProjectId("projectId-1530")
227             .setLocation("location-9355")
228             .build();
229 
230     GetTemplateResponse actualResponse = client.getTemplate(request);
231     Assert.assertEquals(expectedResponse, actualResponse);
232 
233     List<String> actualRequests = mockService.getRequestPaths();
234     Assert.assertEquals(1, actualRequests.size());
235 
236     String apiClientHeaderKey =
237         mockService
238             .getRequestHeaders()
239             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
240             .iterator()
241             .next();
242     Assert.assertTrue(
243         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
244             .matcher(apiClientHeaderKey)
245             .matches());
246   }
247 
248   @Test
getTemplateExceptionTest()249   public void getTemplateExceptionTest() throws Exception {
250     ApiException exception =
251         ApiExceptionFactory.createException(
252             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
253     mockService.addException(exception);
254 
255     try {
256       GetTemplateRequest request =
257           GetTemplateRequest.newBuilder()
258               .setProjectId("projectId-1530")
259               .setLocation("location-9355")
260               .build();
261       client.getTemplate(request);
262       Assert.fail("No exception raised");
263     } catch (InvalidArgumentException e) {
264       // Expected exception.
265     }
266   }
267 }
268