package com.braintreepayments.api.models;

import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static com.braintreepayments.testutils.FixturesHelper.stringFromFixture;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class AndroidPayConfigurationTest {

    @Test(timeout = 1000)
    @SmallTest
    public void parsesAndroidPayConfigurationFromToken() throws JSONException {
        Configuration configuration = Configuration.fromJson(
                stringFromFixture("configuration_with_android_pay.json"));

        AndroidPayConfiguration androidPayConfiguration = configuration.getAndroidPay();

        assertTrue(androidPayConfiguration.isEnabled(getTargetContext()));
        assertEquals("google-auth-fingerprint", androidPayConfiguration.getGoogleAuthorizationFingerprint());
        assertEquals("Android Pay Merchant", androidPayConfiguration.getDisplayName());
        assertEquals("sandbox", androidPayConfiguration.getEnvironment());
        assertEquals("visa", androidPayConfiguration.getSupportedNetworks()[0]);
        assertEquals("mastercard", androidPayConfiguration.getSupportedNetworks()[1]);
        assertEquals("amex", androidPayConfiguration.getSupportedNetworks()[2]);
        assertEquals("discover", androidPayConfiguration.getSupportedNetworks()[3]);
    }

    @Test(timeout = 1000)
    @SmallTest
    public void fromJson_parsesConfiguration() throws JSONException {
        JSONObject json = new JSONObject(stringFromFixture("configuration_with_android_pay.json"))
                .getJSONObject("androidPay");

        AndroidPayConfiguration androidPayConfiguration = AndroidPayConfiguration.fromJson(json);

        assertTrue(androidPayConfiguration.isEnabled(getTargetContext()));
        assertEquals("google-auth-fingerprint", androidPayConfiguration.getGoogleAuthorizationFingerprint());
        assertEquals("Android Pay Merchant", androidPayConfiguration.getDisplayName());
        assertEquals("sandbox", androidPayConfiguration.getEnvironment());
        assertEquals("visa", androidPayConfiguration.getSupportedNetworks()[0]);
        assertEquals("mastercard", androidPayConfiguration.getSupportedNetworks()[1]);
        assertEquals("amex", androidPayConfiguration.getSupportedNetworks()[2]);
        assertEquals("discover", androidPayConfiguration.getSupportedNetworks()[3]);
    }

    @Test(timeout = 1000)
    @SmallTest
    public void fromJson_returnsNewAndroidPayConfigurationWithDefaultValuesWhenJSONObjectIsNull() {
        AndroidPayConfiguration androidPayConfiguration = AndroidPayConfiguration.fromJson(null);

        assertFalse(androidPayConfiguration.isEnabled(getTargetContext()));
        assertNull(androidPayConfiguration.getGoogleAuthorizationFingerprint());
        assertNull(androidPayConfiguration.getDisplayName());
        assertNull(androidPayConfiguration.getEnvironment());
        assertEquals(0, androidPayConfiguration.getSupportedNetworks().length);
    }

    @Test(timeout = 1000)
    @SmallTest
    public void fromJson_returnsNewAndroidPayConfigurationWithDefaultValuesWhenNoDataIsPresent() {
        AndroidPayConfiguration androidPayConfiguration = AndroidPayConfiguration.fromJson(new JSONObject());

        assertFalse(androidPayConfiguration.isEnabled(getTargetContext()));
        assertNull(androidPayConfiguration.getGoogleAuthorizationFingerprint());
        assertNull(androidPayConfiguration.getDisplayName());
        assertNull(androidPayConfiguration.getEnvironment());
        assertEquals(0, androidPayConfiguration.getSupportedNetworks().length);
    }
}

@@ -142,16 +142,132 @@
 lTest;%0A%0A
+import com.google.android.gms.common.ConnectionResult;%0Aimport com.google.android.gms.common.GoogleApiAvailability;%0A%0A
 import o
@@ -289,16 +289,16 @@
 eption;%0A
-
 import o
@@ -729,16 +729,59 @@
 ertTrue;
+%0Aimport static junit.framework.Assert.fail;
 %0A%0A@RunWi
@@ -1103,24 +1103,70 @@
 y.json%22));%0A%0A
+        failIfNotGooglePlayServicesDevice();%0A%0A
         Andr
@@ -2185,24 +2185,70 @@
 roidPay%22);%0A%0A
+        failIfNotGooglePlayServicesDevice();%0A%0A
         Andr
@@ -3259,24 +3259,70 @@
 son(null);%0A%0A
+        failIfNotGooglePlayServicesDevice();%0A%0A
         asse
@@ -3924,24 +3924,70 @@
 Object());%0A%0A
+        failIfNotGooglePlayServicesDevice();%0A%0A
         asse
@@ -4247,32 +4247,32 @@
 Environment());%0A
-
         assertEq
@@ -4340,11 +4340,293 @@
 );%0A    %7D
+%0A%0A    public static void failIfNotGooglePlayServicesDevice()%7B%0A        if(GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getTargetContext()) !=%0A                ConnectionResult.SUCCESS)%7B%0A            fail(%22Not using a Google Play Services device.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Check for Google Play Services