@isTest
private class NFM012RestTest {
    static testMethod void resend_test() {
        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '02';
        transfer.Internal_Value__c = '02Internal';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '03';
        transfer.Internal_Value__c = '03Internal';
        transfers.add(transfer);
        insert transfers;

        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rects.size() == 0) {
            return;
        }
        Account myAccount2 = new Account(name='NFM012Tst1',AgentCode_Ext__c = 'NFM012Tst1',RecordTypeId = rects[0].Id);
        insert myAccount2;
        NFM012Rest.Deposit Deposit = new NFM012Rest.Deposit();
        NFM012Rest.GeneralData GeneralData = new NFM012Rest.GeneralData();
        Deposit.GeneralData = new NFM012Rest.GeneralData[] { GeneralData };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Deposit.Monitoring = new NFMUtil.Monitoring();
        Deposit.Monitoring.MessageGroupNumber = nowStr + '21';

        // insert
        GeneralData.EndUserNo                              = 'NFM012Tst1';
        GeneralData.UpdateDate                             = String.valueOf(Date.today());
        GeneralData.DepositAmount                          = Decimal.valueOf('10000000');
        System.Test.startTest();
        // NFM012Rest.NFM012(Deposit);
       BatchIF_Log__c rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false){
        	NFM012Rest.main(rowData.Id);
        } 
        System.Test.stopTest();
        Account[] rslts = [select Management_Code__c, Delete_Flag__c,
                                  Name,
                                  Phone,
                                  Fax,
                                  Postal_Code__c,
                                  Address_Together__c,
                                  Tax_Practice_No__c,
                                  Tax_Practice_Expiration_Date__c,
                                  Business_Authorization_No__c,
                                  Business_Paper_Expiration_Date__c,
                                  Medical_Equipment_Num__c,
                                  Medical_Equipment_Expiration_Date__c,
                                  Registration_Day__c,
                                  Ban_On_Use_Date__c,
                                  Sales_Shop_Class__c
                             from Account where Deposit_Receipt_Finished__c = :GeneralData.DepositAmount];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM012Tst1', rslts[0].Management_Code__c);
            System.assertEquals('NFM012Tst1', rslts[0].Name);
            System.assertEquals(False, rslts[0].Delete_Flag__c);
        }
        
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'NFM012'
                                        order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;
        // NFM012Rest.execute(rowbl[0].id);
         rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false)
            NFM012Rest.main(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(0, bl[0].retry_cnt__c);
    }

     static testMethod void testNFM012_void() {
      List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '02';
        transfer.Internal_Value__c = '02Internal';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '03';
        transfer.Internal_Value__c = '03Internal';
        transfers.add(transfer);
        insert transfers;
        // null
        // NFM012Rest.NFM012(null);

        NFM012Rest.GeneralData GeneralData = new NFM012Rest.GeneralData();
        NFM012Rest.Deposit Deposit = new NFM012Rest.Deposit();
        Deposit.GeneralData = new NFM012Rest.GeneralData[] { GeneralData };
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Deposit.Monitoring = new NFMUtil.Monitoring();
        Deposit.Monitoring.MessageGroupNumber = nowStr + '02';
        // Monitoringをセットしない
        // NFM012Rest.NFM012(Deposit);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false){
        	NFM012Rest.main(rowData.Id);
        } 
        
        // Datetime nowDT = Datetime.now();
        // String nowStr = nowDT.format('yyyyMMddHHmm');
        Deposit.Monitoring = new NFMUtil.Monitoring();
        Deposit.Monitoring.MessageGroupNumber = nowStr + '01';
        // 必須項目をセットしない
        // NFM012Rest.NFM012(Deposit);
        rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false){
        	NFM012Rest.main(rowData.Id);
        } 

        Account[] rslts = [select Id from Account];
        System.assertEquals(0, rslts.size());
    }

    static testMethod void testNFM012_insert(){

      // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '02';
        transfer.Internal_Value__c = '02Internal';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '03';
        transfer.Internal_Value__c = '03Internal';
        transfers.add(transfer);
        insert transfers;

    	NFM012Rest.GeneralData GeneralData = new NFM012Rest.GeneralData();
        NFM012Rest.GeneralData GeneralData2 = new NFM012Rest.GeneralData();
        NFM012Rest.Deposit Deposit = new NFM012Rest.Deposit();
        Deposit.GeneralData = new NFM012Rest.GeneralData[] { GeneralData, GeneralData2 };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Deposit.Monitoring = new NFMUtil.Monitoring();
        Deposit.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeneralData.EndUserNo                              = 'NFM012Tst1';
        GeneralData.UpdateDate                             = String.valueOf(Date.today());
        GeneralData.DepositAmount                          = Decimal.valueOf('12345678');

        System.Test.startTest();
        // NFM012Rest.NFM012(Deposit);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false){
        	NFM012Rest.main(rowData.Id);
        } 
        System.Test.stopTest();
        Account[] rslts = [select Management_Code__c, Delete_Flag__c,
                                  Name,
                                  Phone,
                                  Fax,
                                  Postal_Code__c,
                                  Address_Together__c,
                                  Tax_Practice_No__c,
                                  Tax_Practice_Expiration_Date__c,
                                  Business_Authorization_No__c,
                                  Business_Paper_Expiration_Date__c,
                                  Medical_Equipment_Num__c,
                                  Medical_Equipment_Expiration_Date__c,
                                  Registration_Day__c,
                                  Ban_On_Use_Date__c,
                                  Sales_Shop_Class__c
                             from Account where AgentCode_Ext__c = :GeneralData.EndUserNo];
        System.assertEquals(0, rslts.size());
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c, Type__c, Log__c, ErrorLog__c from BatchIF_Log__c where Type__c = 'NFM012' and RowDataFlg__c = False order by CreatedDate desc];
        System.assertEquals(true, bl[0].ErrorLog__c.indexOf('NFM012Tst1, FIELD_CUSTOM_VALIDATION_EXCEPTION') >= 0, bl[0].ErrorLog__c);
    }

     static testMethod void testNFM012_update(){

        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '02';
        transfer.Internal_Value__c = '02Internal';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Sales_Shop_Class__c';
        transfer.External_Value__c = '03';
        transfer.Internal_Value__c = '03Internal';
        transfers.add(transfer);
        insert transfers;
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rects.size() == 0) {
            return;
        }
        Account myAccount2 = new Account(name='NFM012Tst1',AgentCode_Ext__c = 'NFM012Tst1',RecordTypeId = rects[0].Id);
        insert myAccount2;
    	NFM012Rest.Deposit Deposit = new NFM012Rest.Deposit();
        NFM012Rest.GeneralData GeneralData = new NFM012Rest.GeneralData();
        Deposit.GeneralData = new NFM012Rest.GeneralData[] { GeneralData };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Deposit.Monitoring = new NFMUtil.Monitoring();
        Deposit.Monitoring.MessageGroupNumber = nowStr + '21';

        // insert
        GeneralData.EndUserNo                              = 'NFM012Tst1';
        GeneralData.UpdateDate                             = String.valueOf(Date.today());
        GeneralData.DepositAmount                          = Decimal.valueOf('10000000');

        System.Test.startTest();
        // NFM012Rest.NFM012(Deposit);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Deposit.Monitoring, 'NFM012', Deposit.GeneralData);
        if (String.isBlank(rowData.Log__c) == false){
        	NFM012Rest.main(rowData.Id);
        } 
        System.Test.stopTest();
        Account[] rslts = [select Management_Code__c, Delete_Flag__c,
                                  Name,
                                  Phone,
                                  Fax,
                                  Postal_Code__c,
                                  Address_Together__c,
                                  Tax_Practice_No__c,
                                  Tax_Practice_Expiration_Date__c,
                                  Business_Authorization_No__c,
                                  Business_Paper_Expiration_Date__c,
                                  Medical_Equipment_Num__c,
                                  Medical_Equipment_Expiration_Date__c,
                                  Registration_Day__c,
                                  Ban_On_Use_Date__c,
                                  Sales_Shop_Class__c
                             from Account where Deposit_Receipt_Finished__c = :GeneralData.DepositAmount];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM012Tst1', rslts[0].Management_Code__c);
            System.assertEquals('NFM012Tst1', rslts[0].Name);
            System.assertEquals(False, rslts[0].Delete_Flag__c);
        }
    }

    @isTest 
        static void test_method_Seven() {

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

        String JsonMsg = '{"Deposit":{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM012","MessageGroupNumber":"20180001722190"},"GeneralData":[{"EndUserNo":"0000021729","UpdateDate":20191022,"DepositAmount":"555.42"}]}}';
        req.requestURI = 'services/apexrest/NFM012/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM012Rest.execute();

        Test.stopTest();
       
    }
}