/**
 * @author shentianyi@sohobb.jp
 * @date 2017.08.30
 *
 * @description This class contains unit tests for validating
 * the behavior of FixtureSetManageController.cls
 *
 * This class includes four Unit test method that
 * verify whether a particular piece of code is working properly.
 *
 * This class has the 90% coverage to the FixtureSetManageController.cls
 */
@isTest
private class FixtureSetManageControllerTest {
	/**
	* @description データ作成
	*/
	@testSetup static void setupTestData() {
		List<Product2> productData = new List<Product2>();
    	for (Integer i = 1; i <= 25; i++) {
    		productData.add(new Product2(Name = 'Test_' + i, isActive = true, Asset_Model_No__c = 'M_' + i));
            if (i <= 15) {
                productData.add(new Product2(Name = 'Star_' + i, isActive = true, Asset_Model_No__c = 'F_' + i));
            }
    	}
    	insert productData;
	}
    static Fixture_Set__c fixtureSet { get {
        if(fixtureSet == null) {
            fixtureSet = new Fixture_Set__c(Name='testFixtureSet01');
            insert fixtureSet;
        }
        return fixtureSet;
    }}
    static String recid { get { return 'pt_recid='+fixtureSet.Id; }} 
    
    /**
	* @description Initテスト
	* It is mainly to test whether error occurs when it init
	*/
    static testMethod void unitTestInit() {
    	
        PageReference pageRef = new PageReference('/apex/FixtureSetManage?'+recid);
        Test.setCurrentPage(pageRef);
        
        FixtureSetManageController mockPage = new FixtureSetManageController();

        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = mockPage;
        mockPage.myComponentController.init();
        
        Test.startTest();
        mockPage.init();
        Test.stopTest();

        System.debug('init Success');
    }

    /**
    * @description Searchテスト
    * It is mainly to test whether it can search the specific data
    */
    static testMethod void unitTestsearchOpp() {
        PageReference pageRef = new PageReference('/apex/FixtureSetManage?'+recid);
        Test.setCurrentPage(pageRef);
        
        FixtureSetManageController mockPage = new FixtureSetManageController();

        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = mockPage;
        mockPage.myComponentController.init();

        Test.startTest();
        mockPage.assetModelNo = 'M';         
        mockPage.searchOpp();
        Test.stopTest();

        System.assertEquals(25, mockPage.getMyComponentController().recordAllCount);
        System.debug('searchOpp Success');

    }


    /**
    * @description Cancelテスト
    * It is mainly to test whether error occurs when it cancel
    */
    static testMethod void unitTestcancel() {
                
        PageReference pageRef = new PageReference('/apex/FixtureSetManage?'+recid);
        Test.setCurrentPage(pageRef);
        
        FixtureSetManageController mockPage = new FixtureSetManageController();

        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = mockPage;
        mockPage.myComponentController.init();
        
        Test.startTest();               
        mockPage.cancel();
        Test.stopTest();

        System.debug('cancel Success');
        
    }


    /**
    * @description Saveテスト
    * It is mainly to test whether view is correct
    * when the view is updated
    */
    static testMethod void unitTestsave() {
        
        PageReference pageRef = new PageReference('/apex/FixtureSetManage?'+recid);
        Test.setCurrentPage(pageRef);
        
        FixtureSetManageController mockPage = new FixtureSetManageController();

        mockPage.myComponentController = new CreateRelationListPagingCmpCtrl();
        mockPage.myComponentController.pageController = mockPage;
        mockPage.myComponentController.init();
        //System.debug('ParentId:'+mockPage.parentId+':'+mockPage.myComponentController.pageController.parentId);
        list<Product2> listProduct = [Select Id,Name,Asset_Model_No__c From Product2 Where Name Like 'Test%'];
        /**
        * @description データ作成
        */
        List<Fixture_Set_Detail__c> testdata = new List<Fixture_Set_Detail__c>();

        testdata.add(new Fixture_Set_Detail__c(SortInt__c = 1, Name_CHN_Created__c = 'MTJ_1'
                , Is_Body__c = true, Is_Optional__c = false, Quantity__c = 1
                , Is_OneToOne__c = false, Fixture_Set__c = fixtureSet.Id
                , Product2__c = listProduct[0].Id
                , UniqueKey__c = fixtureSet.Id + ':' + listProduct[0].Id));

        for (Integer i = 2; i <= 4; i++) {
            testdata.add(new Fixture_Set_Detail__c(SortInt__c = i, Name_CHN_Created__c = 'MTJ_' + i
                , Is_Body__c = false, Is_Optional__c = true, Quantity__c = 2
                , Is_OneToOne__c = false, Fixture_Set__c = fixtureSet.Id
                , Product2__c = listProduct[i-1].Id
                , UniqueKey__c = fixtureSet.Id + ':' + listProduct[i-1].Id));
        }

        for (Integer i = 5; i <= 7; i++) {
            testdata.add(new Fixture_Set_Detail__c(SortInt__c = i, Name_CHN_Created__c = 'MTJ_' + i
                , Is_Body__c = false, Is_Optional__c = true, Quantity__c = 1
                , Is_OneToOne__c = true,Fixture_Set__c = fixtureSet.Id
                , Product2__c = listProduct[i-1].Id
                , UniqueKey__c = fixtureSet.Id + ':' + listProduct[i-1].Id));
        }

        insert testdata;

        mockPage.init();
        mockPage.assetModelNo = 'M';
        mockPage.searchOpp();
        //
        mockPage.family = 'M';
        mockPage.category2 = 'M';
        mockPage.category3 = 'M';
        mockPage.category4 = 'M';
        mockPage.category5 = 'M';
        mockPage.assetModelNo = 'M';
        mockPage.multiStatus = 'M';
        mockPage.getSqlWhereStr();
        //system.assert(mockPage.viewList.size()==0,'Fixture_Set_Detail is blank.');
        Test.startTest();
        for (Integer i = 0; i <= 6; i++) {
             mockPage.viewList[i].check = true;
        }
        mockPage.saveType = '1';
        mockPage.save();
        mockPage.viewList[2].check = false;
        mockPage.viewList[4].check = false;
        mockPage.saveType = '2';
        mockPage.save();
        mockPage.viewList[3].check = false;
        mockPage.saveType = '98';
        mockPage.save();
        Test.stopTest();


        System.assertEquals(true, mockPage.viewList[1].check);
        System.assertEquals(false, mockPage.viewList[3].check);
        System.debug('save Success');

        List<Fixture_Set_Detail__c> fs = [Select Id, Name_CHN__c, Name_CHN_Created__c From Fixture_Set_Detail__c];
        System.assertEquals(fs.size(), 4);
        for (Fixture_Set_Detail__c fd : fs) {
            System.assertEquals(fd.Name_CHN__c, fd.Name_CHN_Created__c);
        }
        
    }
}