@isTest
private class AttachmentTriggerTest {
    static void setupTestData() {
        OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = Date.today().addDays(1), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc2 = new OlympusCalendar__c(Date__c = Date.today().addDays(2), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc3 = new OlympusCalendar__c(Date__c = Date.today().addDays(3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc4 = new OlympusCalendar__c(Date__c = Date.today().addDays(4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc5 = new OlympusCalendar__c(Date__c = Date.today().addDays(5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc6 = new OlympusCalendar__c(Date__c = Date.today().addDays(6), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc7 = new OlympusCalendar__c(Date__c = Date.today().addDays(7), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc8 = new OlympusCalendar__c(Date__c = Date.today().addDays(8), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc9 = new OlympusCalendar__c(Date__c = Date.today().addDays(9), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc10 = new OlympusCalendar__c(Date__c = Date.today().addDays(10), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc11 = new OlympusCalendar__c(Date__c = Date.today().addDays(11), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc12 = new OlympusCalendar__c(Date__c = Date.today().addDays(12), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc13 = new OlympusCalendar__c(Date__c = Date.today().addDays(-1), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc14 = new OlympusCalendar__c(Date__c = Date.today().addDays(-2), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc15 = new OlympusCalendar__c(Date__c = Date.today().addDays(-3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc16 = new OlympusCalendar__c(Date__c = Date.today().addDays(-4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc17 = new OlympusCalendar__c(Date__c = Date.today().addDays(-5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);

        insert new OlympusCalendar__c[] {oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12,oc13,oc14,oc15,oc16,oc17};
    }
    static testMethod void myTest1() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account accIE = new Account(
            Name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer IE',
            FacilityName__c='abc',
            PostCode__c='000000'
        );
        insert accIE; 
        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accIE.Id;
        con.MobilePhone = 'MobilePhoneD__c';
        con.OtherPhone = 'OtherPhoneD__c';
        con.Fax = 'FaxD__c';
        con.Email = 'EmailD__c@test.com';
        con.Phone = 'PhoneD__c';
        con.Title = 'TitleD__c';
        con.Address1__c = 'Address1D__c';
        con.Address2__c = 'Address2D__c';
        con.Address3__c = 'Address3D__c';
        con.Postcode__c = '100111';
        con.ContactStatus__c = 'ContactStatusD__c';
        con.CancelReason__c = 'CancelReasonD__c';
        con.StatusD__c = 'Pass';
        insert con;
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE'
        );
        insert pricebook;
         
        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        // 有预定下单日
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accIE.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct'
        );
        insert opp;
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;
        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            EndUser__c = con.Id
        );
        insert odr;

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

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

        delete att;
    }

     static testMethod void myTest2() {
        setupTestData();
        // Implement test code
        List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        
        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
        loaner.DEMO_PURPOSE__C = '演示';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已出库指示';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;
        Attachment att = new Attachment(
            Name = '借用协议-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;
        Attachment att2 = new Attachment(
            Name = '装机确认-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;

        Attachment att3 = new Attachment(
            Name = '收货确认-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att3;
        
        try{
        Attachment att4 = new Attachment(
            Name = '借用协议-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att4;
        }catch(Exception ex){
            try{
                Attachment att21 = new Attachment(
                    Name = '装机确认-123456.pdf',
                    parentId = loaner.Id,
                    Body = EncodingUtil.base64Decode('test')
                );
                insert att21;
            }catch(Exception ex1){
                try{
                    Attachment att31 = new Attachment(
                        Name = '收货确认-123456.pdf',
                        parentId = loaner.Id,
                        Body = EncodingUtil.base64Decode('test')
                    );
                    insert att31;
                }catch(Exception ex2){
                    return;
                }
            } 
        } 
     }


      static testMethod void myTest3() {
        setupTestData();
        // Implement test code
        List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        
        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
        loaner.DEMO_PURPOSE__C = '演示';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已出库指示';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;
        Attachment att = new Attachment(
            Name = '借用协议-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;
        Attachment att2 = new Attachment(
            Name = '装机确认-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;

        Attachment att3 = new Attachment(
            Name = '收货确认-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att3;

        delete att;
        delete att2;
        delete att3;
    }
     static testMethod void myTest4() {
        setupTestData();
        // Implement test code
        List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        
        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
        loaner.DEMO_PURPOSE__C = '演示';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已出库指示';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        loaner.HP_Received_Sign_Date__c  = Date.today();
        loaner.Bollow_Date__c = Date.today();
        insert loaner;
        Attachment att = new Attachment(
            Name = '借用协议-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;
        Attachment att2 = new Attachment(
            Name = '装机确认-123456.pdf',
            parentId = loaner.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;
        try{
            delete att;
        }catch(Exception ex){
            try{
                delete att2;
            }catch(Exception ex1){
                return;
            } 
        }
    }
    // 业务活动 附件上传
    static testMethod void myTest5() {

        TS_Repair__c tsr = new TS_Repair__c();
        tsr.TSReportDate__c = Date.today();
        insert tsr; 

        User_FaultInfo__c uf1 = new User_FaultInfo__c();
        insert uf1;
        FailureAsset__c fa1 = new FailureAsset__c();
        fa1.model__c = 'test1';
        fa1.FAUserFaultInfo__c = uf1.Id;
        insert fa1;
        Attachment att3 = new Attachment(
            Name = 'not-ORDER-123456.pdf',
            parentId = uf1.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att3;
        
        BusinessActivity__c  ba1 = new BusinessActivity__c();
        ba1.BusinessATS__c = tsr.Id;
        ba1.User_FaultInfo__c = uf1.Id;
        insert ba1;

        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 = ba1.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;

        

    }
    static testMethod void myTest6() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account accIE = new Account(
            Name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer IE',
            FacilityName__c='abc',
            PostCode__c='000000'
        );
        insert accIE; 
        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accIE.Id;
        con.MobilePhone = 'MobilePhoneD__c';
        con.OtherPhone = 'OtherPhoneD__c';
        con.Fax = 'FaxD__c';
        con.Email = 'EmailD__c@test.com';
        con.Phone = 'PhoneD__c';
        con.Title = 'TitleD__c';
        con.Address1__c = 'Address1D__c';
        con.Address2__c = 'Address2D__c';
        con.Address3__c = 'Address3D__c';
        con.Postcode__c = '100111';
        con.ContactStatus__c = 'ContactStatusD__c';
        con.CancelReason__c = 'CancelReasonD__c';
        con.StatusD__c = 'Pass';
        insert con;
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE'
        );
        insert pricebook;
         
        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        // 有预定下单日
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accIE.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            customerType__c = 'B'
        );
        insert opp;
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;
        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            EndUser__c = con.Id
        );
        insert odr;
        TradeCustomerManager__c tra = new TradeCustomerManager__c(
            OrderIdSave__c = odr.Id,
            order__c =  odr.Id
        );
        insert tra;
        Attachment att1 = new Attachment(
            Name = '军事.pdf',
            parentId = tra.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att1;
        Attachment att2 = new Attachment(
            Name = '个人信息.pdf',
            parentId = tra.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;
        att1.Name = 'test.pdf';
        update att1;
        delete att1;
    }
}