Search in sources :

Example 1 with WebSocketRecorder

use of okhttp3.internal.ws.WebSocketRecorder in project java-sdk by watson-developer-cloud.

the class SpeechToTextTest method testClosingInputStreamClosesWebSocket.

@Test
public void testClosingInputStreamClosesWebSocket() throws Exception {
    TestRecognizeCallback callback = new TestRecognizeCallback();
    WebSocketRecorder webSocketRecorder = new WebSocketRecorder("server");
    PipedOutputStream outputStream = new PipedOutputStream();
    InputStream inputStream = new PipedInputStream(outputStream);
    server.enqueue(new MockResponse().withWebSocketUpgrade(webSocketRecorder));
    String customizationId = "id";
    String version = "version";
    Double customizationWeight = 0.1;
    RecognizeOptions options = new RecognizeOptions.Builder().audio(inputStream).contentType(HttpMediaType.createAudioRaw(44000)).customizationId(customizationId).version(version).customizationWeight(customizationWeight).build();
    service.recognizeUsingWebSocket(options, callback);
    WebSocket serverSocket = webSocketRecorder.assertOpen();
    serverSocket.send("{\"state\": {}}");
    outputStream.write(ByteString.encodeUtf8("test").toByteArray());
    outputStream.close();
    webSocketRecorder.assertTextMessage("{\"content-type\":\"audio/l16; rate=44000\"," + "\"action\":\"start\"}");
    webSocketRecorder.assertBinaryMessage(ByteString.encodeUtf8("test"));
    webSocketRecorder.assertTextMessage("{\"action\":\"stop\"}");
    webSocketRecorder.assertExhausted();
    serverSocket.close(1000, null);
    callback.assertConnected();
    callback.assertDisconnected();
    callback.assertNoErrors();
    callback.assertOnTranscriptionComplete();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) PipedInputStream(java.io.PipedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteString(okio.ByteString) WebSocketRecorder(okhttp3.internal.ws.WebSocketRecorder) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WebSocket(okhttp3.WebSocket) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 RecognizeOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 WebSocket (okhttp3.WebSocket)1 WebSocketRecorder (okhttp3.internal.ws.WebSocketRecorder)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1