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

    static testMethod void myUnitTest() {
        // システム管理者
        User user = new User();
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();
        insert user;
        
        // 別ユーザ
        User user2 = new User();
        user2.LastName = '_別';
        user2.FirstName = 'ユーザ';
        user2.Alias = '別ユーザ';
        user2.Email = 'olympusTest04@sunbridge.com';
        user2.Username = 'olympusTest04@sunbridge.com';
        user2.CommunityNickname = '別ユーザ';
        user2.IsActive = true;
        user2.EmailEncodingKey = 'ISO-2022-JP';
        user2.TimeZoneSidKey = 'Asia/Tokyo';
        user2.LocaleSidKey = 'ja_JP';
        user2.LanguageLocaleKey = 'ja';
        user2.ProfileId = System.Label.ProfileId_SystemAdmin;
        user2.Job_Category__c = '销售推广';
        user2.Province__c = '上海市';
        user2.Use_Start_Date__c = Date.today().addMonths(-6);
        user2.SalesManager__c = user.Id;
        user2.BuchangApprovalManagerSales__c = user.Id;
        user2.JingliApprovalManager__c = user.Id;
        user2.BuchangApprovalManager__c = user.Id;
        user2.ZongjianApprovalManager__c = user.Id;
        insert user2;
        
        // 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 from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        
        // insert
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'Katsu テスト';
        insert company;
        
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert section;

        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM001TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert depart;
        
        // insert test
        Report__c rpt = new Report__c();
        rpt.Hospital_Department__c = depart.Id;
        rpt.OwnerId = user2.Id;
        rpt.OPD_ProductCategory1__c ='3D System';
        rpt.OPD_ProductCategory2__c ='OR Imaging Products';
        insert new Report__c[] {rpt};
        rpt = [select Id,SalesManager__c,BuchangApprovalManagerSales__c,JingliApprovalManager__c,
                                        BuchangApprovalManager__c,ZongjianApprovalManager__c 
                                   from Report__c 
                                  where Id = :rpt.Id];
        System.assertEquals(user.Id, rpt.SalesManager__c);
        System.assertEquals(user.Id, rpt.BuchangApprovalManagerSales__c);
        System.assertEquals(user.Id, rpt.JingliApprovalManager__c);
        System.assertEquals(user.Id, rpt.BuchangApprovalManager__c);
        System.assertEquals(user.Id, rpt.ZongjianApprovalManager__c);
        
        // update test
        rpt.Status__c = '申请中';
        update rpt;
        rpt = [select Id,SalesManager__c,BuchangApprovalManagerSales__c,JingliApprovalManager__c,
                                        BuchangApprovalManager__c,ZongjianApprovalManager__c 
                                   from Report__c 
                                  where Id = :rpt.Id];
        System.assertNotEquals(user.Id, rpt.SalesManager__c);
        System.assertNotEquals(user.Id, rpt.BuchangApprovalManagerSales__c);
        System.assertNotEquals(user.Id, rpt.JingliApprovalManager__c);
        System.assertNotEquals(user.Id, rpt.BuchangApprovalManager__c);
        System.assertNotEquals(user.Id, rpt.ZongjianApprovalManager__c);
        System.assertNotEquals(null, rpt.SalesManager__c);
        System.assertNotEquals(null, rpt.BuchangApprovalManagerSales__c);
        System.assertNotEquals(null, rpt.JingliApprovalManager__c);
        System.assertNotEquals(null, rpt.BuchangApprovalManager__c);
        System.assertNotEquals(null, rpt.ZongjianApprovalManager__c);
        
        rpt.OwnerId = user.Id;
        update rpt;
        rpt = [select Id,SalesManager__c,BuchangApprovalManagerSales__c,JingliApprovalManager__c,
                                        BuchangApprovalManager__c,ZongjianApprovalManager__c 
                                   from Report__c 
                                  where Id = :rpt.Id];
        System.assertEquals(UserInfo.getUserId(), rpt.SalesManager__c);
        System.assertEquals(UserInfo.getUserId(), rpt.BuchangApprovalManagerSales__c);
        System.assertEquals(UserInfo.getUserId(), rpt.JingliApprovalManager__c);
        System.assertEquals(UserInfo.getUserId(), rpt.BuchangApprovalManager__c);
        System.assertEquals(UserInfo.getUserId(), rpt.ZongjianApprovalManager__c);
    }
    
    static testMethod void customerSignTest() {
        Report__c rep1 = new Report__c();
        rep1.Customer_sigh_photo__c = 'sign test';
        rep1.Manual_Name__c = 'TEST BJ 医院 普外科 普外科';
        rep1.Evaluation_PDF_number__c = 'AAA';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        insert rep1;
        
        rep1 = [select Customer_sigh_photo_txt__c from Report__c where Id = :rep1.Id];
        System.assertEquals('sign test', rep1.Customer_sigh_photo_txt__c);
    }
}