@isTest
private class NFM006WebServiceTest {

    @isTest
    static void testNFM006_void() {
        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-01';
        transfer.Internal_Value__c = '不要';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-10';
        transfer.Internal_Value__c = '準備中';
        transfers.add(transfer);
        insert transfers;

        // null
        NFM006WebService.NFM006(null);

        NFM006WebService.SFDAStatus SFDAStatus = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatuses SFDAStatuses = new NFM006WebService.SFDAStatuses();
        SFDAStatuses.SFDAStatus = new NFM006WebService.SFDAStatus[] { SFDAStatus };
        // Monitoringをセットしない
        NFM006WebService.NFM006(SFDAStatuses);
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        SFDAStatuses.Monitoring = new NFMUtil.Monitoring();
        SFDAStatuses.Monitoring.MessageGroupNumber = nowStr + '01';
        // 必須項目をセットしない
        NFM006WebService.NFM006(SFDAStatuses);
        SFDAStatuses.Monitoring.MessageGroupNumber = nowStr + '02';
        SFDAStatus.ItemCode                              = 'NFM006Tst1';
        NFM006WebService.NFM006(SFDAStatuses);

        Product2[] rslts = [select Id from Product2];
        System.assertEquals(0, rslts.size());
    }
    
    @isTest
    static void testNFM006_insert() {
        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-01';
        transfer.Internal_Value__c = '不要';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-10';
        transfer.Internal_Value__c = '準備中';
        transfers.add(transfer);
        insert transfers;

        NFM006WebService.SFDAStatus SFDAStatus = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatus SFDAStatus2 = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatuses SFDAStatuses = new NFM006WebService.SFDAStatuses();
        SFDAStatuses.SFDAStatus = new NFM006WebService.SFDAStatus[] { SFDAStatus, SFDAStatus2 };

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

        // insert
        SFDAStatus.ItemCode          = 'NFM006Tst1';
        SFDAStatus.Status            = '1541-01';
        SFDAStatus.SFDANo            = 'SFDA(I)20053221287';
        SFDAStatus.EffectiveDateFrom = '20110502';
        SFDAStatus.EffectiveDateTo   = '99991230';
        SFDAStatus.PurposeOfAdvice   = '2';
        SFDAStatus2.ItemCode          = 'NFM006Tst1';
        SFDAStatus2.Status            = '1541-01';
        SFDAStatus2.SFDANo            = 'SFDA(I)20053221287';
        SFDAStatus2.EffectiveDateFrom = '20110501';
        SFDAStatus2.EffectiveDateTo   = '99991231';
        SFDAStatus2.PurposeOfAdvice   = '2';

        System.Test.startTest();
        NFM006WebService.NFM006(SFDAStatuses);
        System.Test.stopTest();
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus.ItemCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM006Tst1', rslts[0].ProductCode);
            System.assertEquals('不要', rslts[0].SFDA_Status__c);
            System.assertEquals('SFDA(I)20053221287', rslts[0].SFDA_Approbation_No__c);
            System.assertEquals(date.newinstance(2011,  5,  1), rslts[0].SFDA_Approbated_Date__c);
            System.assertEquals(date.newinstance(4000, 12, 31), rslts[0].SFDA_Expiration_Date__c);
        }
    }
    
    @isTest
    static void testNFM006_update() {
        Product2 pro1 = new Product2(Name='NFM006Tst1',IsActive=true,Family='GI',Asset_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',Manual_Entry__c=false);
        Product2 pro2 = new Product2(Name='NFM006Tst2',IsActive=true,Family='GI',Asset_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',Manual_Entry__c=false);
        insert new Product2[] {pro1, pro2};
        pro1.ProductCode_Ext__c = 'NFM006Tst1';
        pro2.ProductCode_Ext__c = 'NFM006Tst2';
        update new Product2[] {pro1, pro2};
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :pro2.ProductCode_Ext__c];
        System.assertEquals(1, rslts.size());

        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-01';
        transfer.Internal_Value__c = '不要';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-10';
        transfer.Internal_Value__c = '準備中';
        transfers.add(transfer);
        insert transfers;

        NFM006WebService.SFDAStatus SFDAStatus = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatus SFDAStatus2 = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatuses SFDAStatuses = new NFM006WebService.SFDAStatuses();
        SFDAStatuses.SFDAStatus = new NFM006WebService.SFDAStatus[] { SFDAStatus, SFDAStatus2 };

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

        // update
        SFDAStatus.ItemCode          = 'NFM006Tst1';
        SFDAStatus.Status            = '1541-10';
        SFDAStatus.SFDANo            = 'SFDA(J)20053221287';
        SFDAStatus.EffectiveDateFrom = '20120501';
        SFDAStatus.EffectiveDateTo   = '20131231';
        SFDAStatus.PurposeOfAdvice   = '3';
        SFDAStatus2.ItemCode          = 'NFM006Tst2';
        SFDAStatus2.Status            = '1541-01';
        SFDAStatus2.SFDANo            = 'SFDA(K)20053221287';
        SFDAStatus2.EffectiveDateFrom = '19000101';
        SFDAStatus2.EffectiveDateTo   = '99991230';
        SFDAStatus2.PurposeOfAdvice   = '2';

        System.Test.startTest();
        NFM006WebService.NFM006(SFDAStatuses);
        System.Test.stopTest();
        rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus.ItemCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM006Tst1', rslts[0].ProductCode);
            System.assertEquals('準備中', rslts[0].SFDA_Status__c);
            System.assertEquals('SFDA(J)20053221287', rslts[0].SFDA_Approbation_No__c);
            System.assertEquals(date.newinstance(2012,  5,  1), rslts[0].SFDA_Approbated_Date__c);
            System.assertEquals(date.newinstance(2013, 12, 31), rslts[0].SFDA_Expiration_Date__c);
        }
        rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus2.ItemCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM006Tst2', rslts[0].ProductCode);
            System.assertEquals('不要', rslts[0].SFDA_Status__c);
            System.assertEquals('SFDA(K)20053221287', rslts[0].SFDA_Approbation_No__c);
            System.assertEquals(date.newinstance(1900, 1,  1), rslts[0].SFDA_Approbated_Date__c);
            System.assertEquals(date.newinstance(4000, 12, 31), rslts[0].SFDA_Expiration_Date__c);
        }
    }
    
    @isTest
    static void testNFM006_delete() {
        Product2 pro1 = new Product2(Name='NFM006Tst1',IsActive=true,Family='GI',Asset_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',Manual_Entry__c=false);
        insert new Product2[] {pro1};
        pro1.ProductCode_Ext__c = 'NFM006Tst1';
        update new Product2[] {pro1};
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :pro1.ProductCode_Ext__c];
        // deleteは無視
        System.assertEquals(1, rslts.size());

        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-01';
        transfer.Internal_Value__c = '不要';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-10';
        transfer.Internal_Value__c = '準備中';
        transfers.add(transfer);
        insert transfers;

        NFM006WebService.SFDAStatus SFDAStatus = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatuses SFDAStatuses = new NFM006WebService.SFDAStatuses();
        SFDAStatuses.SFDAStatus = new NFM006WebService.SFDAStatus[] { SFDAStatus };

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

        // delete
        SFDAStatus.ItemCode          = 'NFM006Tst1';
        SFDAStatus.Status            = '1541-10';
        SFDAStatus.SFDANo            = 'SFDA(J)20053221287';
        SFDAStatus.EffectiveDateFrom = '20120501';
        SFDAStatus.EffectiveDateTo   = '20131231';
        SFDAStatus.PurposeOfAdvice   = '1';

        System.Test.startTest();
        NFM006WebService.NFM006(SFDAStatuses);
        System.Test.stopTest();
        rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus.ItemCode];
        // deleteは無視
        System.assertEquals(1, rslts.size());
    }

    //resend_test
    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          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-01';
        transfer.Internal_Value__c = '不要';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Product2';
        transfer.Column__c         = 'SFDA_Status__c';
        transfer.External_Value__c = '1541-10';
        transfer.Internal_Value__c = '準備中';
        transfers.add(transfer);
        insert transfers;

        NFM006WebService.SFDAStatus SFDAStatus = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatus SFDAStatus2 = new NFM006WebService.SFDAStatus();
        NFM006WebService.SFDAStatuses SFDAStatuses = new NFM006WebService.SFDAStatuses();
        SFDAStatuses.SFDAStatus = new NFM006WebService.SFDAStatus[] { SFDAStatus, SFDAStatus2 };

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

        // update
        SFDAStatus.ItemCode          = 'NFM006Tst1';
        SFDAStatus.Status            = '1541-10';
        SFDAStatus.SFDANo            = 'SFDA(J)20053221287';
        SFDAStatus.EffectiveDateFrom = '20120501';
        SFDAStatus.EffectiveDateTo   = '20131231';
        SFDAStatus.PurposeOfAdvice   = '3';
        SFDAStatus2.ItemCode          = 'NFM006Tst2';
        SFDAStatus2.Status            = '1541-01';
        SFDAStatus2.SFDANo            = 'SFDA(K)20053221287';
        SFDAStatus2.EffectiveDateFrom = '19000101';
        SFDAStatus2.EffectiveDateTo   = '99991230';
        SFDAStatus2.PurposeOfAdvice   = '2';
        System.Test.startTest();
        NFM006WebService.NFM006(SFDAStatuses);
        System.Test.stopTest();
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus.ItemCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM006Tst1', rslts[0].ProductCode);
            System.assertEquals('準備中', rslts[0].SFDA_Status__c);
            System.assertEquals('SFDA(J)20053221287', rslts[0].SFDA_Approbation_No__c);
            System.assertEquals(date.newinstance(2012,  5,  1), rslts[0].SFDA_Approbated_Date__c);
            System.assertEquals(date.newinstance(2013, 12, 31), rslts[0].SFDA_Expiration_Date__c);
        }
        rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                       SFDA_Status__c,
                                       SFDA_Approbation_No__c,
                                       SFDA_Approbated_Date__c,
                                       SFDA_Expiration_Date__c
                                  from Product2
                                 where ProductCode_Ext__c = :SFDAStatus2.ItemCode];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('NFM006Tst2', rslts[0].ProductCode);
            System.assertEquals('不要', rslts[0].SFDA_Status__c);
            System.assertEquals('SFDA(K)20053221287', rslts[0].SFDA_Approbation_No__c);
            System.assertEquals(date.newinstance(1900, 1,  1), rslts[0].SFDA_Approbated_Date__c);
            System.assertEquals(date.newinstance(4000, 12, 31), rslts[0].SFDA_Expiration_Date__c);
        }

        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'NFM006'
                                        order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

        NFM006WebService.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(1, bl.size());
        System.assertEquals(0, bl[0].retry_cnt__c);
    }
}