• 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.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.cloud.texttospeech.v1beta1.stub.HttpJsonTextToSpeechStub;
29 import com.google.protobuf.ByteString;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import javax.annotation.Generated;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 
41 @Generated("by gapic-generator-java")
42 public class TextToSpeechClientHttpJsonTest {
43   private static MockHttpService mockService;
44   private static TextToSpeechClient client;
45 
46   @BeforeClass
startStaticServer()47   public static void startStaticServer() throws IOException {
48     mockService =
49         new MockHttpService(
50             HttpJsonTextToSpeechStub.getMethodDescriptors(),
51             TextToSpeechSettings.getDefaultEndpoint());
52     TextToSpeechSettings settings =
53         TextToSpeechSettings.newHttpJsonBuilder()
54             .setTransportChannelProvider(
55                 TextToSpeechSettings.defaultHttpJsonTransportProviderBuilder()
56                     .setHttpTransport(mockService)
57                     .build())
58             .setCredentialsProvider(NoCredentialsProvider.create())
59             .build();
60     client = TextToSpeechClient.create(settings);
61   }
62 
63   @AfterClass
stopServer()64   public static void stopServer() {
65     client.close();
66   }
67 
68   @Before
setUp()69   public void setUp() {}
70 
71   @After
tearDown()72   public void tearDown() throws Exception {
73     mockService.reset();
74   }
75 
76   @Test
listVoicesTest()77   public void listVoicesTest() throws Exception {
78     ListVoicesResponse expectedResponse =
79         ListVoicesResponse.newBuilder().addAllVoices(new ArrayList<Voice>()).build();
80     mockService.addResponse(expectedResponse);
81 
82     String languageCode = "languageCode-2092349083";
83 
84     ListVoicesResponse actualResponse = client.listVoices(languageCode);
85     Assert.assertEquals(expectedResponse, actualResponse);
86 
87     List<String> actualRequests = mockService.getRequestPaths();
88     Assert.assertEquals(1, actualRequests.size());
89 
90     String apiClientHeaderKey =
91         mockService
92             .getRequestHeaders()
93             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
94             .iterator()
95             .next();
96     Assert.assertTrue(
97         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
98             .matcher(apiClientHeaderKey)
99             .matches());
100   }
101 
102   @Test
listVoicesExceptionTest()103   public void listVoicesExceptionTest() throws Exception {
104     ApiException exception =
105         ApiExceptionFactory.createException(
106             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
107     mockService.addException(exception);
108 
109     try {
110       String languageCode = "languageCode-2092349083";
111       client.listVoices(languageCode);
112       Assert.fail("No exception raised");
113     } catch (InvalidArgumentException e) {
114       // Expected exception.
115     }
116   }
117 
118   @Test
synthesizeSpeechTest()119   public void synthesizeSpeechTest() throws Exception {
120     SynthesizeSpeechResponse expectedResponse =
121         SynthesizeSpeechResponse.newBuilder()
122             .setAudioContent(ByteString.EMPTY)
123             .addAllTimepoints(new ArrayList<Timepoint>())
124             .setAudioConfig(AudioConfig.newBuilder().build())
125             .build();
126     mockService.addResponse(expectedResponse);
127 
128     SynthesisInput input = SynthesisInput.newBuilder().build();
129     VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().build();
130     AudioConfig audioConfig = AudioConfig.newBuilder().build();
131 
132     SynthesizeSpeechResponse actualResponse = client.synthesizeSpeech(input, voice, audioConfig);
133     Assert.assertEquals(expectedResponse, actualResponse);
134 
135     List<String> actualRequests = mockService.getRequestPaths();
136     Assert.assertEquals(1, actualRequests.size());
137 
138     String apiClientHeaderKey =
139         mockService
140             .getRequestHeaders()
141             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
142             .iterator()
143             .next();
144     Assert.assertTrue(
145         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
146             .matcher(apiClientHeaderKey)
147             .matches());
148   }
149 
150   @Test
synthesizeSpeechExceptionTest()151   public void synthesizeSpeechExceptionTest() throws Exception {
152     ApiException exception =
153         ApiExceptionFactory.createException(
154             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
155     mockService.addException(exception);
156 
157     try {
158       SynthesisInput input = SynthesisInput.newBuilder().build();
159       VoiceSelectionParams voice = VoiceSelectionParams.newBuilder().build();
160       AudioConfig audioConfig = AudioConfig.newBuilder().build();
161       client.synthesizeSpeech(input, voice, audioConfig);
162       Assert.fail("No exception raised");
163     } catch (InvalidArgumentException e) {
164       // Expected exception.
165     }
166   }
167 }
168