/**
 * 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.
 */
 // 2018年7月12日 OpponentBidInformation2Asset trigger的test class by 张玉山
@isTest
private class OpponentBidInformation2AssetTest {
    /**
     * 新規して、update、競合1
     * update、競合2
     */
    static testMethod void testOpponentBidInformation1() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 recodetype');
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 recodetype');
        }
        List<RecordType> rectLost = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Opponent_Bid_Information__c' and DeveloperName IN ('OpponentBidInformation', 'PCL_Cancel_report') order by DeveloperName desc];
        if (rectLost.size() == 0) {
            throw new ControllerUtil.myException('not found Lost_cancel_report recodetype');
        }

        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = '病院テスト2';
        insert new Account[] {company1, company2};

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;

        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;

        insert new Account[] {depart1, 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        = true;
        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        = true;
        Product2 prd3 = new Product2();
        prd3.ProductCode_Ext__c     = 'HistoryPrd3';
        prd3.ProductCode            = 'HistoryPrd3';
        prd3.Repair_Product_Code__c = 'HistoryPrd3_RP';
        prd3.Name                   = 'HistoryPrd3';
        prd3.Manual_Entry__c        = true;
        Product2 prd4 = new Product2();
        prd4.ProductCode_Ext__c     = 'HistoryPrd4';
        prd4.ProductCode            = 'HistoryPrd4';
        prd4.Repair_Product_Code__c = 'HistoryPrd4_RP';
        prd4.Name                   = 'HistoryPrd4';
        prd4.Manual_Entry__c        = true;
        insert new Product2[] {prd1, prd2, prd3, prd4};

        // 商談
        Opportunity opp1 = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today(),
            Hospital__c = company1.Id,
            Department_Class__c = dc1s[0].Id,
            AccountId = depart1.Id
        );
        Opportunity opp2 = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today(),
            Hospital__c = company2.Id,
            Department_Class__c = dc2s[0].Id,
            AccountId = depart2.Id
        );
        insert new Opportunity[] {opp1, opp2};

        //资产
        list<Asset> Assets = new list<Asset>();
        Asset Assert1 = new Asset(
                    Name = '*',
                    Opportunity__c = opp1.id,
                    Department_Class__c = opp1.Department_Class__c,
                    AccountId = opp1.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True,
                    Product2Id = prd1.Id
         );
        Assets.add(Assert1);
        Asset Assert2 = new Asset(
                    Name = '*',
                    Opportunity__c = opp1.id,
                    Department_Class__c = opp1.Department_Class__c,
                    AccountId = opp1.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True,
                    Product2Id = prd2.Id
         );
        Assets.add(Assert2);
        Asset Assert3 = new Asset(
                    Name = '*',
                    Opportunity__c = opp1.id,
                    Department_Class__c = opp1.Department_Class__c,
                    AccountId = opp1.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True,
                    Product2Id = prd3.Id
         );
        Assets.add(Assert3);
        Asset Assert4 = new Asset(
                    Name = '*',
                    Opportunity__c = opp1.id,
                    Department_Class__c = opp1.Department_Class__c,
                    AccountId = opp1.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True,
                    Product2Id = prd4.Id
         );
        Assets.add(Assert4);        
        upsert(Assets);

        System.Test.StartTest();
        // 失单
        Opponent_Bid_Information__c obi1 = new Opponent_Bid_Information__c(
            Opportunity__c          = opp1.Id, 
            CompetitorProduct1__c   = prd1.Id,
            CompetitorProduct2__c   = prd2.Id,
            CompetitorProduct3__c   = prd3.Id,
            CompetitorProduct4__c   = prd4.Id,
            RecordTypeId            = rectLost[0].Id,
            CompetitorAsset1__c     = Assert1.id,
            CompetitorAsset2__c     = Assert2.id,
            CompetitorAsset3__c     = Assert3.id,
            CompetitorAsset4__c     = Assert4.id
        );

        insert obi1;
        
        // Assert
        obi1 = [Select Id, CompetitorAsset1__c, CompetitorAsset1__r.Product2Id from Opponent_Bid_Information__c where Id = :obi1.Id];
        List<Asset> asts = [Select Id, Opportunity__c from Asset where Id = :obi1.CompetitorAsset1__c];
        System.assertEquals(1, asts.size());
        System.assertEquals(opp1.Id, asts[0].Opportunity__c);
        System.assertEquals(True, obi1.CompetitorAsset1__c <> null);
        System.assertEquals(prd1.Id, obi1.CompetitorAsset1__r.Product2Id);
        
        // update1
        obi1.CompetitorProduct1__c = prd2.Id;
        update obi1;
        Assert1.Product2Id = prd2.Id;
        update Assert1;
        obi1 = [Select Id, CompetitorAsset1__c, CompetitorAsset1__r.Product2Id from Opponent_Bid_Information__c where Id = :obi1.Id];
        System.assertEquals(True, obi1.CompetitorAsset1__c <> null);
        System.assertEquals(prd2.Id, obi1.CompetitorAsset1__r.Product2Id);

        // delete
        delete obi1;
        asts = [Select Id from Asset where (Id = :Assert1.id or Id = :Assert2.id or Id = :Assert3.id or Id = :Assert4.id)];
        System.assertEquals(0, asts.size());
        System.Test.StopTest();
    }

}