@isTest
private class ReceivingNoteSummarySelectControllerTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @param typeStringListString 格式：'备品类型,管理种类;备品类型,管理种类...'
    @description 造出指定类型的清单数据,并插入数据库
    */
    static void setupTestData(String typeStringListString){
        factory = new ReceivingNoteTestDataFactory();
        factory.setupMasterData();
        if(String.isBlank(typeStringListString)){
            typeStringListString = '固定资产,个体管理;附属品,个体管理;附属品,数量管理;耗材,数量管理;低值易耗品,数量管理';
        }
        List<String> typeStringList = typeStringListString.split(';');
        factory.setupReceivingNoteData(typeStringList);
    }
    /**
    @description 测试searchOpp()方法
    */
    static testMethod void unitTestSearchOpp(){
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Fixture_Set__c = null;
        upsert rns Internal_Asset_number__c;

        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);

        Test.setCurrentPage(ref);
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();

        Test.startTest();
        controller.init();
        // 初始时一览中的配套字段没有被赋值
        rns = [select Fixture_Set__c
                                        from ReceivingNoteSummary__c
                                        WHERE Id=:rnsId];
        System.assertEquals(null, rns.Fixture_Set__c);

        // 查找配套
        controller.keyword = 'SB-FS-01';
        controller.searchOpp(); // 查找方法中包括保存配套字段的操作
        System.assertEquals(1, controller.viewList.size());

        Test.stopTest();
    }

    /**
    @description 给一览对象的配套字段赋值
    */
    static testMethod void unitTestSave(){

        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Fixture_Set__c = null;
        upsert rns Internal_Asset_number__c;

        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);

        Test.setCurrentPage(ref);
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();

        Test.startTest();
        controller.init();

        // 初始时一览中的配套字段没有被赋值
        rns = [SELECT Fixture_Set__c
                FROM ReceivingNoteSummary__c
                WHERE Id=:rnsId];
        System.assertEquals(null, rns.Fixture_Set__c);

        // 不加检索关键词会显示所有可选配套
        controller.keyword = '';
        controller.searchOpp();
        System.assertEquals(1, controller.viewList.size());

        // 勾选配套
        controller.viewList[0].check = true;

        // 保存
        controller.save();

        // 查看是否被赋上值
        ReceivingNoteSummary__c newRns = [select Fixture_Set__r.Fixture_Set_Body_Model_No__c
                                        from ReceivingNoteSummary__c
                                        WHERE Id=:rnsId];
        System.assertEquals('SB-FS-MODEL-01', newRns.Fixture_Set__r.Fixture_Set_Body_Model_No__c);
        Test.stopTest();
    }

    /**
    @description 发送邮件
    */
    static testMethod void unitTestSendEmail() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
        Test.setCurrentPage(ref);

        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        ReceivingNoteSummary__c rns = [select Name, No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals(null, rns.No_Matching_Reminder_Email_Text__c);

        Test.startTest();
        // 初始化
        controller.init();

        String emailMsg = rns.Name;
        String emailBody = 'ARE YOU OK?';
        ref.getParameters().put('emailBody', emailBody);
        controller.sendEmail();

        ReceivingNoteSummary__c newRns = [select No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals('【自由填写内容】\nARE YOU OK?\n备品配套：\nSB-FS-01', newRns.No_Matching_Reminder_Email_Text__c);

        Test.stopTest();
    }
    static testMethod void unitTestCleanFixtureSet(){
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteDetail__c newRnd = new ReceivingNoteDetail__c();
        newRnd.Fixture_Set_Detail__c = factory.fsdList[1].Id;
        newRnd.ReceivingNoteSummary__c = rnsId;
        insert newRnd;

        List <ReceivingNoteDetail__c> rndList = [
                SELECT Id, Fixture_Set_Detail__c
                FROM ReceivingNoteDetail__c
                WHERE ReceivingNoteSummary__c=:rnsId];
        System.assertEquals(2, rndList.size());
        System.assertNotEquals(null, rndList[0].Fixture_Set_Detail__c);

        List<ReceivingNoteSummary__c> rnsList = [SELECT Id, Fixture_Set__c FROM ReceivingNoteSummary__c WHERE Id=:rnsId];
        System.assertNotEquals(null, rnsList[0].Fixture_Set__c);

        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
        Test.setCurrentPage(ref);

        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        ReceivingNoteSummary__c rns = [select Name, No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals(null, rns.No_Matching_Reminder_Email_Text__c);

        Test.startTest();
        // 初始化
        controller.init();
        controller.cleanFixtureSet();
        Test.stopTest();
        rndList = [SELECT Id, Fixture_Set_Detail__c FROM ReceivingNoteDetail__c];
        System.assertEquals(1, rndList.size());
        System.assertEquals(null, rndList[0].Fixture_Set_Detail__c);
        rnsList = [SELECT Id, Fixture_Set__c FROM ReceivingNoteSummary__c];
        System.assertEquals(null, rnsList[0].Fixture_Set__c);
    }
}