|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.ibm.watson.developer_cloud.language_translation.v2; |
| 17 | + |
| 18 | +import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
| 19 | +import static org.mockserver.model.HttpRequest.request; |
| 20 | +import static org.mockserver.model.HttpResponse.response; |
| 21 | +import io.netty.handler.codec.http.HttpHeaders; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.logging.Level; |
| 28 | +import java.util.logging.Logger; |
| 29 | + |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.mockserver.integration.ClientAndServer; |
| 35 | +import org.mockserver.model.Header; |
| 36 | +import org.mockserver.model.Parameter; |
| 37 | +import org.mockserver.verify.VerificationTimes; |
| 38 | + |
| 39 | +import com.google.gson.JsonArray; |
| 40 | +import com.google.gson.JsonObject; |
| 41 | +import com.google.gson.JsonPrimitive; |
| 42 | +import com.ibm.watson.developer_cloud.WatsonServiceTest; |
| 43 | +import com.ibm.watson.developer_cloud.language_translation.v2.model.IdentifiableLanguage; |
| 44 | +import com.ibm.watson.developer_cloud.language_translation.v2.model.IdentifiedLanguage; |
| 45 | +import com.ibm.watson.developer_cloud.language_translation.v2.model.Translation; |
| 46 | +import com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationModel; |
| 47 | +import com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult; |
| 48 | +import com.ibm.watson.developer_cloud.util.GsonSingleton; |
| 49 | +import com.ibm.watson.developer_cloud.util.MediaType; |
| 50 | + |
| 51 | +/** |
| 52 | + * Created by nizar. |
| 53 | + */ |
| 54 | +public class LanguageTranslationTest extends WatsonServiceTest { |
| 55 | + |
| 56 | + /** The Constant log. */ |
| 57 | + private static final Logger log = Logger.getLogger(LanguageTranslationTest.class.getName()); |
| 58 | + |
| 59 | + /** The model id. */ |
| 60 | + private String modelId; |
| 61 | + |
| 62 | + /** The service. */ |
| 63 | + private LanguageTranslation service; |
| 64 | + |
| 65 | + /** Mock Server *. */ |
| 66 | + private ClientAndServer mockServer; |
| 67 | + |
| 68 | + /** The text. */ |
| 69 | + private String text; |
| 70 | + |
| 71 | + /** The Constant LANGUAGE_TRANSLATION_PATH. (value is "/v2/translate") */ |
| 72 | + private final static String LANGUAGE_TRANSLATION_PATH = "/v2/translate"; |
| 73 | + |
| 74 | + /** The Constant IDENTIFIABLE_LANGUAGES_PATH. (value is "/v2/identifiable_languages") */ |
| 75 | + private final static String IDENTIFIABLE_LANGUAGES_PATH = "/v2/identifiable_languages"; |
| 76 | + |
| 77 | + /** The Constant GET_MODELS_PATH. (value is "/v2/models") */ |
| 78 | + private final static String GET_MODELS_PATH = "/v2/models"; |
| 79 | + |
| 80 | + /** The Constant IDENTITY_PATH. (value is "/v2/identify") */ |
| 81 | + private final static String IDENTITY_PATH = "/v2/identify"; |
| 82 | + |
| 83 | + /** |
| 84 | + * Start mock server. |
| 85 | + */ |
| 86 | + @Before |
| 87 | + public void startMockServer() { |
| 88 | + try { |
| 89 | + mockServer = startClientAndServer(Integer.parseInt(prop.getProperty("mock.server.port"))); |
| 90 | + service = new LanguageTranslation(); |
| 91 | + service.setApiKey(""); |
| 92 | + service.setEndPoint("http://" + prop.getProperty("mock.server.host") + ":" |
| 93 | + + prop.getProperty("mock.server.port")); |
| 94 | + } catch (NumberFormatException e) { |
| 95 | + log.log(Level.SEVERE, "Error mocking the service", e); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Stop mock server. |
| 102 | + */ |
| 103 | + @After |
| 104 | + public void stopMockServer() { |
| 105 | + mockServer.stop(); |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * (non-Javadoc) |
| 110 | + * |
| 111 | + * @see com.ibm.watson.developercloud.WatsonServiceTest#setUp() |
| 112 | + */ |
| 113 | + @Override |
| 114 | + @Before |
| 115 | + public void setUp() throws Exception { |
| 116 | + super.setUp(); |
| 117 | + modelId = prop.getProperty("language_translation.model_id"); |
| 118 | + text = "The IBM Watson team is awesome"; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test translate. |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void testTranslate() { |
| 126 | + |
| 127 | + // Mocking the response |
| 128 | + Map<String, Object> response = new HashMap<String, Object>(); |
| 129 | + List<Translation> translations = new ArrayList<Translation>(); |
| 130 | + |
| 131 | + Translation t = new Translation().withTranslation("El equipo es increible IBM Watson"); |
| 132 | + translations.add(t); |
| 133 | + |
| 134 | + response.put("word_count", 6); |
| 135 | + response.put("character_count", 20); |
| 136 | + response.put("translations", translations); |
| 137 | + |
| 138 | + String[] text1 = new String[] { text }; |
| 139 | + |
| 140 | + JsonObject contentJson = new JsonObject(); |
| 141 | + JsonArray paragraphs = new JsonArray(); |
| 142 | + for (String paragraph : text1) { |
| 143 | + paragraphs.add(new JsonPrimitive(paragraph)); |
| 144 | + } |
| 145 | + contentJson.add(LanguageTranslation.TEXT, paragraphs); |
| 146 | + mockServer.when( |
| 147 | + request().withMethod("POST").withPath(LANGUAGE_TRANSLATION_PATH) |
| 148 | + .withQueryStringParameter(new Parameter(LanguageTranslation.MODEL_ID, modelId)) |
| 149 | + .withBody(contentJson.toString()) |
| 150 | + |
| 151 | + ) |
| 152 | + .respond( |
| 153 | + response().withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON)) |
| 154 | + .withBody(GsonSingleton.getGson().toJson(response))); |
| 155 | + |
| 156 | + TranslationResult translationResult = service.translate(text, modelId); |
| 157 | + testTranslationResult(text, translationResult); |
| 158 | + |
| 159 | + Map<String, Object> params = new HashMap<String, Object>(); |
| 160 | + params.put(LanguageTranslation.TEXT, new String[] { text }); |
| 161 | + params.put(LanguageTranslation.MODEL_ID, modelId); |
| 162 | + |
| 163 | + translationResult = service.translate(params); |
| 164 | + testTranslationResult(text, translationResult); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Test translation result. |
| 169 | + * |
| 170 | + * @param text |
| 171 | + * the text |
| 172 | + * @param translationResult |
| 173 | + * the translation result |
| 174 | + */ |
| 175 | + private void testTranslationResult(String text, TranslationResult translationResult) { |
| 176 | + Assert.assertNotNull(translationResult); |
| 177 | + Assert.assertEquals(translationResult.getWordCount(), text.split(" ").length); |
| 178 | + Assert.assertNotNull(translationResult.getTranslations()); |
| 179 | + Assert.assertNotNull(translationResult.getTranslations().get(0).getTranslation()); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test Get Identifiable languages. |
| 184 | + */ |
| 185 | + @Test |
| 186 | + public void testGetIdentifiableLanguages() { |
| 187 | + |
| 188 | + Map<String, Object> response = new HashMap<String, Object>(); |
| 189 | + List<IdentifiableLanguage> langs = new ArrayList<IdentifiableLanguage>(); |
| 190 | + langs.add(new IdentifiableLanguage("af", "Afrikaans")); |
| 191 | + langs.add(new IdentifiableLanguage("ar", "Arabic")); |
| 192 | + langs.add(new IdentifiableLanguage("az", "Azerbaijani")); |
| 193 | + langs.add(new IdentifiableLanguage("zh", "Chinese")); |
| 194 | + response.put("languages", langs); |
| 195 | + |
| 196 | + System.out.println(GsonSingleton.getGson().toJson(response)); |
| 197 | + |
| 198 | + mockServer.when(request().withPath(IDENTIFIABLE_LANGUAGES_PATH)).respond( |
| 199 | + response().withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON)) |
| 200 | + .withBody(GsonSingleton.getGson().toJson(response))); |
| 201 | + |
| 202 | + List<IdentifiableLanguage> languages = service.getIdentifiableLanguages(); |
| 203 | + |
| 204 | + mockServer.verify(request().withPath(IDENTIFIABLE_LANGUAGES_PATH), VerificationTimes.exactly(1)); |
| 205 | + Assert.assertNotNull(languages); |
| 206 | + Assert.assertFalse(languages.containsAll(langs)); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Test Get Identifiable languages. |
| 211 | + */ |
| 212 | + @Test |
| 213 | + public void testGetModel() { |
| 214 | + // Mock response |
| 215 | + TranslationModel tm = new TranslationModel(); |
| 216 | + String model_id = "not-a-real-model"; |
| 217 | + tm.setModelId(model_id); |
| 218 | + tm.setSource("en"); |
| 219 | + tm.setBaseModelId("en-es"); |
| 220 | + tm.setDomain("news"); |
| 221 | + tm.setCustomizable(false); |
| 222 | + tm.setDefaultModel(false); |
| 223 | + tm.setOwner("not-a-real-user"); |
| 224 | + tm.setStatus("error"); |
| 225 | + tm.setName("custom-english-to-spanish"); |
| 226 | + |
| 227 | + System.out.println(GsonSingleton.getGson().toJson(tm)); |
| 228 | + |
| 229 | + mockServer.when(request().withPath(GET_MODELS_PATH + "/" + model_id)).respond( |
| 230 | + response().withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON)) |
| 231 | + .withBody(GsonSingleton.getGson().toJson(tm))); |
| 232 | + |
| 233 | + TranslationModel model = service.getModel("not-a-real-model"); |
| 234 | + |
| 235 | + mockServer.verify(request().withPath(GET_MODELS_PATH + "/" + model_id), VerificationTimes.exactly(1)); |
| 236 | + Assert.assertNotNull(model); |
| 237 | + // Assert.assertEquals(model.getModelId(),tm.getModelId()); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Test Get Models. |
| 242 | + */ |
| 243 | + @Test |
| 244 | + public void testGetModels() { |
| 245 | + |
| 246 | + Map<String, Object> response = new HashMap<String, Object>(); |
| 247 | + List<TranslationModel> tsModels = new ArrayList<TranslationModel>(); |
| 248 | + |
| 249 | + TranslationModel tm = new TranslationModel(); |
| 250 | + tm.setModelId("not-a-real-model"); |
| 251 | + tm.setSource("en"); |
| 252 | + tm.setBaseModelId("en-es"); |
| 253 | + tm.setDomain("news"); |
| 254 | + tm.setCustomizable(false); |
| 255 | + tm.setDefaultModel(false); |
| 256 | + tm.setOwner("not-a-real-user"); |
| 257 | + tm.setStatus("error"); |
| 258 | + tm.setName("custom-english-to-spanish"); |
| 259 | + tsModels.add(tm); |
| 260 | + |
| 261 | + TranslationModel tm1 = new TranslationModel(); |
| 262 | + tm1.setModelId("not-a-real-model-2"); |
| 263 | + tm1.setSource("en"); |
| 264 | + tm1.setBaseModelId("es"); |
| 265 | + tm1.setDomain("news"); |
| 266 | + tm1.setCustomizable(false); |
| 267 | + tm1.setDefaultModel(false); |
| 268 | + tm1.setOwner("not-a-real-user"); |
| 269 | + tm1.setStatus("error"); |
| 270 | + tm1.setName("custom-english-to-spanish"); |
| 271 | + tsModels.add(tm1); |
| 272 | + |
| 273 | + TranslationModel tm2 = new TranslationModel(); |
| 274 | + tm2.setModelId("ar-en"); |
| 275 | + tm2.setSource("en"); |
| 276 | + tm2.setBaseModelId(""); |
| 277 | + tm2.setDomain("news"); |
| 278 | + tm2.setCustomizable(true); |
| 279 | + tm2.setDefaultModel(true); |
| 280 | + tm2.setOwner(""); |
| 281 | + tm2.setStatus("available"); |
| 282 | + tm2.setName("custom-english-to-spanish"); |
| 283 | + tsModels.add(tm2); |
| 284 | + |
| 285 | + response.put("models", tsModels); |
| 286 | + |
| 287 | + mockServer.when(request().withPath(GET_MODELS_PATH)).respond( |
| 288 | + response().withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON)) |
| 289 | + .withBody(GsonSingleton.getGson().toJson(response))); |
| 290 | + |
| 291 | + List<TranslationModel> models = service.getModels(); |
| 292 | + |
| 293 | + mockServer.verify(request().withPath(GET_MODELS_PATH), VerificationTimes.exactly(1)); |
| 294 | + Assert.assertNotNull(models); |
| 295 | + |
| 296 | + Assert.assertNotNull(models); |
| 297 | + Assert.assertFalse(models.isEmpty()); |
| 298 | + Assert.assertNotNull(models.containsAll(tsModels)); |
| 299 | + |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Test Identify. |
| 304 | + */ |
| 305 | + @Test |
| 306 | + public void testIdentify() { |
| 307 | + |
| 308 | + Map<String, Object> response = new HashMap<String, Object>(); |
| 309 | + List<IdentifiedLanguage> langs = new ArrayList<IdentifiedLanguage>(); |
| 310 | + langs.add(new IdentifiedLanguage("en", 0.877159)); |
| 311 | + langs.add(new IdentifiedLanguage("af", 0.0752636)); |
| 312 | + langs.add(new IdentifiedLanguage("nl", 0.0301593)); |
| 313 | + |
| 314 | + response.put("languages", langs); |
| 315 | + |
| 316 | + mockServer.when(request().withMethod("POST").withPath(IDENTITY_PATH).withBody(text)).respond( |
| 317 | + response().withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON)) |
| 318 | + .withBody(GsonSingleton.getGson().toJson(response))); |
| 319 | + |
| 320 | + List<IdentifiedLanguage> identifiedLanguages = service.identify(text); |
| 321 | + Assert.assertNotNull(identifiedLanguages); |
| 322 | + Assert.assertFalse(identifiedLanguages.isEmpty()); |
| 323 | + Assert.assertNotNull(identifiedLanguages.containsAll(langs)); |
| 324 | + } |
| 325 | + |
| 326 | + /** |
| 327 | + * Test Translate with an invalid model. |
| 328 | + */ |
| 329 | + @Test |
| 330 | + public void testTranslateNotSupported() { |
| 331 | + boolean fail = true; |
| 332 | + try { |
| 333 | + // Mocking the response |
| 334 | + service.translate("X", "FOO-BAR-FOO"); |
| 335 | + } catch (Exception e) { |
| 336 | + fail = false; |
| 337 | + } |
| 338 | + Assert.assertFalse(fail); |
| 339 | + } |
| 340 | +} |
0 commit comments