@isTest
private class SBGITMWebServiceTest {
        @isTest
	static void testSBDITM_void() {
	Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMWebService.GeData GeData = new SBGITMWebService.GeData();
        SBGITMWebService.GeDatas GeDatas = new SBGITMWebService.GeDatas();
        GeDatas.GeData = new SBGITMWebService.GeData[] { GeData };
        // 空っぽ1
        SBGITMWebService.SBGITM(GeDatas);

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';
        GeData.EffectiveDateTo                            = '29121103';
        GeData.ProductCode                = 'SBGITMTst1';
        SBGITMWebService.SBGITM(GeDatas);

        // 必須項目をセットしない
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '02';
        GeData.EffectiveDateTo                            = '29121103';
        GeData.ProductCode                = 'SBGITMTst1';
        SBGITMWebService.SBGITM(GeDatas);

        // ZHではない
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '03';
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription      = 'GIF-X0261:stop2012.8::Gastroscopy';
        SBGITMWebService.SBGITM(GeDatas);

        Product2[] rslts = [select Id from Product2 where ProductCode = :GeData.ProductCode];
        System.assertEquals(0, rslts.size());
	}
        @isTest
        static void testSBGITM_insert() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMWebService.GeDatas GeDatas = new SBGITMWebService.GeDatas();
        SBGITMWebService.GeData GeData = new SBGITMWebService.GeData();
        SBGITMWebService.GeData GeData2 = new SBGITMWebService.GeData();
        GeDatas.GeData = new SBGITMWebService.GeData[] { GeData, GeData2 };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';

        // insert2
        GeData2.ProductCode                = 'SBGITMTst2';
        GeData2.ProductDescription                 = 'ABCD12345:ABC';
        GeData2.ProductStatus                    = '0';
        GeData2.EffectiveDateTo                      = '20121101';

        System.Test.startTest();
        SBGITMWebService.SBGITM(GeDatas);
        System.Test.stopTest();
        Product2[] rslts = [select Id,Name,
                                ProductCode,
                                Product_ECCode__c,
                                EffectiveDateTo__c,
                                ProductModels__c,
                                ProductStatus__c
                              from Product2
                             where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst1', rslts[0].ProductCode);
        }
        rslts = [select Id, 
                        ProductCode,Name,
                        Product_ECCode__c,
                        EffectiveDateTo__c,
                        ProductModels__c,
                        ProductStatus__c
                from Product2
                where Product_ECCode__c = 'ABCD12345'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst2', rslts[0].ProductCode);
            System.assertEquals('ABC', rslts[0].Name);
        }
    }

    @isTest
        static void testSBGITM_update() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMWebService.GeDatas GeDatas = new SBGITMWebService.GeDatas();
        SBGITMWebService.GeData GeData = new SBGITMWebService.GeData();
        GeDatas.GeData = new SBGITMWebService.GeData[] { GeData };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';


        System.Test.startTest();
        SBGITMWebService.SBGITM(GeDatas);
        System.Test.stopTest();
        Product2[] rslts = [select Id, 
                                ProductCode, 
                                Product_ECCode__c,
                                EffectiveDateTo__c,
                                ProductModels__c,
                                ProductStatus__c
                              from Product2
                             where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst1', rslts[0].ProductCode);
        }
    }

    @isTest
        static void testSBGITM_resend() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        SBGITMWebService.GeDatas GeDatas = new SBGITMWebService.GeDatas();
        SBGITMWebService.GeData GeData = new SBGITMWebService.GeData();
        GeDatas.GeData = new SBGITMWebService.GeData[] { GeData };
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        GeData.ProductCode                = 'SBGITMTst1';
        GeData.ProductDescription                 = 'ABCD1234:ABC';
        GeData.ProductStatus                    = '1';
        GeData.EffectiveDateTo                      = '20121101';


        System.Test.startTest();
        SBGITMWebService.SBGITM(GeDatas);
        System.Test.stopTest();
        Product2[] rslts = [select Id, 
                                ProductCode, 
                                Product_ECCode__c,
                                EffectiveDateTo__c,
                                ProductModels__c,
                                ProductStatus__c
                              from Product2
                             where Product_ECCode__c = 'ABCD1234'];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('SBGITMTst1', rslts[0].ProductCode);
        }
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'SBGITM'
                                        order by CreatedDate desc];
        List<BatchIF_Log__c> rowbl1 = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        order by CreatedDate desc];
        System.debug(rowbl1);
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

        SBGITMWebService.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);
    }
}