@isTest
private class SBGITMRestTest {
    public static Product2 prd1;
    public static Product2 prd2;
    public static Product2 prd3;
    public static Product2 prd4;

    static void inittransfersData() { 
        prd1 = new Product2();
        prd1.Product_ECCode__c      = 'testSBG001';
        prd1.ProductCode            = 'SBGITMTst1';
        prd1.Name                   = 'testSBG001';
        prd1.IsActive               = true;
        prd1.MaterialStatus_one_Start__c = Date.today().addDays(-2);
        prd1.MaterialStatus_one_End__c = Date.today().addDays(-1);
        prd1.MaterialStatus_two_Start__c = Date.today().addDays(-3);
        prd1.MaterialStatus_two_End__c = Date.today().addDays(-1);

        prd2 = new Product2();
        prd2.Product_ECCode__c      = 'testSBG002';
        prd2.ProductCode            = 'SBGITMTst2';
        prd2.Name                   = 'testSBG002';
        prd2.IsActive               = true;
        prd2.MaterialStatus_one_Start__c = Date.today().addDays(-2);
        prd2.MaterialStatus_one_End__c = Date.today().addDays(22);
        prd2.MaterialStatus_two_Start__c = Date.today().addDays(-3);
        prd2.MaterialStatus_two_End__c = Date.today().addDays(-1);

        prd3 = new Product2();
        prd3.Product_ECCode__c      = 'testSBG002';
        prd3.ProductCode            = 'SBGITMTst22';
        prd3.Name                   = 'testSBG002';
        prd3.IsActive               = true;
        prd3.MaterialStatus_one_Start__c = Date.today().addDays(-2);
        prd3.MaterialStatus_one_End__c = Date.today().addDays(22);
        prd3.MaterialStatus_two_Start__c = Date.today().addDays(-3);
        prd3.MaterialStatus_two_End__c = Date.today().addDays(122);

        prd4 = new Product2();
        prd4.Product_ECCode__c      = 'testSBG002';
        prd4.ProductCode            = 'SBGITMTst222';
        prd4.Name                   = 'testSBG002';
        prd4.IsActive               = true;
        prd4.MaterialStatus_one_Start__c = Date.today().addDays(-2);
        prd4.MaterialStatus_one_End__c = Date.today().addDays(22);
        prd4.MaterialStatus_two_Start__c = Date.today().addDays(-3);
        prd4.MaterialStatus_two_End__c = Date.today().addDays(21);
        insert new Product2[] {prd1, prd2,prd3,prd4};
    }
    //@isTest
    static void testSBGITM_insert() {
        inittransfersData();
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMRest.GeDatas GeDatas = new SBGITMRest.GeDatas();
        SBGITMRest.GeData GeData = new SBGITMRest.GeData();
        SBGITMRest.GeData GeData2 = new SBGITMRest.GeData();
        GeDatas.GeData = new SBGITMRest.GeData[] { GeData, GeData2 };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst3';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';
        GeData.Exemption = '1';
        GeData.IsRadiant = '1';
        GeData.StatusEffectiveDateFrom = '20200522';


        // insert2
        GeData2.ProductCode                = 'SBGITMTst4';
        GeData2.ProductDescription                 = 'ABCD12345:ABC';
        GeData2.ProductStatus                    = '0';
        GeData2.EffectiveDateTo                      = '20121101';

        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBGITM', GeDatas.GeData);
        SBGITMRest.executefuture(rowData.Id);
        System.Test.stopTest();
        Product2[] rslts = [select Id, Name,
                            ProductCode,
                            Product_ECCode__c,
                            EffectiveDateTo__c,
                            ProductModels__c,
                            ProductStatus__c
                            from Product2
                            where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst3', rslts[0].ProductCode);
        }
        rslts = [select Id,
                 ProductCode, Name,
                 Product_ECCode__c,
                 EffectiveDateTo__c,
                 ProductModels__c,
                 ProductStatus__c
                 from Product2
                 where Product_ECCode__c = 'ABCD12345'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst4', rslts[0].ProductCode);
            System.assertEquals('ABC', rslts[0].Name);
        }
    }

    //@isTest
    static void testSBGITM_update() {
        inittransfersData();
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMRest.GeDatas GeDatas = new SBGITMRest.GeDatas();
        SBGITMRest.GeData GeData = new SBGITMRest.GeData();
        GeDatas.GeData = new SBGITMRest.GeData[] { GeData };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';
        GeData.Exemption = '1';
        GeData.IsRadiant = '1';
        GeData.StatusEffectiveDateFrom = '20200522';

        // insert
        GeData.ProductCode                = 'SBGITMTst2';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';
        GeData.Exemption = '1';
        GeData.IsRadiant = '1';
        GeData.StatusEffectiveDateFrom = '20200522';

        // insert
        GeData.ProductCode                = 'SBGITMTst22';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';
        GeData.Exemption = '1';
        GeData.IsRadiant = '1';
        GeData.StatusEffectiveDateFrom = '20200522';
        
        GeData.ProductCode                = 'SBGITMTst222';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';
        GeData.Exemption = '1';
        GeData.IsRadiant = '1';
        GeData.StatusEffectiveDateFrom = '20200522';
        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBGITM', GeDatas.GeData);
        SBGITMRest.executefuture(rowData.Id);
        System.Test.stopTest();
        Product2[] rslts = [select Id,
                            ProductCode,
                            Product_ECCode__c,
                            EffectiveDateTo__c,
                            ProductModels__c,
                            ProductStatus__c
                            from Product2
                            where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst222', rslts[0].ProductCode);
        }
    }

    //@isTest
    static void testSBGITM_resend() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMRest.GeDatas GeDatas = new SBGITMRest.GeDatas();
        SBGITMRest.GeData GeData = new SBGITMRest.GeData();
        GeDatas.GeData = new SBGITMRest.GeData[] { GeData };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';


        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeDatas.Monitoring, 'SBGITM', GeDatas.GeData);
        SBGITMRest.executefuture(rowData.Id);
        System.Test.stopTest();
        Product2[] rslts = [select Id,
                            ProductCode,
                            Product_ECCode__c,
                            EffectiveDateTo__c,
                            ProductModels__c,
                            ProductStatus__c
                            from Product2
                            where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst1', rslts[0].ProductCode);
        }
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                      ErrorLog__c
                                      from BatchIF_Log__c
                                      where RowDataFlg__c = true
                                              and Type__c = 'SBGITM'
                                                      order by CreatedDate desc];
        List<BatchIF_Log__c> rowbl1 = [Select Id, Log__c,
                                       ErrorLog__c
                                       from BatchIF_Log__c
                                       where RowDataFlg__c = true
                                               order by CreatedDate desc];
        System.debug(rowbl1);
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

        SBGITMRest.executefuture(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(1, bl[0].retry_cnt__c);
    }



    @isTest static void test_method_Seven() {

        Test.startTest();

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

        String JsonMsg = '{"GeDatas": {"Monitoring": {"Tag": "MSGH","Sender": 1330,"Receiver": 1592,"MessageType": "SBGITM","MessageGroupNumber": 331591,"NumberOfRecord": 1,"TransmissionDateTime": 201903251115},"GeData": [{"ProductStatus": "1","ProductDescription": "SP-KIT-WEDGE-MQIV1072:111 ","ProductCode":"U8831946","Other3": "","Other2": "","Other1": "","EffectiveDateTo": "99991231"}]}}';
        req.requestURI = 'services/apexrest/SBGITM/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response = res;

        SBGITMRest.execute();

        Test.stopTest();

        // { "GeDatas": {"Monitoring": {"Tag": "MSGH","Sender": 1330,"Receiver": 1592,"MessageType": "SBG017", "MessageGroupNumber": 331591, "NumberOfRecord": 1, "TransmissionDateTime": 201903251115 },"GeData": [{ "StockAnswer": "OK111","SAPQuotationCode": "20022780  ", "Other3": "", "Other2": "", "Other1": "",  "OpportunityCode": "O-2018-067607"}]}}

        //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"}]}');
    }
}