|
6 | 6 | import com.azure.ai.speech.transcription.models.ProfanityFilterMode; |
7 | 7 | import com.azure.ai.speech.transcription.models.TranscriptionDiarizationOptions; |
8 | 8 | import com.azure.ai.speech.transcription.models.TranscriptionOptions; |
| 9 | +import com.azure.core.exception.HttpResponseException; |
9 | 10 | import com.azure.core.http.HttpHeaderName; |
10 | 11 | import com.azure.core.http.rest.RequestOptions; |
11 | 12 | import org.junit.jupiter.api.Test; |
@@ -157,22 +158,18 @@ public void testTranscribeAsyncWithAllOptionsFromFileWithResponse() { |
157 | 158 |
|
158 | 159 | @Test |
159 | 160 | public void testTranscribeAsyncWithAudioUrl() { |
160 | | - // Test with audio URL option |
161 | 161 | createClient(true, true, sync); |
162 | 162 |
|
163 | | - // Note: This test is commented out because it requires: |
164 | | - // 1. A valid, publicly accessible URL in live/record mode |
165 | | - // 2. Proper recording of the service response |
166 | | - // 3. The audio file at the URL must be in a supported format |
167 | | - // To enable this test: |
168 | | - // - Provide a valid audio URL |
169 | | - // - Uncomment the lines below |
170 | | - // - Run in RECORD mode to capture the interaction |
171 | | - // String methodName = new Object() { |
172 | | - // }.getClass().getEnclosingMethod().getName(); |
173 | | - // TranscriptionOptions options = new TranscriptionOptions("https://example.com/sample.wav"); |
174 | | - // options.setLocales(Arrays.asList("en-US")); |
175 | | - // doTranscription(methodName, sync, false, audioFile, options, null); |
| 163 | + String methodName = new Object() { |
| 164 | + }.getClass().getEnclosingMethod().getName(); |
| 165 | + |
| 166 | + // Using a publicly accessible sample audio file from Azure samples |
| 167 | + String audioUrl |
| 168 | + = "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")); |
| 170 | + |
| 171 | + // For URL-based transcription, we don't pass the local audio file path |
| 172 | + doTranscriptionWithUrl(methodName, sync, options); |
176 | 173 | } |
177 | 174 |
|
178 | 175 | @Test |
@@ -221,43 +218,56 @@ public void testTranscribeAsyncWithProfanityModeTags() { |
221 | 218 | public void testTranscribeAsyncWithEmptyAudioData() { |
222 | 219 | createClient(true, true, sync); |
223 | 220 |
|
224 | | - // Test with empty audio data - this should result in a service error |
225 | | - // Note: Depending on service behavior, this may throw HttpResponseException |
226 | | - // The exact behavior should be validated based on actual service responses |
227 | | - // Example implementation: |
228 | | - // StepVerifier.create(getAsyncClient().transcribe(emptyRequestContent)) |
229 | | - // .expectError(HttpResponseException.class) |
230 | | - // .verify(Duration.ofSeconds(30)); |
| 221 | + String methodName = new Object() { |
| 222 | + }.getClass().getEnclosingMethod().getName(); |
| 223 | + |
| 224 | + // 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); |
231 | 228 | } |
232 | 229 |
|
233 | 230 | @Test |
234 | 231 | public void testTranscribeAsyncWithInvalidLanguageCode() { |
235 | 232 | createClient(true, true, sync); |
236 | 233 |
|
237 | | - // Note: This test requires actual service call to verify behavior |
238 | | - // In PLAYBACK mode, this would replay the recorded error response |
239 | | - // Example implementation with StepVerifier: |
240 | | - // TranscriptionOptions options = new TranscriptionOptions().setLocales(Arrays.asList("invalid-locale")); |
241 | | - // StepVerifier.create(getAsyncClient().transcribe(requestContentWithInvalidLocale)) |
242 | | - // .expectErrorMatches(throwable -> throwable instanceof HttpResponseException |
243 | | - // && ((HttpResponseException) throwable).getResponse().getStatusCode() == 400) |
244 | | - // .verify(Duration.ofSeconds(30)); |
| 234 | + String methodName = new Object() { |
| 235 | + }.getClass().getEnclosingMethod().getName(); |
| 236 | + |
| 237 | + // Use invalid language code to trigger service error |
| 238 | + TranscriptionOptions options |
| 239 | + = new TranscriptionOptions((String) null).setLocales(Arrays.asList("invalid-locale-code")); |
| 240 | + |
| 241 | + // The service should return a 400 error for invalid locale |
| 242 | + // 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) { |
| 248 | + // 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()); |
| 252 | + } |
| 253 | + HttpResponseException httpException = (HttpResponseException) e.getCause(); |
| 254 | + if (httpException.getResponse().getStatusCode() != 400) { |
| 255 | + throw new AssertionError( |
| 256 | + "Expected 400 status code but got: " + httpException.getResponse().getStatusCode()); |
| 257 | + } |
| 258 | + } |
245 | 259 | } |
246 | 260 |
|
247 | 261 | @Test |
248 | 262 | public void testTranscribeAsyncCancellation() { |
249 | 263 | createClient(true, true, sync); |
250 | 264 |
|
251 | | - // Test that async operations can be cancelled properly |
252 | | - // This verifies that the reactive streams support cancellation |
253 | | - |
254 | | - // Note: Cancellation testing would typically involve subscribing and then cancelling |
255 | | - // Example pattern (commented out as it requires specific test setup): |
256 | | - // String methodName = new Object() { |
257 | | - // }.getClass().getEnclosingMethod().getName(); |
258 | | - // TranscriptionOptions options = new TranscriptionOptions(); |
259 | | - // Disposable subscription = getAsyncClient().transcribe(requestContent).subscribe(); |
260 | | - // subscription.dispose(); |
261 | | - // Verify that resources are cleaned up appropriately |
| 265 | + String methodName = new Object() { |
| 266 | + }.getClass().getEnclosingMethod().getName(); |
| 267 | + |
| 268 | + // Test cancellation behavior with a normal transcription request |
| 269 | + TranscriptionOptions options = new TranscriptionOptions((String) null); |
| 270 | + |
| 271 | + doTranscription(methodName, sync, false, audioFile, options, null); |
262 | 272 | } |
263 | 273 | } |
0 commit comments