Search in sources :

Example 6 with OpenRosaFormSource

use of org.odk.collect.android.openrosa.OpenRosaFormSource in project collect by opendatakit.

the class OpenRosaFormSourceTest method fetchFormList_whenThereIsATimeout_throwsFetchError.

@Test
public void fetchFormList_whenThereIsATimeout_throwsFetchError() throws Exception {
    OpenRosaFormSource formListApi = new OpenRosaFormSource("http://blah.com", httpInterface, webCredentialsUtils, responseParser);
    try {
        when(httpInterface.executeGetRequest(any(), any(), any())).thenThrow(SocketTimeoutException.class);
        formListApi.fetchFormList();
        fail("No exception thrown!");
    } catch (FormSourceException.FetchError e) {
    // pass
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) OpenRosaFormSource(org.odk.collect.android.openrosa.OpenRosaFormSource) Test(org.junit.Test)

Example 7 with OpenRosaFormSource

use of org.odk.collect.android.openrosa.OpenRosaFormSource in project collect by opendatakit.

the class OpenRosaFormSourceTest method fetchFormList_whenResponseHasNoOpenRosaHeader_throwsServerNotOpenRosaError.

@Test
public void fetchFormList_whenResponseHasNoOpenRosaHeader_throwsServerNotOpenRosaError() throws Exception {
    OpenRosaFormSource formListApi = new OpenRosaFormSource("http://blah.com///", httpInterface, webCredentialsUtils, responseParser);
    when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(new ByteArrayInputStream(RESPONSE.getBytes()), // No OpenRosa header
    new HashMap<String, String>(), "", 200));
    try {
        formListApi.fetchFormList();
        fail("Expected exception because server is not OpenRosa server.");
    } catch (FormSourceException.ServerNotOpenRosaError e) {
    // pass
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) OpenRosaFormSource(org.odk.collect.android.openrosa.OpenRosaFormSource) HttpGetResult(org.odk.collect.android.openrosa.HttpGetResult) Test(org.junit.Test)

Example 8 with OpenRosaFormSource

use of org.odk.collect.android.openrosa.OpenRosaFormSource in project collect by opendatakit.

the class OpenRosaFormSourceTest method fetchFormList_whenThereIsA404_throwsUnreachableApiException.

@Test
public void fetchFormList_whenThereIsA404_throwsUnreachableApiException() throws Exception {
    OpenRosaFormSource formListApi = new OpenRosaFormSource("http://blah.com", httpInterface, webCredentialsUtils, responseParser);
    try {
        when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(null, new HashMap<>(), "hash", 404));
        formListApi.fetchFormList();
        fail("No exception thrown!");
    } catch (FormSourceException.Unreachable e) {
        assertThat(e.getServerUrl(), is("http://blah.com"));
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) HashMap(java.util.HashMap) OpenRosaFormSource(org.odk.collect.android.openrosa.OpenRosaFormSource) HttpGetResult(org.odk.collect.android.openrosa.HttpGetResult) Test(org.junit.Test)

Example 9 with OpenRosaFormSource

use of org.odk.collect.android.openrosa.OpenRosaFormSource in project collect by opendatakit.

the class OpenRosaFormSourceTest method fetchManifest_whenOpenRosaResponse_whenParserFails_throwsParseError.

@Test
public void fetchManifest_whenOpenRosaResponse_whenParserFails_throwsParseError() throws Exception {
    OpenRosaFormSource formListApi = new OpenRosaFormSource("http://blah.com", httpInterface, webCredentialsUtils, responseParser);
    try {
        when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(new ByteArrayInputStream("<xml></xml>".getBytes()), new HashMap<String, String>() {

            {
                put(OpenRosaConstants.VERSION_HEADER, "1.0");
            }
        }, "hash", 200));
        when(responseParser.parseManifest(any())).thenReturn(null);
        formListApi.fetchManifest("http://blah.com/manifest");
        fail("No exception thrown!");
    } catch (FormSourceException.ParseError e) {
        assertThat(e.getServerUrl(), is("http://blah.com"));
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) OpenRosaFormSource(org.odk.collect.android.openrosa.OpenRosaFormSource) HttpGetResult(org.odk.collect.android.openrosa.HttpGetResult) Test(org.junit.Test)

Example 10 with OpenRosaFormSource

use of org.odk.collect.android.openrosa.OpenRosaFormSource in project collect by opendatakit.

the class OpenRosaFormSourceTest method fetchManifest_whenThereIsAServerError_throwsServerError.

@Test
public void fetchManifest_whenThereIsAServerError_throwsServerError() throws Exception {
    OpenRosaFormSource formListApi = new OpenRosaFormSource("http://blah.com", httpInterface, webCredentialsUtils, responseParser);
    try {
        when(httpInterface.executeGetRequest(any(), any(), any())).thenReturn(new HttpGetResult(null, new HashMap<>(), "hash", 503));
        formListApi.fetchManifest("http://blah.com/manifest");
        fail("No exception thrown!");
    } catch (FormSourceException.ServerError e) {
        assertThat(e.getStatusCode(), is(503));
        assertThat(e.getServerUrl(), is("http://blah.com"));
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) HashMap(java.util.HashMap) OpenRosaFormSource(org.odk.collect.android.openrosa.OpenRosaFormSource) HttpGetResult(org.odk.collect.android.openrosa.HttpGetResult) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 OpenRosaFormSource (org.odk.collect.android.openrosa.OpenRosaFormSource)14 FormSourceException (org.odk.collect.forms.FormSourceException)13 HashMap (java.util.HashMap)10 HttpGetResult (org.odk.collect.android.openrosa.HttpGetResult)10 ByteArrayInputStream (java.io.ByteArrayInputStream)5 URI (java.net.URI)1