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

    static testMethod void myUnitTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        // layout作成
        OFSHospitalLayout__c layout = new OFSHospitalLayout__c();
        layout.Name = 'GI-Case';
        layout.recordType_devName__c = 'AccountCase_Department_Class_GI';
        layout.section1__c = '{"id":"001","title":"# of Room"}';
        layout.field1__c = '{"sectionId":"001","label":"OLY Room","parentLabel":"Endoscopy Room","apiNum":"OLY_endscopy_room__c","apiPrice":"OLY_endscopy_room_price__c"}';
        layout.field2__c = '{"sectionId":"001","label":"Competitor\'s Room","parentLabel":"Endoscopy Room","apiNum":"Competitor_endoscopy_room__c","apiPrice":"Competitor_endoscopy_room_price__c"}';
        insert layout;
        
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'AccountCaseHistoryTestHp1', OwnerId = hpOwner.Id);
        insert hp;
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c in ('消化科')];
        
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 
        Account_Number_of_case__c anoc = new Account_Number_of_case__c();
        anoc.Account__c = dc1s[0].Id;
        anoc.OCM_Period__c = selectedTab;
        insert anoc;
        
        PageReference page = new PageReference('/apex/AccountCaseHospital?id=' + hp.Id);
        System.Test.setCurrentPage(page);
        AccountCaseHospitalController controller = new AccountCaseHospitalController(new ApexPages.StandardController(hp));
        controller.init();
        
        List<Account> dcList = [select Id, Department_Class_Label__c from Account where ParentId = :hp.Id order by Department_Class_Label__c];
        System.assertEquals('HP', controller.recordDeveloperName);
        System.assertEquals(1, controller.dcList.size());
        System.assertEquals(dcList[0].Id, controller.selectedTab);
        
        page = new PageReference('/apex/AccountCaseHospital?id=' + dc1s[0].Id);
        System.Test.setCurrentPage(page);
        controller = new AccountCaseHospitalController(new ApexPages.StandardController(dc1s[0]));
        controller.init();
        
        System.assertNotEquals('HP', controller.recordDeveloperName);
        System.assertEquals(2, controller.historyList.size());
        System.assertEquals(anoc.Id, controller.selectedTab);
        
        controller.getDcCount();
        controller.getHistoryCount();
    }
}