|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. 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 | + |
| 17 | +package com.google.cloud.examples.translate.snippets; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.translate.Detection; |
| 24 | +import com.google.cloud.translate.Language; |
| 25 | +import com.google.cloud.translate.Translation; |
| 26 | +import com.google.cloud.translate.testing.RemoteTranslateHelper; |
| 27 | + |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +public class ITTranslateSnippets { |
| 36 | + |
| 37 | + private static TranslateSnippets translateSnippets; |
| 38 | + |
| 39 | + private static final String[] LANGUAGES = {"af", "sq", "ar", "hy", "az", "eu", "be", "bn", "bs", |
| 40 | + "bg", "ca", "ceb", "ny", "zh-TW", "hr", "cs", "da", "nl", "en", "eo", "et", "tl", "fi", "fr", |
| 41 | + "gl", "ka", "de", "el", "gu", "ht", "ha", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", |
| 42 | + "it", "ja", "jw", "kn", "kk", "km", "ko", "lo", "la", "lv", "lt", "mk", "mg", "ms", "ml", |
| 43 | + "mt", "mi", "mr", "mn", "my", "ne", "no", "fa", "pl", "pt", "ro", "ru", "sr", "st", "si", |
| 44 | + "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "te", "th", "tr", "uk", "ur", "uz", |
| 45 | + "vi", "cy", "yi", "yo", "zu"}; |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void beforeClass() { |
| 49 | + RemoteTranslateHelper helper = RemoteTranslateHelper.create(); |
| 50 | + translateSnippets = new TranslateSnippets(helper.options().service()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testListSupportedLanguages() { |
| 55 | + Set<String> supportedLanguages = new HashSet<>(); |
| 56 | + List<Language> languages = translateSnippets.listSupportedLanguages(); |
| 57 | + for (Language language : languages) { |
| 58 | + supportedLanguages.add(language.code()); |
| 59 | + assertNotNull(language.name()); |
| 60 | + } |
| 61 | + for (String code : LANGUAGES) { |
| 62 | + assertTrue(supportedLanguages.contains(code)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testListSupportedLanguagesWithTarget() { |
| 68 | + Set<String> supportedLanguages = new HashSet<>(); |
| 69 | + List<Language> languages = translateSnippets.listSupportedLanguagesWithTarget(); |
| 70 | + for (Language language : languages) { |
| 71 | + supportedLanguages.add(language.code()); |
| 72 | + assertNotNull(language.name()); |
| 73 | + } |
| 74 | + for (String code : LANGUAGES) { |
| 75 | + assertTrue(supportedLanguages.contains(code)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testDetectLanguageOfTexts() { |
| 81 | + List<Detection> detections = translateSnippets.detectLanguageOfTexts(); |
| 82 | + Detection detection = detections.get(0); |
| 83 | + assertEquals("en", detection.language()); |
| 84 | + detection = detections.get(1); |
| 85 | + assertEquals("es", detection.language()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testDetectLanguageOfTextList() { |
| 90 | + List<Detection> detections = translateSnippets.detectLanguageOfTextList(); |
| 91 | + Detection detection = detections.get(0); |
| 92 | + assertEquals("en", detection.language()); |
| 93 | + detection = detections.get(1); |
| 94 | + assertEquals("es", detection.language()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testDetectLanguageOfText() { |
| 99 | + Detection detection = translateSnippets.detectLanguageOfText(); |
| 100 | + assertEquals("en", detection.language()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testTranslateTextList() { |
| 105 | + List<Translation> translations = translateSnippets.translateTexts(); |
| 106 | + Translation translation = translations.get(0); |
| 107 | + assertEquals("Hello, World!", translation.translatedText()); |
| 108 | + assertEquals("en", translation.sourceLanguage()); |
| 109 | + translation = translations.get(1); |
| 110 | + assertEquals("Hello World!", translation.translatedText()); |
| 111 | + assertEquals("es", translation.sourceLanguage()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testTranslateText() { |
| 116 | + Translation translation = translateSnippets.translateText(); |
| 117 | + assertEquals("Hello World!", translation.translatedText()); |
| 118 | + assertEquals("es", translation.sourceLanguage()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testTranslateTextWithOptions() { |
| 123 | + Translation translation = translateSnippets.translateTextWithOptions(); |
| 124 | + assertEquals("Hallo Welt!", translation.translatedText()); |
| 125 | + assertEquals("es", translation.sourceLanguage()); |
| 126 | + } |
| 127 | +} |
0 commit comments