@isTest
private class NFM001TriggerTest {

    // calloutのtriggerなので、assertがなにもできないです。
    // NFM001.trigger、NFM001Controller、Nfm001Sync のカバー率を確認
    static testMethod void testInsert() {
        // 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;
        System.assertEquals('', NFM001Controller.debug_msg);
        
        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;
        System.assertEquals('', NFM001Controller.debug_msg);

        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;
        //System.assertEquals('1,NFM001_callout_insert_*', NFM001Controller.debug_msg);
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name         = 'NFM001TestDepart2';
        depart2.Department_Name__c  = 'NFM002TestDepart';
        depart2.ParentId            = section.Id;
        depart2.Department_Class__c = section.Id;
        depart2.Hospital__c         = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert depart2;
        System.assertEquals('', NFM001Controller.debug_msg);

        // 診療科更新
        depart.Name         = 'Katsu診療科';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update depart;
        System.assertEquals('', NFM001Controller.debug_msg);
        System.Test.startTest();
        // 診療科departを削除
        depart.Is_Active__c = '無効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '1,NFM001_callout_logic_deletesssss';
        update depart;
        System.assertEquals(true, NFM001Controller.debug_msg.startsWith('1,NFM001_callout_logic_delete'));
        String assertDateTo = NFM001Controller.debug_msg.substring('1,NFM001_callout_logic_delete'.length() + 1);
        Account departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart.Id];
        System.assertEquals(null, departAssert.DepartmentEffectiveDateTo__c);
        
        // 病院を削除
        company.Is_Active__c = '無効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '2,NFM001_callout_logic_deletessss';
        update company;
        System.assertEquals(true, NFM001Controller.debug_msg.startsWith('2,NFM001_callout_logic_delete'));
        assertDateTo = NFM001Controller.debug_msg.substring('2,NFM001_callout_logic_delete'.length() + 1);
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart2.Id];
        System.assertEquals(null, departAssert.DepartmentEffectiveDateTo__c);

        // 病院をundelete
        company.Is_Active__c = '有効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update company;
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart.Id];
        System.assertEquals('草案中', departAssert.Is_Active__c);
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart2.Id];
        System.assertEquals('有効', departAssert.Is_Active__c);
        System.Test.stopTest();
    }
    
    // 病院更新１
    static testMethod void testUpdate1_1() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
        acc.Is_Active__c = '有効';
        update acc;
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Is_Active__c = '無効';
        //update acc;
        //System.assertEquals(false, NFM001Controller.debug_msg.startsWith('1,NFM001_callout_logic_delete'));
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Is_Active__c = '有効';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Site = 'Katsu テスト Site';
        //acc.Is_Active__c = '有効';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Phone = '0312345678';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Fax = '0387654321';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.OCM_Category__c = 'Katsu テスト OCM_Category__c';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
    }
    static testMethod void testUpdate1_2() {
        List<RecordType> rects1_2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_2.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_2[0].Id;
        acc.Name = 'Katsu KatsuKatsuテスト';
        acc.Site = 'koKatsu';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Site = 'Katsu テスト Site';
        acc.Is_Active__c = '有効';
        update acc;
        Test.stopTest();
    }
    static testMethod void testUpdate1_3() {
         List<RecordType> rects1_3 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_3.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_3[0].Id;
        acc.Name = 'Katsu テKatsuスト';
        acc.Site = 'kook';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Fax = '0387654321';
        acc.Is_Active__c = '有効';
        update acc;
        //System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    static testMethod void testUpdate1_4() {
         List<RecordType> rects1_4 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_4.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_4[0].Id;
        acc.Name = 'Katsu atsu テステスト';
        acc.Site = 'kook';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Phone = '0312345678';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    

    // 病院更新２
    static testMethod void testUpdate2_1() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        Test.startTest();
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Postal_Code__c = '1-3-56';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }

    static testMethod void testUpdate2_2() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Town__c = 'Katsu Town__c';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    static testMethod void testUpdate2_3() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Street__c = 'Katsu テスト Street__c';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    
    // 病院.Name更新
    static testMethod void testUpdate3() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};

        // update
        company1.Name = 'Katsu1';
        company2.Name = 'Katsu2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update hps;

        // assert
        company1 = [Select Id, Name,Sys_Dept_Name_Change_Chk__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name,Sys_Dept_Name_Change_Chk__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, Sys_Dept_Name_Change_Chk__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, Sys_Dept_Name_Change_Chk__c from Account where Id = :depart2.Id];
        System.assertEquals('Katsu1', company1.Name);
        //System.assertEquals(company1.Name + ' ' + dc1s[0].Department_Class_Label__c, dc1s[0].Name);
        //System.assertEquals(company1.Name + ' ' + dc1s[0].Department_Class_Label__c + ' ' + depart1.Department_Name__c, depart1.Name);
        //System.assertEquals(false, company1.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, dc1s[0].Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, depart1.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals('Katsu2', company2.Name);
        //System.assertEquals(company2.Name + ' ' + dc2s[1].Department_Class_Label__c, dc2s[1].Name);
        //System.assertEquals(company2.Name + ' ' + dc2s[1].Department_Class_Label__c + ' ' + depart2.Department_Name__c, depart2.Name);
        //System.assertEquals(false, company2.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, dc2s[1].Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, depart2.Sys_Dept_Name_Change_Chk__c);
    }
    
    // 病院.OCM_Category__c と 診療科.OCM_Category__c の更新
    static testMethod void testUpdate4() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company1.OCM_Category__c = 'M1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        company2.OCM_Category__c = 'M2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};

        // update HP の OCM_Category__c
        company1.OCM_Category__c = 'L1';
        company2.OCM_Category__c = 'L2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update hps;

        // assert
        company1 = [Select Id, Name, OCM_Category__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, OCM_Category__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, OCM_Category__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, OCM_Category__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart2.Id];
        System.assertEquals('L1', company1.OCM_Category__c);
        System.assertEquals('L2', company2.OCM_Category__c);
        System.assertEquals('M1', dc1s[0].OCM_Category__c);
        System.assertEquals('M2', dc2s[1].OCM_Category__c);
        System.assertEquals('M2', depart1.OCM_Category__c);
        System.assertEquals('M2', depart2.OCM_Category__c);
        System.assertEquals('', NFM001Controller.debug_msg);
        
        // update 診療科 の OCM_Category__c
        // 実際は 診療科を直接更新しない、ここは、NFM001 送信対象になってないことを確認
        depart1.OCM_Category__c = 'D1';
        depart2.OCM_Category__c = 'D2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update new Account[] {depart1, depart2};
        depart1 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart2.Id];
        
        // assert
        System.assertEquals('', NFM001Controller.debug_msg);
        System.assertEquals('D1', depart1.OCM_Category__c);
        System.assertEquals('D2', depart2.OCM_Category__c);
    }
    
    // State_Master__c 変更のテスト
    // 病院.State_Master__c と 診療科.State_Master__c の更新
    static testMethod void testUpdate5() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        // RecordType re = new RecordType (id = '01210000000QemGAAS');
        // List<RecordType> rectCo = new List<RecordType>();
        // rectCo.add(re);
        if (rectCo.size() == 0) {
            return;
        }

        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Address_Level__c al1_1 = new Address_Level__c(
            Name = '四川省'
        );
        Address_Level__c al1_2 = new Address_Level__c(
            Name = '上海市'
        );
        insert new List<Address_Level__c> {al1_1, al1_2};
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company1.OCM_Category__c = 'M1';
        company1.State_Master__c = al1_1.Id;
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        company2.OCM_Category__c = 'M2';
        company2.State_Master__c = al1_2.Id;
        List<Account> hps = new Account[] {company1, company2};
        insert hps;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};

        // assert
        company1 = [Select Id, Name, State_Text__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, State_Text__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart2.Id];
        System.assertEquals('四川省', company1.State_Text__c);
        System.assertEquals('上海市', company2.State_Text__c);
        System.assertEquals('四川省', dc1s[0].State_Text__c);
        System.assertEquals('上海市', dc2s[1].State_Text__c);
        System.assertEquals('四川省', depart1.State_Text__c);
        System.assertEquals('上海市', depart2.State_Text__c);

        // update HP の State_Master__c
        company1.State_Master__c = al1_2.Id;
        company1.Is_Active__c = '有効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        hps = new Account[] {company1, company2};
        update hps;

        // assert
        company1 = [Select Id, Name, State_Text__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, State_Text__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart2.Id];
        System.assertEquals('四川省', company1.State_Text__c);
        System.assertEquals('上海市', company2.State_Text__c);
        System.assertEquals('四川省', dc1s[0].State_Text__c);
        System.assertEquals('上海市', dc2s[1].State_Text__c);
        System.assertEquals('四川省', depart1.State_Text__c);
        System.assertEquals('上海市', depart2.State_Text__c);
        System.assertEquals('', NFM001Controller.debug_msg);
    }
    /**
     *@param
     *@description 经销商医院名字更新
     *@description 病院。コンタクト、ユーザーなどの初期データ作る
     *@return 
     */
    static testMethod void testUpdateAgencyName() {
        Account account1 = new Account();
        Account account2 = new Account();
        Contact contact2 = new Contact();
        User user = new User();
        Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;

        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
        List<Account> acc2list=[select RecordTypeId, Name from Account];
        System.assertEquals(10,acc2list.size());
        // 取引先責任者
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        List<Contact> contact2list=[select Id, FirstName from Contact];
        System.assertEquals('責任者',contact2list[0].FirstName);
        // ユーザー用户
        Profile p = [select Id from Profile where Name = '901_经销商社区普通权限_2重验证(ET)'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com';
        user.Alias = 'テユ';
        user.responsibility__c = '华北营业一部';
        user.OCM_Management_Province__c = '北京';
        user.CommunityNickname = 'テストユーザー';
        insert user;
        List<user> users = [select Id, Name, LastName, FirstName from User where LastName='テスト'];
        System.assertEquals('ユーザー',users[0].FirstName );
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        insert agency_hospital_link;
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        account1.Name = 'test医院1';
        Test.startTest();        
        update account1;
        Test.stopTest();
        List<Agency_Hospital_Link__c> agencylistCheck = [select Id, Name from Agency_Hospital_Link__c ];
        System.assertEquals(1, agencylistCheck.size());
        System.assertEquals('test1医院', agencylistCheck[0].Name);        

    }
    // Resendtest
    static testMethod void resend_test() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        insert acc;
        System.assertEquals('', NFM001Controller.debug_msg);
        
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
        System.Test.startTest();
        update acc;
        System.Test.stopTest();
        List<BatchIF_Log__c> rowbl = [Select Id,
                                            Log__c,
                                            Log2__c,
                                            ErrorLog__c,
                                            retry_cnt__c
                                        from BatchIF_Log__c
                                        where Type__c = 'NFM001'
                                        and RowDataFlg__c = true
                                        order by CreatedDate desc];
        System.assertEquals(0, rowbl.size());
        //System.assertEquals(1,rowbl[0].retry_cnt__c);
        System.debug('sizeis' + rowbl.size());
        //System.debug('countis' + rowbl[0].retry_cnt__c);

         //NFM001Controller.execute(rowbl[0],null);
        //List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.assertEquals(2, bl[0].retry_cnt__c);

        //NFM001Controller.execute(rowbl[0],null);
        //bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.assertEquals(3, bl[0].retry_cnt__c);
    }
@isTest
    static void resend_testok() {
        // 病院 の recode type を取得
        Test.setMock(WebServiceMock.class, new NFMTest_Mock());
        System.debug('ccccc');
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        System.debug('pppppp');
        insert acc;
        System.debug('oooooo');
        System.assertEquals('', NFM001Controller.debug_msg);
          System.debug('dddddd');
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
  System.debug('eeeeee');
        System.Test.startTest();
        update acc;
        System.Test.stopTest();
  System.debug('ffffff');
        List<BatchIF_Log__c> rowbl = [Select Id,
                                            Log__c,
                                            Log2__c,
                                            ErrorLog__c,
                                            retry_cnt__c
                                        from BatchIF_Log__c
                                        where Type__c = 'NFM001'
                                        and RowDataFlg__c = true
                                        order by CreatedDate desc];
        System.assertEquals(0, rowbl.size());
        //System.assertEquals(0,rowbl[0].retry_cnt__c);

        //Test.setMock(HttpCalloutMock.class, new NFMTestMock());
        //NFM001Controller.execute(rowbl[0],null);
        //System.Test.stopTest();
        //System.debug(rowbl[0]);
        //List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.debug(bl[0]);

        //System.assertEquals(0, bl[0].retry_cnt__c);

    }
    @isTest
    static void accountUpdateName() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }

        List<RecordType> rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCo.size() == 0) {
            return;
        }

        //经销商
        Account myAccount1 = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        RecordTypeId = rectCo[0].Id,
                                        //start
                                        Delete_Flag__c = false,
                                        Sales_Shop_Class__c = '特约经销商(区域)',
                                        AgencyContract_Management_Code__c = '8075802',
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(+1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(+1),
                                        Ban_On_Use_Date__c = Date.today().addDays(+1),
                                        DGLicenseTo__c = Date.today().addDays(+1),
                                        Ban_On_Use_DateDD__c = Date.today().addDays(+1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(+1),
                                        Is_Active__c = '有効',
                                        OCM_Category__c = 'H0');
        insert myAccount1;
        myAccount1.AgencyContract_Management_Code__c = '8075803';
        myAccount1.Dealer_discount__c = 30;
        update myAccount1;
        //经销商有效合同
        Account myAccount2 = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract[0].Id,
                                            Delete_Flag__c = false,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(+1),
                                            Agent_Ref__c =myAccount1.Id,
                                            ParentId =myAccount1.Id);
        insert myAccount2;
        myAccount2.Contract_Decide_Start_Date__c = Date.today().addDays(+2);
        update myAccount2;

        //NFM001Controller.isRunning = false;
        //Boolean isChanged = false;
        //Boolean needChild = true;
        myAccount1.Name = 'Testaccount0012';
        System.Test.StartTest();
        update myAccount1;
        Account acc = [select Name,OCM_Category__c,id,Agency_With_EffectiveContract__c,Sap2sfdcDealers_ModifycationLogo__c from Account where id = :myAccount1.Id ];
        Account acd = [select Is_Active_Formula__c from Account where id =:myAccount2.id];
        System.assertEquals('Testaccount0012',acc.Name);
        System.assertEquals('有效',acd.Is_Active_Formula__c);
        System.Test.StopTest();

    }
   
}