Search in sources :

Example 1 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method asyncRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void asyncRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    try {
        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
        int sampleRate = 44100;
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRate(sampleRate).build();
        String uri = "gs://bucket_name/file_name.flac";
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
        client.asyncRecognizeAsync(config, audio).get();
        Assert.fail("No exception raised");
    } catch (ExecutionException e) {
        Assert.assertEquals(ApiException.class, e.getCause().getClass());
        ApiException apiException = (ApiException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), apiException.getStatusCode());
    }
}
Also used : RecognitionAudio(com.google.cloud.speech.v1beta1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding) StatusRuntimeException(io.grpc.StatusRuntimeException) RecognitionConfig(com.google.cloud.speech.v1beta1.RecognitionConfig) ExecutionException(java.util.concurrent.ExecutionException) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 2 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method asyncRecognizeTest.

@Test
@SuppressWarnings("all")
public void asyncRecognizeTest() throws Exception {
    AsyncRecognizeResponse expectedResponse = AsyncRecognizeResponse.newBuilder().build();
    Operation resultOperation = Operation.newBuilder().setName("asyncRecognizeTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockSpeech.addResponse(resultOperation);
    RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
    int sampleRate = 44100;
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRate(sampleRate).build();
    String uri = "gs://bucket_name/file_name.flac";
    RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
    AsyncRecognizeResponse actualResponse = client.asyncRecognizeAsync(config, audio).get();
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSpeech.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AsyncRecognizeRequest actualRequest = (AsyncRecognizeRequest) actualRequests.get(0);
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertEquals(audio, actualRequest.getAudio());
}
Also used : RecognitionAudio(com.google.cloud.speech.v1beta1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding) AsyncRecognizeRequest(com.google.cloud.speech.v1beta1.AsyncRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1beta1.RecognitionConfig) Operation(com.google.longrunning.Operation) AsyncRecognizeResponse(com.google.cloud.speech.v1beta1.AsyncRecognizeResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 3 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method longRunningRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void longRunningRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    try {
        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
        int sampleRateHertz = 44100;
        String languageCode = "en-US";
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRateHertz(sampleRateHertz).setLanguageCode(languageCode).build();
        String uri = "gs://bucket_name/file_name.flac";
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
        client.longRunningRecognizeAsync(config, audio).get();
        Assert.fail("No exception raised");
    } catch (ExecutionException e) {
        Assert.assertEquals(ApiException.class, e.getCause().getClass());
        ApiException apiException = (ApiException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), apiException.getStatusCode());
    }
}
Also used : RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding) StatusRuntimeException(io.grpc.StatusRuntimeException) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) ExecutionException(java.util.concurrent.ExecutionException) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 4 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method recognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void recognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    try {
        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
        int sampleRateHertz = 44100;
        String languageCode = "en-US";
        RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRateHertz(sampleRateHertz).setLanguageCode(languageCode).build();
        String uri = "gs://bucket_name/file_name.flac";
        RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
        client.recognize(config, audio);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding) StatusRuntimeException(io.grpc.StatusRuntimeException) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 5 with RecognitionConfig

use of com.google.cloud.speech.v1.RecognitionConfig in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method recognizeTest.

@Test
@SuppressWarnings("all")
public void recognizeTest() {
    RecognizeResponse expectedResponse = RecognizeResponse.newBuilder().build();
    mockSpeech.addResponse(expectedResponse);
    RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
    int sampleRateHertz = 44100;
    String languageCode = "en-US";
    RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(encoding).setSampleRateHertz(sampleRateHertz).setLanguageCode(languageCode).build();
    String uri = "gs://bucket_name/file_name.flac";
    RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uri).build();
    RecognizeResponse actualResponse = client.recognize(config, audio);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSpeech.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    RecognizeRequest actualRequest = (RecognizeRequest) actualRequests.get(0);
    Assert.assertEquals(config, actualRequest.getConfig());
    Assert.assertEquals(audio, actualRequest.getAudio());
}
Also used : RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) AudioEncoding(com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding) LongRunningRecognizeRequest(com.google.cloud.speech.v1.LongRunningRecognizeRequest) RecognizeRequest(com.google.cloud.speech.v1.RecognizeRequest) StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) LongRunningRecognizeResponse(com.google.cloud.speech.v1.LongRunningRecognizeResponse) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)5 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)5 ApiException (com.google.api.gax.grpc.ApiException)4 AudioEncoding (com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding)4 RecognitionAudio (com.google.cloud.speech.v1beta1.RecognitionAudio)4 RecognitionConfig (com.google.cloud.speech.v1beta1.RecognitionConfig)4 AudioEncoding (com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding)4 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 LongRunningRecognizeRequest (com.google.cloud.speech.v1.LongRunningRecognizeRequest)2 LongRunningRecognizeResponse (com.google.cloud.speech.v1.LongRunningRecognizeResponse)2 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)2 Operation (com.google.longrunning.Operation)2 ExecutionException (java.util.concurrent.ExecutionException)2 SpeechClient (com.google.cloud.speech.spi.v1.SpeechClient)1 RecognizeRequest (com.google.cloud.speech.v1.RecognizeRequest)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)1 StreamingRecognizeRequest (com.google.cloud.speech.v1.StreamingRecognizeRequest)1