Search in sources :

Example 1 with CustomWord

use of com.ibm.watson.speech_to_text.v1.model.CustomWord in project java-sdk by watson-developer-cloud.

the class SpeechToTextTest method testAddWords.

/**
 * Test add words.
 *
 * @throws InterruptedException the interrupted exception
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testAddWords() throws InterruptedException, FileNotFoundException {
    String id = "foo";
    Word newWord = loadFixture("src/test/resources/speech_to_text/word.json", Word.class);
    Map<String, Object> wordsAsMap = new HashMap<String, Object>();
    wordsAsMap.put("words", new Word[] { newWord });
    server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody("{}"));
    CustomWord word = new CustomWord();
    word.setWord(newWord.getWord());
    word.setDisplayAs(newWord.getDisplayAs());
    word.setSoundsLike(newWord.getSoundsLike());
    AddWordsOptions addOptions = new AddWordsOptions.Builder().customizationId(id).addWords(word).build();
    service.addWords(addOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals("POST", request.getMethod());
    assertEquals(String.format(PATH_WORDS, id), request.getPath());
    assertEquals(GSON.toJson(wordsAsMap), request.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CustomWord(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CustomWord) Word(com.ibm.watson.developer_cloud.speech_to_text.v1.model.Word) AddWordsOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddWordsOptions) HashMap(java.util.HashMap) CustomWord(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CustomWord) JsonObject(com.google.gson.JsonObject) ByteString(okio.ByteString) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with CustomWord

use of com.ibm.watson.speech_to_text.v1.model.CustomWord in project java-sdk by watson-developer-cloud.

the class SpeechToTextTest method testAddWordsWOptions.

// Test the addWords operation with a valid options model parameter
@Test
public void testAddWordsWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "";
    String addWordsPath = "/v1/customizations/testString/words";
    server.enqueue(new MockResponse().setResponseCode(201).setBody(mockResponseBody));
    // Construct an instance of the CustomWord model
    CustomWord customWordModel = new CustomWord.Builder().word("testString").soundsLike(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).displayAs("testString").build();
    // Construct an instance of the AddWordsOptions model
    AddWordsOptions addWordsOptionsModel = new AddWordsOptions.Builder().customizationId("testString").words(new java.util.ArrayList<CustomWord>(java.util.Arrays.asList(customWordModel))).build();
    // Invoke addWords() with a valid options model and verify the result
    Response<Void> response = speechToTextService.addWords(addWordsOptionsModel).execute();
    assertNotNull(response);
    Void responseObj = response.getResult();
    assertNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, addWordsPath);
    // Verify that there is no query string
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) AddWordsOptions(com.ibm.watson.speech_to_text.v1.model.AddWordsOptions) CustomWord(com.ibm.watson.speech_to_text.v1.model.CustomWord) Test(org.testng.annotations.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 JsonObject (com.google.gson.JsonObject)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 AddWordsOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddWordsOptions)1 CustomWord (com.ibm.watson.developer_cloud.speech_to_text.v1.model.CustomWord)1 Word (com.ibm.watson.developer_cloud.speech_to_text.v1.model.Word)1 AddWordsOptions (com.ibm.watson.speech_to_text.v1.model.AddWordsOptions)1 CustomWord (com.ibm.watson.speech_to_text.v1.model.CustomWord)1 HashMap (java.util.HashMap)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1