@isTest
private class NFM002WebServiceTest {


    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;

        NFM002WebService.Agent Agent = new NFM002WebService.Agent();
        NFM002WebService.CustomerGeneralData CustomerGeneralData = new NFM002WebService.CustomerGeneralData();
        Agent.CustomerGeneralData = new NFM002WebService.CustomerGeneralData[] { CustomerGeneralData };

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

        // update
        CustomerGeneralData.AgentCode                              = 'NFM002Tst1';
        CustomerGeneralData.AgentName                              = 'AgentNamE';
        CustomerGeneralData.Telephone                              = '03-12345679';
        CustomerGeneralData.Fax                                    = '03-87654322';
        CustomerGeneralData.PostalCode                             = '123-68';
        CustomerGeneralData.Address                                = '上海市南市区1-3-6';
        CustomerGeneralData.RegionCode                             = 'Nil';
        CustomerGeneralData.VATRegistrationNumber                  = '11010667820981Y';
        CustomerGeneralData.VATRegistrationNOvalidTo               = '20140331';
        CustomerGeneralData.MedicalProductionSalesLicenseNO        = '京000001';
        CustomerGeneralData.MedicalProductionSalesLicenseNOvalidTo = '20131231';
        CustomerGeneralData.BusinessLicenseNO                      = '6101012411215-B';
        CustomerGeneralData.BusinessLicenseNOvalidTo               = '20131130';
        CustomerGeneralData.ValidFrom                              = '20020101';
        CustomerGeneralData.ValidTo                                = String.valueOf(Date.today().addDays(2));
        CustomerGeneralData.PurposeOfAdvice                        = '3';
        CustomerGeneralData.AgentCategory                          = '03';
        System.Test.startTest();
        NFM002WebService.NFM002(Agent);
        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 Management_Code__c = :CustomerGeneralData.AgentCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM002Tst1', rslts[0].Management_Code__c);
            System.assertEquals('AgentNamE', rslts[0].Name);
            System.assertEquals(False, rslts[0].Delete_Flag__c);
        }

        // retry_cnt__c がクリアされること
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = true and Type__c = 'NFM002' order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;
        NFM002WebService.execute(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 testNFM002_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
        NFM002WebService.NFM002(null);

        NFM002WebService.CustomerGeneralData CustomerGeneralData = new NFM002WebService.CustomerGeneralData();
        NFM002WebService.Agent Agent = new NFM002WebService.Agent();
        Agent.CustomerGeneralData = new NFM002WebService.CustomerGeneralData[] { CustomerGeneralData };
        // Monitoringをセットしない
        NFM002WebService.NFM002(Agent);
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Agent.Monitoring = new NFMUtil.Monitoring();
        Agent.Monitoring.MessageGroupNumber = nowStr + '01';
        // 必須項目をセットしない
        NFM002WebService.NFM002(Agent);

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

    static testMethod void testNFM002_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;
        NFM002WebService.CustomerGeneralData CustomerGeneralData = new NFM002WebService.CustomerGeneralData();
        NFM002WebService.CustomerGeneralData CustomerGeneralData2 = new NFM002WebService.CustomerGeneralData();
        NFM002WebService.Agent Agent = new NFM002WebService.Agent();
        Agent.CustomerGeneralData = new NFM002WebService.CustomerGeneralData[] { CustomerGeneralData, CustomerGeneralData2 };

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

        // insert
        CustomerGeneralData.AgentCode                              = 'NFM002Tst1';
        CustomerGeneralData.AgentName                              = 'AgentName';
        CustomerGeneralData.Telephone                              = '03-12345678';
        CustomerGeneralData.Fax                                    = '03-87654321';
        CustomerGeneralData.PostalCode                             = '123-67';
        CustomerGeneralData.Address                                = '上海市南市区1-3-5';
        CustomerGeneralData.RegionCode                             = 'Nil';
        CustomerGeneralData.VATRegistrationNumber                  = '11010667820981X';
        CustomerGeneralData.VATRegistrationNOvalidTo               = '20130331';
        CustomerGeneralData.MedicalProductionSalesLicenseNO        = '京000000';
        CustomerGeneralData.MedicalProductionSalesLicenseNOvalidTo = '20121231';
        CustomerGeneralData.BusinessLicenseNO                      = '6101012411215-A';
        CustomerGeneralData.BusinessLicenseNOvalidTo               = '20121130';
        CustomerGeneralData.ValidFrom                              = '20010101';
        CustomerGeneralData.ValidTo                                = String.valueOf(Date.today().addDays(2));
        CustomerGeneralData.PurposeOfAdvice                        = '2';
        CustomerGeneralData.AgentCategory                          = '02';
        
        // insert2
        CustomerGeneralData2.AgentCode                              = 'NFM002Tst2';
        CustomerGeneralData2.AgentName                              = 'AgentName2';
        CustomerGeneralData2.Telephone                              = '23-12345678';
        CustomerGeneralData2.Fax                                    = '23-87654321';
        CustomerGeneralData2.PostalCode                             = '223-67';
        CustomerGeneralData2.Address                                = '上海市南市区2--3-5';
        CustomerGeneralData2.RegionCode                             = 'Nil';
        CustomerGeneralData2.VATRegistrationNumber                  = '21010667820981X';
        CustomerGeneralData2.VATRegistrationNOvalidTo               = '20130332';
        CustomerGeneralData2.MedicalProductionSalesLicenseNO        = '京000002';
        CustomerGeneralData2.MedicalProductionSalesLicenseNOvalidTo = '20121232';
        CustomerGeneralData2.BusinessLicenseNO                      = '6101012411215-B';
        CustomerGeneralData2.BusinessLicenseNOvalidTo               = '20121132';
        CustomerGeneralData2.ValidFrom                              = '20010102';
        CustomerGeneralData2.ValidTo                                = String.valueOf(Date.today().addDays(2));
        CustomerGeneralData2.PurposeOfAdvice                        = '2';
        CustomerGeneralData2.AgentCategory                          = '02';
        System.Test.startTest();
        NFM002WebService.NFM002(Agent);
        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 Management_Code__c = :CustomerGeneralData.AgentCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM002Tst1', rslts[0].Management_Code__c);
            System.assertEquals('AgentName', rslts[0].Name);
            System.assertEquals(False, rslts[0].Delete_Flag__c);
//            System.assertEquals('03-12345678', rslts[0].Phone);
//            System.assertEquals('03-87654321', rslts[0].Fax);
//            System.assertEquals(CustomerGeneralData.PostalCode, rslts[0].Postal_Code__c);
//            System.assertEquals('上海市南市区1-3-5', rslts[0].Address_Together__c);
//            System.assertEquals('11010667820981X', rslts[0].Tax_Practice_No__c);
//            System.assertEquals(date.newinstance(2013, 3, 31), rslts[0].Tax_Practice_Expiration_Date__c);
//            System.assertEquals('京000000', rslts[0].Business_Authorization_No__c);
//            System.assertEquals(date.newinstance(2012, 12, 31), rslts[0].Business_Paper_Expiration_Date__c);
//            System.assertEquals('6101012411215-A', rslts[0].Medical_Equipment_Num__c);
//            System.assertEquals(date.newinstance(2012, 11, 30), rslts[0].Medical_Equipment_Expiration_Date__c);
//            System.assertEquals(date.newinstance(2001, 1, 1), rslts[0].Registration_Day__c);
//            System.assertEquals(date.newinstance(2011, 12, 31), rslts[0].Ban_On_Use_Date__c);
//            System.assertEquals('02Internal', rslts[0].Sales_Shop_Class__c);
        }
        NFM002WebService.NFM002(Agent);
        rslts = [select Management_Code__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 Management_Code__c = :CustomerGeneralData2.AgentCode];
        System.assertEquals(1, rslts.size());
    }

    static testMethod void testNFM002_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;

        NFM002WebService.Agent Agent = new NFM002WebService.Agent();
        NFM002WebService.CustomerGeneralData CustomerGeneralData = new NFM002WebService.CustomerGeneralData();
        Agent.CustomerGeneralData = new NFM002WebService.CustomerGeneralData[] { CustomerGeneralData };

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

        // update
        CustomerGeneralData.AgentCode                              = 'NFM002Tst1';
        CustomerGeneralData.AgentName                              = 'AgentNamE';
        CustomerGeneralData.Telephone                              = '03-12345679';
        CustomerGeneralData.Fax                                    = '03-87654322';
        CustomerGeneralData.PostalCode                             = '123-68';
        CustomerGeneralData.Address                                = '上海市南市区1-3-6';
        CustomerGeneralData.RegionCode                             = 'Nil';
        CustomerGeneralData.VATRegistrationNumber                  = '11010667820981Y';
        CustomerGeneralData.VATRegistrationNOvalidTo               = '20140331';
        CustomerGeneralData.MedicalProductionSalesLicenseNO        = '京000001';
        CustomerGeneralData.MedicalProductionSalesLicenseNOvalidTo = '20131231';
        CustomerGeneralData.BusinessLicenseNO                      = '6101012411215-B';
        CustomerGeneralData.BusinessLicenseNOvalidTo               = '20131130';
        CustomerGeneralData.ValidFrom                              = '20020101';
        CustomerGeneralData.ValidTo                                = String.valueOf(Date.today().addDays(2));
        CustomerGeneralData.PurposeOfAdvice                        = '3';
        CustomerGeneralData.AgentCategory                          = '03';

        System.Test.startTest();
        NFM002WebService.NFM002(Agent);
        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 Management_Code__c = :CustomerGeneralData.AgentCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM002Tst1', rslts[0].Management_Code__c);
            System.assertEquals('AgentNamE', rslts[0].Name);
            System.assertEquals(False, rslts[0].Delete_Flag__c);
        }
        
    }

    static testMethod void testNFM002_delete() {
        // テストデータ
        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;
        NFM002WebService.CustomerGeneralData CustomerGeneralData = new NFM002WebService.CustomerGeneralData();
        NFM002WebService.Agent Agent = new NFM002WebService.Agent();
        Agent.CustomerGeneralData = new NFM002WebService.CustomerGeneralData[] { CustomerGeneralData };

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

        // delete
        CustomerGeneralData.AgentCode                              = 'NFM002Tst1';
        CustomerGeneralData.AgentName                              = 'AgentNamF';
        CustomerGeneralData.Telephone                              = '03-12345670';
        CustomerGeneralData.Fax                                    = '03-87654323';
        CustomerGeneralData.PostalCode                             = '123-69';
        CustomerGeneralData.Address                                = '上海市南市区1-3-7';
        CustomerGeneralData.RegionCode                             = 'Nil';
        CustomerGeneralData.VATRegistrationNumber                  = '11010667820981Z';
        CustomerGeneralData.VATRegistrationNOvalidTo               = '20120331';
        CustomerGeneralData.MedicalProductionSalesLicenseNO        = '京000002';
        CustomerGeneralData.MedicalProductionSalesLicenseNOvalidTo = '20111231';
        CustomerGeneralData.BusinessLicenseNO                      = '6101012411215-C';
        CustomerGeneralData.BusinessLicenseNOvalidTo               = '20111130';
        CustomerGeneralData.ValidFrom                              = '20000101';
        CustomerGeneralData.ValidTo                                = String.valueOf(Date.today().addDays(2));
        CustomerGeneralData.PurposeOfAdvice                        = '1';
        CustomerGeneralData.AgentCategory                          = '04';
        
        System.Test.startTest();
        NFM002WebService.NFM002(Agent);
        System.Test.stopTest();
        Account[] rslts = [select Id, Name, Delete_Flag__c
                             from Account where Management_Code__c = :CustomerGeneralData.AgentCode];
        // deleteは無視
        //System.assertEquals(0, rslts.size());
        //System.assertEquals('删除:AgentNamF', rslts[0].Name);
        //System.assertEquals(True, rslts[0].Delete_Flag__c);
    }
}