@isTest
private class AccountTargetHospitalControllerTest {

    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作成
        Target_account_manage__c layout = new Target_account_manage__c();
        layout.Name = 'GI-Case';
        layout.department_class__c = '消化科';
        layout.field1__c = '{"label":"CV-290","api":"CV290"}';
        layout.field2__c = '{"label":"CV-170","api":"CV170"}';
        layout.field3__c = '{"label":"EU-ME2","api":"EUME2"}';
        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_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc1s[0].Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        insert anot;

        PageReference page = new PageReference('/apex/AccountTargetHospital?id=' + hp.Id);
        System.Test.setCurrentPage(page);
        AccountTargetHospitalController controller = new AccountTargetHospitalController(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/AccountTargetHospital?id=' + dc1s[0].Id);
        System.Test.setCurrentPage(page);
        controller = new AccountTargetHospitalController(new ApexPages.StandardController(dc1s[0]));
        controller.init();

        System.assertNotEquals('HP', controller.recordDeveloperName);
        System.assertEquals(2, controller.historyList.size());
        System.assertEquals(anot.Id, controller.selectedTab);

        controller.getDcCount();
        controller.getHistoryCount();
    }
}