/**
 * 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 OpportunityPCLControllerTest {

    static testMethod void myUnitTest() {
        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];
        
        Account acc = new Account();
        acc.RecordTypeId = rectCo.Id;
        acc.Name = 'HP test1';
        insert acc;
        
        List<Account> dept = [select Id, Name from Account where ParentId = :acc.Id and Department_Class_Label__c IN ('消化科', '呼吸科') order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = 'Gastoro Intestin Test';
        depart1.ParentId            = dept[0].Id;
        depart1.Department_Class__c = dept[0].Id;
        depart1.Hospital__c         = acc.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dept[1].Id;
        depart2.Department_Class__c = dept[1].Id;
        depart2.Hospital__c         = acc.Id;
        insert new Account[] {depart1, depart2};
        
        RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];
        
        Opportunity opp1 = new Opportunity(
            Name = 'test opp1',
            StageName = '引合',
            CurrencyIsoCode = 'USD',
            CloseDate = Date.today(),
            AccountId = depart1.Id,
            RecordTypeId = oppVND.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = acc.Id,
            Competitor__c = 'A'
        );
        Opportunity opp2 = new Opportunity(
            Name = 'test opp2',
            StageName = '引合',
            CurrencyIsoCode = 'USD',
            CloseDate = Date.today(),
            AccountId = depart2.Id,
            RecordTypeId = oppVND.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = acc.Id,
            Competitor__c = 'B'
        );
        insert new Opportunity[] {opp1, opp2};
        
        OpportunityPCLController pcl = new OpportunityPCLController();
        pcl.init();
        System.assertEquals(2, pcl.oppRecords.size());
        System.assertEquals('E', pcl.oppRecords[0].rec.Competitor__c);
        System.assertEquals('E', pcl.oppRecords[1].rec.Competitor__c);
        pcl.dateField = 'Closing_Bid_Date__c';
        pcl.con1.Birthdate = Date.today().addDays(-7);
        pcl.con2.Birthdate = Date.today().addDays(-3);
        pcl.limits = '50';
        pcl.text3 = 'S:Competitor__c';
        pcl.condition3 = 'equals';
        pcl.value3 = 'E';
        
        pcl.searchOpp();
        System.assertEquals(2, pcl.oppRecords.size());
        System.assertEquals(depart1.Id, pcl.oppRecords[0].rec.AccountId);
        
        pcl.text3 = '';
        pcl.searchOpp();
        pcl.sortKey = '2';
        pcl.sortTable();
        System.assertEquals(2, pcl.oppRecords.size());
        System.assertEquals('E', pcl.oppRecords[1].rec.Competitor__c);
        
        pcl.oppRecords[1].rec.Competitor__c = 'A1';   // ソートの確認
        pcl.saveType = '2';
        Test.startTest();
        pcl.save();
        Test.stopTest();
        System.assertEquals(depart2.Id, pcl.oppRecords[1].rec.AccountId);
    }
}