@isTest
private class SaveAssetByCopyControllerTest {
	private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    private static Account hospital = null;
    private static List<Account> strategicDep = null;
    private static Product2 productA = null;
    private static Account dep = null;
    private static PricebookEntry entry = null;
    private static Asset asset01 = null;
    private static Asset asset02 = null;
    
    static {
        // 病院を作る
        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;
        
        // 戦略科室を得る
        strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        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;
        
        // 製品を作る
        productA = new Product2( Name='テスト商品');
        insert productA;
        
        // 価格表エントリを作成する     
        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;
        
        // 納入機器を作成する
        asset01 = new Asset();
        asset01.Name = 'テスト機器';
        asset01.AccountId = dep.Id;
        asset01.Department_Class__c = strategicDep[0].Id;
        asset01.Hospital__c = hospital.Id;
        asset01.SerialNumber = 'testserial';
        asset01.LastSFDCRepairReturn_day__c = System.today();
        insert asset01;
        
        asset02 = new Asset();
        asset02.Name = 'テスト機器2';
        asset02.AccountId = dep.Id;
        asset02.Department_Class__c = strategicDep[0].Id;
        asset02.Hospital__c = hospital.Id;
        asset02.SerialNumber = 'testserial2';
        asset02.LastSFDCRepairReturn_day__c = System.today();
        insert asset02;
        
    }
    
    static testMethod void myUnitTest() {
        ApexPages.currentPage().getParameters().put('oldid', asset01.Id);
        ApexPages.currentPage().getParameters().put('newid', asset02.Id);
        
        SaveAssetByCopyController sabc = new SaveAssetByCopyController();
        sabc.init();
        
        // assert
        Asset ast01 = [Select LastSFDCRepairReturn_day__c from Asset where Id = :asset01.Id];
        Asset ast02 = [Select LastSFDCRepairReturn_day__c from Asset where Id = :asset02.Id];
        System.assertEquals(System.today(), ast01.LastSFDCRepairReturn_day__c);
        System.assertEquals(null, ast02.LastSFDCRepairReturn_day__c);
    }
}