@isTest
private class NFM117RestTest {
    static testMethod void testMethod1() {
        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Repair__c';
        transfer.Column__c         = 'With_Maintenance_Contract__c';
        transfer.External_Value__c = '0';
        transfer.Internal_Value__c = '無';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Repair__c';
        transfer.Column__c         = 'With_Maintenance_Contract__c';
        transfer.External_Value__c = '1';
        transfer.Internal_Value__c = '有';
        transfers.add(transfer);
        insert transfers;
        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.Name         = 'NFM117TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_GI').getRecordTypeId();
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;

        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM117TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        
        
        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM117Prd1';
        prd1.ProductCode            = 'NFM117Prd1';
        prd1.Repair_Product_Code__c = 'NFM117Prd1_RP';
        prd1.Name                   = 'NFM117Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.CanRepairAccsessary__c = true;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM117Prd2';
        prd2.ProductCode            = 'NFM117Prd2';
        prd2.Repair_Product_Code__c = 'NFM117Prd2_RP';
        prd2.Name                   = 'NFM117Prd2';
        prd2.Manual_Entry__c        = false;
        prd2.CanRepairAccsessary__c = true;
        prdList.add(prd2);
        insert prdList;
 

        Asset ast = new Asset();
        ast.Name                   = 'NFM117Ast1';
        ast.AccountId              = depart.Id;
        ast.Department_Class__c    = section.Id;
        ast.Hospital__c            = company.Id;
        ast.Product2Id             = prd1.Id;
        ast.Quantity               = 100;
        ast.SerialNumber           = 'NFM117SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        
        insert ast;
        ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber 
                                 from Asset
                                where Id = :ast.Id];

        Repair__c rpr = new Repair__c();
        rpr.Incharge_Staff__c = Userinfo.getUserId();
        rpr.Status__c = '1.受理完毕';
        rpr.SAPRepairNo__c   = 'NFM117Repair1';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.work_location_select__c = '北京办事处';
        rpr.SalesOfficeCode_selection__c = '北京';
        rpr.Repair_Detail__c = 'test';
        rpr.Returns_Product_way__c = '返送医院';
        rpr.FSE_ApplyForRepair_time__c = Date.today();
        insert rpr;
        Repair__c rpr2 = [Select Id, SAPRepairNo__c,PartsreadyDate__c from Repair__c where Id = :rpr.Id];

        NFM117Rest.GeDatas GeDatas = new NFM117Rest.GeDatas();
        NFM117Rest.GeData GeData = new NFM117Rest.GeData();
        GeDatas.GeData = new NFM117Rest.GeData[]{GeData};

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';

        GeData.SAPRepairNo = 'NFM117Repair1';
        GeData.PartsreadyDate = '2022-06-06T00:00:00';


        //System.Test.startTest();
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM117', GeDatas.GeData);
        NFM117Rest.executefuture(rowData.Id);

        rowData = NFMUtil.saveRowData(Monitoring, 'NFM117', GeDatas.GeData);
        NFM117Rest.executefuture(rowData.Id);

        //System.assertEquals('2022-06-06', rpr2.PartsreadyDate__c);
        //System.Test.stopTest();
    }
    static testMethod void testMethod2() {
    	//Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SAP","Receiver":"SFDC","NumberOfRecord":"1","MessageType":"NFM117","MessageGroupNumber":"20210000005088"},"GeData":[{"SAPRepairNo":"000010267138","PartsreadyDate":"2022-06-06T00:00:00"}]}';
        req.requestURI = 'services/apexrest/NFM117/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM117Rest.execute();

        //Test.stopTest();
    }
    static testMethod void testMethod3() {
    	//Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SAP","Receiver":"SFDC","NumberOfRecord":"1","MessageType":"NFM117","MessageGroupNumber":"20210000005088"},"GeData":[{"SAPRepairNo":"","PartsreadyDate":"2022-06-06T00:00:00"}]}';
        req.requestURI = 'services/apexrest/NFM117/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM117Rest.execute();

        //Test.stopTest();
    }
    static testMethod void testMethod4() {
    	//Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SAP","Receiver":"SFDC","NumberOfRecord":"1","MessageType":"NFM117","MessageGroupNumber":"20210000005088"},"GeData":[{"SAPRepairNo":"000010267138","PartsreadyDate":""}]}';
        req.requestURI = 'services/apexrest/NFM117/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM117Rest.execute();

        //Test.stopTest();
    }
}