@isTest
public with sharing class NFM209RestTest {
     static testMethod void testMethod1() {
        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'TradeComplianceStatus__c';
        transfer.External_Value__c = 'W';
        transfer.Internal_Value__c = '白名单';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'TradeComplianceStatus__c';
        transfer.External_Value__c = 'B';
        transfer.Internal_Value__c = '黑名单';
        transfers.add(transfer);
        insert transfers;
        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.Name         = 'NFM209TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_GI').getRecordTypeId();
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;

        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM209TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // company = [select Id, Name,Management_Code_Auto__c from Account
        //                         where Id = :company.Id];
        company = [select Id, Name from Account
        where Id = :company.Id];
        NFM209Rest.GeDatas GeDatas = new NFM209Rest.GeDatas();
        NFM209Rest.GeData GeData = new NFM209Rest.GeData();
        GeDatas.GeData = new NFM209Rest.GeData[]{GeData};

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';


        // GeData.custCode = company.Management_Code_Auto__c;                 
        GeData.complStatus = 'B';                 
        GeData.orgSysId='123';                   
        GeData.glbBusiNo='123';                  
        GeData.custTp='123';                      
        GeData.entityTp='123';                  
        GeData.custName='123';                    
        //System.Test.startTest();
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM209', GeDatas.GeData);
        NFM209Rest.executefuture(rowData.Id);

        rowData = NFMUtil.saveRowData(Monitoring, 'NFM209', GeDatas.GeData);
        NFM209Rest.executefuture(rowData.Id);
}

 static testMethod void testMethod2() {
    	//Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SAP","Receiver":"SFDC","NumberOfRecord":"1","MessageType":"NFM117","MessageGroupNumber":"20210000005088"},"GeData":[{"custCode":"8098008","complStatus":"黑名单","orgSysId":"00987","glbBusiNo":"001","custTp":"Person","entityTp":"经销商","custName":"大连东控睿康医疗管理有限公司"}]}';
        req.requestURI = 'services/apexrest/NFM209/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM209Rest.execute();

        //Test.stopTest();
    }


    static testMethod void testMethod3(){
        // テストデータ
        System.runAs(new User(Id = Userinfo.getUserId())) {
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(Oly_TriggerHandler.class.getName());
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'TradeComplianceStatus__c';
        transfer.External_Value__c = 'W';
        transfer.Internal_Value__c = '白名单';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'TradeComplianceStatus__c';
        transfer.External_Value__c = 'B';
        transfer.Internal_Value__c = '黑名单';
        transfers.add(transfer);
        insert transfers;
        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.Name         = 'NFM209TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_GI').getRecordTypeId();
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;

        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM209TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;


        Contact contact2 = new Contact();
        contact2.AccountId = depart.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        //user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Dept__c='医疗华北营业本部';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();


        Product2 productA = new Product2( Name = 'テスト商品',
                                              Fixture_Model_No__c = 'n01');
            insert productA;
        Fixture_Set__c fixtures1 = new Fixture_Set__c();
            fixtures1.Name = 'set1';
            fixtures1.Loaner_name__c = 'name1';
            insert new Fixture_Set__c[] {fixtures1};


        Fixture_Set_Detail__c fsd1 = new Fixture_Set_Detail__c();
            fsd1.Product2__c = productA.Id;
            fsd1.Fixture_Set__c = fixtures1.Id;
            fsd1.Is_Body__c = true;
            fsd1.Is_Optional__c = false;
            fsd1.UniqueKey__c = fixtures1.Id + ':' + productA.Id;
            fsd1.SortInt__c = 1;

            insert new Fixture_Set_Detail__c[] {fsd1};

        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Person_In_Charge__c = user.Id;
        raObj.applyUser__c = user.Id;
        raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal());
        raObj.Asset_loaner_start_day__c = Date.today();
        raObj.Asset_loaner_closed_day__c = Date.today().addDays(10);
        raObj.Request_return_day__c = Date.today() + 30;
        raObj.Demo_purpose_text__c = '申请理由Test';
        raObj.Request_demo_detail__c = 'IT测试';
        raObj.Accsessary__c = '水气按钮';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = '其他';
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = company.Id;
        raObj.Strategic_dept__c = depart.Id;
        raObj.Account__c = depart.Id;
        raObj.Request_shipping_day__c = Date.toDay();  // 希望到货日
        raObj.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        // 预定归还日自动生成 预定归还日=（希望到货日+希望借用天数）-1
        // raObj.Request_return_day__c = Date.toDay();  // 预定归还日
        // B.OLY_OCM-669 入力规则: 希望借用天数不能为空
        raObj.Phone_number__c = '1234567890';
        raObj.Loaner_medical_Staff__c = contact2.Id;
        insert raObj;

        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
            raesObj.Rental_Apply__c = raObj.Id;
            raesObj.Fixture_Set__c = fixtures1.Id;
            raesObj.Cancel_Select__c = false;
            raesObj.Rental_Start_Date__c = Date.toDay();
            raesObj.Rental_End_Date__c = Date.toDay();
            raesObj.IndexFromUniqueKey__c = 1;
            raesObj.UniqueKey__c = '1:'+ fixtures1.Id + ':1';
            insert raesObj;
            Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();

            raesdObj1.Rental_Apply__c = raObj.Id;
            raesdObj1.Fixture_Set_Detail__c = fsd1.Id;
            raesdObj1.Rental_Num__c = 1;
            raesdObj1.Queue_Number__c = null;
            raesdObj1.Is_Body__c = true;
            raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj1.IndexFromUniqueKey__c = 1;
            raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsd1.Id + ':1';
            raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
            raesdObj1.FSD_Is_Optional__c = false;
            raesdObj1.FSD_Is_OneToOne__c = false;
            raesdObj1.ApplyPersonAppended__c = false;
            raesdObj1.FSD_Fixture_Model_No__c = 'n01';
            raesdObj1.Fixture_Model_No_text__c = 'n01';
            raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj1.Product_category_text__c = 'GI';
            raesdObj1.Equipment_Type_text__c = '产品试用';
            raesdObj1.Cancel_Select__c = false;
            insert raesdObj1;

        // company = [select Id, Name,Management_Code_Auto__c from Account
        //                         where Id = :company.Id];
        company = [select Id, Name from Account
                                where Id = :company.Id];
        NFM209Rest.GeDatas GeDatas = new NFM209Rest.GeDatas();
        NFM209Rest.GeData GeData = new NFM209Rest.GeData();
        GeDatas.GeData = new NFM209Rest.GeData[]{GeData};

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';


        // GeData.custCode = company.Management_Code_Auto__c;                 
        GeData.complStatus = 'B';                 
        GeData.orgSysId='123';                   
        GeData.glbBusiNo='123';                  
        GeData.custTp='123';                      
        GeData.entityTp='123';                  
        GeData.custName='123';                    
        //System.Test.startTest();
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM209', GeDatas.GeData);
        NFM209Rest.executefuture(rowData.Id);

        rowData = NFMUtil.saveRowData(Monitoring, 'NFM209', GeDatas.GeData);
        NFM209Rest.executefuture(rowData.Id);  
        }          
    }
}