• 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.grpc.GaxGrpcProperties;
21 import com.google.api.gax.grpc.testing.LocalChannelProvider;
22 import com.google.api.gax.grpc.testing.MockGrpcService;
23 import com.google.api.gax.grpc.testing.MockServiceHelper;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.protobuf.AbstractMessage;
27 import io.grpc.StatusRuntimeException;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.UUID;
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 FlexTemplatesServiceClientTest {
42   private static MockFlexTemplatesService mockFlexTemplatesService;
43   private static MockServiceHelper mockServiceHelper;
44   private LocalChannelProvider channelProvider;
45   private FlexTemplatesServiceClient client;
46 
47   @BeforeClass
startStaticServer()48   public static void startStaticServer() {
49     mockFlexTemplatesService = new MockFlexTemplatesService();
50     mockServiceHelper =
51         new MockServiceHelper(
52             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockFlexTemplatesService));
53     mockServiceHelper.start();
54   }
55 
56   @AfterClass
stopServer()57   public static void stopServer() {
58     mockServiceHelper.stop();
59   }
60 
61   @Before
setUp()62   public void setUp() throws IOException {
63     mockServiceHelper.reset();
64     channelProvider = mockServiceHelper.createChannelProvider();
65     FlexTemplatesServiceSettings settings =
66         FlexTemplatesServiceSettings.newBuilder()
67             .setTransportChannelProvider(channelProvider)
68             .setCredentialsProvider(NoCredentialsProvider.create())
69             .build();
70     client = FlexTemplatesServiceClient.create(settings);
71   }
72 
73   @After
tearDown()74   public void tearDown() throws Exception {
75     client.close();
76   }
77 
78   @Test
launchFlexTemplateTest()79   public void launchFlexTemplateTest() throws Exception {
80     LaunchFlexTemplateResponse expectedResponse =
81         LaunchFlexTemplateResponse.newBuilder().setJob(Job.newBuilder().build()).build();
82     mockFlexTemplatesService.addResponse(expectedResponse);
83 
84     LaunchFlexTemplateRequest request =
85         LaunchFlexTemplateRequest.newBuilder()
86             .setProjectId("projectId-894832108")
87             .setLaunchParameter(LaunchFlexTemplateParameter.newBuilder().build())
88             .setLocation("location1901043637")
89             .setValidateOnly(true)
90             .build();
91 
92     LaunchFlexTemplateResponse actualResponse = client.launchFlexTemplate(request);
93     Assert.assertEquals(expectedResponse, actualResponse);
94 
95     List<AbstractMessage> actualRequests = mockFlexTemplatesService.getRequests();
96     Assert.assertEquals(1, actualRequests.size());
97     LaunchFlexTemplateRequest actualRequest = ((LaunchFlexTemplateRequest) actualRequests.get(0));
98 
99     Assert.assertEquals(request.getProjectId(), actualRequest.getProjectId());
100     Assert.assertEquals(request.getLaunchParameter(), actualRequest.getLaunchParameter());
101     Assert.assertEquals(request.getLocation(), actualRequest.getLocation());
102     Assert.assertEquals(request.getValidateOnly(), actualRequest.getValidateOnly());
103     Assert.assertTrue(
104         channelProvider.isHeaderSent(
105             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
106             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
107   }
108 
109   @Test
launchFlexTemplateExceptionTest()110   public void launchFlexTemplateExceptionTest() throws Exception {
111     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
112     mockFlexTemplatesService.addException(exception);
113 
114     try {
115       LaunchFlexTemplateRequest request =
116           LaunchFlexTemplateRequest.newBuilder()
117               .setProjectId("projectId-894832108")
118               .setLaunchParameter(LaunchFlexTemplateParameter.newBuilder().build())
119               .setLocation("location1901043637")
120               .setValidateOnly(true)
121               .build();
122       client.launchFlexTemplate(request);
123       Assert.fail("No exception raised");
124     } catch (InvalidArgumentException e) {
125       // Expected exception.
126     }
127   }
128 }
129