66import com .azure .ai .speech .transcription .models .ProfanityFilterMode ;
77import com .azure .ai .speech .transcription .models .TranscriptionDiarizationOptions ;
88import com .azure .ai .speech .transcription .models .TranscriptionOptions ;
9+ import com .azure .ai .speech .transcription .models .TranscriptionResult ;
910import com .azure .core .exception .HttpResponseException ;
1011import com .azure .core .http .HttpHeaderName ;
1112import com .azure .core .http .rest .RequestOptions ;
13+ import com .azure .core .util .BinaryData ;
14+ import org .junit .jupiter .api .BeforeEach ;
1215import org .junit .jupiter .api .Test ;
16+ import reactor .test .StepVerifier ;
1317
1418import java .util .Arrays ;
19+ import java .util .Collections ;
20+
21+ import static org .junit .jupiter .api .Assertions .fail ;
1522
1623/**
1724 * Tests for TranscriptionAsyncClient (asynchronous client).
1825 */
1926class TranscriptionAsyncClientTest extends TranscriptionClientTestBase {
27+ private TranscriptionAsyncClient client ;
2028
21- private final Boolean sync = false ; // All tests in this file use the async client
29+ @ BeforeEach
30+ public void setupTest () {
31+ this .client = configureBuilder (true , true ).buildAsyncClient ();
32+ }
2233
2334 /***********************************************************************************
2435 *
@@ -28,184 +39,95 @@ class TranscriptionAsyncClientTest extends TranscriptionClientTestBase {
2839
2940 @ Test
3041 public void testTranscribeAsyncBasicFromFile () {
31- createClient (true , true , sync );
32-
33- String methodName = new Object () {
34- }.getClass ().getEnclosingMethod ().getName ();
35-
36- TranscriptionOptions options = new TranscriptionOptions ((String ) null );
37-
38- doTranscription (methodName , sync , false , audioFile , options , null );
42+ transcribeAndVerifyResult ("testTranscribeAsyncBasicFromFile" , fromAudioFile ());
3943 }
4044
4145 @ Test
4246 public void testTranscribeAsyncWithLanguageFromFile () {
43- createClient (true , true , sync );
44-
45- String methodName = new Object () {
46- }.getClass ().getEnclosingMethod ().getName ();
47-
48- TranscriptionOptions options = new TranscriptionOptions ((String ) null ).setLocales (Arrays .asList ("en-US" ));
49-
50- doTranscription (methodName , sync , false , audioFile , options , null );
47+ transcribeAndVerifyResult ("testTranscribeAsyncWithLanguageFromFile" ,
48+ fromAudioFile ().setLocales (Collections .singletonList ("en-US" )));
5149 }
5250
5351 @ Test
5452 public void testTranscribeAsyncWithMultipleLanguagesFromFile () {
55- createClient (true , true , sync );
56-
57- String methodName = new Object () {
58- }.getClass ().getEnclosingMethod ().getName ();
59-
60- TranscriptionOptions options
61- = new TranscriptionOptions ((String ) null ).setLocales (Arrays .asList ("en-US" , "es-ES" , "fr-FR" ));
62-
63- doTranscription (methodName , sync , false , audioFile , options , null );
53+ transcribeAndVerifyResult ("testTranscribeAsyncWithMultipleLanguagesFromFile" ,
54+ fromAudioFile ().setLocales (Arrays .asList ("en-US" , "es-ES" , "fr-FR" )));
6455 }
6556
6657 @ Test
6758 public void testTranscribeAsyncWithDiarizationFromFile () {
68- createClient (true , true , sync );
69-
70- String methodName = new Object () {
71- }.getClass ().getEnclosingMethod ().getName ();
72-
73- TranscriptionDiarizationOptions diarizationOptions = new TranscriptionDiarizationOptions ().setMaxSpeakers (5 );
74-
75- TranscriptionOptions options
76- = new TranscriptionOptions ((String ) null ).setDiarizationOptions (diarizationOptions );
77-
78- doTranscription (methodName , sync , false , audioFile , options , null );
59+ transcribeAndVerifyResult ("testTranscribeAsyncWithDiarizationFromFile" ,
60+ fromAudioFile ().setDiarizationOptions (new TranscriptionDiarizationOptions ().setMaxSpeakers (5 )));
7961 }
8062
8163 @ Test
8264 public void testTranscribeAsyncWithProfanityFilterFromFile () {
83- createClient (true , true , sync );
84-
85- String methodName = new Object () {
86- }.getClass ().getEnclosingMethod ().getName ();
87-
88- TranscriptionOptions options
89- = new TranscriptionOptions ((String ) null ).setProfanityFilterMode (ProfanityFilterMode .MASKED );
90-
91- doTranscription (methodName , sync , false , audioFile , options , null );
65+ transcribeAndVerifyResult ("testTranscribeAsyncWithProfanityFilterFromFile" ,
66+ fromAudioFile ().setProfanityFilterMode (ProfanityFilterMode .MASKED ));
9267 }
9368
9469 @ Test
9570 public void testTranscribeAsyncWithChannelsFromFile () {
96- createClient (true , true , sync );
97-
98- String methodName = new Object () {
99- }.getClass ().getEnclosingMethod ().getName ();
100-
101- TranscriptionOptions options = new TranscriptionOptions ((String ) null ).setActiveChannels (Arrays .asList (0 ));
102-
103- doTranscription (methodName , sync , false , audioFile , options , null );
71+ transcribeAndVerifyResult ("testTranscribeAsyncWithChannelsFromFile" ,
72+ fromAudioFile ().setActiveChannels (Collections .singletonList (0 )));
10473 }
10574
10675 @ Test
10776 public void testTranscribeAsyncAllOptionsFromFile () {
108- createClient (true , true , sync );
109-
110- String methodName = new Object () {
111- }.getClass ().getEnclosingMethod ().getName ();
112-
113- TranscriptionDiarizationOptions diarizationOptions = new TranscriptionDiarizationOptions ().setMaxSpeakers (5 );
114-
115- TranscriptionOptions options = new TranscriptionOptions ((String ) null ).setLocales (Arrays .asList ("en-US" ))
116- .setDiarizationOptions (diarizationOptions )
77+ TranscriptionOptions options = fromAudioFile ().setLocales (Collections .singletonList ("en-US" ))
78+ .setDiarizationOptions (new TranscriptionDiarizationOptions ().setMaxSpeakers (5 ))
11779 .setProfanityFilterMode (ProfanityFilterMode .MASKED )
118- .setActiveChannels (Arrays . asList (0 ));
80+ .setActiveChannels (Collections . singletonList (0 ));
11981
120- doTranscription ( methodName , sync , false , audioFile , options , null );
82+ transcribeAndVerifyResult ( "testTranscribeAsyncAllOptionsFromFile" , options );
12183 }
12284
12385 @ Test
12486 public void testTranscribeAsyncBasicFromFileWithResponse () {
125- createClient (true , true , sync );
126-
127- String methodName = new Object () {
128- }.getClass ().getEnclosingMethod ().getName ();
129-
130- TranscriptionOptions options = new TranscriptionOptions ((String ) null );
131- RequestOptions requestOptions
132- = new RequestOptions ().addHeader (HttpHeaderName .fromString ("x-custom-header" ), "custom-value" );
133-
134- doTranscription (methodName , sync , true , audioFile , options , requestOptions );
87+ transcribeAndVerifyResponse ("testTranscribeAsyncBasicFromFileWithResponse" , fromAudioFile (),
88+ new RequestOptions ().addHeader (HttpHeaderName .fromString ("x-custom-header" ), "custom-value" ));
13589 }
13690
13791 @ Test
13892 public void testTranscribeAsyncWithAllOptionsFromFileWithResponse () {
139- createClient (true , true , sync );
140-
141- String methodName = new Object () {
142- }.getClass ().getEnclosingMethod ().getName ();
143-
144- TranscriptionDiarizationOptions diarizationOptions = new TranscriptionDiarizationOptions ().setMaxSpeakers (5 );
145-
146- TranscriptionOptions options
147- = new TranscriptionOptions ((String ) null ).setLocales (Arrays .asList ("en-US" , "es-ES" ))
148- .setDiarizationOptions (diarizationOptions )
149- .setProfanityFilterMode (ProfanityFilterMode .REMOVED )
150- .setActiveChannels (Arrays .asList (0 , 1 ));
93+ TranscriptionOptions options = fromAudioFile ().setLocales (Arrays .asList ("en-US" , "es-ES" ))
94+ .setDiarizationOptions (new TranscriptionDiarizationOptions ().setMaxSpeakers (5 ))
95+ .setProfanityFilterMode (ProfanityFilterMode .REMOVED )
96+ .setActiveChannels (Arrays .asList (0 , 1 ));
15197
15298 RequestOptions requestOptions
15399 = new RequestOptions ().addHeader (HttpHeaderName .fromString ("x-custom-header" ), "custom-value" )
154100 .addQueryParam ("test-param" , "test-value" );
155101
156- doTranscription ( methodName , sync , true , audioFile , options , requestOptions );
102+ transcribeAndVerifyResponse ( "testTranscribeAsyncWithAllOptionsFromFileWithResponse" , options , requestOptions );
157103 }
158104
159105 @ Test
160106 public void testTranscribeAsyncWithAudioUrl () {
161- createClient (true , true , sync );
162-
163- String methodName = new Object () {
164- }.getClass ().getEnclosingMethod ().getName ();
165-
166107 // Using a publicly accessible sample audio file from Azure samples
167108 String audioUrl
168109 = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-speech-sdk/master/sampledata/audiofiles/aboutSpeechSdk.wav" ;
169- TranscriptionOptions options = new TranscriptionOptions (audioUrl ).setLocales (Arrays .asList ("en-US" ));
170110
171- // For URL-based transcription, we don't pass the local audio file path
172- doTranscriptionWithUrl ( methodName , sync , options );
111+ transcribeAndVerifyResult ( "testTranscribeAsyncWithAudioUrl" ,
112+ new TranscriptionOptions ( audioUrl ). setLocales ( Collections . singletonList ( "en-US" )) );
173113 }
174114
175115 @ Test
176116 public void testTranscribeAsyncWithProfanityModeMasked () {
177- createClient (true , true , sync );
178-
179- String methodName = new Object () {
180- }.getClass ().getEnclosingMethod ().getName ();
181-
182- TranscriptionOptions options
183- = new TranscriptionOptions ((String ) null ).setProfanityFilterMode (ProfanityFilterMode .MASKED );
184- doTranscription (methodName , sync , false , audioFile , options , null );
117+ transcribeAndVerifyResult ("testTranscribeAsyncWithProfanityModeMasked" ,
118+ fromAudioFile ().setProfanityFilterMode (ProfanityFilterMode .MASKED ));
185119 }
186120
187121 @ Test
188122 public void testTranscribeAsyncWithProfanityModeRemoved () {
189- createClient (true , true , sync );
190-
191- String methodName = new Object () {
192- }.getClass ().getEnclosingMethod ().getName ();
193-
194- TranscriptionOptions options
195- = new TranscriptionOptions ((String ) null ).setProfanityFilterMode (ProfanityFilterMode .REMOVED );
196- doTranscription (methodName , sync , false , audioFile , options , null );
123+ transcribeAndVerifyResult ("testTranscribeAsyncWithProfanityModeTags" ,
124+ fromAudioFile ().setProfanityFilterMode (ProfanityFilterMode .REMOVED ));
197125 }
198126
199127 @ Test
200128 public void testTranscribeAsyncWithProfanityModeTags () {
201- createClient (true , true , sync );
202-
203- String methodName = new Object () {
204- }.getClass ().getEnclosingMethod ().getName ();
205-
206- TranscriptionOptions options
207- = new TranscriptionOptions ((String ) null ).setProfanityFilterMode (ProfanityFilterMode .TAGS );
208- doTranscription (methodName , sync , false , audioFile , options , null );
129+ transcribeAndVerifyResult ("testTranscribeAsyncWithProfanityModeTags" ,
130+ fromAudioFile ().setProfanityFilterMode (ProfanityFilterMode .TAGS ));
209131 }
210132
211133 /***********************************************************************************
@@ -216,58 +138,47 @@ public void testTranscribeAsyncWithProfanityModeTags() {
216138
217139 @ Test
218140 public void testTranscribeAsyncWithEmptyAudioData () {
219- createClient (true , true , sync );
220-
221- String methodName = new Object () {
222- }.getClass ().getEnclosingMethod ().getName ();
223-
224141 // Test with minimal audio data - service should handle gracefully
225- TranscriptionOptions options = new TranscriptionOptions ((String ) null );
226-
227- doTranscription (methodName , sync , false , audioFile , options , null );
142+ transcribeAndVerifyResult ("testTranscribeAsyncWithEmptyAudioData" , fromAudioFile ());
228143 }
229144
230145 @ Test
231146 public void testTranscribeAsyncWithInvalidLanguageCode () {
232- createClient (true , true , sync );
233-
234- String methodName = new Object () {
235- }.getClass ().getEnclosingMethod ().getName ();
236-
237147 // Use invalid language code to trigger service error
238- TranscriptionOptions options
239- = new TranscriptionOptions ((String ) null ).setLocales (Arrays .asList ("invalid-locale-code" ));
148+ TranscriptionOptions options = fromAudioFile ().setLocales (Collections .singletonList ("invalid-locale-code" ));
240149
241150 // The service should return a 400 error for invalid locale
242151 // doTranscription wraps exceptions in RuntimeException, so we catch that
243- try {
244- doTranscription (methodName , sync , false , audioFile , options , null );
245- // Should not reach here - the above should throw an exception
246- throw new AssertionError ("Expected RuntimeException with HttpResponseException cause but none was thrown" );
247- } catch (RuntimeException e ) {
152+ StepVerifier .create (client .transcribe (options )).verifyErrorSatisfies (e -> {
248153 // Expected behavior - verify the cause is HttpResponseException with 400 status
249- if (!(e .getCause () instanceof HttpResponseException )) {
250- throw new AssertionError (
251- "Expected RuntimeException cause to be HttpResponseException but got: " + e .getCause ().getClass ());
154+ if (!(e instanceof HttpResponseException )) {
155+ fail ("Expected RuntimeException cause to be HttpResponseException but got: " + e .getClass ());
252156 }
253- HttpResponseException httpException = (HttpResponseException ) e . getCause () ;
157+ HttpResponseException httpException = (HttpResponseException ) e ;
254158 if (httpException .getResponse ().getStatusCode () != 400 ) {
255- throw new AssertionError (
256- "Expected 400 status code but got: " + httpException .getResponse ().getStatusCode ());
159+ fail ("Expected 400 status code but got: " + httpException .getResponse ().getStatusCode ());
257160 }
258- }
161+ });
259162 }
260163
261164 @ Test
262165 public void testTranscribeAsyncCancellation () {
263- createClient (true , true , sync );
264-
265- String methodName = new Object () {
266- }.getClass ().getEnclosingMethod ().getName ();
267-
268166 // Test cancellation behavior with a normal transcription request
269- TranscriptionOptions options = new TranscriptionOptions ((String ) null );
167+ transcribeAndVerifyResult ("testTranscribeAsyncCancellation" , fromAudioFile ());
168+ }
169+
170+ private void transcribeAndVerifyResult (String testName , TranscriptionOptions options ) {
171+ StepVerifier .create (client .transcribe (options ))
172+ .assertNext (result -> validateTranscriptionResult (testName , result ))
173+ .verifyComplete ();
174+ }
270175
271- doTranscription (methodName , sync , false , audioFile , options , null );
176+ private void transcribeAndVerifyResponse (String testName , TranscriptionOptions options ,
177+ RequestOptions requestOptions ) {
178+ BinaryData multipartBody = createMultipartBody (options , requestOptions );
179+ StepVerifier .create (client .transcribeWithResponse (multipartBody , requestOptions )).assertNext (response -> {
180+ printHttpRequestAndResponse (response );
181+ validateTranscriptionResult (testName , response .getValue ().toObject (TranscriptionResult .class ));
182+ }).verifyComplete ();
272183 }
273184}
0 commit comments