OpenRosaXmlFetcher { @SuppressWarnings("PMD.AvoidRethrowingException") public DocumentFetchResult getXML(String urlString) throws UnknownHostException, SSLException { Document doc; HttpGetResult inputStreamResult; try { inputStreamResult = fetch(urlString, HTTP_CONTENT_TYPE_TEXT_XML); if (inputStreamResult.getStatusCode() != HttpURLConnection.HTTP_OK) { String error = "getXML failed while accessing " + urlString + " with status code: " + inputStreamResult.getStatusCode(); Timber.e(error); return new DocumentFetchResult(error, inputStreamResult.getStatusCode()); } try (InputStream resultInputStream = inputStreamResult.getInputStream(); InputStreamReader streamReader = new InputStreamReader(resultInputStream, "UTF-8")) { doc = new Document(); KXmlParser parser = new KXmlParser(); parser.setInput(streamReader); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); doc.parse(parser); } } catch (UnknownHostException | SSLException e) { throw e; } catch (Exception e) { String error = "Parsing failed with " + e.getMessage() + " while accessing " + urlString; Timber.e(error); return new DocumentFetchResult(error, 0); } return new DocumentFetchResult(doc, inputStreamResult.isOpenRosaResponse(), inputStreamResult.getHash()); } OpenRosaXmlFetcher(OpenRosaHttpInterface httpInterface, WebCredentialsUtils webCredentialsUtils); @SuppressWarnings("PMD.AvoidRethrowingException") DocumentFetchResult getXML(String urlString); @Nullable InputStream getFile(@NonNull String downloadUrl, @Nullable final String contentType); WebCredentialsUtils getWebCredentialsUtils(); void updateWebCredentialsUtils(WebCredentialsUtils webCredentialsUtils);  }
@Test public void getXML_returnsResultWith0Status() throws Exception { HashMap<String, String> headers = new HashMap<>(); headers.put(OpenRosaConstants.VERSION_HEADER, "1.0"); when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult( new ByteArrayInputStream("".getBytes()), headers, "hash", 200 )); DocumentFetchResult result = openRosaXMLFetcher.getXML("http: assertThat(result.responseCode, equalTo(0)); assertThat(result.isOpenRosaResponse, equalTo(true)); assertThat(result.errorMessage, nullValue()); }
@Test public void getXML_whenUnsuccessful_returnsResultWithStatusAndErrorMessage() throws Exception { when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(null, new HashMap<>(), "", 500)); DocumentFetchResult result = openRosaXMLFetcher.getXML("http: assertThat(result.responseCode, equalTo(500)); assertThat(result.errorMessage, equalTo("getXML failed while accessing http: }
@Test public void getXML_whenHttpInterfaceThrowsAnException_returnsResultWith0StatusAndErrorMessage() throws Exception { when(httpInterface.executeGetRequest(any(), any(), any())).thenThrow(new Exception()); DocumentFetchResult result = openRosaXMLFetcher.getXML("http: assertThat(result.responseCode, equalTo(0)); assertThat(result.errorMessage, equalTo("Parsing failed with null while accessing http: }