1919import static org .junit .Assert .assertEquals ;
2020import static org .junit .Assert .assertSame ;
2121
22+ import com .google .api .services .translate .model .DetectionsResourceItems ;
2223import com .google .api .services .translate .model .LanguagesResource ;
2324import com .google .cloud .RetryParams ;
2425import com .google .cloud .translate .Translate .LanguageListOption ;
@@ -56,6 +57,12 @@ public class TranslateImplTest {
5657 private static final Language LANGUAGE4 = Language .fromPb (LANGUAGE4_PB );
5758 private static final List <Language > LANGUAGES1 = ImmutableList .of (LANGUAGE1 , LANGUAGE2 );
5859 private static final List <Language > LANGUAGES2 = ImmutableList .of (LANGUAGE3 , LANGUAGE4 );
60+ private static final DetectionsResourceItems DETECTION1_PB =
61+ new DetectionsResourceItems ().setLanguage ("en" ).setConfidence (0.9F );
62+ private static final DetectionsResourceItems DETECTION2_PB =
63+ new DetectionsResourceItems ().setLanguage ("en" ).setConfidence (0.8F );
64+ private static final Detection DETECTION1 = Detection .fromPb (DETECTION1_PB );
65+ private static final Detection DETECTION2 = Detection .fromPb (DETECTION2_PB );
5966
6067 // Empty TranslateRpc options
6168 private static final Map <TranslateRpc .Option , ?> EMPTY_RPC_OPTIONS = ImmutableMap .of ();
@@ -104,7 +111,7 @@ public void testGetOptions() {
104111 }
105112
106113 @ Test
107- public void testListSupporteLanguages () {
114+ public void testListSupportedLanguages () {
108115 EasyMock .expect (translateRpcMock .listSupportedLanguages (EMPTY_RPC_OPTIONS ))
109116 .andReturn (ImmutableList .of (LANGUAGE1_PB , LANGUAGE2_PB ));
110117 EasyMock .replay (translateRpcMock );
@@ -113,7 +120,7 @@ public void testListSupporteLanguages() {
113120 }
114121
115122 @ Test
116- public void testListSupporteLanguagesWithOptions () {
123+ public void testListSupportedLanguagesWithOptions () {
117124 EasyMock .expect (translateRpcMock .listSupportedLanguages (LANGUAGE_LIST_OPTIONS ))
118125 .andReturn (ImmutableList .of (LANGUAGE3_PB , LANGUAGE4_PB ));
119126 EasyMock .replay (translateRpcMock );
@@ -122,6 +129,126 @@ public void testListSupporteLanguagesWithOptions() {
122129 LanguageListOption .targetLanguage (TARGET_LANGUAGE )));
123130 }
124131
132+ @ Test
133+ public void testDetect () {
134+ String text = "text" ;
135+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text )))
136+ .andReturn (
137+ ImmutableList .<List <DetectionsResourceItems >>of (ImmutableList .of (DETECTION1_PB )));
138+ EasyMock .replay (translateRpcMock );
139+ initializeService ();
140+ assertEquals (DETECTION1 , translate .detect (text ));
141+ }
142+
143+ @ Test
144+ public void testDetectMultipleDetections () {
145+ String text = "text" ;
146+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text )))
147+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
148+ ImmutableList .of (DETECTION1_PB , DETECTION2_PB )));
149+ EasyMock .replay (translateRpcMock );
150+ initializeService ();
151+ thrown .expect (IllegalStateException .class );
152+ thrown .expectMessage ("Multiple detections found for text: text" );
153+ translate .detect (text );
154+ }
155+
156+ @ Test
157+ public void testDetectNoDetection () {
158+ String text = "text" ;
159+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text )))
160+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
161+ ImmutableList .<DetectionsResourceItems >of ()));
162+ EasyMock .replay (translateRpcMock );
163+ initializeService ();
164+ thrown .expect (IllegalStateException .class );
165+ thrown .expectMessage ("No detection found for text: text" );
166+ translate .detect (text );
167+ }
168+
169+ @ Test
170+ public void testDetectList () {
171+ String text1 = "text" ;
172+ String text2 = "other text" ;
173+ List <String > texts = ImmutableList .of (text1 , text2 );
174+ EasyMock .expect (translateRpcMock .detect (texts ))
175+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
176+ ImmutableList .of (DETECTION1_PB ), ImmutableList .of (DETECTION2_PB )));
177+ EasyMock .replay (translateRpcMock );
178+ initializeService ();
179+ assertEquals (ImmutableList .of (DETECTION1 , DETECTION2 ), translate .detect (texts ));
180+ }
181+
182+ @ Test
183+ public void testDetectListMultipleDetections () {
184+ String text1 = "text" ;
185+ String text2 = "other text" ;
186+ List <String > texts = ImmutableList .of (text1 , text2 );
187+ EasyMock .expect (translateRpcMock .detect (texts ))
188+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
189+ ImmutableList .of (DETECTION1_PB , DETECTION2_PB ), ImmutableList .of (DETECTION1_PB )));
190+ EasyMock .replay (translateRpcMock );
191+ initializeService ();
192+ thrown .expect (IllegalStateException .class );
193+ thrown .expectMessage ("Multiple detections found for text: text" );
194+ translate .detect (texts );
195+ }
196+
197+ @ Test
198+ public void testDetectListNoDetection () {
199+ String text1 = "text" ;
200+ String text2 = "other text" ;
201+ List <String > texts = ImmutableList .of (text1 , text2 );
202+ EasyMock .expect (translateRpcMock .detect (texts ))
203+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
204+ ImmutableList .of (DETECTION1_PB ), ImmutableList .<DetectionsResourceItems >of ()));
205+ EasyMock .replay (translateRpcMock );
206+ initializeService ();
207+ thrown .expect (IllegalStateException .class );
208+ thrown .expectMessage ("No detection found for text: other text" );
209+ translate .detect (texts );
210+ }
211+
212+ @ Test
213+ public void testDetectVararg () {
214+ String text1 = "text" ;
215+ String text2 = "other text" ;
216+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text1 , text2 )))
217+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
218+ ImmutableList .of (DETECTION1_PB ), ImmutableList .of (DETECTION2_PB )));
219+ EasyMock .replay (translateRpcMock );
220+ initializeService ();
221+ assertEquals (ImmutableList .of (DETECTION1 , DETECTION2 ), translate .detect (text1 , text2 ));
222+ }
223+
224+ @ Test
225+ public void testDetectVarargMultipleDetections () {
226+ String text1 = "text" ;
227+ String text2 = "other text" ;
228+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text1 , text2 )))
229+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
230+ ImmutableList .of (DETECTION1_PB , DETECTION2_PB ), ImmutableList .of (DETECTION1_PB )));
231+ EasyMock .replay (translateRpcMock );
232+ initializeService ();
233+ thrown .expect (IllegalStateException .class );
234+ thrown .expectMessage ("Multiple detections found for text: text" );
235+ translate .detect (text1 , text2 );
236+ }
237+
238+ @ Test
239+ public void testDetectVarargNoDetection () {
240+ String text1 = "text" ;
241+ String text2 = "other text" ;
242+ EasyMock .expect (translateRpcMock .detect (ImmutableList .of (text1 , text2 )))
243+ .andReturn (ImmutableList .<List <DetectionsResourceItems >>of (
244+ ImmutableList .of (DETECTION1_PB ), ImmutableList .<DetectionsResourceItems >of ()));
245+ EasyMock .replay (translateRpcMock );
246+ initializeService ();
247+ thrown .expect (IllegalStateException .class );
248+ thrown .expectMessage ("No detection found for text: other text" );
249+ translate .detect (text1 , text2 );
250+ }
251+
125252 @ Test
126253 public void testRetryableException () {
127254 EasyMock .expect (translateRpcMock .listSupportedLanguages (EMPTY_RPC_OPTIONS ))
0 commit comments