• 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.cloud.texttospeech.v1beta1;
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.api.gax.rpc.StatusCode;
27 import com.google.longrunning.Operation;
28 import com.google.protobuf.AbstractMessage;
29 import com.google.protobuf.Any;
30 import io.grpc.StatusRuntimeException;
31 import java.io.IOException;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.UUID;
35 import java.util.concurrent.ExecutionException;
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 TextToSpeechLongAudioSynthesizeClientTest {
46   private static MockServiceHelper mockServiceHelper;
47   private static MockTextToSpeechLongAudioSynthesize mockTextToSpeechLongAudioSynthesize;
48   private LocalChannelProvider channelProvider;
49   private TextToSpeechLongAudioSynthesizeClient client;
50 
51   @BeforeClass
startStaticServer()52   public static void startStaticServer() {
53     mockTextToSpeechLongAudioSynthesize = new MockTextToSpeechLongAudioSynthesize();
54     mockServiceHelper =
55         new MockServiceHelper(
56             UUID.randomUUID().toString(),
57             Arrays.<MockGrpcService>asList(mockTextToSpeechLongAudioSynthesize));
58     mockServiceHelper.start();
59   }
60 
61   @AfterClass
stopServer()62   public static void stopServer() {
63     mockServiceHelper.stop();
64   }
65 
66   @Before
setUp()67   public void setUp() throws IOException {
68     mockServiceHelper.reset();
69     channelProvider = mockServiceHelper.createChannelProvider();
70     TextToSpeechLongAudioSynthesizeSettings settings =
71         TextToSpeechLongAudioSynthesizeSettings.newBuilder()
72             .setTransportChannelProvider(channelProvider)
73             .setCredentialsProvider(NoCredentialsProvider.create())
74             .build();
75     client = TextToSpeechLongAudioSynthesizeClient.create(settings);
76   }
77 
78   @After
tearDown()79   public void tearDown() throws Exception {
80     client.close();
81   }
82 
83   @Test
synthesizeLongAudioTest()84   public void synthesizeLongAudioTest() throws Exception {
85     SynthesizeLongAudioResponse expectedResponse = SynthesizeLongAudioResponse.newBuilder().build();
86     Operation resultOperation =
87         Operation.newBuilder()
88             .setName("synthesizeLongAudioTest")
89             .setDone(true)
90             .setResponse(Any.pack(expectedResponse))
91             .build();
92     mockTextToSpeechLongAudioSynthesize.addResponse(resultOperation);
93 
94     SynthesizeLongAudioRequest request =
95         SynthesizeLongAudioRequest.newBuilder()
96             .setParent("parent-995424086")
97             .setInput(SynthesisInput.newBuilder().build())
98             .setAudioConfig(AudioConfig.newBuilder().build())
99             .setOutputGcsUri("outputGcsUri-489598154")
100             .setVoice(VoiceSelectionParams.newBuilder().build())
101             .build();
102 
103     SynthesizeLongAudioResponse actualResponse = client.synthesizeLongAudioAsync(request).get();
104     Assert.assertEquals(expectedResponse, actualResponse);
105 
106     List<AbstractMessage> actualRequests = mockTextToSpeechLongAudioSynthesize.getRequests();
107     Assert.assertEquals(1, actualRequests.size());
108     SynthesizeLongAudioRequest actualRequest = ((SynthesizeLongAudioRequest) actualRequests.get(0));
109 
110     Assert.assertEquals(request.getParent(), actualRequest.getParent());
111     Assert.assertEquals(request.getInput(), actualRequest.getInput());
112     Assert.assertEquals(request.getAudioConfig(), actualRequest.getAudioConfig());
113     Assert.assertEquals(request.getOutputGcsUri(), actualRequest.getOutputGcsUri());
114     Assert.assertEquals(request.getVoice(), actualRequest.getVoice());
115     Assert.assertTrue(
116         channelProvider.isHeaderSent(
117             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
118             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
119   }
120 
121   @Test
synthesizeLongAudioExceptionTest()122   public void synthesizeLongAudioExceptionTest() throws Exception {
123     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
124     mockTextToSpeechLongAudioSynthesize.addException(exception);
125 
126     try {
127       SynthesizeLongAudioRequest request =
128           SynthesizeLongAudioRequest.newBuilder()
129               .setParent("parent-995424086")
130               .setInput(SynthesisInput.newBuilder().build())
131               .setAudioConfig(AudioConfig.newBuilder().build())
132               .setOutputGcsUri("outputGcsUri-489598154")
133               .setVoice(VoiceSelectionParams.newBuilder().build())
134               .build();
135       client.synthesizeLongAudioAsync(request).get();
136       Assert.fail("No exception raised");
137     } catch (ExecutionException e) {
138       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
139       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
140       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
141     }
142   }
143 }
144