/**
 * 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 AccountCaseTabControllerTest {
    
    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 ('消化科')];
        
        // 病例数作る
        Account_Number_of_case__c ac1 = new Account_Number_of_case__c(
            Account__c = dc1s[0].Id,
            OCM_Period__c = '146P',
            OCM_Year__c = '2013年度'
        );
        insert ac1;
        
        PageReference page = new PageReference('/apex/AccountCaseTab?id=' + ac1.Id + '&accid=' + dc1s[0].Id + '&t=146P');
        System.Test.setCurrentPage(page);
        AccountCaseTabController controller = new AccountCaseTabController();
        controller.init();
        
        System.assertNotEquals(null, controller.history);
        // update
        controller.history.rec.Additional_comment__c = 'aaaaa';
        controller.save();
        
        page = new PageReference('/apex/AccountCaseTab?id=&accid=' + dc1s[0].Id + '&t=');
        System.Test.setCurrentPage(page);
        controller = new AccountCaseTabController();
        controller.init();
        // insert
        controller.history.rec.OCM_Year__c = '2014年度';
        controller.save();
        
        List<Account_Number_of_case__c> acs = [select Id, Unique_key__c from Account_Number_of_case__c where Account__c = :dc1s[0].Id];
        System.assertEquals(2, acs.size());
    }
    
    private static integer FIELDMAX = 100;
    
    // 病例数設定に項目が正しいかどうか
    static testMethod void makeSqlTest() {
        List<OFSHospitalLayout__c> layoutList = OFSHospitalLayout__c.getall().values();
        for (OFSHospitalLayout__c layout : layoutList) {
            // 病例数のみチェック
            if (layout.recordType_devName__c.startsWith('AccountCase_')) {
                String soql = 'select Id';
                for (Integer i = 1; i <= FIELDMAX; i++) {
                    String strI = 'field' + i + '__c';
                    String fieldStr = String.valueOf(layout.get(strI));
                    
                    if (String.isBlank(fieldStr) == false) {
                        Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(fieldStr);
                        String apiNum = String.valueOf(m.get('apiNum'));
                        String apiPrice = String.valueOf(m.get('apiPrice'));
                        
                        if (String.isBlank(apiNum) == false) {
                            soql += ', ' + apiNum;
                        }
                        if (String.isBlank(apiPrice) == false) {
                            soql += ', ' + apiPrice;
                        }
                    }
                }
                soql += ' from Account_Number_of_case__c limit 1';
                Database.query(soql);
                // ExceptionがなければOK
            }
        }
    }
}