From 1985e26d07a775507dd4094eba730f925ccd4bbc Mon Sep 17 00:00:00 2001 From: ligi Date: Thu, 12 Nov 2015 16:50:40 +0100 Subject: [PATCH] Add tests for the URLRewriteController --- android/build.gradle | 2 +- .../ui/quirk_fix/URLRewriteActivity.java | 2 +- .../ui/quirk_fix/URLRewriteController.java | 26 +++++--- .../passandroid/unittest/ThePassField.java | 1 + .../unittest/TheURLRewriteController.java | 61 +++++++++++++++++++ 5 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 android/src/test/java/org/ligi/passandroid/unittest/TheURLRewriteController.java diff --git a/android/build.gradle b/android/build.gradle index ece9b71a..bd350698 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -15,7 +15,6 @@ buildscript { } classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' classpath 'de.mobilej.unmock:UnMockPlugin:0.3.5' - } } @@ -144,6 +143,7 @@ dependencies { testCompile 'com.squareup.assertj:assertj-android:1.1.1' testCompile 'com.android.support:support-annotations:23.1.0' testCompile 'junit:junit:4.12' + testCompile 'org.mockito:mockito-core:1.9.5' // cannot use this new version: https://github.com/zxing/zxing/issues/165 // WARNING: might work on some devices or the emulator - but fails on others diff --git a/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java b/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java index 8a78a6ee..76fd67bd 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java +++ b/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteActivity.java @@ -16,7 +16,7 @@ public class URLRewriteActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final String url = URLRewriteController.getUrlByHost(getIntent().getData()); + final String url = new URLRewriteController(Tracker.get()).getUrlByUri(getIntent().getData()); if (url == null) { new AlertDialog.Builder(this).setTitle("Workaround failed") diff --git a/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteController.java b/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteController.java index cbdd8c21..4e4bd87c 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteController.java +++ b/android/src/main/java/org/ligi/passandroid/ui/quirk_fix/URLRewriteController.java @@ -2,12 +2,20 @@ package org.ligi.passandroid.ui.quirk_fix; import android.net.Uri; -import org.ligi.passandroid.Tracker; +import org.ligi.passandroid.TrackerInterface; import java.net.URLEncoder; public class URLRewriteController { - public static String getUrlByHost(final Uri uri) { + + private final TrackerInterface tracker; + + public URLRewriteController(TrackerInterface tracker) { + this.tracker = tracker; + } + + public String getUrlByUri(final Uri uri) { + if (uri.getHost().endsWith(".virginaustralia.com")) { // mobile. or checkin. return getVirginAustraliaURL(uri); } @@ -23,14 +31,14 @@ public class URLRewriteController { return null; } - private static String getVirginAustraliaURL(final Uri uri) { + private String getVirginAustraliaURL(final Uri uri) { final String passId; if (uri.toString().contains("CheckInApiIntegration")) { passId = uri.getQueryParameter("key"); - Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "virgin_australia2", null); + tracker.trackEvent("quirk_fix", "redirect_attempt", "virgin_australia2", null); } else { - Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "virgin_australia1", null); + tracker.trackEvent("quirk_fix", "redirect_attempt", "virgin_australia1", null); passId = uri.getQueryParameter("c"); } @@ -38,21 +46,21 @@ public class URLRewriteController { return null; } - Tracker.get().trackEvent("quirk_fix", "redirect", "virgin_australia", null); + tracker.trackEvent("quirk_fix", "redirect", "virgin_australia", null); return "https://mobile.virginaustralia.com/boarding/pass.pkpass?key=" + URLEncoder.encode(passId); } - private static String getCathay(Uri uri) { + private String getCathay(Uri uri) { final String passId = uri.getQueryParameter("v"); - Tracker.get().trackEvent("quirk_fix", "redirect_attempt", "cathay", null); + tracker.trackEvent("quirk_fix", "redirect_attempt", "cathay", null); if (passId == null) { return null; } - Tracker.get().trackEvent("quirk_fix", "redirect", "cathay", null); + tracker.trackEvent("quirk_fix", "redirect", "cathay", null); return "https://www.cathaypacific.com/icheckin2/PassbookServlet?v=" + URLEncoder.encode(passId); } diff --git a/android/src/test/java/org/ligi/passandroid/unittest/ThePassField.java b/android/src/test/java/org/ligi/passandroid/unittest/ThePassField.java index 521766fd..bbc1d6c8 100644 --- a/android/src/test/java/org/ligi/passandroid/unittest/ThePassField.java +++ b/android/src/test/java/org/ligi/passandroid/unittest/ThePassField.java @@ -5,6 +5,7 @@ import org.json.JSONObject; import org.junit.Test; import org.ligi.passandroid.model.AppleStylePassTranslation; import org.ligi.passandroid.model.PassField; + import static org.assertj.core.api.Assertions.assertThat; public class ThePassField { diff --git a/android/src/test/java/org/ligi/passandroid/unittest/TheURLRewriteController.java b/android/src/test/java/org/ligi/passandroid/unittest/TheURLRewriteController.java new file mode 100644 index 00000000..14d6fbec --- /dev/null +++ b/android/src/test/java/org/ligi/passandroid/unittest/TheURLRewriteController.java @@ -0,0 +1,61 @@ +package org.ligi.passandroid.unittest; + +import android.net.Uri; + +import org.junit.Test; +import org.ligi.passandroid.TrackerInterface; +import org.ligi.passandroid.ui.quirk_fix.URLRewriteController; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class TheURLRewriteController { + + private URLRewriteController tested = new URLRewriteController(mock(TrackerInterface.class)); + + @Test + public void tesRejection() { + final String res = tested.getUrlByUri(Uri.parse("http://foo.bar")); + + assertThat(res).isNull(); + } + + @Test + public void testThatBrusselWorks() { + final String res = tested.getUrlByUri(Uri.parse("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/")); + + assertThat(res).isEqualTo("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/passbook"); + } + + + @Test + public void testThatSwissWorks() { + final String res = tested.getUrlByUri(Uri.parse("http://mbp.swiss.com/mobiqa/wap/14foo/83bar/")); + + assertThat(res).isEqualTo("http://prod.wap.ncrwebhost.mobi/mobiqa/wap/14foo/83bar/passbook"); + } + + @Test + public void testThatCathayWorks() { + final String res = tested.getUrlByUri(Uri.parse("https://www.cathaypacific.com/foo?v=bar")); + + assertThat(res).isEqualTo("https://www.cathaypacific.com/icheckin2/PassbookServlet?v=bar"); + } + + + @Test + public void testVirgin1() { + final String res = tested.getUrlByUri(Uri.parse("https://bazz.virginaustralia.com/boarding/CheckInApiIntegration?key=foo")); + + assertThat(res).isEqualTo("https://mobile.virginaustralia.com/boarding/pass.pkpass?key=foo"); + } + + + @Test + public void testVirgin() { + final String res = tested.getUrlByUri(Uri.parse("https://bazz.virginaustralia.com/boarding/pass.pkpass?c=foo")); + + assertThat(res).isEqualTo("https://mobile.virginaustralia.com/boarding/pass.pkpass?key=foo"); + } + +}