Skip to content

Commit c5f24a3

Browse files
committed
test(Speech to Text): Fix grammar integration test
1 parent 682a52b commit c5f24a3

File tree

1 file changed

+54
-45
lines changed
  • speech-to-text/src/test/java/com/ibm/watson/developer_cloud/speech_to_text/v1

1 file changed

+54
-45
lines changed

speech-to-text/src/test/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToTextIT.java

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import com.ibm.watson.developer_cloud.WatsonServiceTest;
1616
import com.ibm.watson.developer_cloud.http.HttpMediaType;
17-
import com.ibm.watson.developer_cloud.service.exception.ConflictException;
1817
import com.ibm.watson.developer_cloud.service.exception.NotFoundException;
1918
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.AcousticModel;
2019
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.AcousticModels;
@@ -41,6 +40,7 @@
4140
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetAudioOptions;
4241
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetCorpusOptions;
4342
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetGrammarOptions;
43+
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetLanguageModelOptions;
4444
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetModelOptions;
4545
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetWordOptions;
4646
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.Grammar;
@@ -77,7 +77,6 @@
7777
import java.io.FileNotFoundException;
7878
import java.util.Arrays;
7979
import java.util.List;
80-
import java.util.UUID;
8180
import java.util.concurrent.CountDownLatch;
8281
import java.util.concurrent.TimeUnit;
8382
import java.util.logging.Logger;
@@ -898,50 +897,60 @@ public void testDeleteUserData() {
898897

899898
@Test
900899
public void testGrammarOperations() throws FileNotFoundException, InterruptedException {
901-
String grammarName = "java-sdk-test-grammar" + UUID.randomUUID();
902-
903-
for (int i = 0; i < 10; i++) {
904-
try {
905-
AddGrammarOptions addGrammarOptions = new AddGrammarOptions.Builder()
906-
.customizationId(customizationId)
907-
.grammarFile(new FileInputStream(SAMPLE_GRAMMAR))
908-
.grammarName(grammarName)
909-
.contentType(AddGrammarOptions.ContentType.APPLICATION_SRGS)
910-
.build();
911-
service.addGrammar(addGrammarOptions).execute();
912-
913-
ListGrammarsOptions listGrammarsOptions = new ListGrammarsOptions.Builder()
914-
.customizationId(customizationId)
915-
.build();
916-
Grammars listGrammarsResponse = service.listGrammars(listGrammarsOptions).execute();
917-
assertNotNull(listGrammarsResponse);
918-
boolean found = false;
919-
for (Grammar g : listGrammarsResponse.getGrammars()) {
920-
if (g.getName().equals(grammarName)) {
921-
found = true;
922-
break;
923-
}
924-
}
925-
assertTrue(found);
926-
927-
GetGrammarOptions getGrammarOptions = new GetGrammarOptions.Builder()
928-
.customizationId(customizationId)
929-
.grammarName(grammarName)
930-
.build();
931-
Grammar getGrammarResponse = service.getGrammar(getGrammarOptions).execute();
932-
assertNotNull(getGrammarResponse);
933-
assertEquals(grammarName, getGrammarResponse.getName());
934-
935-
DeleteGrammarOptions deleteGrammarOptions = new DeleteGrammarOptions.Builder()
936-
.customizationId(customizationId)
937-
.grammarName(grammarName)
938-
.build();
939-
service.deleteGrammar(deleteGrammarOptions).execute();
940-
} catch (ConflictException e) {
941-
System.out.println("Service wasn't quite ready yet. Error: " + e.getMessage());
942-
Thread.sleep(5000);
943-
System.out.println("Trying again...");
900+
while (!isCustomizationReady(customizationId)) {
901+
Thread.sleep(5000);
902+
}
903+
904+
String grammarName = "java-sdk-test-grammar";
905+
906+
AddGrammarOptions addGrammarOptions = new AddGrammarOptions.Builder()
907+
.customizationId(customizationId)
908+
.grammarFile(new FileInputStream(SAMPLE_GRAMMAR))
909+
.grammarName(grammarName)
910+
.contentType(AddGrammarOptions.ContentType.APPLICATION_SRGS)
911+
.allowOverwrite(true)
912+
.build();
913+
service.addGrammar(addGrammarOptions).execute();
914+
915+
ListGrammarsOptions listGrammarsOptions = new ListGrammarsOptions.Builder()
916+
.customizationId(customizationId)
917+
.build();
918+
Grammars listGrammarsResponse = service.listGrammars(listGrammarsOptions).execute();
919+
assertNotNull(listGrammarsResponse);
920+
boolean found = false;
921+
for (Grammar g : listGrammarsResponse.getGrammars()) {
922+
if (g.getName().equals(grammarName)) {
923+
found = true;
924+
break;
944925
}
945926
}
927+
assertTrue(found);
928+
929+
GetGrammarOptions getGrammarOptions = new GetGrammarOptions.Builder()
930+
.customizationId(customizationId)
931+
.grammarName(grammarName)
932+
.build();
933+
Grammar getGrammarResponse = service.getGrammar(getGrammarOptions).execute();
934+
assertNotNull(getGrammarResponse);
935+
assertEquals(grammarName, getGrammarResponse.getName());
936+
937+
while (!isCustomizationReady(customizationId)) {
938+
Thread.sleep(5000);
939+
}
940+
941+
DeleteGrammarOptions deleteGrammarOptions = new DeleteGrammarOptions.Builder()
942+
.customizationId(customizationId)
943+
.grammarName(grammarName)
944+
.build();
945+
service.deleteGrammar(deleteGrammarOptions).execute();
946+
}
947+
948+
private boolean isCustomizationReady(String customizationId) {
949+
GetLanguageModelOptions getLanguageModelOptions = new GetLanguageModelOptions.Builder()
950+
.customizationId(customizationId)
951+
.build();
952+
LanguageModel model = service.getLanguageModel(getLanguageModelOptions).execute();
953+
return model.getStatus().equals(LanguageModel.Status.READY)
954+
|| model.getStatus().equals(LanguageModel.Status.AVAILABLE);
946955
}
947956
}

0 commit comments

Comments
 (0)