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.StatusCode; 26 import com.google.api.gax.rpc.testing.FakeStatusCode; 27 import com.google.cloud.texttospeech.v1beta1.stub.HttpJsonTextToSpeechLongAudioSynthesizeStub; 28 import com.google.longrunning.Operation; 29 import com.google.protobuf.Any; 30 import java.io.IOException; 31 import java.util.List; 32 import java.util.concurrent.ExecutionException; 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 TextToSpeechLongAudioSynthesizeClientHttpJsonTest { 43 private static MockHttpService mockService; 44 private static TextToSpeechLongAudioSynthesizeClient client; 45 46 @BeforeClass startStaticServer()47 public static void startStaticServer() throws IOException { 48 mockService = 49 new MockHttpService( 50 HttpJsonTextToSpeechLongAudioSynthesizeStub.getMethodDescriptors(), 51 TextToSpeechLongAudioSynthesizeSettings.getDefaultEndpoint()); 52 TextToSpeechLongAudioSynthesizeSettings settings = 53 TextToSpeechLongAudioSynthesizeSettings.newHttpJsonBuilder() 54 .setTransportChannelProvider( 55 TextToSpeechLongAudioSynthesizeSettings.defaultHttpJsonTransportProviderBuilder() 56 .setHttpTransport(mockService) 57 .build()) 58 .setCredentialsProvider(NoCredentialsProvider.create()) 59 .build(); 60 client = TextToSpeechLongAudioSynthesizeClient.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 synthesizeLongAudioTest()77 public void synthesizeLongAudioTest() throws Exception { 78 SynthesizeLongAudioResponse expectedResponse = SynthesizeLongAudioResponse.newBuilder().build(); 79 Operation resultOperation = 80 Operation.newBuilder() 81 .setName("synthesizeLongAudioTest") 82 .setDone(true) 83 .setResponse(Any.pack(expectedResponse)) 84 .build(); 85 mockService.addResponse(resultOperation); 86 87 SynthesizeLongAudioRequest request = 88 SynthesizeLongAudioRequest.newBuilder() 89 .setParent("projects/project-5698/locations/location-5698/voices/voice-5698") 90 .setInput(SynthesisInput.newBuilder().build()) 91 .setAudioConfig(AudioConfig.newBuilder().build()) 92 .setOutputGcsUri("outputGcsUri-489598154") 93 .setVoice(VoiceSelectionParams.newBuilder().build()) 94 .build(); 95 96 SynthesizeLongAudioResponse actualResponse = client.synthesizeLongAudioAsync(request).get(); 97 Assert.assertEquals(expectedResponse, actualResponse); 98 99 List<String> actualRequests = mockService.getRequestPaths(); 100 Assert.assertEquals(1, actualRequests.size()); 101 102 String apiClientHeaderKey = 103 mockService 104 .getRequestHeaders() 105 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 106 .iterator() 107 .next(); 108 Assert.assertTrue( 109 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 110 .matcher(apiClientHeaderKey) 111 .matches()); 112 } 113 114 @Test synthesizeLongAudioExceptionTest()115 public void synthesizeLongAudioExceptionTest() throws Exception { 116 ApiException exception = 117 ApiExceptionFactory.createException( 118 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 119 mockService.addException(exception); 120 121 try { 122 SynthesizeLongAudioRequest request = 123 SynthesizeLongAudioRequest.newBuilder() 124 .setParent("projects/project-5698/locations/location-5698/voices/voice-5698") 125 .setInput(SynthesisInput.newBuilder().build()) 126 .setAudioConfig(AudioConfig.newBuilder().build()) 127 .setOutputGcsUri("outputGcsUri-489598154") 128 .setVoice(VoiceSelectionParams.newBuilder().build()) 129 .build(); 130 client.synthesizeLongAudioAsync(request).get(); 131 Assert.fail("No exception raised"); 132 } catch (ExecutionException e) { 133 } 134 } 135 } 136