@isTest
private class NFM108RestTest {
    static testMethod void testNFM108Repair_void() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM108TestCompany';
        upsert company;
        Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM108TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // 再取得
        List<Account> accList = new List<Account>();
        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // accList.add(company);
        // section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        // accList.add(section);
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        // accList.add(depart);
        company = [select Management_Code__c, Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c, Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c, Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM108Prd1';
        prd1.ProductCode            = 'NFM108Prd1';
        prd1.Repair_Product_Code__c = 'NFM108Prd1_RP';
        prd1.Name                   = 'NFM108Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM108Prd2';
        prd2.ProductCode            = 'NFM108Prd2';
        prd2.Repair_Product_Code__c = 'NFM108Prd2_RP';
        prd2.Name                   = 'NFM108Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                             = 'NFM108Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM108SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;
        // ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
        //                          from Asset
        //                         where Id = :ast.Id];
        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
                                 from Asset
                                where Id = :ast.Id];

        Repair__c rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM108Rpr1';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        insert rpr;
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id];

        // そのままreturn
        // null
        //NFM108Rest.NFM108(null);
        
        NFM108Rest.Payment Payment         = new NFM108Rest.Payment();
        NFM108Rest.GeneralData GeneralData = new NFM108Rest.GeneralData();
        Payment.GeneralData          = new NFM108Rest.GeneralData[] { GeneralData };

        // Monitoringをセットしない
        //NFM108Rest.NFM108(Payment);
        
        Datetime nowDT = Datetime.now();
        String nowStr  = nowDT.format('yyyyMMddHHmm');
        Payment.Monitoring = new NFMUtil.Monitoring();
        Payment.Monitoring.MessageGroupNumber = nowStr + '01';
        //// 必須項目をセットしない
        ////NFM108Rest.NFM108(Payment);
       NFMUtil.Monitoring Monitoring = Payment.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM108', Payment.GeneralData);
        NFM108Rest.main(rowData.Id);


        // PaymentSource Undefined
        Payment.Monitoring.MessageGroupNumber = nowStr + '02';
        GeneralData.SFDCRepairNo = 'ABC';
        //NFM108Rest.NFM108(Payment);

         //System.Test.startTest();
        //NFMUtil.Monitoring Monitoring = Payment.Monitoring;
        rowData = NFMUtil.saveRowData(Monitoring, 'NFM108', Payment.GeneralData);
        NFM108Rest.main(rowData.Id);

        List<BatchIF_Log__c> iflogList = [select Id, RowDataFlg__c, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = :Payment.Monitoring.MessageGroupNumber];
        System.assertEquals(2, iflogList.size());
        System.assertEquals(true, iflogList[0].RowDataFlg__c);
        System.assertEquals('[{"SFDCRepairNo":"ABC","PaymentSource":null,"PaymentDate":null,"PaymentAmount":null,"BaddebtsFlg":null,"ArrearsBasePaymentTerm":null,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]', iflogList[0].Log__c);

        // PaymentSource Undefined
        NFMUtil.MaxLogColumnLength = 2;
        Payment.Monitoring.MessageGroupNumber = nowStr + '03';
        GeneralData.SFDCRepairNo = 'ABC';
         rowData = NFMUtil.saveRowData(Monitoring, 'NFM108', Payment.GeneralData);
        NFM108Rest.main(rowData.Id);
        //NFM108Rest.NFM108(Payment);
        iflogList = [select Id, RowDataFlg__c, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c from BatchIF_Log__c where MessageGroupNumber__c = :Payment.Monitoring.MessageGroupNumber];
System.debug('iflogList=' + iflogList);
        System.assertEquals(2, iflogList.size());
        System.assertEquals(true, iflogList[0].RowDataFlg__c);
        System.assertEquals('[{"SFDCRepairNo":"ABC","PaymentSource":null,"PaymentDate":null,"PaymentAmount":null,"BaddebtsFlg":null,"ArrearsBasePaymentTerm":null,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]',
                iflogList[0].Log__c + iflogList[0].Log2__c + iflogList[0].Log3__c + iflogList[0].Log4__c + iflogList[0].Log5__c + iflogList[0].Log6__c + iflogList[0].Log7__c
                + iflogList[0].Log8__c + iflogList[0].Log9__c + iflogList[0].Log10__c + iflogList[0].Log11__c + iflogList[0].Log12__c + iflogList[0].ErrorLog__c);
    }

     //rawdata より Insert テスト
    static testMethod void testExecute_insert() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM108TestCompany';
        upsert company;
        Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM108TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // 再取得
        List<Account> accList = new List<Account>();
        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // accList.add(company);
        // section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        // accList.add(section);
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        // accList.add(depart);
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c,  Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c,  Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM108Prd1';
        prd1.ProductCode            = 'NFM108Prd1';
        prd1.Repair_Product_Code__c = 'NFM108Prd1_RP';
        prd1.Name                   = 'NFM108Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM108Prd2';
        prd2.ProductCode            = 'NFM108Prd2';
        prd2.Repair_Product_Code__c = 'NFM108Prd2_RP';
        prd2.Name                   = 'NFM108Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                             = 'NFM108Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM108SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;
        // ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
        //                          from Asset
        //                         where Id = :ast.Id];
        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c,  Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
                                 from Asset
                                where Id = :ast.Id];

        Repair__c rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM108SAP';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        insert rpr;
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id];
        String strSFDCRepairNo = rpr.Name + ':123';

        // rawdata
        BatchIF_Log__c rawData = new BatchIF_Log__c(
            RowDataFlg__c = true,
            MessageGroupNumber__c = 'MG_1234567890',
            Log__c = '[{"SFDCRepairNo":"' + strSFDCRepairNo + '","PaymentSource":"维修单","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]'
        );
        insert rawData;

        // Test & assert
        Test.startTest();
        NFM108Rest.executefuture(rawData.Id);
        Test.stopTest();
        List<BatchIF_Log__c> iflogList = [select Id, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = 'MG_1234567890' and RowDataFlg__c = false];
        System.assertEquals(1, iflogList.size());
        System.assertEquals(null, iflogList[0].ErrorLog__c);
//        System.assertEquals(null, iflogList[0].Log__c);
        Repair_receipt__c rc_Res = [select Id, Name, SFDCRepairNo__c, PaymentVoucher__c from Repair_receipt__c where UniqueKey__c = :strSFDCRepairNo];
        System.assertEquals(strSFDCRepairNo, rc_Res.Name);
        System.assertEquals(rpr.Name, rc_Res.SFDCRepairNo__c);
        System.assertEquals('123', rc_Res.PaymentVoucher__c);
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c, Received_Date__c, BaddebtsDate__c from Repair__c where Id = :rpr.Id];
        System.assertEquals(Date.newInstance(2016, 1, 1), rpr.Received_Date__c);
        System.assertEquals(Date.newInstance(2016, 1, 1), rpr.BaddebtsDate__c);
    }

    // rawdata より Resend テスト
    static testMethod void testExecute_resend() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM108TestCompany';
        upsert company;
        Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM108TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // 再取得
        List<Account> accList = new List<Account>();
        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // accList.add(company);
        // section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        // accList.add(section);
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        // accList.add(depart);
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c,  Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c,  Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM108Prd1';
        prd1.ProductCode            = 'NFM108Prd1';
        prd1.Repair_Product_Code__c = 'NFM108Prd1_RP';
        prd1.Name                   = 'NFM108Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM108Prd2';
        prd2.ProductCode            = 'NFM108Prd2';
        prd2.Repair_Product_Code__c = 'NFM108Prd2_RP';
        prd2.Name                   = 'NFM108Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                             = 'NFM108Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM108SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;
        // ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
        //                          from Asset
        //                         where Id = :ast.Id];
        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c,  Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
                                 from Asset
                                where Id = :ast.Id];

        Repair__c rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM108SAP';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        insert rpr;
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id];
        String strSFDCRepairNo = rpr.Name + ':123';

        // rawdata
        BatchIF_Log__c rawData = new BatchIF_Log__c(
            RowDataFlg__c = true,
            retry_cnt__c = 1,
            MessageGroupNumber__c = 'MG_1234567890',
            Log__c = '[{"SFDCRepairNo":"' + strSFDCRepairNo + '","PaymentSource":"维修单","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]'
        );
        insert rawData;

        // Test & assert
        Test.startTest();
        NFM108Rest.executefuture(rawData.Id);
        Test.stopTest();
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
                Type__c, Log__c, ErrorLog__c,retry_cnt__c
                from BatchIF_Log__c
                where Id = :rawData.id];
        System.assertEquals(0, bl[0].retry_cnt__c);
        List<BatchIF_Log__c> iflogList = [select Id, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = 'MG_1234567890' and RowDataFlg__c = false];
        System.assertEquals(1, iflogList.size());
        System.assertEquals(null, iflogList[0].ErrorLog__c);
//        System.assertEquals(null, iflogList[0].Log__c);
        Repair_receipt__c rc_Res = [select Id, Name, SFDCRepairNo__c, PaymentVoucher__c from Repair_receipt__c where UniqueKey__c = :strSFDCRepairNo];
        System.assertEquals(strSFDCRepairNo, rc_Res.Name);
        System.assertEquals(rpr.Name, rc_Res.SFDCRepairNo__c);
        System.assertEquals('123', rc_Res.PaymentVoucher__c);
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c, Received_Date__c, BaddebtsDate__c from Repair__c where Id = :rpr.Id];
        System.assertEquals(Date.newInstance(2016, 1, 1), rpr.Received_Date__c);
        System.assertEquals(Date.newInstance(2016, 1, 1), rpr.BaddebtsDate__c);
    }

    static testMethod void testExecute2() {
    	ControllerUtil.EscapeNFM001Trigger = true;
    	StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM108TestCompany';
        upsert company;
        Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM108TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // 再取得
        List<Account> accList = new List<Account>();
        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // accList.add(company);
        // section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        // accList.add(section);
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        // accList.add(depart);
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c,  Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c,  Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM108Prd1';
        prd1.ProductCode            = 'NFM108Prd1';
        prd1.Repair_Product_Code__c = 'NFM108Prd1_RP';
        prd1.Name                   = 'NFM108Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM108Prd2';
        prd2.ProductCode            = 'NFM108Prd2';
        prd2.Repair_Product_Code__c = 'NFM108Prd2_RP';
        prd2.Name                   = 'NFM108Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                             = 'NFM108Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM108SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;
        // ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
        //                          from Asset
        //                         where Id = :ast.Id];
        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c,  Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
                                 from Asset
                                where Id = :ast.Id];

        Repair__c rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM108SAP';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        insert rpr;
        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id];
        String strSFDCRepairNo = rpr.Name + ':123';

        Maintenance_Contract__c mc1 = new Maintenance_Contract__c();
        mc1.Name = 'mc1';
        mc1.Past_update_contract__c = true;
        mc1.RecordTypeId = '01210000000QjeFAAS';
        mc1.CurrencyIsoCode = 'CNY';
        mc1.Status__c = '契約';
        mc1.Hospital__c = company.Id;
        mc1.Department_Class__c = section.Id;
        mc1.Department__c = depart.Id;
        mc1.Maintenance_Contract_No__c = 'mc1';
        mc1.Contract_Conclusion_Date__c = Date.today();
        mc1.Contract_End_Date__c = Date.today().addDays(1);
        mc1.SalesOfficeCode_selection__c = '北京RC';
        insert mc1;

        // rawdata
        BatchIF_Log__c rawData = new BatchIF_Log__c(
            RowDataFlg__c = true,
            MessageGroupNumber__c = 'MG_1234567890',
            Log__c = '[{"SFDCRepairNo":"mc1","PaymentSource":"维修合同","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]'
        );
        insert rawData;

        // Test & assert
        Test.startTest();
        NFM108Rest.executefuture(rawData.Id);
        Test.stopTest();
    }

    @isTest 
    static void test_method_Execute() {

        Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Payment": {"Monitoring": {"Tag": "MSGH","Sender": "8405","Receiver": "1330","MessageType": "NFM108","MessageGroupNumber": "20190000003152","NumberOfRecord": "1","TransmissionDateTime": "2019-10-15","Text": "123"},"GeneralData": [{"SFDCRepairNo": "GZ-RS-GD0050502:1400002546","PaymentSource": "维修合同","PaymentDate": "20180709","PaymentAmount": "178560.00","BaddebtsFlg": "","ArrearsBasePaymentTerm": "0.00","ArrearsBaseInvoice": "0.00","ARAmountBasePaymentTerm": "178560.00","ARAmountBaseInvoice": "178560.00","ARAmountBaseAverage": "4275.86","AccumulativeTotalPaymentAmount": "178560.00","AccumulativeARAmountBaseAverage": "8551.72"}, {"SFDCRepairNo": "GZ-RS-GX0049607:1400002547","PaymentSource": "维修合同","PaymentDate": "20180719","PaymentAmount": "15647.00","BaddebtsFlg": "","ArrearsBasePaymentTerm": "27.00","ArrearsBaseInvoice": "27.00","ARAmountBasePaymentTerm": "15674.00","ARAmountBaseInvoice": "15674.00","ARAmountBaseAverage": "1126.01","AccumulativeTotalPaymentAmount": "15647.00","AccumulativeARAmountBaseAverage": "6756.06"}]}}';
        req.requestURI = 'services/apexrest/NFM108/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM108Rest.execute();

        Test.stopTest();
        //Test.setMock(HttpCalloutMock.class, new NFM205RestCalloutMock());
        //NFM205Rest.execute1('{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM205","MessageGroupNumber":"20180001722190"},"GeData":[{"Opportunity_Code":"HKBJ-GI-BJ0588881","Bidding_No":"as123121","Bidding_Content":"招标内容","Bid_Project_Name":"招标项目名","Bid_Date":"20181203","Authorized_Status":"1","Authorized_Finish_Sales":"30033","Authorized_DB_No":"MGZWESQHN201812001","Authorized_Date":"20190316","Autholization_Activated_Date":"20190415"}]}');
    }

    //@isTest 
    //static void test_method_Abnormal() {
    //	RestRequest req = new RestRequest();
    //    RestResponse res = new RestResponse();

    //    String JsonMsg = '{"Payment": {"Monitoring": {"Tag": "MSGH","Sender": "8405","Receiver": "1330","MessageType": "NFM108","MessageGroupNumber": "20190000003152","NumberOfRecord": "1","TransmissionDateTime": "2019-10-15","Text": "123"},"GeneralData": [{"SFDCRepairNo": "GZ-RS-GD0050502:1400002546","PaymentSource": "naerzz","PaymentDate": "20180709","PaymentAmount": "178560.00","BaddebtsFlg": "","ArrearsBasePaymentTerm": "0.00","ArrearsBaseInvoice": "0.00","ARAmountBasePaymentTerm": "178560.00","ARAmountBaseInvoice": "178560.00","ARAmountBaseAverage": "4275.86","AccumulativeTotalPaymentAmount": "178560.00","AccumulativeARAmountBaseAverage": "8551.72"}, {"SFDCRepairNo": "GZ-RS-GX0049607:1400002547","PaymentSource": "维修合同","PaymentDate": "20180719","PaymentAmount": "15647.00","BaddebtsFlg": "","ArrearsBasePaymentTerm": "27.00","ArrearsBaseInvoice": "27.00","ARAmountBasePaymentTerm": "15674.00","ARAmountBaseInvoice": "15674.00","ARAmountBaseAverage": "1126.01","AccumulativeTotalPaymentAmount": "15647.00","AccumulativeARAmountBaseAverage": "6756.06"}]}}';
    //    req.requestURI = 'services/apexrest/NFM108/execute';
    //    req.httpMethod = 'POST';
    //    req.requestBody = Blob.valueof(JsonMsg);
    //    RestContext.request = req;
    //    RestContext.response= res;

    //    NFM108Rest.execute();
    //}
}