@Test public void testRetryableException() { EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new TranslateException(500, "internalError")) .andReturn(ImmutableList.of(LANGUAGE1_PB, LANGUAGE2_PB)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); assertEquals(LANGUAGES1, translate.listSupportedLanguages()); verify(); }
@Test public void testRuntimeException() { String exceptionMessage = "Artificial runtime exception"; EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); verify(); }
@Override protected Serializable[] serializableObjects() { TranslateOptions options = TranslateOptions.newBuilder() .setApiKey(API_KEY) .setCredentials(NoCredentials.getInstance()) .build(); TranslateOptions otherOptions = options.toBuilder().build(); return new Serializable[] { DETECTION, TRANSLATION, TRANSLATE_EXCEPTION, LANGUAGE_LIST_OPTION, TRANSLATE_OPTION, options, otherOptions }; }
@Test public void testNonRetryableException() { String exceptionMessage = "Not Implemented"; EasyMock.expect(translateRpcMock.listSupportedLanguages(EMPTY_RPC_OPTIONS)) .andThrow(new TranslateException(501, exceptionMessage)); EasyMock.replay(translateRpcMock); translate = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); verify(); }
@Test public void testCredentialsOverridesApiKey() { Credentials credentials = NoCredentials.getInstance(); TranslateOptions overridden = options.toBuilder().setCredentials(credentials).build(); assertSame(overridden.getCredentials(), credentials); assertNull(overridden.getApiKey()); } }