@isTest
private class ReceivingNoteExaminationControllerTest {
    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('固定资产,个体管理');
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

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

        ReceivingNoteExaminationController controller = new ReceivingNoteExaminationController();

        Test.startTest();
        controller.init();
        System.assertEquals(1, controller.esdList.size());
        Test.stopTest();
    }
    /**
    @description 测试保存功能
    */
    static testMethod void unitTestSave() {
        setupTestData('固定资产,个体管理');
        // 明细数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.DataConfirmation__c = true;
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'FALSE';
        upsert rnd Product_Serial_No__c;
        // 一览收货确认
        ReceivingNoteSummary__c  rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Received_Confirm_Time__c = System.Now();
        upsert rns Internal_Asset_number__c;
        // 明细验收
        rnd.AcceptanceTime__c = System.Now();
        rnd.AcceptanceResult__c = 'OK';
        upsert rnd Product_Serial_No__c;

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

        ReceivingNoteExaminationController controller = new ReceivingNoteExaminationController();

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

        rnd = [
                SELECT Inspection_result_after__c
                     , After_Inspection_time__c
                     , Inspection_staff_After__c
                     , RND_Status__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id =: controller.esdList[0].rnd.Id
            ];
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(null, rnd.Inspection_result_after__c);
        System.assertEquals(null, rnd.After_Inspection_time__c);
        System.assertEquals(null, rnd.Inspection_staff_After__c);
        System.assertEquals(FixtureUtil.RNDStatusMap.get(FixtureUtil.RNDStatus.Yi_Qing_Dian.ordinal()),rnd.RND_Status__c);

        // 页面上修改数据
        ReceivingNoteDetail__c rndPage = controller.esdList[0].rnd;
        rndPage.Inspection_result_after__c = 'OK';
        rndPage.Inspection_Comment__c = 'comment';
        controller.esdList[0].isChecked = true;
        controller.save();

        rnd = [
                SELECT Inspection_result_after__c
                     , After_Inspection_time__c
                     , Inspection_Comment__c
                     , Inspection_staff_After__c
                     , RND_Status__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id =: controller.esdList[0].rnd.Id
            ];
        System.assertEquals('OK', rnd.Inspection_result_after__c);
        System.assertNotEquals(null, rnd.After_Inspection_time__c);
        System.assertEquals('comment', rnd.Inspection_Comment__c);
        System.assertEquals(Userinfo.getUserId(), rnd.Inspection_staff_After__c);
        System.assertEquals(FixtureUtil.RNDStatusMap.get(FixtureUtil.RNDStatus.Yi_Yan_Shou.ordinal()), rnd.RND_Status__c);

        Test.stopTest();
    }
    /**
    @description 测试detailEquals()方法
    */
    static testMethod void unitTestDetailEquals(){
        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        ReceivingNoteDetail__c rnd2 = new ReceivingNoteDetail__c();

        Test.startTest();
        ReceivingNoteExaminationController controller = new ReceivingNoteExaminationController();
        rnd1.Inspection_result_after__c = 'OK';
        rnd1.Inspection_Comment__c = 'COMMENT';
        rnd1.Product_Name_Check__c = 'OK';
        rnd1.Model_Check__c = 'OK';
        rnd1.NMPA_Approbation_No_Check__c = 'OK';
        rnd1.Production_Date_Check__c = 'OK';
        rnd1.Guarantee_End_Check__c = 'OK';
        rnd1.Produce_Company_Check__c = 'OK';
        rnd1.Vender_Check__c = 'OK';


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

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

        rnd2.Inspection_Comment__c = 'COMMENT';
        rnd2.Product_Name_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Product_Name_Check__c = 'OK';
        rnd2.Model_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Model_Check__c = 'OK';
        rnd2.NMPA_Approbation_No_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.NMPA_Approbation_No_Check__c = 'OK';
        rnd2.Production_Date_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Production_Date_Check__c = 'OK';
        rnd2.Guarantee_End_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Guarantee_End_Check__c = 'OK';
        rnd2.Produce_Company_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Produce_Company_Check__c = 'OK';
        rnd2.Vender_Check__c = 'NG';
        System.assertEquals(false, controller.detailEquals(rnd1,rnd2));

        rnd2.Vender_Check__c = 'OK';
        System.assertEquals(true, controller.detailEquals(rnd1,rnd2));

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

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

        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();
    }

}