forked from getodk/collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for positive HTTP action in DownloadFormListTask
Issue: getodk#620
- Loading branch information
Showing
3 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
collect_app/src/androidTest/java/org/odk/collect/android/tasks/DownloadFormListTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.odk.collect.android.tasks; | ||
|
||
import java.net.*; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
|
||
import org.junit.*; | ||
import org.odk.collect.android.logic.FormDetails; | ||
|
||
import okhttp3.mockwebserver.*; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.odk.collect.android.test.MockedServerTestUtils.*; | ||
import static org.odk.collect.android.test.TestUtils.*; | ||
|
||
public class DownloadFormListTaskTest { | ||
private MockWebServer server; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
server = new MockWebServer(); | ||
server.start(); | ||
configAppFor(server); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
public void shouldProcessAndReturnAFormList() throws Exception { | ||
// given | ||
willRespond(server, RawTestCase.RESPONSE); | ||
|
||
// when | ||
Map<String, FormDetails> fetched = new DownloadFormListTask().doInBackground(); | ||
|
||
// then | ||
RecordedRequest r = server.takeRequest(1, TimeUnit.MILLISECONDS); | ||
assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* org.odk.collect.android/.*")); | ||
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent")); | ||
assertEquals("1.0", r.getHeader("X-OpenRosa-Version")); | ||
|
||
// and | ||
assertEquals(2, fetched.size()); | ||
|
||
// and | ||
FormDetails f1 = fetched.get("one"); | ||
assertNull(f1.errorStr); | ||
assertEquals("The First Form", f1.formName); | ||
assertEquals("https://example.com/formXml?formId=one", f1.downloadUrl); | ||
assertEquals(null, f1.manifestUrl); | ||
assertEquals("one", f1.formID); | ||
assertEquals(null, f1.formVersion); | ||
|
||
// and | ||
FormDetails f2 = fetched.get("two"); | ||
assertNull(f2.errorStr); | ||
assertEquals("The Second Form", f2.formName); | ||
assertEquals("https://example.com/formXml?formId=two", f2.downloadUrl); | ||
assertEquals(null, f2.manifestUrl); | ||
assertEquals("two", f2.formID); | ||
assertEquals(null, f2.formVersion); | ||
} | ||
} | ||
|
||
class RawTestCase { | ||
public static final String RESPONSE = join( | ||
"HTTP/1.1 200 OK\r", | ||
"X-OpenRosa-Version: 1.0\r", | ||
"X-OpenRosa-Accept-Content-Length: 10485760\r", | ||
"Content-Type: text/xml; charset=utf-8\r", | ||
"X-Cloud-Trace-Context: cb84da0bfcb4da37910faf33b10ca190;o=1\r", | ||
"Date: Tue, 18 Apr 2017 15:45:03 GMT\r", | ||
"Server: Google Frontend\r", | ||
"Content-Length: 2235\r", | ||
"Alt-Svc: quic=\":443\"; ma=2592000; v=\"37,36,35\"\r", | ||
"Connection: close\r", | ||
"\r", | ||
"<xforms xmlns=\"http://openrosa.org/xforms/xformsList\">", | ||
"<xform><formID>one</formID>", | ||
"<name>The First Form</name>", | ||
"<majorMinorVersion></majorMinorVersion>", | ||
"<version></version>", | ||
"<hash>md5:b71c92bec48730119eab982044a8adff</hash>", | ||
"<downloadUrl>https://example.com/formXml?formId=one</downloadUrl>", | ||
"</xform>", | ||
"<xform><formID>two</formID>", | ||
"<name>The Second Form</name>", | ||
"<majorMinorVersion></majorMinorVersion>", | ||
"<version></version>", | ||
"<hash>md5:4428adffbbec48771c9230119eab9820</hash>", | ||
"<downloadUrl>https://example.com/formXml?formId=two</downloadUrl>", | ||
"</xform>", | ||
"</xforms>"); | ||
|
||
private static String join(String... strings) { | ||
StringBuilder bob = new StringBuilder(); | ||
for(String s : strings) bob.append(s).append('\n'); | ||
return bob.toString(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
collect_app/src/androidTest/java/org/odk/collect/android/test/MockedServerTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.odk.collect.android.test; | ||
|
||
import android.content.SharedPreferences.Editor; | ||
import android.preference.*; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
import java.security.*; | ||
import java.security.cert.*; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
|
||
import javax.net.ssl.*; | ||
|
||
import org.junit.*; | ||
import org.odk.collect.android.application.*; | ||
import org.odk.collect.android.preferences.*; | ||
|
||
import okhttp3.mockwebserver.*; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public final class MockedServerTestUtils { | ||
private MockedServerTestUtils() {} | ||
|
||
public static void configAppFor(MockWebServer server) { | ||
Editor prefs = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext()).edit(); | ||
prefs.putString(PreferenceKeys.KEY_SERVER_URL, server.url("/").toString()); | ||
if(!prefs.commit()) throw new RuntimeException("Failed to set up SharedPreferences for MockWebServer"); | ||
} | ||
|
||
public static void willRespond(MockWebServer server, String... headersAndBody) { | ||
MockResponse response = new MockResponse(); | ||
|
||
if(headersAndBody.length == 1) { | ||
String[] parts = headersAndBody[0].split("\r\n\r\n", 2); | ||
|
||
String[] headerLines = parts[0].split("\r\n"); | ||
|
||
response.setStatus(headerLines[0]); | ||
|
||
for(int i=1; i<headerLines.length; ++i) { | ||
String[] headerParts = headerLines[i].split(": ", 2); | ||
response.addHeader(headerParts[0], headerParts[1]); | ||
} | ||
|
||
response.setBody(parts[1]); | ||
} else { | ||
for(int i=0; i<headersAndBody.length-1; ++i) { | ||
String[] headerParts = headersAndBody[i].split(": ", 2); | ||
response.addHeader(headerParts[0], headerParts[1]); | ||
} | ||
|
||
response.setBody(headersAndBody[headersAndBody.length - 1]); | ||
} | ||
|
||
server.enqueue(response); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
collect_app/src/androidTest/java/org/odk/collect/android/test/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.odk.collect.android.test; | ||
|
||
public final class TestUtils { | ||
private TestUtils() {} | ||
|
||
public static void assertMatches(String expectedPattern, Object actual) { | ||
if(!testMatches(expectedPattern, actual)) { | ||
throw new AssertionError(String.format("Expected <%s> to match <%s>.", actual, expectedPattern)); | ||
} | ||
} | ||
|
||
public static void assertMatches(String message, String expectedPattern, Object actual) { | ||
if(!testMatches(expectedPattern, actual)) { | ||
throw new AssertionError(String.format("%s Expected <%s> to match <%s>.", message, actual, expectedPattern)); | ||
} | ||
} | ||
|
||
private static boolean testMatches(String expectedPattern, Object actual) { | ||
if(expectedPattern == null) { | ||
throw new IllegalArgumentException("No pattern provided."); | ||
} | ||
if(actual == null) { | ||
return false; | ||
} | ||
return actual.toString().matches(expectedPattern); | ||
} | ||
} |