@isTest
private class UserFaultInfoHandlerTest {

    public static TS_Repair__c tsr;
    public static TS_Repair__c tsr2;
    public static BusinessActivity__c ba1;
    public static BusinessActivity__c ba2;
    public static BusinessActivity__c ba3;
    public static BusinessActivity__c ba4;

    public static User_FaultInfo__c uf1;
    public static User_FaultInfo__c uf2;
    public static User_FaultInfo__c uf3;

    public static FailureAsset__c fa1;
    public static FailureAsset__c fa2;
    public static FailureAsset__c fa3;
    static void basicData() {

        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name ='SSBG DEALER IE'];
        Account dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c='000000';
        insert dealer;
        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id);
        insert core;
        User testUser = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = '00e28000001sZE6',TimeZoneSidKey='Asia/Shanghai', UserName='testUser01@prectech.com');

        uf1 = new User_FaultInfo__c();
        // uf1.ASSIGNED_TO__c = UserD().Id;
        // uf1.Interactions__c = 'TEST01';
        insert uf1;

        tsr = new TS_Repair__c();
        tsr.TSReportDate__c = Date.today();
        insert tsr;    
        tsr2 = new TS_Repair__c();
        tsr2.TSReportDate__c = Date.today();
        insert tsr2;
        fa1 = new FailureAsset__c();
        fa1.model__c = 'test1';
        fa1.FAUserFaultInfo__c = uf1.Id;
        insert fa1;
        ba1 = new BusinessActivity__c();
        ba1.BusinessATS__c = tsr.Id;
        ba1.workingHours__c = 2;
        ba1.BusinessAApprovalOpinion__c = '同意';
        ba1.User_FaultInfo__c = uf1.Id;
        insert ba1;
        ba4 = new BusinessActivity__c();
        ba4.BusinessATS__c = tsr.Id;
        ba4.workingHours__c = 7;
        ba4.BusinessAApprovalOpinion__c = '';
        ba4.User_FaultInfo__c = uf1.Id;
        insert ba4;

        uf2 = new User_FaultInfo__c();
        insert uf2;
        fa2 = new FailureAsset__c();
        fa2.model__c = 'test2';
        fa2.FAUserFaultInfo__c = uf2.Id;
        insert fa2;
        ba2 = new BusinessActivity__c();
        ba2.BusinessATS__c = tsr2.Id;
        ba2.workingHours__c = 2;
        ba2.User_FaultInfo__c = uf2.Id;
        insert ba2;

        uf3 = new User_FaultInfo__c();
        insert uf3;
        fa3 = new FailureAsset__c();
        fa3.model__c = 'test3';
        fa3.FAUserFaultInfo__c = uf3.Id;
        insert fa3;
        ba3 = new BusinessActivity__c();
        ba3.BusinessATS__c = tsr2.Id;
        ba3.workingHours__c = 2;
        ba3.User_FaultInfo__c = uf3.Id;
        insert ba3;

        Attachment att = new Attachment(
            Name = 'ORDER-123456.pdf',
            parentId = ba1.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;

        Attachment att2 = new Attachment(
            Name = 'not-ORDER-123456.pdf',
            parentId = ba2.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;

    }
    static testMethod void testMethod1() {
        basicData();
        User user2 = UserD();
        uf1.ASSIGNED_TO__c = user2.Id;
        uf1.SEND_TO_CUSTOMER__c = true;
        uf1.INTERNAL_ONLY__c = true;
        uf1.Interactions__c = 'TEST';
        update uf1;
    }

    static testMethod void testMethod2() {
        // basicData();
        // User user2 = UserD();
        // uf1.ASSIGNED_TO__c = user2.Id;
        // uf1.SEND_TO_CUSTOMER__c = true;
        // uf1.INTERNAL_ONLY__c = true;
        // // uf1.Interactions__c = 'TEST';
        // update uf1;

        User_FaultInfo__c ufn = new User_FaultInfo__c();
        ufn.ASSIGNED_TO__c = UserD().Id;
        ufn.Interactions__c = 'TEST01';
        insert ufn;
    }
    

    public static User UserD(){
        User user2 = new User();
        user2.LastName = 'test2';
        user2.FirstName = 'test2';
        user2.Alias = 'test2';
        user2.Email = 'test2@test.com';
        user2.Username = 'Xin_Prectech@olympus.com.cn';
        user2.CommunityNickname = 'test2';
        user2.IsActive = true;
        user2.EmailEncodingKey = 'ISO-2022-JP';
        user2.TimeZoneSidKey = 'Asia/Tokyo';
        user2.LocaleSidKey = 'ja_JP';
        user2.LanguageLocaleKey = 'ja';
        user2.ProfileId = '00e28000001sZE6';//System.Label.SystemAdmin;
        insert user2;

        return user2;
    }    
}