@isTest
public class SyncAccountContactToAWSTest {

    @Testsetup
    static void setup(){
        List<String> strList = new List<String>();
        strList.add('Agency_Contact__c');
        strList.add('Agency_Contact__cV2');
        strList.add('Contact');
        TestDataUtility.CreatePIPolicyConfigurations(strList);
    } 

	class HttpMock implements HttpCalloutMock{
        
        public HTTPResponse respond(HTTPRequest request) {
        // 创建一个假的回应
        System.debug('------------------------------------------------------');
        HttpResponse response = new HttpResponse();
        string body = '';
        system.debug(request.getEndpoint());
        if(request.getEndpoint().contains('token')){
            system.debug('url=token');
            response.setHeader('Content-Type', 'application/json');
            body='{ "message": "", "object": "freqfewqfewewfewfew", "status": "", "success": true, "timestamp": 0, "txId": "" }';          
                } else if(request.getEndpoint().contains('insert')){
                    system.debug('url=Insert');
                    response.setHeader('Content-Type', 'application/json');
            body='{ "message": "", "object": { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" }, "status": "", "success": true, "timestamp": 0, "txId": "" }';          
                } else if(request.getEndpoint().contains('update')){
                    system.debug('url=update');
                    response.setHeader('Content-Type', 'application/json');
            body='{ "message": "", "object": { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" }, "status": "", "success": true, "timestamp": 0, "txId": "" }';          
                } else{}
        
        response.setBody(body);
        response.setStatus('OK');
        response.setStatusCode(200);
        return response;
        // }
    }
    }
    @isTest
    static void startTest() {
        Test.setMock(HttpCalloutMock.class, new HttpMock());
        List<Contact> conList = TestDataUtility.CreateContacts(1);
        List<Agency_Contact__c> lra = new List<Agency_Contact__c>();
        lra.add(new Agency_Contact__c(AWS_Data_Id__c='Test AWS',Contact__c=conList[0].Id));
        insert lra;
        
        System.Test.startTest();
        try{
            //String soql = 'SELECT id,Doctor_Division1__c,Doctor_Division1_Encrypted__c,Name,Name_Encrypted__c,Type__c,Type_Encrypted__c,AWS_Data_Id__c,Contact__c, Contact__r.Doctor_Division1_Encrypted__c,Contact__r.LastName_Encrypted__c, Contact__r.Type_Encrypted__c FROM Agency_Contact__c WHERE id=\'a2R10000001cfatEAA\' And AWS_Data_Id__c =\'\' And Contact__c != null';
            String soql = 'SELECT id,Doctor_Division1__c,Doctor_Division1_Encrypted__c,Name,Name_Encrypted__c,Type__c,Type_Encrypted__c,AWS_Data_Id__c,Contact__c, Contact__r.Doctor_Division1_Encrypted__c,Contact__r.LastName_Encrypted__c, Contact__r.Type_Encrypted__c FROM Agency_Contact__c WHERE AWS_Data_Id__c =\'Test AWS\'';
            List<Agency_Contact__c> acList = Database.query(soql) ;
            Map<Id,Contact> ContactMap = new Map<Id,Contact>();
            ContactMap.put(conList[0].Id,conList[0]);
            SyncAccountContactToAWS contactBatch = new SyncAccountContactToAWS(soql,ContactMap);
            SyncAccountContactToAWS contactBatchTest = new SyncAccountContactToAWS();
            Id execBTId = Database.executeBatch(contactBatch,1);
        }catch(Exception e){
            system.debug('Exception from sync aws batch:'+ e.getMessage());
        }
        try{
            SyncAccountContactToAWS.assignOnceOneMinuteLater(new List<Agency_Contact__c>(),new Map<Id,Contact>());
        }catch(Exception e){
            system.debug('Exception from sync aws scheduler:'+ e.getMessage());
        }       
        System.Test.stopTest();
    }
    
    // static testMethod void testExecute() {
    //     // This test runs a scheduled job at midnight Sept. 3rd. 2022
    //     String CRON_EXP = '0 0 0 3 9 ? 2022';
    //     String soql = 'SELECT id,Doctor_Division1__c,Doctor_Division1_Encrypted__c,Name,Name_Encrypted__c,Type__c,Type_Encrypted__c,AWS_Data_Id__c,Contact__c, Contact__r.Doctor_Division1_Encrypted__c,Contact__r.LastName_Encrypted__c, Contact__r.Type_Encrypted__c FROM Agency_Contact__c WHERE id=\'a2R10000001cfatEAA\' And AWS_Data_Id__c =\'\' And Contact__c != null';

    //     System.Test.startTest();
    //     // Schedule the test job
    //     String jobId = system.schedule('SyncAccountContactToAWSTest', CRON_EXP, new SyncAccountContactToAWS(soql));
    //     // Get the information from the CronTrigger API object
    //     CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
    //     // Verify the expressions are the same
    //     System.assertEquals(CRON_EXP, ct.CronExpression);
    //     // Verify the job has not run
    //     System.assertEquals(0, ct.TimesTriggered);
    //     // Verify the next time the job will run
    //     System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
    //     System.Test.StopTest();
    // }

}