@isTest
private class ConsumApplyTriggerHandlerTest {

    //deloitte-zhj 20231023 增加覆盖率 start
    // @TestSetup
    // static void setup(){
    //     TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Consum_Apply__c'});
    // } 

    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 testMethod void Test1(){
        Test.startTest();
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Consum_Apply__c'});
        Test.setMock(HttpCalloutMock.class, new HttpMock());
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User u = new User( Alias = 'test213123',
                Email = 'testuser'+'zhj'+'@olympus.com',
                Emailencodingkey = 'UTF-8', 
                Lastname = 'TestUser'+'zhj123', 
                Languagelocalekey = 'zh_CN',
                Localesidkey = 'zh_CN', 
                Profileid = p.id, 
                Country = 'China',
                IsActive = true,
                Timezonesidkey = 'America/Sao_Paulo', 
                Username = 'testuser'+'zhj123'+'@olympus.com',
                Job_Category__c = '销售服务'
        );
        Consum_Apply__c oldCa = TestDataUtility.CreateConsumApply(1)[0];
        Consum_Apply__c newCa = new Consum_Apply__c();
        newCa.Name = 'Test_20231023';
        newCa.Status__c = '草案中';
        newCa.Person_In_Charge__c = u.Id;//备品出借担当
        newCa.Old_Consum_Apply__c = oldCa.Id;
        insert newCa;
        // List<Consum_Apply__c> fendanList = new List<Consum_Apply__c>();
        // fendanList.add(newCa);
        // ConsumApplyTriggerHandler.decryptInsertFuture(JSON.serialize(fendanList));
        Test.stopTest();
    }
    //deloitte-zhj 20231023 增加覆盖率 end

    static testMethod void testMethod1() {
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.setupTestData();
        factory.selectDetails();
    }

    // static ConsumTestDataFactory factory;
    // // 造 两个一览，各一条明细，草案中
    // @testSetup static void setupTestData(){
    //     ConsumTestDataFactory factory = new ConsumTestDataFactory();
    //     factory.setupTestData();
    //     factory.selectDetails(2);
    //     TestDataUtility.CreatePIPolicyConfiguration();
    // }
    // 䓍案中分单
    static testMethod void testSaveSplitError() {
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.setupTestData();
        factory.selectDetails(2);
        TestDataUtility.CreatePIPolicyConfiguration();
        Consum_Apply__c ca = [SELECT Id FROM Consum_Apply__c];
        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id FROM Consum_Apply_Equipment_Set__c WHERE Consum_Apply__c=:ca.Id];
        System.assertEquals(2, caesList .size());

        PageReference ref = new PageReference('/apex/ConsumApplySplit');
        ref.getParameters().put('objId', ca.Id);
        ref.getParameters().put('raesIds', caesList[0].Id);
        Test.setCurrentPage(ref);
        ConsumApplySplitController controller = new ConsumApplySplitController();
        controller.init();

        Test.startTest();
        controller.cloneRas.Split_Apply_Reason__c = '分批发货分单';
        controller.saveSplit();
        Test.stopTest();

        // 分单失败，只有一个申请
        List<Consum_Apply__c> caList = [SELECT Id FROM Consum_Apply__c];
        System.assertEquals(1, caList.size());
    }

    // 批准后分单
    static testMethod void testSaveSplit() {
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.setupTestData();
        factory.selectDetails(2);
        TestDataUtility.CreatePIPolicyConfiguration();
        // 变成已批准
        // ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.approve();

        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id FROM Consum_Apply_Equipment_Set__c WHERE Consum_Apply__c=:factory.ca.Id];
        System.assertEquals(2, caesList .size());

        PageReference ref = new PageReference('/apex/ConsumApplySplit');
        ref.getParameters().put('objId', factory.ca.Id);
        ref.getParameters().put('raesIds', caesList[0].Id);
        Test.setCurrentPage(ref);
        ConsumApplySplitController controller = new ConsumApplySplitController();
        controller.init();

        Test.startTest();
        controller.cloneRas.Split_Apply_Reason__c = '分批发货分单';
        controller.saveSplit();
        Test.stopTest();

        // 分单成功，有两个申请
        List<Consum_Apply__c> caList = [SELECT Id FROM Consum_Apply__c];
        System.assertEquals(2, caList.size());
    }

    static testMethod void testCoverUp() {
        ConsumApplyTriggerHandler.test();
    }
}