@isTest
private class NFM107RestTest {
    @isTest static void testNFM107Repair() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }


        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM107TestCompany';
        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  = 'NFM107TestDepart';
        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     = 'NFM107Prd1';
        prd1.ProductCode            = 'NFM107Prd1';
        prd1.Repair_Product_Code__c = 'NFM107Prd1_RP';
        prd1.Name                   = 'NFM107Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM107Prd2';
        prd2.ProductCode            = 'NFM107Prd2';
        prd2.Repair_Product_Code__c = 'NFM107Prd2_RP';
        prd2.Name                   = 'NFM107Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;

        Asset ast = new Asset();
        ast.Name                             = 'NFM107Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM107SerialNumber';
        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   = 'NFM107Rpr1';
        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];

        // Account hp1 = new Account(RecordTypeId = rectCo[0].Id, Name = 'testHp1', OwnerId = hpOwner.Id);
        // insert hp1;
        // そのままreturn
        // null
        // NFM107Rest.NFM107(null);
        // NFM107Rest.InvoiceRest InvoiceRest = new NFM107Rest.InvoiceRest();
        // NFM107Rest.Invoice Invoice         = new NFM107Rest.Invoice();
        // NFM107Rest.GeneralData GeneralData = new NFM107Rest.GeneralData();
        // InvoiceRest.Invoice = Invoice;
        // Invoice.GeneralData          = new NFM107Rest.GeneralData[] { GeneralData };

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

        String JsonMsg = '{"Invoice": {"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM104","MessageGroupNumber":"20180001722190"},"GeneralData":[{"SFDCRepairNo":"RS-201910-566388","ReverseFlag":"","PayerNo":"false","InvoiceResource":"维修成本","InvoiceAmount":"","GoldenTaxInvoiceNo":"CostInformation","GoldenTaxDate":"20180716","BilltoPartyNo":"false","AccuTotalInvoiceAmount":"","AccuTotalCost":"44172.65"}]}}';
        req.requestURI = 'services/apexrest/NFM107/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM107Rest.execute();

        JsonMsg = '{"Invoice": {"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM104","MessageGroupNumber":"20180001722190"},"GeneralData":[{"SFDCRepairNo":"RS-201910-566388","ReverseFlag":"","PayerNo":"","InvoiceResource":"维修单","InvoiceAmount":"","GoldenTaxInvoiceNo":"CostInformation","GoldenTaxDate":"20180716","BilltoPartyNo":"","AccuTotalInvoiceAmount":"","AccuTotalCost":"44172.65"}]}}';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        // RestContext.response= res;

        NFM107Rest.execute();
        Test.stopTest();
    }

    @isTest static void testNFM107Repair1() {
        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) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }


        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM107TestCompany';
        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  = 'NFM107TestDepart';
        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);


        // Account hp1 = new Account(RecordTypeId = rectCo[0].Id, Name = 'testHp1', OwnerId = hpOwner.Id);
        // insert hp1;
        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 = 'RS-201910-566388';
        mc1.Contract_Conclusion_Date__c = Date.today();
        mc1.Contract_End_Date__c = Date.today().addDays(1);
        mc1.SalesOfficeCode_selection__c = '北京RC';
        insert mc1;

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

        String JsonMsg = '{"Invoice": {"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM104","MessageGroupNumber":"20180001722190"},"GeneralData":[{"SFDCRepairNo":"RS-201910-566388","ReverseFlag":"","PayerNo":"","InvoiceResource":"维修合同","InvoiceAmount":"","GoldenTaxInvoiceNo":"CostInformation","GoldenTaxDate":"20180716","BilltoPartyNo":"","AccuTotalInvoiceAmount":"","AccuTotalCost":"44172.65"}]}}';
        req.requestBody = Blob.valueof(JsonMsg);
        
        req.requestURI = 'services/apexrest/NFM107/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM107Rest.execute();
        Test.stopTest();
    }
    // rawdata より upsert テスト
     @isTest  static void testExecute_upsert() {
         List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
         if (rectCo.size() == 0) {
             return;
         }
         List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
         if (rectSct.size() == 0) {
             return;
         }
         List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
         if (rectDpt.size() == 0) {
             return;
         }
         // テストデータ
         Account company = new Account();
         company.RecordTypeId = rectCo[0].Id;
         company.Name         = 'NFM107TestCompany';
         upsert company;
         company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id];
         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  = 'NFM107TestDepart';
         depart.ParentId            = section.Id;
         depart.Department_Class__c = section.Id;
         depart.Hospital__c         = company.Id;
         upsert depart;
         depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id];

         // 再取得
         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     = 'NFM107Prd1';
         prd1.ProductCode            = 'NFM107Prd1';
         prd1.Repair_Product_Code__c = 'NFM107Prd1_RP';
         prd1.Name                   = 'NFM107Prd1';
         prd1.Manual_Entry__c        = false;
         prdList.add(prd1);
         Product2 prd2 = new Product2();
         prd2.ProductCode_Ext__c     = 'NFM107Prd2';
         prd2.ProductCode            = 'NFM107Prd2';
         prd2.Repair_Product_Code__c = 'NFM107Prd2_RP';
         prd2.Name                   = 'NFM107Prd2';
         prd2.Manual_Entry__c        = false;
         prdList.add(prd2);
         insert prdList;

         Asset ast = new Asset();
         ast.Name                             = 'NFM107Ast1';
         ast.AccountId                        = depart.Id;
         ast.Department_Class__c              = section.Id;
         ast.Hospital__c                      = company.Id;
         ast.Product2Id                       = prd1.Id;
         ast.SerialNumber                     = 'NFM107SerialNumber';
         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   = 'NFM107Rpr1';
         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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1';
         // rawdata
         BatchIF_Log__c rawData = new BatchIF_Log__c(
             RowDataFlg__c = true,
             MessageGroupNumber__c = 'MG_1234567890',
             Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null'
                		+ ',"PayerNo":"' + depart.Management_Code__c + '"'
             		+ ',"InvoiceResource":"维修单","InvoiceAmount":null'
             		+ ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"'
             		+ ',"GoldenTaxDate":null'
             		+ ',"BilltoPartyNo":"00' + company.Management_Code__c + '"'
             		+ ',"AccuTotalInvoiceAmount":null,"AccuTotalCost":null}]'
         );
         insert rawData;

          // Test & assert
         Test.startTest();
         NFM107Rest.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, PayerNo__c, PayerNoAccount__c, BilltoPartyNo__c, BilltoPartyNoAccount__c from Repair_receipt__c where UniqueKey__c = :strUniqueKey];
         System.assertEquals('GoldenTaxInvoiceNo1', rc_Res.Name);
         System.assertEquals(depart.Management_Code__c, rc_Res.PayerNo__c);
         System.assertEquals('00' + company.Management_Code__c, rc_Res.BilltoPartyNo__c);
         System.assertEquals(depart.Id, rc_Res.PayerNoAccount__c);
         System.assertEquals(company.Id, rc_Res.BilltoPartyNoAccount__c);
     }

//     // rawdata より upsert テスト
//     // PayerNo not exist
     @isTest  static void testExecute_upsert2() {
         List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
         if (rectCo.size() == 0) {
             return;
         }
         List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
         if (rectSct.size() == 0) {
             return;
         }
         List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
         if (rectDpt.size() == 0) {
             return;
         }
         // テストデータ
         Account company = new Account();
         company.RecordTypeId = rectCo[0].Id;
         company.Name         = 'NFM107TestCompany';
         upsert company;
         company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id];
         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  = 'NFM107TestDepart';
         depart.ParentId            = section.Id;
         depart.Department_Class__c = section.Id;
         depart.Hospital__c         = company.Id;
         upsert depart;
         depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id];

         // 再取得
         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     = 'NFM107Prd1';
         prd1.ProductCode            = 'NFM107Prd1';
         prd1.Repair_Product_Code__c = 'NFM107Prd1_RP';
         prd1.Name                   = 'NFM107Prd1';
         prd1.Manual_Entry__c        = false;
         prdList.add(prd1);
         Product2 prd2 = new Product2();
         prd2.ProductCode_Ext__c     = 'NFM107Prd2';
         prd2.ProductCode            = 'NFM107Prd2';
         prd2.Repair_Product_Code__c = 'NFM107Prd2_RP';
         prd2.Name                   = 'NFM107Prd2';
         prd2.Manual_Entry__c        = false;
         prdList.add(prd2);
         insert prdList;

         Asset ast = new Asset();
         ast.Name                             = 'NFM107Ast1';
         ast.AccountId                        = depart.Id;
         ast.Department_Class__c              = section.Id;
         ast.Hospital__c                      = company.Id;
         ast.Product2Id                       = prd1.Id;
         ast.SerialNumber                     = 'NFM107SerialNumber';
         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   = 'NFM107Rpr1';
         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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1';
         // rawdata
         BatchIF_Log__c rawData = new BatchIF_Log__c(
             RowDataFlg__c = true,
             MessageGroupNumber__c = 'MG_1234567890',
             Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null'
                		+ ',"PayerNo":"1' + depart.Management_Code__c + '"'
             		+ ',"InvoiceResource":"维修单","InvoiceAmount":null'
             		+ ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"'
             		+ ',"GoldenTaxDate":null'
             		+ ',"BilltoPartyNo":"' + company.Management_Code__c + '"'
             		+ ',"AccuTotalInvoiceAmount":null,"AccuTotalCost":null}]'
         );
         insert rawData;

          // Test & assert
         Test.startTest();
         NFM107Rest.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.assert((iflogList[0].ErrorLog__c.indexOf('GoldenTaxInvoiceNo1:PayerNo[') >= 0), iflogList[0].ErrorLog__c);
 //        System.assertEquals(null, iflogList[0].Log__c);
         Repair_receipt__c rc_Res = [select Id, Name, PayerNo__c, PayerNoAccount__c, BilltoPartyNo__c, BilltoPartyNoAccount__c from Repair_receipt__c where UniqueKey__c = :strUniqueKey];
         System.assertEquals('GoldenTaxInvoiceNo1', rc_Res.Name);
         System.assertEquals('1' + depart.Management_Code__c, rc_Res.PayerNo__c);
         System.assertEquals(company.Management_Code__c, rc_Res.BilltoPartyNo__c);
         System.assertEquals(null, rc_Res.PayerNoAccount__c);
         System.assertEquals(company.Id, rc_Res.BilltoPartyNoAccount__c);
    }
   
//    // rawdata より update テスト
     @isTest  static void testExecute_update() {
         List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
         if (rectCo.size() == 0) {
             return;
         }
         List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
         if (rectSct.size() == 0) {
             return;
         }
         List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
         if (rectDpt.size() == 0) {
             return;
         }
         // テストデータ
         Account company = new Account();
         company.RecordTypeId = rectCo[0].Id;
         company.Name         = 'NFM107TestCompany';
         upsert company;
         company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id];
         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  = 'NFM107TestDepart';
         depart.ParentId            = section.Id;
         depart.Department_Class__c = section.Id;
         depart.Hospital__c         = company.Id;
         upsert depart;
         depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id];

         // 再取得
         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     = 'NFM107Prd1';
         prd1.ProductCode            = 'NFM107Prd1';
         prd1.Repair_Product_Code__c = 'NFM107Prd1_RP';
         prd1.Name                   = 'NFM107Prd1';
         prd1.Manual_Entry__c        = false;
         prdList.add(prd1);
         Product2 prd2 = new Product2();
         prd2.ProductCode_Ext__c     = 'NFM107Prd2';
         prd2.ProductCode            = 'NFM107Prd2';
         prd2.Repair_Product_Code__c = 'NFM107Prd2_RP';
         prd2.Name                   = 'NFM107Prd2';
         prd2.Manual_Entry__c        = false;
         prdList.add(prd2);
         insert prdList;

         Asset ast = new Asset();
         ast.Name                             = 'NFM107Ast1';
         ast.AccountId                        = depart.Id;
         ast.Department_Class__c              = section.Id;
         ast.Hospital__c                      = company.Id;
         ast.Product2Id                       = prd1.Id;
         ast.SerialNumber                     = 'NFM107SerialNumber';
         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   = 'NFM107Rpr1';
         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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1';
         // rawdata
         BatchIF_Log__c rawData = new BatchIF_Log__c(
             RowDataFlg__c = true,
             retry_cnt__c = 1,
             MessageGroupNumber__c = 'MG_1234567890',
             Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null'
                		+ ',"PayerNo":null'
             		+ ',"InvoiceResource":"维修成本","InvoiceAmount":null'
             		+ ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"'
             		+ ',"GoldenTaxDate":"20160314"'
             		+ ',"BilltoPartyNo":"00' + company.Management_Code__c + '"'
             		+ ',"AccuTotalInvoiceAmount":null'
             		+ ',"AccuTotalCost":"6000.00"}]'
         );
         insert rawData;

          // Test & assert
         Test.startTest();
         NFM107Rest.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__c rc = [select Id, Name, Repair_cost__c, Repair_cost_date__c from Repair__c where Id = :rpr.Id];
         System.assertEquals(6000, rc.Repair_cost__c);
 		System.assertEquals(Date.newInstance(2016,3,14), rc.Repair_cost_date__c);
     }
}