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

    static testMethod void myUnitTest() {
        List<Opportunity> opps = new List<Opportunity>();
        
//        Product2 pro = new Product2( Name='テスト商品');
//        insert pro;
        
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest01@sunbridge.com';
        u1.CommunityNickname = 'あ';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.Id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        insert u1;
        
        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.Id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        insert u2;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'aiueo';
        opp1.StageName = 'contact';
        opp1.CloseDate = Date.today();
        opp1.OwnerId = u1.Id;
        opp1.Owner_System__c = u1.Id;
        opps.add(opp1);
        
        Opportunity opp2 = new Opportunity();
        opp2.Name = 'aiueo2';
        opp2.StageName = '引合';
        opp2.CloseDate = Date.today();
        opp2.OwnerId = u2.Id;
        opp2.Owner_System__c = u2.Id;
        opps.add(opp2);
        
        insert opps;
        
        List<Opportunity2__c> opp2_List = [select Id from Opportunity2__c where Opportunity__c IN :opps];
        System.assertEquals(2, opp2_List.size());
       
        Opportunity2__c opp2c = [select Id, Owner_System__c, OwnerId, StageName__c from Opportunity2__c where Opportunity__c = :opp1.Id];
        System.assertEquals(u1.Id, opp2c.Owner_System__c);
        System.assertEquals(u1.Id, opp2c.OwnerId);
        System.assertEquals('contact', opp2c.StageName__c);
        
        opp2c = [select Id, Owner_System__c, OwnerId, StageName__c from Opportunity2__c where Opportunity__c = :opp2.Id];
        System.assertEquals(u2.Id, opp2c.Owner_System__c);
        System.assertEquals(u2.Id, opp2c.OwnerId);
        System.assertEquals('询价', opp2c.StageName__c);
        
        System.Test.startTest();
//        u2 = [select Id from User where Id = :u2.Id];
//        u2.IsActive = false;
//        update u2;
        
        opp2 = [select Id from Opportunity where Id = :opp2.Id];
        opp2.StageName = '敗戦';
//        opp2.Competitor_Product1__c = pro.Id;
        //opp2.Lost_Reason__c = 'フォロー不足';
        update opp2;
        
        opp2c = [select Id, StageName__c, Owner_System__c, OwnerId from Opportunity2__c where Opportunity__c = :opp2.Id];
        System.assertEquals('失单', opp2c.StageName__c);
//        System.assertEquals(UserInfo.getUserId(), opp2c.Owner_System__c);
//        System.assertEquals(UserInfo.getUserId(), opp2c.OwnerId);
        
        delete opps;
        opp2_List = [select Id from Opportunity2__c where Opportunity__c IN :opps];
        System.assertEquals(0, opp2_List.size());
        System.Test.stopTest();
    }
}