/**
 * 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 MakeAssetHistoryTriggerTest {
    public static Account depart2;

    static Asset[] createAsset() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return null;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return null;
        }
        List<RecordType> rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        if (rectDpt2.size() == 0) {
            return null;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'HistoryTestCompany';
        upsert company;
        Account section = [Select Id from Account where RecordType.Name = '戦略科室分類 消化科' and ParentId =: company.Id];
        Account section2 = [Select Id from Account where RecordType.Name = '戦略科室分類 呼吸科' and ParentId =: company.Id];
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'HistoryTestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart2 = new Account();
        depart2.RecordTypeId = rectDpt2[0].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = 'HistoryTestDepart2';
        depart2.ParentId            = section2.Id;
        depart2.Department_Class__c = section2.Id;
        depart2.Hospital__c         = company.Id;
        insert new Account[] {depart, depart2};

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'HistoryPrd1';
        prd1.ProductCode            = 'HistoryPrd1';
        prd1.Repair_Product_Code__c = 'HistoryPrd1_RP';
        prd1.Name                   = 'HistoryPrd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'HistoryPrd2';
        prd2.ProductCode            = 'HistoryPrd2';
        prd2.Repair_Product_Code__c = 'HistoryPrd2_RP';
        prd2.Name                   = 'HistoryPrd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                   = 'HistoryAst1';
        ast.AccountId              = depart.Id;
        ast.Department_Class__c    = section.Id;
        ast.Hospital__c            = company.Id;
        ast.Product2Id             = prd1.Id;
        ast.SerialNumber           = 'HistorySerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        ast.Status                 = '使用中';
        Asset ast2 = new Asset();
        ast2.Name                   = 'HistoryAst2';
        ast2.AccountId              = depart.Id;
        ast2.Department_Class__c    = section.Id;
        ast2.Hospital__c            = company.Id;
        ast2.Product2Id             = prd1.Id;
        ast2.SerialNumber           = 'HistorySerialNumber2';
        ast2.Guarantee_period_for_products__c = Date.today();
        ast2.InstallDate                      = Date.today();
        ast2.Status                 = '使用中';
        insert new Asset[] {ast, ast2};
        // ast = [select Id, CurrencyIsoCode, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber 
        //          from Asset
        //         where Id = :ast.Id];
        // ast2 = [select Id, CurrencyIsoCode, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber 
        //          from Asset
        //         where Id = :ast2.Id];
        ast = [select Id, CurrencyIsoCode, Name, Product_Serial_No__c, AccountId, Department_Class__c,  Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber 
                 from Asset
                where Id = :ast.Id];
        ast2 = [select Id, CurrencyIsoCode, Name, Product_Serial_No__c, AccountId, Department_Class__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber 
                 from Asset
                where Id = :ast2.Id];

        return new Asset[] {ast, ast2};
    }

    static testMethod void testInsert() {
        Asset ast = createAsset()[0];
        
        // HistoryテーブルのassertEquals
        List<AssetHistory__c> ahList = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name desc];
        //System.assertEquals(1, ahList.size());
        //System.assertEquals(ast.Id, ahList[0].AssetId__c);
        //System.assertEquals(ast.CurrencyIsoCode, ahList[0].CurrencyIsoCode);
        //System.assertEquals('Id', ahList[0].Field_Api_Name__c);
        //System.assertEquals(null, ahList[0].OldValue__c);
        //System.assertEquals(null, ahList[0].NewValue__c);
        
        // 削除のテスト
        delete ast;
        ahList = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name];
        System.assertEquals(0, ahList.size());
        
    }

    /**
    static testMethod void testUpdate() {
        Asset[] astList = createAsset();
        Asset ast = astList[0];
        Asset ast2 = astList[1];
        ast.Inspection_Result__c = '正常';
        ast.Inspection_report_number__c = 'A';
        ast2.Inspection_Result__c = '不正常';
        ast2.Inspection_report_number__c = 'A';
        update new Asset[] {ast, ast2};

        // HistoryテーブルのassertEquals
        List<AssetHistory__c> ahList = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c, Inspection_Result__c, Inspection_Times__c, Inspection_Count__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name];
        List<AssetHistory__c> ah2List = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c, Inspection_Result__c, Inspection_Times__c, Inspection_Count__c from AssetHistory__c Where AssetId__c = :ast2.Id order by Name];
        //System.assertEquals(2, ahList.size());
        System.assertEquals(ast.Id, ahList[0].AssetId__c);
        System.assertEquals(ast.CurrencyIsoCode, ahList[0].CurrencyIsoCode);
        System.assertEquals('Inspection_report_number__c', ahList[0].Field_Api_Name__c);
        System.assertEquals(null, ahList[0].OldValue__c);
        System.assertEquals('A', ahList[0].NewValue__c);
        System.assertEquals(ast.Inspection_Result__c, ahList[0].Inspection_Result__c);
        System.assertEquals(1, ah2List.size());
        System.assertEquals(1, ahList[0].Inspection_Times__c + ah2List[0].Inspection_Times__c);         // 2件のうち、1件だけ1です。
        System.assertEquals(2, ahList[0].Inspection_Count__c + ah2List[0].Inspection_Count__c);         // 2件のうち、2件とも
        
                // 点検の項目を更新
        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Job_Category__c = '销售服务';
        loginUser.Employee_No__c = null;
        update loginUser;
        ast.Last_inspection_staff__c = UserInfo.getUserId();
        ast.Inspection_report_number__c = 'OCM-12345';
        ast2.Last_inspection_staff__c = UserInfo.getUserId();
        ast2.Inspection_report_number__c = 'OCM-12345';
        OFSInsReportAssetHistoryController.astHistoryMap = new Map<Id, Map<String, AssetHistory__c>>();
        OFSInsReportAssetHistoryController.astHistoryUpsertMap = new Map<Id, Map<String, AssetHistory__c>>();
        update new Asset[] {ast, ast2};

        ahList = [Select Id, Name, AssetId__c, Asset_Inspection_ExtID__c, Field_Api_Name__c, OldValue__c, NewValue__c, Inspection_name__c, Inspection_staff__c, Inspection_Staff_State__c, Inspection_Times__c, Inspection_Count__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name];
        ah2List = [Select Id, Name, AssetId__c, Asset_Inspection_ExtID__c, Field_Api_Name__c, OldValue__c, NewValue__c, Inspection_name__c, Inspection_staff__c, Inspection_Staff_State__c, Inspection_Times__c, Inspection_Count__c from AssetHistory__c Where AssetId__c = :ast2.Id order by Name];
        System.assertEquals(2, ahList.size());
        System.assertEquals(ast.Id, ahList[1].AssetId__c);
        System.assertEquals(loginUser.Id, ahList[1].Inspection_name__c);
        System.assertEquals(loginUser.Alias, ahList[1].Inspection_staff__c);
        System.assertEquals(loginUser.Province__c, ahList[1].Inspection_Staff_State__c);
        System.assertEquals('Inspection_report_number__c', ahList[1].Field_Api_Name__c);
        System.assertEquals('OCM-12345', ahList[1].NewValue__c);
        System.assertEquals('OCM-12345', ahList[1].NewValue__c);
System.debug('ahList:' + ahList);
System.debug('ah2List:' + ah2List);
        System.assertEquals(2, ah2List.size());
        System.assertEquals('Inspection_report_number__c', ah2List[1].Field_Api_Name__c);
        System.assertEquals('OCM-12345', ah2List[1].NewValue__c);
        System.assertEquals(1, ahList[0].Inspection_Times__c + ah2List[0].Inspection_Times__c);         // 2件のうち、1件だけ1です。
        System.assertEquals(2, ahList[0].Inspection_Count__c + ah2List[0].Inspection_Count__c);         // 2件のうち、2件とも
        System.assertEquals(1, ahList[1].Inspection_Times__c + ah2List[1].Inspection_Times__c);         // 2件のうち、1件だけ1です。
        System.assertEquals(2, ahList[1].Inspection_Count__c + ah2List[1].Inspection_Count__c);         // 2件のうち、2件とも
        
        // AccountIdの更新
        ast.AccountId = depart2.Id;
        update ast;
        ahList = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name];
        System.assertEquals(3, ahList.size());
        
        // 点検番号をnullにする
        ast.Inspection_report_number__c = '';
        ast.Inspection_Result__c = '';
        ast.Last_inspection_staff__c = null;
        update ast;
        ahList = [Select Id, AssetId__c, CurrencyIsoCode, Field_Api_Name__c, OldValue__c, NewValue__c, Inspection_name__c, Inspection_staff__c, Inspection_Staff_State__c from AssetHistory__c Where AssetId__c = :ast.Id order by Name];
        System.assertEquals(2, ahList.size());
    }
	*/
}