package com.tapad.tapestry;

import android.os.Build;
import android.test.AndroidTestCase;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.tapad.tracking.deviceidentification.TypedIdentifier.TYPE_ANDROID_ID_MD5;

public class TapestryClientTest extends AndroidTestCase {
    public static final String TEST_URL = "http://tapestry.tapad.com/tapestry/1";
    public static final String HTTPS_TEST_URL = "https://tapestry.tapad.com/tapestry/1";
    public static final String TEST_PARTNER_ID = "1";
    private TapestryTracking tracking = null;
    
    @Override
    protected void setUp() throws Exception {
        tracking = new TapestryTracking(getContext());
    }

    public void test_can_connect_over_https() {
        TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, HTTPS_TEST_URL);
        TapestryResponse response = client.sendSynchronously(new TapestryRequest().setData("color", "blue"));
        assertEquals(response.getData("color").get(0), "blue");
    }

    public void test_returns_opt_out_if_opted_out() {
        TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, TEST_URL);
        client.optOut(getContext());
        TapestryResponse response = client.sendSynchronously(new TapestryRequest());
        assertEquals(response.getErrors().get(0).getType(), TapestryError.OPTED_OUT);
        client.optIn(getContext());
    }

    public void test_should_send_requests_and_receive_responses() {
        TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, TEST_URL);
        TapestryResponse response = client.sendSynchronously(new TapestryRequest().setData("color", "blue"));
        assertEquals(response.getData("color").get(0), "blue");
    }

    public void test_should_send_asynchronous_requests() throws Exception {
        TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, TEST_URL);
        final AtomicBoolean called = new AtomicBoolean();
        client.send(new TapestryRequest(), new TapestryCallback() {
            @Override
            public void receive(TapestryResponse response) {
                called.set(true);
            }
        });
        Thread.sleep(1000);
        assertTrue(called.get());
    }

    public void test_should_return_errors_in_response() {
        TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, "bad url");
        TapestryResponse response = client.sendSynchronously(new TapestryRequest());
        assertEquals(response.getErrors().get(0).getType(), TapestryError.CLIENT_REQUEST_ERROR);
    }
    
    public void testHttpStackImplementation() {
    	TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, TEST_URL);
    	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
    		assertEquals(HttpUrlConnectionStack.class, client.getStack().getClass());
    	}else {
    		assertEquals(HttpClientStack.class, client.getStack().getClass());
    	}
    }
}
@@ -26,33 +26,8 @@
 y;%0A%0A
-import android.os.Build;%0A
 impo
@@ -114,100 +114,8 @@
 n;%0A%0A
-import static com.tapad.tracking.deviceidentification.TypedIdentifier.TYPE_ANDROID_ID_MD5;%0A%0A
 publ
@@ -396,180 +396,8 @@
 %221%22;
-%0A    private TapestryTracking tracking = null;%0A    %0A    @Override%0A    protected void setUp() throws Exception %7B%0A        tracking = new TapestryTracking(getContext());%0A    %7D
 %0A%0A  
@@ -489,32 +489,36 @@
 estryClient(
-tracking
+getContext()
 , TEST_PARTN
@@ -823,32 +823,36 @@
 estryClient(
-tracking
+getContext()
 , TEST_PARTN
@@ -1235,32 +1235,36 @@
 estryClient(
-tracking
+getContext()
 , TEST_PARTN
@@ -1585,32 +1585,36 @@
 estryClient(
-tracking
+getContext()
 , TEST_PARTN
@@ -2074,32 +2074,36 @@
 estryClient(
-tracking
+getContext()
 , TEST_PARTN
@@ -2313,392 +2313,5 @@
   %7D%0A
-    %0A    public void testHttpStackImplementation() %7B%0A    %09TapestryClient client = new TapestryClient(tracking, TEST_PARTNER_ID, TEST_URL);%0A    %09if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.GINGERBREAD) %7B%0A    %09%09assertEquals(HttpUrlConnectionStack.class, client.getStack().getClass());%0A    %09%7Delse %7B%0A    %09%09assertEquals(HttpClientStack.class, client.getStack().getClass());%0A    %09%7D%0A    %7D%0A
 %7D

fix tests