LanguageTranslator extends BaseService { public ServiceCall<TranslationResult> translate(TranslateOptions translateOptions) { com.ibm.cloud.sdk.core.util.Validator.notNull( translateOptions, "translateOptions cannot be null"); String[] pathSegments = {"v3/translate"}; RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments)); builder.query("version", versionDate); Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("language_translator", "v3", "translate"); for (Entry<String, String> header : sdkHeaders.entrySet()) { builder.header(header.getKey(), header.getValue()); } builder.header("Accept", "application/json"); final JsonObject contentJson = new JsonObject(); contentJson.add( "text", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(translateOptions.text())); if (translateOptions.modelId() != null) { contentJson.addProperty("model_id", translateOptions.modelId()); } if (translateOptions.source() != null) { contentJson.addProperty("source", translateOptions.source()); } if (translateOptions.target() != null) { contentJson.addProperty("target", translateOptions.target()); } builder.bodyJson(contentJson); ResponseConverter<TranslationResult> responseConverter = ResponseConverterUtils.getValue( new com.google.gson.reflect.TypeToken<TranslationResult>() {}.getType()); return createServiceCall(builder.build(), responseConverter); } LanguageTranslator(String versionDate);  LanguageTranslator(String versionDate, Authenticator authenticator);  LanguageTranslator(String versionDate, String serviceName);  LanguageTranslator(String versionDate, String serviceName, Authenticator authenticator); ServiceCall<Languages> listLanguages(ListLanguagesOptions listLanguagesOptions); ServiceCall<Languages> listLanguages(); ServiceCall<TranslationResult> translate(TranslateOptions translateOptions); ServiceCall<IdentifiableLanguages> listIdentifiableLanguages(
      ListIdentifiableLanguagesOptions listIdentifiableLanguagesOptions); ServiceCall<IdentifiableLanguages> listIdentifiableLanguages(); ServiceCall<IdentifiedLanguages> identify(IdentifyOptions identifyOptions); ServiceCall<TranslationModels> listModels(ListModelsOptions listModelsOptions); ServiceCall<TranslationModels> listModels(); ServiceCall<TranslationModel> createModel(CreateModelOptions createModelOptions); ServiceCall<DeleteModelResult> deleteModel(DeleteModelOptions deleteModelOptions); ServiceCall<TranslationModel> getModel(GetModelOptions getModelOptions); ServiceCall<DocumentList> listDocuments(ListDocumentsOptions listDocumentsOptions); ServiceCall<DocumentList> listDocuments(); ServiceCall<DocumentStatus> translateDocument(
      TranslateDocumentOptions translateDocumentOptions); ServiceCall<DocumentStatus> getDocumentStatus(
      GetDocumentStatusOptions getDocumentStatusOptions); ServiceCall<Void> deleteDocument(DeleteDocumentOptions deleteDocumentOptions); ServiceCall<InputStream> getTranslatedDocument(
      GetTranslatedDocumentOptions getTranslatedDocumentOptions);  }
@Test public void testTranslate() throws InterruptedException { server.enqueue(jsonResponse(singleTranslation)); final String text = texts.get(0); final Map<String, ?> requestBody = ImmutableMap.of("text", Collections.singleton(text), "model_id", modelId); TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).modelId(modelId).build(); TranslationResult translationResult = service.translate(translateOptions).execute().getResult(); RecordedRequest request = server.takeRequest(); assertEquals(LANGUAGE_TRANSLATION_PATH + VERSION_PARAM, request.getPath()); assertEquals("POST", request.getMethod()); assertEquals(GSON.toJson(requestBody), request.getBody().readUtf8()); testTranslationResult(text, translationResult); }
@Test public void testTranslateMultiple() throws InterruptedException { server.enqueue(jsonResponse(multipleTranslations)); final Map<String, ?> requestBody = ImmutableMap.of("text", texts, "model_id", modelId); TranslateOptions translateOptions = new TranslateOptions.Builder().text(texts).modelId(modelId).build(); TranslationResult translationResult = service.translate(translateOptions).execute().getResult(); RecordedRequest request = server.takeRequest(); assertEquals(LANGUAGE_TRANSLATION_PATH + VERSION_PARAM, request.getPath()); assertEquals("POST", request.getMethod()); assertEquals(GSON.toJson(requestBody), request.getBody().readUtf8()); assertEquals(2, translationResult.getTranslations().size()); assertEquals( translations.get(texts.get(0)), translationResult.getTranslations().get(0).getTranslation()); assertEquals( translations.get(texts.get(1)), translationResult.getTranslations().get(1).getTranslation()); }
@Test(expected = BadRequestException.class) public void testTranslateNotSupported() { Map<String, ?> response = ImmutableMap.of("error_code", 400, "error message", "error"); server.enqueue(jsonResponse(response).setResponseCode(400)); TranslateOptions translateOptions = new TranslateOptions.Builder().addText("X").modelId("FOO-BAR-FOO").build(); service.translate(translateOptions).execute(); }
@Test(expected = IllegalArgumentException.class) public void testTranslateWithNull() { TranslateOptions translateOptions = new TranslateOptions.Builder().build(); service.translate(translateOptions).execute(); }