|
14 | 14 |
|
15 | 15 | import com.ibm.watson.developer_cloud.WatsonServiceTest; |
16 | 16 | import com.ibm.watson.developer_cloud.http.HttpMediaType; |
17 | | -import com.ibm.watson.developer_cloud.service.exception.ConflictException; |
18 | 17 | import com.ibm.watson.developer_cloud.service.exception.NotFoundException; |
19 | 18 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.AcousticModel; |
20 | 19 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.AcousticModels; |
|
41 | 40 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetAudioOptions; |
42 | 41 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetCorpusOptions; |
43 | 42 | 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; |
44 | 44 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetModelOptions; |
45 | 45 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetWordOptions; |
46 | 46 | import com.ibm.watson.developer_cloud.speech_to_text.v1.model.Grammar; |
|
77 | 77 | import java.io.FileNotFoundException; |
78 | 78 | import java.util.Arrays; |
79 | 79 | import java.util.List; |
80 | | -import java.util.UUID; |
81 | 80 | import java.util.concurrent.CountDownLatch; |
82 | 81 | import java.util.concurrent.TimeUnit; |
83 | 82 | import java.util.logging.Logger; |
@@ -898,50 +897,60 @@ public void testDeleteUserData() { |
898 | 897 |
|
899 | 898 | @Test |
900 | 899 | 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; |
944 | 925 | } |
945 | 926 | } |
| 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); |
946 | 955 | } |
947 | 956 | } |
0 commit comments