|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. 2025 - 2025 |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package com.ibm.watsonx.ai; |
| 6 | + |
| 7 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 8 | +import static com.github.tomakehurst.wiremock.client.WireMock.containing; |
| 9 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 10 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
| 14 | +import static org.mockito.ArgumentMatchers.any; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileNotFoundException; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 25 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 26 | +import org.mockito.quality.Strictness; |
| 27 | +import com.ibm.watsonx.ai.core.Language; |
| 28 | +import com.ibm.watsonx.ai.transcription.TranscriptionRequest; |
| 29 | +import com.ibm.watsonx.ai.transcription.TranscriptionService; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +public class TranscriptionServiceTest extends AbstractWatsonxTest { |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setup() { |
| 36 | + when(mockAuthenticator.token()).thenReturn("token"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void should_transcribe_audio_successfully() throws Exception { |
| 41 | + |
| 42 | + var file = Files.createTempFile("", ""); |
| 43 | + file.toFile().deleteOnExit(); |
| 44 | + Files.writeString(file, "the ending was terrific."); |
| 45 | + |
| 46 | + wireMock.stubFor(post("/ml/v1/audio/transcriptions?version=%s".formatted(API_VERSION)) |
| 47 | + .withHeader("Authorization", equalTo("Bearer token")) |
| 48 | + .withHeader("Content-Type", containing("multipart/form-data; boundary=----watsonx-ai-sdk")) |
| 49 | + .withRequestBody(containing("name=\"model\"")) |
| 50 | + .withRequestBody(containing("openai/whisper-tiny")) |
| 51 | + .withRequestBody(containing("name=\"language\"")) |
| 52 | + .withRequestBody(containing("it")) |
| 53 | + .withRequestBody(containing("name=\"project_id\"")) |
| 54 | + .withRequestBody(containing("pid")) |
| 55 | + .withRequestBody(containing("the ending was terrific.")) |
| 56 | + .willReturn(aResponse() |
| 57 | + .withStatus(200) |
| 58 | + .withHeader("Content-Type", "application/json") |
| 59 | + .withBody(""" |
| 60 | + { |
| 61 | + "model": "openai/whisper-tiny", |
| 62 | + "text": "the ending was terrific.", |
| 63 | + "created_at": "2023-07-21T16:52:32.190Z", |
| 64 | + "token_count": 8 |
| 65 | + }"""))); |
| 66 | + |
| 67 | + TranscriptionService service = TranscriptionService.builder() |
| 68 | + .authenticator(mockAuthenticator) |
| 69 | + .baseUrl("http://localhost:".concat(String.valueOf(wireMock.getPort()))) |
| 70 | + .projectId("pid") |
| 71 | + .modelId("openai/whisper-tiny") |
| 72 | + .build(); |
| 73 | + |
| 74 | + var result = service.transcribe(file.toAbsolutePath().toString(), Language.ITALIAN); |
| 75 | + assertEquals("openai/whisper-tiny", result.model()); |
| 76 | + assertEquals("the ending was terrific.", result.text()); |
| 77 | + assertEquals(8, result.tokenCount()); |
| 78 | + assertEquals("2023-07-21T16:52:32.190Z", result.createdAt()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void should_transcribe_audio_successfully_overriding_parameters() throws Exception { |
| 83 | + |
| 84 | + var file = Files.createTempFile("", ""); |
| 85 | + file.toFile().deleteOnExit(); |
| 86 | + Files.writeString(file, "the ending was terrific."); |
| 87 | + |
| 88 | + wireMock.stubFor(post("/ml/v1/audio/transcriptions?version=%s".formatted(API_VERSION)) |
| 89 | + .withHeader("Authorization", equalTo("Bearer token")) |
| 90 | + .withHeader("Content-Type", containing("multipart/form-data; boundary=----watsonx-ai-sdk")) |
| 91 | + .withHeader(TRANSACTION_ID_HEADER, equalTo("transaction-id")) |
| 92 | + .withRequestBody(containing("name=\"model\"")) |
| 93 | + .withRequestBody(containing("my-openai/whisper-tiny")) |
| 94 | + .withRequestBody(containing("name=\"language\"")) |
| 95 | + .withRequestBody(containing("en")) |
| 96 | + .withRequestBody(containing("name=\"project_id\"")) |
| 97 | + .withRequestBody(containing("mid")) |
| 98 | + .withRequestBody(containing("name=\"space_id\"")) |
| 99 | + .withRequestBody(containing("sid")) |
| 100 | + .withRequestBody(containing("the ending was terrific.")) |
| 101 | + .willReturn(aResponse() |
| 102 | + .withStatus(200) |
| 103 | + .withHeader("Content-Type", "application/json") |
| 104 | + .withBody(""" |
| 105 | + { |
| 106 | + "model": "openai/whisper-tiny", |
| 107 | + "text": "the ending was terrific.", |
| 108 | + "created_at": "2023-07-21T16:52:32.190Z", |
| 109 | + "token_count": 8 |
| 110 | + }"""))); |
| 111 | + |
| 112 | + TranscriptionService service = TranscriptionService.builder() |
| 113 | + .authenticator(mockAuthenticator) |
| 114 | + .baseUrl("http://localhost:".concat(String.valueOf(wireMock.getPort()))) |
| 115 | + .projectId("pid") |
| 116 | + .modelId("openai/whisper-tiny") |
| 117 | + .build(); |
| 118 | + |
| 119 | + var result = service.transcribe( |
| 120 | + TranscriptionRequest.builder() |
| 121 | + .projectId("mid") |
| 122 | + .spaceId("sid") |
| 123 | + .modelId("my-openai/whisper-tiny") |
| 124 | + .file(file.toAbsolutePath().toString()) |
| 125 | + .transactionId("transaction-id") |
| 126 | + .build()); |
| 127 | + |
| 128 | + assertEquals("openai/whisper-tiny", result.model()); |
| 129 | + assertEquals("the ending was terrific.", result.text()); |
| 130 | + assertEquals(8, result.tokenCount()); |
| 131 | + assertEquals("2023-07-21T16:52:32.190Z", result.createdAt()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @MockitoSettings(strictness = Strictness.LENIENT) |
| 136 | + void should_throw_file_not_found_exception() { |
| 137 | + var ex = assertThrowsExactly(RuntimeException.class, () -> TranscriptionRequest.builder().file("/fileNotFound/file.txt").build()); |
| 138 | + assertEquals(FileNotFoundException.class, ex.getCause().getClass()); |
| 139 | + |
| 140 | + TranscriptionService service = TranscriptionService.builder() |
| 141 | + .authenticator(mockAuthenticator) |
| 142 | + .baseUrl("http://localhost:".concat(String.valueOf(wireMock.getPort()))) |
| 143 | + .projectId("pid") |
| 144 | + .modelId("openai/whisper-tiny") |
| 145 | + .build(); |
| 146 | + |
| 147 | + ex = assertThrowsExactly(RuntimeException.class, () -> service.transcribe(new File("/fileNotFound/file.txt"), Language.ENGLISH)); |
| 148 | + assertEquals(FileNotFoundException.class, ex.getCause().getClass()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void should_throw_io_exception() throws Exception { |
| 153 | + |
| 154 | + when(mockHttpClient.send(any(), any())).thenThrow(IOException.class); |
| 155 | + |
| 156 | + withWatsonxServiceMock(() -> { |
| 157 | + |
| 158 | + TranscriptionService service = TranscriptionService.builder() |
| 159 | + .authenticator(mockAuthenticator) |
| 160 | + .baseUrl("http://localhost:".concat(String.valueOf(wireMock.getPort()))) |
| 161 | + .projectId("pid") |
| 162 | + .modelId("openai/whisper-tiny") |
| 163 | + .build(); |
| 164 | + |
| 165 | + var ex = assertThrows(RuntimeException.class, () -> service.transcribe(new ByteArrayInputStream("Hello".getBytes()), Language.ITALIAN)); |
| 166 | + assertEquals(IOException.class, ex.getCause().getClass()); |
| 167 | + }); |
| 168 | + } |
| 169 | +} |
0 commit comments