@isTest
private class NFM114RestTest {
    static Account Agency;
    static Product2 product;

    static void initData() {
        Account account2 = new Account();
		account2.Name = 'test1经销商';
		List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '经销商联系人'];
        account2.RecordTypeId = rectCo[0].Id;
        insert account2;
        ID AgencyID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('office').getRecordTypeId();
        Agency = new Account(ParentId = account2.Id, RecordTypeId = AgencyID, Name = 'NFM114TestAgency', FSE_SP_Main_Leader__c = UserInfo.getUserId(), FSE_GI_Main_Leader__c = UserInfo.getUserId());
        insert Agency;
        // Agency = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :Agency.Id];
        Agency = [select Management_Code__c,  Name, Id from Account where Id = :Agency.Id];

        product = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05', ProductCode = 'A06000008601',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert product;
    }
    static testMethod void testNFM114_01() {
        initData();

        NFM114Rest.GeneralDate GeneralDate = new NFM114Rest.GeneralDate();
        NFM114Rest.GeneralDates GeneralDates = new NFM114Rest.GeneralDates();
        NFM114Rest.Detail Detail = new NFM114Rest.Detail();
        GeneralDates.GeneralDate = new NFM114Rest.GeneralDate[] { GeneralDate };

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeneralDates.Monitoring = new NFMUtil.Monitoring();
        GeneralDates.Monitoring.MessageGroupNumber = nowStr + '01';
        
        Sample_order_list_DN__c samOrder = new Sample_order_list_DN__c();
        samOrder.DNNumber__c = '121233';
        upsert samOrder;

        Sample_inventory_sheet__c samSheet = new Sample_inventory_sheet__c();
        samSheet.Agency__c      = Agency.Id;
        samSheet.ProductCode__c = product.ProductCode;
        samSheet.Batch__c       = '123123';
        samSheet.ValidTo__c     = Date.today().addDays(1);
        samSheet.Pro_Model__c   = 'sadasd';
        samSheet.Inventory__c   = 112;
        upsert samSheet;

        Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GeneralDates.Monitoring, 'NFM114', GeneralDates.GeneralDate);
        GeneralDates.Monitoring.MessageGroupNumber = nowStr + '02';
        GeneralDate.AgencyCode      = Agency.Management_Code__c;
        GeneralDate.DNNumber        = '20211118000';
        GeneralDate.DeliveryDate    = '20201024';
        GeneralDate.Detail          = new List<NFM114Rest.Detail>();
        Detail.MaterialNumber   = product.ProductCode;
        Detail.Batch            = '123123';
        Detail.ValidTo          = '20211218';
        Detail.PackingQTY       = '100';
        Detail.DeliveryQTY      = null;
        GeneralDate.Detail.add(Detail);
        GeneralDates.GeneralDate.add(GeneralDate);

        rowData = NFMUtil.saveRowData(GeneralDates.Monitoring, 'NFM114', GeneralDates.GeneralDate);
        if (String.isBlank(rowData.Log__c) == false){
            NFM114Rest.main(rowData.Id);
        }
        Test.stopTest();
    }

    /* 测试调用execute方法 */
    @isTest static void test_method_execute() {
        initData();

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

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"202111041510","Text":"","Tag":"MSGH","Sender":"8402","Receiver":"1330","NumberOfRecord":"1","MessageType":"NFM114","MessageGroupNumber":"20210003412004"},"GeneralDate":[{"DNNumber":"20211117018","DeliveryDate":"20201024","AgencyCode":"' + Agency.Management_Code__c + '","Detail":[{"MaterialNumber":"A06000008601","Batch":"123123","PackingQTY":"100","DeliveryQTY":"50"}]}]}';
        req.requestURI = 'services/apexrest/NFM114/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

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