• 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.protobuf.AbstractMessage;
27 import com.google.protobuf.ByteString;
28 import io.grpc.StatusRuntimeException;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.UUID;
34 import javax.annotation.Generated;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 
42 @Generated("by gapic-generator-java")
43 public class TextToSpeechClientTest {
44   private static MockServiceHelper mockServiceHelper;
45   private static MockTextToSpeech mockTextToSpeech;
46   private LocalChannelProvider channelProvider;
47   private TextToSpeechClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() {
51     mockTextToSpeech = new MockTextToSpeech();
52     mockServiceHelper =
53         new MockServiceHelper(
54             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockTextToSpeech));
55     mockServiceHelper.start();
56   }
57 
58   @AfterClass
stopServer()59   public static void stopServer() {
60     mockServiceHelper.stop();
61   }
62 
63   @Before
setUp()64   public void setUp() throws IOException {
65     mockServiceHelper.reset();
66     channelProvider = mockServiceHelper.createChannelProvider();
67     TextToSpeechSettings settings =
68         TextToSpeechSettings.newBuilder()
69             .setTransportChannelProvider(channelProvider)
70             .setCredentialsProvider(NoCredentialsProvider.create())
71             .build();
72     client = TextToSpeechClient.create(settings);
73   }
74 
75   @After
tearDown()76   public void tearDown() throws Exception {
77     client.close();
78   }
79 
80   @Test
listVoicesTest()81   public void listVoicesTest() throws Exception {
82     ListVoicesResponse expectedResponse =
83         ListVoicesResponse.newBuilder().addAllVoices(new ArrayList<Voice>()).build();
84     mockTextToSpeech.addResponse(expectedResponse);
85 
86     String languageCode = "languageCode-2092349083";
87 
88     ListVoicesResponse actualResponse = client.listVoices(languageCode);
89     Assert.assertEquals(expectedResponse, actualResponse);
90 
91     List<AbstractMessage> actualRequests = mockTextToSpeech.getRequests();
92     Assert.assertEquals(1, actualRequests.size());
93     ListVoicesRequest actualRequest = ((ListVoicesRequest) actualRequests.get(0));
94 
95     Assert.assertEquals(languageCode, actualRequest.getLanguageCode());
96     Assert.assertTrue(
97         channelProvider.isHeaderSent(
98             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
99             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
100   }
101 
102   @Test
listVoicesExceptionTest()103   public void listVoicesExceptionTest() throws Exception {
104     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
105     mockTextToSpeech.addException(exception);
106 
107     try {
108       String languageCode = "languageCode-2092349083";
109       client.listVoices(languageCode);
110       Assert.fail("No exception raised");
111     } catch (InvalidArgumentException e) {
112       // Expected exception.
113     }
114   }
115 
116   @Test
synthesizeSpeechTest()117   public void synthesizeSpeechTest() throws Exception {
118     SynthesizeSpeechResponse expectedResponse =
119         SynthesizeSpeechResponse.newBuilder()
120             .setAudioContent(ByteString.EMPTY)
121             .addAllTimepoints(new ArrayList<Timepoint>())
122             .setAudioConfig(AudioConfig.newBuilder().build())
123             .build();
124     mockTextToSpeech.addResponse(expectedResponse);
125 
126     SynthesisInput input = SynthesisInput.newBuilder().build();
127     VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().build();
128     AudioConfig audioConfig = AudioConfig.newBuilder().build();
129 
130     SynthesizeSpeechResponse actualResponse = client.synthesizeSpeech(input, voice, audioConfig);
131     Assert.assertEquals(expectedResponse, actualResponse);
132 
133     List<AbstractMessage> actualRequests = mockTextToSpeech.getRequests();
134     Assert.assertEquals(1, actualRequests.size());
135     SynthesizeSpeechRequest actualRequest = ((SynthesizeSpeechRequest) actualRequests.get(0));
136 
137     Assert.assertEquals(input, actualRequest.getInput());
138     Assert.assertEquals(voice, actualRequest.getVoice());
139     Assert.assertEquals(audioConfig, actualRequest.getAudioConfig());
140     Assert.assertTrue(
141         channelProvider.isHeaderSent(
142             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
143             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
144   }
145 
146   @Test
synthesizeSpeechExceptionTest()147   public void synthesizeSpeechExceptionTest() throws Exception {
148     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
149     mockTextToSpeech.addException(exception);
150 
151     try {
152       SynthesisInput input = SynthesisInput.newBuilder().build();
153       VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().build();
154       AudioConfig audioConfig = AudioConfig.newBuilder().build();
155       client.synthesizeSpeech(input, voice, audioConfig);
156       Assert.fail("No exception raised");
157     } catch (InvalidArgumentException e) {
158       // Expected exception.
159     }
160   }
161 }
162