OpenRosaFormListApi implements FormListApi { @Override public List<FormListItem> fetchFormList() throws FormApiException { if (serverURL == null) { throw new UnsupportedOperationException("Using deprecated constructor!"); } DocumentFetchResult result = mapException(() -> openRosaXMLFetcher.getXML(getFormListURL())); if (result.errorMessage != null) { if (result.responseCode == HTTP_UNAUTHORIZED) { throw new FormApiException(AUTH_REQUIRED); } else if (result.responseCode == HTTP_NOT_FOUND) { throw new FormApiException(UNREACHABLE, serverURL); } else { throw new FormApiException(FETCH_ERROR); } } if (result.isOpenRosaResponse) { return parseFormList(result); } else { return parseLegacyFormList(result); } } OpenRosaFormListApi(String serverURL, String formListPath, OpenRosaHttpInterface openRosaHttpInterface, WebCredentialsUtils webCredentialsUtils); @Deprecated  OpenRosaFormListApi(OpenRosaXmlFetcher openRosaXmlFetcher); @Override List<FormListItem> fetchFormList(); @Override ManifestFile fetchManifest(String manifestURL); @Override InputStream fetchForm(String formURL); @Override InputStream fetchMediaFile(String mediaFileURL); @Override void updateUrl(String url); @Override void updateWebCredentialsUtils(WebCredentialsUtils webCredentialsUtils);  }
@Test public void fetchFormList_removesTrailingSlashesFromUrl() throws Exception { OpenRosaHttpInterface httpInterface = mock(OpenRosaHttpInterface.class); WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class); OpenRosaFormListApi formListApi = new OpenRosaFormListApi("http: when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(new ByteArrayInputStream(RESPONSE.getBytes()), Collections.emptyMap(), "", 200)); formListApi.fetchFormList(); verify(httpInterface).executeGetRequest(eq(new URI("http: }
@Test public void fetchFormList_whenThereIsAnUnknownHostException_throwsUnreachableFormApiException() throws Exception { OpenRosaHttpInterface httpInterface = mock(OpenRosaHttpInterface.class); WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class); OpenRosaFormListApi formListApi = new OpenRosaFormListApi("http: try { when(httpInterface.executeGetRequest(any(), any(), any())).thenThrow(UnknownHostException.class); formListApi.fetchFormList(); fail("No exception thrown!"); } catch (FormApiException e) { assertThat(e.getType(), is(UNREACHABLE)); assertThat(e.getServerUrl(), is("http: } }
@Test public void fetchFormList_whenThereIsAnSSLException_throwsSecurityErrorFormApiException() throws Exception { OpenRosaHttpInterface httpInterface = mock(OpenRosaHttpInterface.class); WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class); OpenRosaFormListApi formListApi = new OpenRosaFormListApi("http: try { when(httpInterface.executeGetRequest(any(), any(), any())).thenThrow(SSLException.class); formListApi.fetchFormList(); fail("No exception thrown!"); } catch (FormApiException e) { assertThat(e.getType(), is(SECURITY_ERROR)); assertThat(e.getServerUrl(), is("http: } }
@Test public void fetchFormList_whenThereIsA404_throwsUnreachableApiException() throws Exception { OpenRosaHttpInterface httpInterface = mock(OpenRosaHttpInterface.class); WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class); OpenRosaFormListApi formListApi = new OpenRosaFormListApi("http: try { when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(null, new HashMap<>(), "hash", 404)); formListApi.fetchFormList(); fail("No exception thrown!"); } catch (FormApiException e) { assertThat(e.getType(), is(UNREACHABLE)); assertThat(e.getServerUrl(), is("http: } }