@isTest
private class SelectAssetExtensionTester {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static Asset createAsset(String input, String accountid, String dcId, String hpId) {
        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId;
        asset.Hospital__c = hpId;
        asset.SerialNumber = 'testserial';
        insert asset;
        return asset;
    }
    
    static testMethod void myTest01() {
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        insert hospital;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品');
        insert productA;
        
        // 価格表エントリを作成する     
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        entry.Product2Id = productA.Id;
        insert entry;
        
        // 納入機器を作成する
        Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset03 = createAsset('asset03', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset04 = createAsset('asset04', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset05 = createAsset('asset05', dep.Id, strategicDep[0].Id, hospital.Id);
        
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        insert contract;

        //List<Maintenance_Contract_Asset__c> selectedAsset = [SELECT Id, Name, Maintenance_Contract__c, Asset__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c = :contract.Id];
        //System.assertEquals( 5, selectedAsset.size()); 
        // Visualforceページの引数設定
        ApexPages.currentPage().getParameters().put( 'targetContractId', contract.Id);
        
        // テスト対象クラス実体化
        ApexPages.StandardController sc = new ApexPages.StandardController( contract);
        SelectAssetExtension target = new SelectAssetExtension( sc);
        

        List<Asset> assetRecords = [SELECT Id, Name, CheckBox__c, SerialNumber,
                                 Department_Name__c, Installation_Site__c, Posting_Date__c,
                                 Asset_Owner__c
                                 FROM Asset
                                 WHERE Hospital__c = :hospital.Id
                                         AND ( AssetMark__c != '耗材' OR Product2.Family != 'ET' )
                                         ORDER BY Department_Name__c, Posting_Date__c, SerialNumber];
        System.assertEquals( 5, assetRecords.size());


        target.init();
        //20191220 modify start
        //List<Asset> assets01 = target.assetRecords
        System.assertEquals( 1, target.assetLists.size()); 
        List<Asset> assets01 = new List<Asset>();
        if (target.assetLists.size() > 0) {
            assets01 = target.assetLists[0];
        }
        System.assertEquals( 5, assets01.size());
        //20191220 modify end
        for ( Asset local : assets01) {
            System.assertEquals( local.checkBox__c, false, '01'); 
        }
        
        //target.cancel();
        
        target.save();
        List<Asset> assets02 =[SELECT ID, checkbox__c FROM Asset WHERE Id IN :assets01];
        for ( Asset local : assets02) {
            System.assertEquals( local.checkBox__c, false, '02');
            local.checkbox__c = true; 
        }
        
        target.save();
        List<Asset> assets03 =[SELECT ID, checkbox__c FROM Asset WHERE Id IN :assets01];
        for ( Asset local : assets03) {
            System.assertEquals( local.checkBox__c, false, '03');
        }
        
        //20191220 modify start
        assets01[0].checkbox__c = true;
        //target.assetRecords[0].checkbox__c = true;

        target.save();
    }
    
    static testMethod void sortTableTest() {
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        insert hospital;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品');
        insert productA;
        
        // 価格表エントリを作成する     
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        entry.Product2Id = productA.Id;
        insert entry;
        
        // 納入機器を作成する
        Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset03 = createAsset('asset03', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset04 = createAsset('asset04', dep.Id, strategicDep[0].Id, hospital.Id);
        Asset asset05 = createAsset('asset05', dep.Id, strategicDep[0].Id, hospital.Id);
        
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        insert contract;
        
        // Visualforceページの引数設定
        ApexPages.currentPage().getParameters().put( 'targetContractId', contract.Id);
        
        // テスト対象クラス実体化
        ApexPages.StandardController sc = new ApexPages.StandardController( contract);
        SelectAssetExtension target = new SelectAssetExtension( sc);
        
        target.init();
        
        target.getProductCount();
        
        target.sortKey = '1';
        //target.assetLists[0][0].checkbox__c = true;
        target.sortTable();
        target.sortTable();
    }
}