@isTest
public with sharing class ConsumFixtureSetSelectControllerTest {
    static ConsumTestDataFactory factory;
    @testSetup 
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration('Consum_Apply_Equipment_Set__c');
    }    
    static void setupTestData(){
        factory = new ConsumTestDataFactory();
        factory.setupTestData();
    }
    static testMethod void testSave() {
        setupTestData();
        Consum_Apply__c ca = [SELECT Id FROM Consum_Apply__c LIMIT 1];

        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [SELECT Id
                                                                 FROM Consum_Apply_Equipment_Set_Detail__c
                                                                 WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(0, caesdList.size());

        PageReference ref = new PageReference('/apex/ConsumFixtureSetSelect?pt_recid=' + ca.Id );
        Test.setCurrentPage(ref);
        ConsumFixtureSetSelectController controller = new ConsumFixtureSetSelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
        Test.startTest();
        // 检索
        controller.init();
        controller.keyword = 'MH-241';
        controller.searchOpp();
        System.assertEquals(1, controller.viewlist.size());
        // 保存
        controller.viewlist[0].check = true;
        Consum_Apply_Equipment_Set__c caes =(Consum_Apply_Equipment_Set__c) controller.viewlist[0].sobj;
        caes.Consum_Num__c = 1;
        ref = controller.save();
        ref.getParameters().put('reset','YES');
        controller.save();

        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id, Model_No__c FROM Consum_Apply_Equipment_Set__c WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(1, caesList.size());
        System.assertEquals('MH-241', caesList[0].Model_No__c);
        caesdList = [SELECT Id
                          , Asset__c
                          , Fixture_Model_No__c
                          , Provisional_Select_Time__c
                          , Internal_asset_location__c
                          , Salesdepartment__c
                          , SerialNumber__c
                          , RAESD_Status__c
                      FROM Consum_Apply_Equipment_Set_Detail__c
                      WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(1, caesdList.size());
        System.assertNotEquals(null, caesdList[0].Asset__c);
        System.assertNotEquals(null, caesdList[0].Provisional_Select_Time__c);
        System.assertEquals('上海 备品中心', caesdList[0].Internal_asset_location__c);
        System.assertEquals('0.备品中心', caesdList[0].Salesdepartment__c);
        System.assertEquals('sn01', caesdList[0].SerialNumber__c);
        System.assertEquals('草案中', caesdList[0].RAESD_Status__c);

        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.approve();

        caesdList = [SELECT Id
                          , Asset__c
                          , Fixture_Model_No__c
                          , Provisional_Select_Time__c
                          , Internal_asset_location__c
                          , Salesdepartment__c
                          , SerialNumber__c
                          , RAESD_Status__c
                      FROM Consum_Apply_Equipment_Set_Detail__c
                      WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(1, caesdList.size());
        System.assertNotEquals(null, caesdList[0].Asset__c);
        System.assertNotEquals(null, caesdList[0].Provisional_Select_Time__c);
        System.assertEquals('上海 备品中心', caesdList[0].Internal_asset_location__c);
        System.assertEquals('0.备品中心', caesdList[0].Salesdepartment__c);
        System.assertEquals('sn01', caesdList[0].SerialNumber__c);
        System.assertEquals('暂定分配', caesdList[0].RAESD_Status__c);

        Test.stopTest();
    }
    static testMethod void testReset() {
        setupTestData();
        // Consum_Apply_Equipment_Set_Detail__c caesd = TestDataUtility.CreateConsumApplyEquipmentSetDetail(1)[0];
        factory = new ConsumTestDataFactory();
        factory.selectDetails();
        Consum_Apply__c ca = [SELECT Id FROM Consum_Apply__c LIMIT 1];
        PageReference ref = new PageReference('/apex/ConsumFixtureSetSelect');
        ref.getParameters().put('pt_recid',ca.Id);
        ref.getParameters().put('message','保存成功');
        ref.getParameters().put('level','info');
        ref.getParameters().put('saveType','true');
        // ref.getParameters().put('pt_recid',ca.Id);
        // ref.getParameters().put('raesid',caesd.Id);
        // ref.getParameters().put('level',ca.Id);
        // ref.getParameters().put('message',caesd.Id);
        Test.setCurrentPage(ref);
        ConsumFixtureSetSelectController controller = new ConsumFixtureSetSelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();

        // 检索
        Test.startTest();
        controller.init();
        controller.keyword = 'MH-241';
        controller.searchOpp();
        System.assertEquals(1, controller.viewlist.size());

        // 适用
        controller.reSetYouXiaoKuCun();
        Test.stopTest();
        List<Consum_Apply_Equipment_Set__c> caesList = [SELECT Id
                                                          FROM Consum_Apply_Equipment_Set__c
                                                         WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(0, caesList.size());
        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [SELECT Id
                                                                  FROM Consum_Apply_Equipment_Set_Detail__c
                                                                  WHERE Consum_Apply__c =: ca.Id];
        System.assertEquals(0, caesdList.size());
        ref.getParameters().put('level','warning');
        ref.getParameters().put('message','test');
        controller.showMessage();
        ref.getParameters().put('level','error');
        controller.showMessage();
        controller.cancel();
        ref.getParameters().put('reset','YES');
    }
}