@isTest
private class LeadTriggerTest {
    // -------------------------------------------------------------------------
    // リードと取引先責任者を関連付けるトリガーのテスト
    // -------------------------------------------------------------------------
    // 以下、ターゲットとなるトリガーの機能
    //   メールアドレスが一致する contactを探します。
    //   複数見つかる場合は、最終更新日が一番新しい contactに関連付けます。
    //   メールアドレスが null のリードは無視します。
    // 以下、トリガーのロジック
    //   メアド一覧を得る
    //   メアド一覧を元に contactを得る
    //   更新対象のリードに、contactへの参照をセットする

    public static Contact createContact( String inputName, String inputEmail) {
        Contact ret = new Contact();
        ret.LastName = inputName;
        ret.Email = inputEmail;
        ret.Firstname='ZZ2';
        // insert ret;
        // ret = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Id = :ret.Id];
        return ret;
    }

    public static Account createAccount( String recordTypeId1, String recordTypeId2 ) {
        Account acc = new Account();
        acc.RecordTypeId = recordTypeId1;
        acc.Name = 'HP test1';
        insert acc;

        List<Account> dept = [select Id, Name from Account where ParentId = :acc.Id and Department_Class_Label__c IN ('消化科', '呼吸科') order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = recordTypeId2;
        depart1.Name         = '*';
        depart1.Department_Name__c  = 'Gastoro Intestin Test';
        depart1.ParentId            = dept[0].Id;
        depart1.Department_Class__c = dept[0].Id;
        depart1.Hospital__c         = acc.Id;
        insert depart1;

        acc = [SELECT Id, OwnerId, Department_Class__c FROM Account WHERE Id = :depart1.Id];
        return acc;
    }

    public static Lead createLead( String inputName, String inputEmail) {
        Lead ret = createLeadData( inputName, inputEmail);
        insert ret;
        ret = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :ret.Id];
        return ret;
    }

    public static Lead createLeadData( String inputName, String inputEmail) {
        Lead ret = new Lead();
        ret.LastName = inputName;
        ret.Email = inputEmail;
        ret.company = 'TestCompany';
        return ret;
    }

    private static testMethod void test01() {
        // コンタクトを3件作成する
        // リードを3件作成する
        // リード3件が、コンタクト3件と一対一で対応することを確認
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con02_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con03_20140519@test.com'); 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User local = new User(Test_staff__c = true,Employee_No__c ='ocm99999', 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);
        local.Job_Category__c = '销售推广';
        local.Province__c = '北京市';
        insert local;

        Lead lead01 = createLead( 'con01', 'con01_20140519@test.com');
        lead01.SI_PromoteInquiry__c = true;
        lead01.OwnerId = local.Id;
        update lead01;
        Lead lead02 = createLead( 'con02', 'con02_20140519@test.com');
        Lead lead03 = createLead( 'con03', 'con03_20140519@test.com');
        
        //System.assertEquals( con01.Id, lead01.Contact__c);
        //System.assertEquals( con02.Id, lead02.Contact__c);
        //System.assertEquals( con03.Id, lead03.Contact__c);
    }

    private static testMethod void test02() {
        // コンタクトを3件作成する
        // リードを1件作成する
        // リード1件が、コンタクト1件と対応していることを確認する
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con02_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con03_20140519@test.com'); 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User local2 = new User(Test_staff__c = true,Employee_No__c ='ocm99998', LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner2@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        local2.Job_Category__c = '销售推广';
        local2.Province__c = '辽宁省';
        insert local2;
        
        Lead lead01 = createLead( 'con01', 'con01_20140519@test.com');
        lead01.SI_PromoteInquiry__c = true;
        lead01.OwnerId = local2.Id;
        update lead01;
        
        //System.assertEquals( con01.Id, lead01.Contact__c);
    }

    private static testMethod void test03() {
        // 同じアドレスのコンタクトを3件作成する
        // リードを1件作成する
        // リード3件が、コンタクト3件と一対一で対応することを確認
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con01_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con01_20140519@test.com'); 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User local3 = new User(Test_staff__c = true,Employee_No__c ='ocm99997', LastName = 'hp3', FirstName = 'owner', Alias = 'hp3', CommunityNickname = 'hpOwner3', Email = 'olympus_hpowner3@sunbridge.com', Username = 'olympus_hpowner3@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        local3.Job_Category__c = '销售推广';
        local3.Province__c = '湖北省';
        insert local3;

        Lead lead01 = createLead( 'con01', 'con01_20140519@test.com');
        lead01.SI_PromoteInquiry__c = true;
        lead01.OwnerId = local3.Id;
        update lead01;
        
        //System.assertEquals( con03.Id, lead01.Contact__c);
    }

    private static testMethod void test04() {
        // コンタクトを3件作成する
        // リードを3件作成する
        // リード3件が、コンタクト3件と一対一で対応しないことを確認する
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con02_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con03_20140519@test.com'); 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User local = new User(Test_staff__c = true,Employee_No__c ='ocm99999', 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);
        local.Job_Category__c = '销售推广';
        local.Province__c = '四川省';
        insert local;

        User local2 = new User(Test_staff__c = true,Employee_No__c ='ocm99998', LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner2@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        local2.Job_Category__c = '销售推广';
        local2.Province__c = '浙江省';
        insert local2;
        
        Lead lead01 = createLead( 'con01', 'con11_20140519@test1.com');
        lead01.SI_PromoteInquiry__c = true;
        lead01.OwnerId = local.Id;
        update lead01;
        Lead lead02 = createLead( 'con02', 'con12_20140519@test1.com');
        lead02.SI_PromoteInquiry__c = true;
        lead02.OwnerId = local2.Id;
        update lead02;
        Lead lead03 = createLead( 'con03', 'con13_20140519@test1.com');
        
        //System.assertEquals( null, lead01.Contact__c);
        //System.assertEquals( null, lead02.Contact__c);
        //System.assertEquals( null, lead03.Contact__c);
    }
    
    private static testMethod void test05() {
        // コンタクトを3件作成する
        // リードを3件、一度に作成する
        // リード3件が、コンタクト3件と一対一で対応することを確認
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con02_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con03_20140519@test.com'); 
        
        Lead lead01 = createLeadData( 'con01', 'con01_20140519@test.com');
        Lead lead02 = createLeadData( 'con02', 'con02_20140519@test.com');
        Lead lead03 = createLeadData( 'con03', 'con03_20140519@test.com');
        List<Lead> leadList = new List<Lead>();
        leadList.add( lead01);
        leadList.add( lead02);
        leadList.add( lead03); 
        insert leadList;
        lead01 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead01.Id];
        lead02 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead02.Id];
        lead03 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead03.Id]; 
        
        //System.assertEquals( con01.Id, lead01.Contact__c);
        //System.assertEquals( con02.Id, lead02.Contact__c);
        //System.assertEquals( con03.Id, lead03.Contact__c);
    }
    
    private static testMethod void test06() {
        // コンタクトを3件作成する
        // リードを3件、一度に作成する
        // リード3件が、コンタクト3件と一対一で対応することを確認
        // スルーされることを確認
        
        // コンタクト3件作成
        // Contact con01 = createContact( 'con01', 'con01_20140519@test.com');
        // Contact con02 = createContact( 'con02', 'con02_20140519@test.com');
        // Contact con03 = createContact( 'con03', 'con03_20140519@test.com'); 
        
        Lead lead01 = createLeadData( 'con01', 'con01_20140519@test.com');
        Lead lead02 = createLeadData( 'con02', 'con02_20140519@test.com');
        Lead lead03 = createLeadData( 'con03', 'con03_20140519@test.com');
        
        StaticParameter.EscapeLeadTrigger = true;
        
        List<Lead> leadList = new List<Lead>();
        leadList.add( lead01);
        leadList.add( lead02);
        leadList.add( lead03); 
        insert leadList;
        lead01 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead01.Id];
        lead02 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead02.Id];
        lead03 = [SELECT Id, FirstName, LastName, Email, Contact__c FROM Lead WHERE Id = :lead03.Id]; 
        
        //System.assertEquals( null, lead01.Contact__c);
        //System.assertEquals( null, lead02.Contact__c);
        //System.assertEquals( null, lead03.Contact__c);
    }

    private static testMethod void test07() {
        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];

        Account acc = createAccount(rectCo.Id, rectDpt[0].Id);

        Lead lead = new Lead();
        lead.LastName = 'con01';
        lead.Email = 'con01_20140519@test.com';
        lead.company = 'TestCompany';
        lead.Department_Class__c = acc.Department_Class__c;
        insert lead;
        lead.owner_not_automatically_update__c = true;
        update lead;
    }
}