@isTest
private class SBG019RestTest {

    @testSetup
    static void testSBG019RestInit() {

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

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

        insert prdbook2;

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

        insert prdbook3;

        PricebookEntry pbe3 = new PricebookEntry();
        pbe3.Pricebook2Id = ControllerUtil.getStandardPricebook().Id;
        pbe3.Product2Id      = prd3.Id;
        pbe3.UnitPrice       = 0;
        pbe3.CurrencyIsoCode = 'CNY';
        pbe3.IsActive        = true;
        insert pbe3;


        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;

    }

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

        //String JsonMsg = '{"GeDatas":{"Monitoring":{"Tag":"MSGH","Sender":1330,"Receiver":1592,"MessageType":"SBG019","MessageGroupNumber":570985,"NumberOfRecord":2,"TransmissionDateTime":202102021106},"GeData":[{"TransforDate":20210202,"GeDataDetails":[{"ProductCode":"000000000001100500","ProductDate":"20210202"},{"ProductCode":"","ProductDate":""}]}]}}';
        String JsonMsg = '{"GeDatas":{"Monitoring":{"Tag":"MSGH","Sender":1330,"Receiver":1592,"MessageType":"SBG019","MessageGroupNumber":570985,"NumberOfRecord":2,"TransmissionDateTime":202102021106},"GeData":[{"TransforDate":20210202,"GeDataDetails":[{"ProductCode":"testSBG01901","ProductDate":"20210202"},{"ProductCode":"testSBG019","ProductDate":"20210202"},{"ProductCode":"","ProductDate":""},{"ProductCode":"testSBG019","ProductDate":""},{"ProductCode":"testSBG019","ProductDate":"20210202"},{"ProductCode":"123456","ProductDate":"20210202"}]}]}}';
        req.requestURI = 'services/apexrest/SBG019/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response = res;

        SBG019Rest.execute();

        Test.stopTest();
    }

}