@Test public void testRequestTranslations() { String responseKey = "randomKeyThatReturnsFromMS"; String responseXml = "response"; Response builderResp = Mockito.mock(Response.class); when(builderResp.getStatusInfo()).thenReturn(Response.Status.OK); when(builderResp.readEntity(String.class)).thenReturn(responseKey); Response webResp = Mockito.mock(Response.class); when(webResp.getStatusInfo()).thenReturn(Response.Status.OK); when(webResp.readEntity(String.class)).thenReturn(responseXml); ResteasyWebTarget webTarget = Mockito.mock(ResteasyWebTarget.class); Invocation.Builder webBuilder = Mockito.mock(Invocation.Builder.class); when(webTarget.request(any(String.class))).thenReturn(webBuilder); when(webBuilder.header(any(), any())).thenReturn(webBuilder); when(webBuilder.post(any())).thenReturn(webResp); when(invocation.invoke()).thenReturn(builderResp); when(restClient.getWebTarget(any())).thenReturn(webTarget); MSTranslateArrayReq req = new MSTranslateArrayReq(); String xml = api.requestTranslations(req); assertThat(xml).isEqualTo(responseXml); verify(webResp).close(); }
protected String requestTranslations(MSTranslateArrayReq req) throws MTException { getTokenIfNeeded(); if (LOG.isDebugEnabled()) { LOG.debug("Source sending:{}", dtoUtil.toXML(req)); } ResteasyWebTarget webTarget = restClient.getWebTarget(TRANSLATIONS_BASE_URL); Response response = webTarget.request(MediaType.TEXT_XML) .header("Content-Type", MediaType.TEXT_XML + "; charset=" + ENCODING) .header("Authorization", token) .post(Entity.xml(dtoUtil.toXML(req))); if (response.getStatusInfo() != Response.Status.OK) { throw new MTException( "Error from Microsoft Translator API: " + response.getStatusInfo().getReasonPhrase()); } String xml = response.readEntity(String.class); response.close(); LOG.debug("Translation from Microsoft Engine:{}", xml); return xml; }
MicrosoftTranslatorClient { protected String requestTranslations(MSTranslateArrayReq req) throws MTException { getTokenIfNeeded(); if (LOG.isDebugEnabled()) { LOG.debug("Source sending:{}", dtoUtil.toXML(req)); } ResteasyWebTarget webTarget = restClient.getWebTarget(TRANSLATIONS_BASE_URL); Response response = webTarget.request(MediaType.TEXT_XML) .header("Content-Type", MediaType.TEXT_XML + "; charset=" + ENCODING) .header("Authorization", token) .post(Entity.xml(dtoUtil.toXML(req))); if (response.getStatusInfo() != Response.Status.OK) { throw new MTException( "Error from Microsoft Translator API: " + response.getStatusInfo().getReasonPhrase()); } String xml = response.readEntity(String.class); response.close(); LOG.debug("Translation from Microsoft Engine:{}", xml); return xml; } }
MicrosoftTranslatorClient { protected String requestTranslations(MSTranslateArrayReq req) throws MTException { getTokenIfNeeded(); if (LOG.isDebugEnabled()) { LOG.debug("Source sending:{}", dtoUtil.toXML(req)); } ResteasyWebTarget webTarget = restClient.getWebTarget(TRANSLATIONS_BASE_URL); Response response = webTarget.request(MediaType.TEXT_XML) .header("Content-Type", MediaType.TEXT_XML + "; charset=" + ENCODING) .header("Authorization", token) .post(Entity.xml(dtoUtil.toXML(req))); if (response.getStatusInfo() != Response.Status.OK) { throw new MTException( "Error from Microsoft Translator API: " + response.getStatusInfo().getReasonPhrase()); } String xml = response.readEntity(String.class); response.close(); LOG.debug("Translation from Microsoft Engine:{}", xml); return xml; } protected  MicrosoftTranslatorClient(String clientSubscriptionKey,
            MicrosoftRestEasyClient restClient, DTOUtil dtoUtil); }
MicrosoftTranslatorClient { protected String requestTranslations(MSTranslateArrayReq req) throws MTException { getTokenIfNeeded(); if (LOG.isDebugEnabled()) { LOG.debug("Source sending:{}", dtoUtil.toXML(req)); } ResteasyWebTarget webTarget = restClient.getWebTarget(TRANSLATIONS_BASE_URL); Response response = webTarget.request(MediaType.TEXT_XML) .header("Content-Type", MediaType.TEXT_XML + "; charset=" + ENCODING) .header("Authorization", token) .post(Entity.xml(dtoUtil.toXML(req))); if (response.getStatusInfo() != Response.Status.OK) { throw new MTException( "Error from Microsoft Translator API: " + response.getStatusInfo().getReasonPhrase()); } String xml = response.readEntity(String.class); response.close(); LOG.debug("Translation from Microsoft Engine:{}", xml); return xml; } protected  MicrosoftTranslatorClient(String clientSubscriptionKey,
            MicrosoftRestEasyClient restClient, DTOUtil dtoUtil);  }
MicrosoftTranslatorClient { protected String requestTranslations(MSTranslateArrayReq req) throws MTException { getTokenIfNeeded(); if (LOG.isDebugEnabled()) { LOG.debug("Source sending:{}", dtoUtil.toXML(req)); } ResteasyWebTarget webTarget = restClient.getWebTarget(TRANSLATIONS_BASE_URL); Response response = webTarget.request(MediaType.TEXT_XML) .header("Content-Type", MediaType.TEXT_XML + "; charset=" + ENCODING) .header("Authorization", token) .post(Entity.xml(dtoUtil.toXML(req))); if (response.getStatusInfo() != Response.Status.OK) { throw new MTException( "Error from Microsoft Translator API: " + response.getStatusInfo().getReasonPhrase()); } String xml = response.readEntity(String.class); response.close(); LOG.debug("Translation from Microsoft Engine:{}", xml); return xml; } protected  MicrosoftTranslatorClient(String clientSubscriptionKey,
            MicrosoftRestEasyClient restClient, DTOUtil dtoUtil);   }