@isTest
private class ReceivingNoteAcceptanceControllerTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @description 安装master数据
    */
    @testSetup static void setupMasterData(){
        factory = new ReceivingNoteTestDataFactory();
        factory.setupMasterData();
    }
    /**
    @param typeStringListString 格式：'备品类型,管理种类;备品类型,管理种类...'
    @description 造出指定类型的清单数据,并插入数据库
    */
    static void setupTestData(String typeStringListString){
        factory = new ReceivingNoteTestDataFactory();
        if(String.isBlank(typeStringListString)){
            typeStringListString = '固定资产,个体管理;附属品,个体管理;附属品,数量管理;耗材,数量管理;低值易耗品,数量管理';
        }
        List<String> typeStringList = typeStringListString.split(';');
        factory.setupReceivingNoteData(typeStringList);
    }
    /**
    @description 初始化
    */
    static testMethod void unitTestInit() {
        setupTestData('固定资产,个体管理');
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');

        PageReference ref = new PageReference('/apex/ReceivingNoteAcceptance?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();

        Test.startTest();
        controller.init();
        System.assertEquals(1, controller.esdList.size());
        Test.stopTest();
    }
    static testMethod void unitTestInitError() {
        String idTemp = '123456789012345';

        PageReference ref = new PageReference('/apex/ReceivingNoteAcceptance?id=' + idTemp);
        Test.setCurrentPage(ref);

        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();

        Test.startTest();
        controller.init();
        Test.stopTest();
    }
    /**
    @description 测试保存功能
    */
    static testMethod void unitTestSave(){
        // 状态调整为已收货
        setupTestData('固定资产,个体管理');
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        //rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;
        rnd.SerialNumber__c = null;
        upsert rnd Product_Serial_No__c;

        // 收货
        ReceivingNoteSummary__c  rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Received_Confirm_Time__c = System.Now();
        rns.Package_Status__c = 'OK';
        upsert rns Internal_Asset_number__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteAcceptance?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();

        Test.startTest();
        controller.init();
        System.assertEquals(1, controller.esdList.size());

        // 修改前
        ReceivingNoteDetail__c rndPage = controller.esdList[0].rnd;
        System.assertEquals(null, rndPage.AcceptanceResult__c);
        System.assertEquals(null, rndPage.AcceptanceComments__c);
        //System.assertEquals(null, rndPage.unknow_serial_NO_product__c);
        //System.assertEquals(null, rndPage.SerialNumber__c);
        //System.assertEquals(null, rndPage.PeriodOfUse_Final__c);

        // 勾选【无编号设备】, 不填写【机身编号】
        rndPage.AcceptanceResult__c = 'OK' ;
        rndPage.AcceptanceComments__c = 'COMMENT';
        rndPage.unknow_serial_NO_product__c = 'TRUE';
        rndPage.SerialNumber__c = null;
        rndPage.PeriodOfUse_Final__c = System.today();
        controller.save();

        // 检查修改后结果
        rnd = [
                SELECT AcceptanceResult__c
                     , AcceptanceComments__c
                     , unknow_serial_NO_product__c
                     , SerialNumber__c
                     , PeriodOfUse_Final__c
                  FROM ReceivingNoteDetail__c];
        /*System.assertEquals('OK', rnd.AcceptanceResult__c);
        System.assertEquals('COMMENT', rnd.AcceptanceComments__c);
        System.assertEquals('TRUE', rnd.unknow_serial_NO_product__c);
        System.assertEquals(null, rnd.SerialNumber__c);
        System.assertEquals(System.today(), rnd.PeriodOfUse_Final__c);*/

        // 不勾选【无编号设备】, 填写【机身编号】
        rndPage.AcceptanceResult__c = 'NG' ;
        rndPage.AcceptanceComments__c = 'COMMENT2';
        rndPage.unknow_serial_NO_product__c = 'FALSE';
        rndPage.SerialNumber__c = '123';
        controller.save();

        // 检查修改后结果
        rnd = [
                SELECT AcceptanceResult__c
                     , AcceptanceComments__c
                     , unknow_serial_NO_product__c
                     , SerialNumber__c
                  FROM ReceivingNoteDetail__c
                ];
        /*System.assertEquals('NG', rnd.AcceptanceResult__c);
        System.assertEquals('COMMENT2', rnd.AcceptanceComments__c);
        System.assertEquals('FALSE', rnd.unknow_serial_NO_product__c);
        System.assertEquals('123', rnd.SerialNumber__c);*/
        Test.stopTest();
    }
    /**
    @description 测试附属品调整, 在ReceivingNoteDetailSelectControllerTest中测试
    */
    static testMethod void unitTestAddAccessory(){

        setupTestData('固定资产,个体管理');
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');

        PageReference ref = new PageReference('/apex/ReceivingNoteAcceptance?id=' + rns.Id);
        Test.setCurrentPage(ref);
        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();
        Test.startTest();
        controller.init();
        Test.stopTest();
    }
    /**
    @description 测试detailEquals()方法
    */
    static testMethod void unitTestDetailEquals(){
        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        ReceivingNoteDetail__c rnd2 = new ReceivingNoteDetail__c();

        Test.startTest();
        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();

        rnd1.AcceptanceResult__c = 'OK';
        rnd1.AcceptanceComments__c = 'COMMENT';
        rnd1.Fixture_QRCode__c = 'QRCODE';
        rnd1.Barcode__c = 'BARCODE';
        rnd1.unknow_serial_NO_product__c='TRUE';
        rnd1.SerialNumber__c = '123';

        rnd2.AcceptanceResult__c = 'OK';
        rnd2.AcceptanceComments__c = 'COMMENT';
        rnd2.Fixture_QRCode__c = 'QRCODE';
        rnd2.Barcode__c = 'BARCODE';
        rnd2.unknow_serial_NO_product__c='TRUE';
        rnd2.SerialNumber__c = '123';

        rnd2.AcceptanceResult__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.AcceptanceResult__c = 'OK';
        rnd2.AcceptanceComments__c = 'COMMENT2';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.AcceptanceComments__c = 'COMMENT';
        rnd2.unknow_serial_NO_product__c='FALSE';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.unknow_serial_NO_product__c='TRUE';
        rnd2.SerialNumber__c = '321';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.SerialNumber__c = '123';
        System.assertEquals(true, controller.detailEquals(rnd1,rnd2));

        Test.stopTest();

    }
    /**
    @description 测试detailEquals()方法
    */
    static testMethod void unitTestStringEquals(){

        Test.startTest();
        ReceivingNoteAcceptanceController controller = new ReceivingNoteAcceptanceController();

        String s1 = 'abc';
        String s2 = 'cba';
        System.assertEquals(false,controller.stringEquals(s1,s2));
        s2 = '';
        System.assertEquals(false,controller.stringEquals(s1,s2));
        s2 = null;
        System.assertEquals(false,controller.stringEquals(s1,s2));

        s1 = '';
        s2 = 'cba';
        System.assertEquals(false,controller.stringEquals(s1,s2));
        s2 = '';
        System.assertEquals(true,controller.stringEquals(s1,s2));
        s2 = null;
        System.assertEquals(true,controller.stringEquals(s1,s2));

        s1 = null;
        s2 = 'cba';
        System.assertEquals(false,controller.stringEquals(s1,s2));
        s2 = '';
        System.assertEquals(true,controller.stringEquals(s1,s2));
        s2 = null;
        System.assertEquals(true,controller.stringEquals(s1,s2));

        Test.stopTest();
    }

}