CollectServerClient { public DocumentFetchResult getXmlDocument(String urlString) { Document doc; HttpGetResult inputStreamResult; try { inputStreamResult = getHttpInputStream(urlString, HTTP_CONTENT_TYPE_TEXT_XML); if (inputStreamResult.getStatusCode() != HttpURLConnection.HTTP_OK) { String error = "getXmlDocument 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 (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()); } @Inject  CollectServerClient(OpenRosaHttpInterface httpInterface, WebCredentialsUtils webCredentialsUtils); DocumentFetchResult getXmlDocument(String urlString); @NonNull HttpGetResult getHttpInputStream(@NonNull String downloadUrl, @Nullable final String contentType); static String getPlainTextMimeType(); static final String HTTP_CONTENT_TYPE_TEXT_XML; }
@Test public void testGetXMLDocumentErrorResponse() { DocumentFetchResult fetchResult = collectServerClientError.getXmlDocument(URL_STRING); assertEquals(fetchResult.errorMessage, "Parsing failed with null while accessing " + URL_STRING); }
@Test public void testGetXMLDocument() { DocumentFetchResult fetchResult = collectServerClient.getXmlDocument(URL_STRING); assertNull(fetchResult.errorMessage); assertEquals(fetchResult.responseCode, 0); assertTrue(fetchResult.isOpenRosaResponse); }