/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SaveMaintenanceByCopyControllerTest {
    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 Maintenance_Contract__c contract1 = null;
    private static Maintenance_Contract__c contract2 = null;
    private static Maintenance_Contract_Asset__c mca = 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';
        insert asset01;
        
        // 维修合同を作成する
        contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract1';
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Contract_Start_Date__c = Date.today() - 10;
        contract1.Contract_End_Date__c = Date.today() + 10;
        insert contract1;
        
        contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Contract_Start_Date__c = Date.today() - 10;
        contract2.Contract_End_Date__c = Date.today() + 10;
        insert contract2;
        
        mca = new Maintenance_Contract_Asset__c();
        mca.Maintenance_Contract__c = contract1.Id;
        mca.Asset__c = asset01.Id;
        insert mca;
    }
    
    static testMethod void myUnitTest() {
        ApexPages.currentPage().getParameters().put('mid', contract1.Id);
        ApexPages.currentPage().getParameters().put('newid', contract2.Id);
        
        SaveMaintenanceByCopyController smb = new SaveMaintenanceByCopyController();
        smb.init();
        
        List<Maintenance_Contract_Asset__c> newMca = [select Asset__c from Maintenance_Contract_Asset__c where Maintenance_Contract__c = :contract2.Id];
        System.assertEquals(1, newMca.size());
        System.assertEquals(asset01.Id, newMca[0].Asset__c);
        
        contract2 = [select Contract_Start_Date__c from Maintenance_Contract__c where Id = :contract2.Id];
        System.assertEquals(null, contract2.Contract_Start_Date__c);
    }
}