@isTest
private class SBG018RestTest {

    public static ProductInventory__c productInventory;
    public static void init() {
        Product2 prd1 = testSBG018Init();
        productInventory = new ProductInventory__c();
        productInventory.TransforDate__c = Date.today() ;
        productInventory.ProductCode__c = prd1.Id;
        productInventory.Plant__c = 'CCTV' ;
        productInventory.ProductSegment__c = 'LS' ;
        productInventory.ProductType__c = 'SP';
        productInventory.InventoryQuantity__c = 22;
        productInventory.TradeType__c = 'Taxation';
        productInventory.ManagementCode__c = prd1.ProductCode +'_LS_SP';
        productInventory.InventoryCode__c =prd1.ProductCode +'_LS_SP_CCTV_Taxation';

        insert productInventory;

    }
    static Product2 testSBG018Init() {

        Product2 prd = new Product2();
        prd.Product_ECCode__c     = 'testSBG018';
        prd.ProductCode            = 'testSBG018';
        prd.Name                   = 'testSBG018';
        prd.IsActive               = true;
        insert prd;
        Pricebook2 prdbook1 = new Pricebook2(
            Name = 'testSBG018',
            ProductSegment__c  = 'BS',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'dealer',
            MachineParts__c = 'Machine',
            isActive = true);

        insert prdbook1;
        Pricebook2 prdbook2 = new Pricebook2(
            Name = 'testSBG018',
            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 testSBG018_void() {

        init();

        SBG018Rest.GeData GeData = new SBG018Rest.GeData();
        SBG018Rest.GeDatas GeDatas = new SBG018Rest.GeDatas();
        GeDatas.GeData = new SBG018Rest.GeData[] { GeData };
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';
        SBG018Rest.GeDataDetails GeDataDetail01 = new  SBG018Rest.GeDataDetails();
        SBG018Rest.GeDataDetails GeDataDetail02 = new  SBG018Rest.GeDataDetails();
        GeData.GeDataDetails = new  SBG018Rest.GeDataDetails[] {GeDataDetail01, GeDataDetail02};
        GeData.Status = 'Z5';
        GeData.TransforDate = String.valueOf(Date.today());//'20210113';
        GeDataDetail01.ProductCode = 'testSBG018';
        GeDataDetail01.Plant = 'CCTV';
        GeDataDetail01.Department = 'LS';
        GeDataDetail01.FGSP = 'SP';
        GeDataDetail01.Qty = '11';
        GeDataDetail01.TradeType = 'Taxation';
        GeDataDetail02.ProductCode = 'testSBG018';
        // 用于触发异常 进入Catch
        GeDataDetail02.Plant = 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.Plant += 'CCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTVCCTV';
        GeDataDetail02.TradeType = 'Taxation';

        GeDataDetail02.Department = 'LS';
        GeDataDetail02.FGSP = 'FG';
        GeDataDetail02.Qty = '11';
        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG018', GeDatas.GeData);
        SBG018Rest.main(rowData.Id);

        System.Test.stopTest();

    }


    @isTest static void testSBG018_void2() {

        init();

        SBG018Rest.GeData GeData = new SBG018Rest.GeData();
        SBG018Rest.GeDatas GeDatas = new SBG018Rest.GeDatas();
        GeDatas.GeData = new SBG018Rest.GeData[] { GeData };
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';
        SBG018Rest.GeDataDetails GeDataDetail01 = new  SBG018Rest.GeDataDetails();
        SBG018Rest.GeDataDetails GeDataDetail02 = new  SBG018Rest.GeDataDetails();
        GeData.GeDataDetails = new  SBG018Rest.GeDataDetails[] {GeDataDetail01, GeDataDetail02};
        GeData.Status = 'Z5';
        GeData.TransforDate = String.valueOf(Date.today());//'20210113';
        GeDataDetail01.ProductCode = 'testSBG018';
        GeDataDetail01.Plant = 'CCTV';
        GeDataDetail01.Department = 'LS';
        GeDataDetail01.FGSP = 'SP';
        GeDataDetail01.Qty = '11';
        GeDataDetail01.TradeType = 'Taxation';
        GeDataDetail02.ProductCode = 'testSBG018';
        // 用于触发异常 进入Catch
        GeDataDetail02.Plant = 'CC31';

        GeDataDetail02.Department = 'LS';
        GeDataDetail02.FGSP = 'FG';
        GeDataDetail02.Qty = '11';
        GeDataDetail02.TradeType = 'Taxation';
        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBG018', GeDatas.GeData);
        SBG018Rest.main(rowData.Id);

        System.Test.stopTest();

    }
    // 测试SBG018Rest execute部分 以及必填字段的验证覆盖
    @isTest static void test_execute() {
        testSBG018Init();
        Test.startTest();
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"GeDatas":{"Monitoring":{"Tag":"MSGH","Sender":1330,"Receiver":1592,"MessageType":"SBG018","MessageGroupNumber":570968,"NumberOfRecord":18,"TransmissionDateTime":202101131323},"GeData":[{"Status":"Z5","TransforDate":20210203,"GeDataDetails":[{"ProductCode":"","Plant":"CC20","Department":"IE","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000001100500","Plant":"CC40","Department":"","FGSP":"FG","Qty":"0.000"},{"ProductCode":"000000000001100500","Plant":"","Department":"IE","FGSP":"SP","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC19","Department":"LS","FGSP":"","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC20","Department":"IE","FGSP":"FG","Qty":"1.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC40","Department":"IE","FGSP":"FG","Qty":"","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC41","Department":"LS","FGSP":"FG","Qty":"Test","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC42","Department":"IE","FGSP":"SP","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC46","Department":"LS","FGSP":"SP","Qty":"6.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003914083","Plant":"CC54","Department":"LS","FGSP":"FG","Qty":"11.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC19","Department":"LS","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC20","Department":"IE","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC40","Department":"IE","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC41","Department":"LS","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC42","Department":"IE","FGSP":"SP","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"000000000003980500","Plant":"CC54","Department":"LS","FGSP":"FG","Qty":"0.000","TradeType":"Tax Exemption"},{"ProductCode":"Q0500040","Plant":"CC22","Department":"NDT","FGSP":"FG","Qty":"0.000","TradeType":"Taxation"},{"ProductCode":"Q0500040","Plant":"CC45","Department":"NDT","FGSP":"FG","Qty":"0.000","TradeType":""}]}]}}';
        req.requestURI = 'services/apexrest/SBG018/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response = res;

        SBG018Rest.execute();

        Test.stopTest();
    }
    // static testMethod void testMethod1() {

    // }
}