@isTest
private class SBG005RestTest {

	static Product2 testSBG005Init() {

        Product2 prd = new Product2();
        prd.Product_ECCode__c     = 'testSBG005';
        prd.ProductCode            = 'testSBG005';
        prd.Name                   = 'testSBG005';
        prd.IsActive               = true;
        insert prd;
        //Pricebook2 prdbook     = new Pricebook2();

        Pricebook2 prdbook1 = new Pricebook2(
            Name = 'testSBG005',
            ProductSegment__c  = 'BS',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'dealer',
            MachineParts__c = 'Machine',
            isActive = true);

        insert prdbook1;
        Pricebook2 prdbook2 = new Pricebook2(
            Name = 'testSBG005',
            ProductSegment__c  = 'BS',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true);

        insert prdbook2;

        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = ControllerUtil.getStandardPricebook().Id;
        pbe.Product2Id      = prd.Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        insert pbe;
        PricebookEntry pbe1 = new PricebookEntry();
        pbe1.Pricebook2Id = ControllerUtil.getStandardPricebook().Id;
        pbe1.Product2Id      = prd.Id;
        pbe1.UnitPrice       = 0;
        pbe1.CurrencyIsoCode = 'USD';
        pbe1.IsActive        = true;
        insert pbe1;

        return prd;

    }

	@isTest
	static void testSBG005_void(){
		testSBG005Init();

		// null
        // SBG005Rest.SBG005(null);

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();
        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        // Monitoringをセットしない
        // SBG005Rest.SBG005(GeDatas);

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';
        // 必須項目をセットしない
        // SBG005Rest.SBG005(GeDatas);

        // 商品コードが存在しない
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '02';
        GeData.ProductCode  = 'testSBG005X';
        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        // BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        // SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        Product2[] rslts = [select Id from Product2 where Product_ECCode__c = :GeData.ProductCode];
        System.assertEquals(0, rslts.size());
	}

    @isTest
	static void testSBG005_update() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;

        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);

        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
	                                 EffectiveDateTo__c,
	                                 ProductModels__c,
	                                 ProductStatus__c
	                            from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
    }

    @isTest
	static void testSBG005_date() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;

        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
	                                 EffectiveDateTo__c,
	                                 ProductModels__c,
	                                 ProductStatus__c
	                            from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


    @isTest
    static void testSBG005_dateA() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;
        GeData.DealerDistinguish   = 'A';

        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


     @isTest
    static void testSBG005_date0() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;



        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        pbe.UseStandardPrice = false;






        insert pbe;






        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);

        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


     @isTest
    static void testSBG005_date1() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121203';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;



        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        Date dateFrom2 = NFMUtil.parseStr2Date('20131202', false);
        Date dateTo2 = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1__c = dateFrom;
        pbe.EffectiveDateTo1__c = dateTo;
        pbe.EffectiveDateFrom2__c = dateFrom2;
        pbe.EffectiveDateTo2__c = dateTo2;
        pbe.UseStandardPrice = false;







        insert pbe;













        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


     @isTest
    static void testSBG005_date2() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121202';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;



        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1__c = dateFrom;
        pbe.EffectiveDateTo1__c = dateTo;
        pbe.UseStandardPrice = false;






        insert pbe;









        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }







      @isTest
    static void testSBG005_dateA0() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;
        GeData.DealerDistinguish = 'A';



        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        pbe.UseStandardPrice = false;






        insert pbe;






        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);

        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


     @isTest
    static void testSBG005_dateA1() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121203';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;
        GeData.DealerDistinguish = 'A';



        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        Date dateFrom2 = NFMUtil.parseStr2Date('20131202', false);
        Date dateTo2 = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1_sales_A__c = dateFrom;
        pbe.EffectiveDateTo1_sales_A__c = dateTo;
        pbe.EffectiveDateFrom2_sales_A__c = dateFrom2;
        pbe.EffectiveDateTo2_sales_A__c = dateTo2;
        pbe.UseStandardPrice = false;







        insert pbe;













        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }


     @isTest
    static void testSBG005_dateA2() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121202';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;
        GeData.DealerDistinguish = 'A';


        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1_sales_A__c = dateFrom;
        pbe.EffectiveDateTo1_sales_A__c = dateTo;
        pbe.UseStandardPrice = false;






        insert pbe;









        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }



     @isTest
    static void testSBG005_date3() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121202';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;


        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1__c = dateFrom;
        pbe.EffectiveDateTo1__c = dateTo;
        pbe.EffectiveDateFrom2__c = dateFrom;
        pbe.EffectiveDateTo2__c = dateTo;
        pbe.UseStandardPrice = false;






        insert pbe;









        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }



     @isTest
    static void testSBG005_dateA3() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121203';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';
        GeData.ProductSegment     = 'BS';
        GeData.MachineParts       = 'Machine';
        GeData.SalesChannel       = 'direct';
        GeData.TradeType          = 'Taxation';
        GeData.SalesPrice          = 100;
        GeData.DealerDistinguish = 'A';


        List<Product2> rslts1 = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2];
        System.debug(rslts1);




        Pricebook2 p2 = [Select Id,Name From Pricebook2 Where SalesChannel__c = 'direct'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = p2.Id;
        pbe.Product2Id      = rslts1[0].Id;
        pbe.UnitPrice       = 0;
        pbe.CurrencyIsoCode = 'CNY';
        pbe.IsActive        = true;
        pbe.KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD';
        Date dateFrom = NFMUtil.parseStr2Date('20131202', false);
        Date dateTo = NFMUtil.parseStr2Date('20191204', true);
        Date dateFrom1 = NFMUtil.parseStr2Date('20131201', false);
        Date dateTo1 = NFMUtil.parseStr2Date('20191204', true);
        pbe.EffectiveDateFrom1_sales_A__c = dateFrom;
        pbe.EffectiveDateTo1_sales_A__c = dateTo;
        pbe.EffectiveDateFrom2_sales_A__c = dateFrom;
        pbe.EffectiveDateTo2_sales_A__c = dateTo;
        pbe.UseStandardPrice = false;






        insert pbe;









        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        List<PricebookEntry>  pres = [Select Id,Name,Pricebook2Id,Product2Id,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,
                                 EffectiveDateTo1__c,EffectiveDateTo2__c,CostPrice1__c,CostPrice2__c,SalesPrice1__c,SalesPrice2__c
                                 from PricebookEntry Where KEY__c = 'BS_Taxation_direct_Machine_testSBG005_USD'];
        List<Pricebook2> pr =   [Select Id,Name,ProductSegment__c,TradeType__c,SalesChannel__c,MachineParts__c
                                 from Pricebook2 Where Id = :pres[0].Pricebook2Id];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);
        System.assertEquals(pr[0].ProductSegment__c,'BS');
        System.assertEquals(pr[0].TradeType__c,'Taxation');
        System.assertEquals(pr[0].SalesChannel__c,'direct');
        System.assertEquals(pr[0].MachineParts__c,'Machine');
    }






    @isTest
    static void testSBG005_resend() {
        Product2 prd = testSBG005Init();
        prd.EffectiveDateTo__c = date.newinstance(2012, 12, 1);
        update prd;

        SBG005Rest.GeData GeData = new SBG005Rest.GeData();
        SBG005Rest.GeDatas GeDatas = new SBG005Rest.GeDatas();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();


        GeDatas.GeData = new SBG005Rest.GeData[] { GeData };
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '32';
        GeData.ProductCode  = 'testSBG005';
        GeData.Currency_x         = 'USD';
        GeData.EffectiveDateFrom  = '20121201';
        GeData.EffectiveDateTo    = '29121204';
        GeData.PurposeOfAdvice    = '2';

        System.Test.startTest();
        // SBG005Rest.SBG005(GeDatas);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG005', GeDatas.GeData);
        SBG005Rest.main(rowData.Id);
        System.Test.stopTest();

        List<Product2> rslts = [select Id, ProductCode, Product_ECCode__c,
                                     EffectiveDateTo__c,
                                     ProductModels__c,
                                     ProductStatus__c
                                from Product2
                                where Product_ECCode__c = :GeData.ProductCode];
        // deleteの場合、EffectiveDateTo が前日
        System.assertEquals(1, rslts.size());
        System.assertEquals(date.newinstance(2012, 12, 01), rslts[0].EffectiveDateTo__c);

        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'SBG005'
                                        order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

        // SBGITMWebService.execute(rowbl[0].id);
        SBGITMRest.main(rowbl[0].id);

        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 = :rowbl[0].id];
        System.assertEquals(1, bl.size());
        System.assertEquals(0, bl[0].retry_cnt__c);
    }

    @isTest static void test_method_Send() {

    Test.startTest();

    RestRequest req = new RestRequest();
    RestResponse res = new RestResponse();

    // String JsonMsg = '{"GeDatas": {"Monitoring": {"Tag": "MSGH","Sender": 1330,"Receiver": 1592,"MessageType": "SBG004","MessageGroupNumber": 331591,"NumberOfRecord": 1,"TransmissionDateTime": 201908081115},"GeData": [{"TradeType": "Taxation","SalesChannel": "99","PurposeOfAdvice": "3","ProductSegment": "IE","ProductCode": "N5702700","Other3": "","Other2": "","Other1": "","MachineParts": "Machine","EffectiveDateTo": "99991231","EffectiveDateFrom": "20190801","Currency_x": "RMB","CostPrice": 12241.00}]}}';
    String JsonMsg = '{"GeDatas": {"Monitoring": {"Tag": "MSGH"},"GeData": [{"TradeType": "Taxation"}]}}';
    req.requestURI = 'services/apexrest/SBG005/execute';
    req.httpMethod = 'POST';
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;

    SBG005Rest.execute();

    Test.stopTest();

    }
}