package com.ringcentral.rcandroidsdk;

import android.test.InstrumentationTestCase;

import com.ringcentral.rcandroidsdk.rcsdk.SDK;
import com.ringcentral.rcandroidsdk.rcsdk.http.RCResponse;
import com.ringcentral.rcandroidsdk.rcsdk.platform.Platform;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by andrew.pang on 7/29/15.
 */
public class APITest extends InstrumentationTestCase {

    public void testApi() throws Exception{
        SDK sdk = new SDK("xhK3uzISTEaEYhFAtadVug", "1YRoPu64TeCOe_ZJy3ggLwGg-QDQd6QaWpSyIT8AxmjA", "SANDBOX");
        final Platform platform = sdk.getPlatform();
        platform.authorize("15856234166", "", "P@ssw0rd",
                new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(Response response) throws IOException {
                        RCResponse authResponse = new RCResponse(response);
                        HashMap<String, String> responseMap = authResponse.getJson();
                        platform.setAuthData(responseMap);
                        //Test Authorization
                        assertTrue(authResponse.checkStatus());

                        //Test Send SMS
                        platform.sendSMS("16502823614", "15856234166", "Test Message",
                                new Callback() {
                                    @Override
                                    public void onFailure(Request request, IOException e) {
                                    }
                                    @Override
                                    public void onResponse(Response response) throws IOException {
                                        RCResponse smsResponse = new RCResponse(response);
                                        assertTrue(smsResponse.checkStatus());
                                    }
                                }
                        );

                        //Test GET Account Info
                        platform.accountInfo(
                                new Callback() {
                                    @Override
                                    public void onFailure(Request request, IOException e) {
                                    }
                                    @Override
                                    public void onResponse(Response response) throws IOException {
                                        RCResponse callLogResponse = new RCResponse(response);
                                        assertTrue(callLogResponse.checkStatus());
                                    }
                                }
                        );

                        //Test GET Call Log
                        platform.callLog(
                                new Callback() {
                                    @Override
                                    public void onFailure(Request request, IOException e) {
                                    }

                                    @Override
                                    public void onResponse(Response response) throws IOException {
                                        RCResponse callLogResponse = new RCResponse(response);
                                        assertTrue(callLogResponse.checkStatus());
                                    }
                                }
                        );

                        //Test GET Message Store
                        platform.messageStore(
                                new Callback() {
                                    @Override
                                    public void onFailure(Request request, IOException e) {
                                    }

                                    @Override
                                    public void onResponse(Response response) throws IOException {
                                        RCResponse messageStoreResponse = new RCResponse(response);
                                        assertTrue(messageStoreResponse.checkStatus());
                                    }
                                }
                        );

                    }
                }
        );
        try{
            Thread.sleep(1000);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }


}

@@ -352,24 +352,84 @@
 .Response;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 import java.
@@ -440,24 +440,24 @@
 OException;%0A
-
 import java.
@@ -2149,32 +2149,1717 @@
 checkStatus());%0A
+                                        try %7B%0A                                            JSONObject json = new JSONObject(smsResponse.getBody());%0A                                            String messageId = json.getString(%22id%22);%0A                                            //Test Delete Message%0A                                            HashMap%3CString, String%3E deleteHeader = new HashMap%3C%3E();%0A                                            deleteHeader.put(%22method%22, %22DELETE%22);%0A                                            deleteHeader.put(%22url%22, %22/restapi/v1.0/account/~/extension/~/message-store/%22 + messageId);%0A                                            platform.delete(deleteHeader,%0A                                                    new Callback() %7B%0A                                                        @Override%0A                                                        public void onFailure(Request request, IOException e) %7B%0A%0A                                                        %7D%0A                                                        @Override%0A                                                        public void onResponse(Response response) throws IOException %7B%0A                                                            RCResponse deleteResponse = new RCResponse(response);%0A                                                            assertTrue(deleteResponse.checkStatus());%0A                                                        %7D%0A                                                    %7D);%0A                                        %7D catch (JSONException e) %7B%0A                                            e.printStackTrace();%0A                                        %7D%0A
                 
@@ -4215,32 +4215,32 @@
 OException e) %7B%0A
-
                 
@@ -4253,32 +4253,33 @@
               %7D%0A
+%0A
                 

Test delete message after sending it