@isTest
private class MDMITMWebServiceTest {

    static testMethod void testMDMITM_void() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        // null
        MDMITMWebService.MDMITM(null);

        MDMITMWebService.ItemMaster ItemMaster = new MDMITMWebService.ItemMaster();
        MDMITMWebService.ItemMasters ItemMasters = new MDMITMWebService.ItemMasters();
        ItemMasters.ItemMaster = new MDMITMWebService.ItemMaster[] { ItemMaster };
        // 空っぽ1
        MDMITMWebService.MDMITM(ItemMasters);

        MDMITMWebService.Record Record = new MDMITMWebService.Record();
        ItemMaster.Record = new MDMITMWebService.Record[] { Record };
        // 空っぽ2
        MDMITMWebService.MDMITM(ItemMasters);

        MDMITMWebService.MaterialDescription MaterialDescription = new MDMITMWebService.MaterialDescription();
        Record.MaterialDescription = new MDMITMWebService.MaterialDescription[] { MaterialDescription };
        // 空っぽ3
        MDMITMWebService.MDMITM(ItemMasters);

        MDMITMWebService.ProductLotSerialNoTraceability ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability();
        Record.ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability[] { ProductLotSerialNoTraceability };
        // 空っぽ4
        MDMITMWebService.MDMITM(ItemMasters);

        Record.BusinessSegmentName = new MDMITMWebService.BusinessSegmentName();
        // 空っぽ5
        MDMITMWebService.MDMITM(ItemMasters);
        
        // OCNではない
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ItemMasters.Monitoring = new NFMUtil.Monitoring();
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '01';
        Record.To                            = 'MMM';
        Record.MaterialNumber                = 'MDMITMTst1';
        MDMITMWebService.MDMITM(ItemMasters);
        
        // 必須項目をセットしない
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '02';
        Record.To                            = 'OCN';
        Record.MaterialNumber                = '';
        MDMITMWebService.MDMITM(ItemMasters);

        // ZHではない
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '03';
        Record.MaterialNumber                = 'MDMITMTst1';
        MaterialDescription.LanguageKey      = 'EN';
        MaterialDescription.Description      = 'GIF-X0261:stop2012.8::Gastroscopy';
        MDMITMWebService.MDMITM(ItemMasters);

        Product2[] rslts = [select Id from Product2 where ProductCode_Ext__c = :Record.MaterialNumber];
        System.assertEquals(0, rslts.size());
    }
    
    // insert時、SFDA_Status__cが 準備中 になる
    static testMethod void testMDMITM_insert() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        MDMITMWebService.ItemMaster ItemMaster = new MDMITMWebService.ItemMaster();
        MDMITMWebService.ItemMasters ItemMasters = new MDMITMWebService.ItemMasters();
        ItemMasters.ItemMaster = new MDMITMWebService.ItemMaster[] { ItemMaster };
        MDMITMWebService.Record Record = new MDMITMWebService.Record();
        MDMITMWebService.Record Record2 = new MDMITMWebService.Record();
        ItemMaster.Record = new MDMITMWebService.Record[] { Record, Record2 };
        MDMITMWebService.MaterialDescription MaterialDescription = new MDMITMWebService.MaterialDescription();
        MDMITMWebService.MaterialDescription MaterialDescription2 = new MDMITMWebService.MaterialDescription();
        Record.MaterialDescription = new MDMITMWebService.MaterialDescription[] { MaterialDescription };
        Record2.MaterialDescription = new MDMITMWebService.MaterialDescription[] { MaterialDescription2 };
        MDMITMWebService.ProductLotSerialNoTraceability ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability();
        MDMITMWebService.ProductLotSerialNoTraceability ProductLotSerialNoTraceability2 = new MDMITMWebService.ProductLotSerialNoTraceability();
        Record.ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability[] { ProductLotSerialNoTraceability };
        Record2.ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability[] { ProductLotSerialNoTraceability2 };
        Record.BusinessSegmentName = new MDMITMWebService.BusinessSegmentName();
        Record2.BusinessSegmentName = new MDMITMWebService.BusinessSegmentName();


        MDMITMWebService.EANUPC EANUPC = new MDMITMWebService.EANUPC();
        Record.EANUPC = new MDMITMWebService.EANUPC[] { EANUPC };
        MDMITMWebService.QuantityPerSalesPackagingUnit QuantityPerSalesPackagingUnit = new MDMITMWebService.QuantityPerSalesPackagingUnit();
        Record.QuantityPerSalesPackagingUnit = new MDMITMWebService.QuantityPerSalesPackagingUnit[] { QuantityPerSalesPackagingUnit };
        Record.MaterialClassificationCode = new MDMITMWebService.MaterialClassificationCode();
        Record2.MaterialClassificationCode = new MDMITMWebService.MaterialClassificationCode();
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ItemMasters.Monitoring = new NFMUtil.Monitoring();
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '11';

        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         = 'SalesPackagingUnit_Chinese__c';
        transfer.External_Value__c = 'PCS';
        transfer.Internal_Value__c = '把';
        transfers.add(transfer);

        insert transfers;
        List<String> eng = new List<String>();
        eng.add('ENG');
        // insert
        Record.MaterialNumber                = 'MDMITMTst1';
        MaterialDescription.LanguageKey      = 'ZH';
        MaterialDescription.Description      = ' GIF-X0260 : 停止2012.8 :: 胃鏡 ';
        Record.ModelForSales                 = 'ABCD1234';
        Record.RepairCode                    = 'RC12345';
        Record.Layer1st                      = 'GI';
        Record.Layer2nd                      = 'L2';
        Record.Layer3rd                      = 'L3';
        Record.Layer4th                      = 'L4';
        Record.Layer5th                      = 'L5';
        ProductLotSerialNoTraceability.Value = 'No tracing';
        Record.LifeTime                      = 3;
        Record.MCompany                      = '製造会社名';
        Record.BaseUnitOfMeasure             = 'PC';
        Record.BusinessSegmentName.Value     = 'G&R';
        Record.To                            = 'OCN';
        Record.MaterialClassificationCode.Value = '103410909020';
        Record.CN_String1                    = eng; 
        QuantityPerSalesPackagingUnit.Value = '12';
        EANUPC.Category = 'G1';
        EANUPC.Value = '4042761051835';
        Record.CN_SalesPackagingUnit = 'PCS';
        Record.GrossWeight = '1';
        Record.WeightUnit = 'KG';
        Record.CN_LonerType = '消耗品';

        // insert2
        Record2.MaterialNumber                = 'MDMITMTst2';
        MaterialDescription2.LanguageKey      = 'ZH';
        MaterialDescription2.Description      = ' ：： 胃鏡2 ';
        Record2.ModelForSales                 = 'ABCD2234';
        Record2.RepairCode                    = 'RC22345';
        Record2.Layer1st                      = 'GI2';
        Record2.Layer2nd                      = 'L22';
        Record2.Layer3rd                      = 'L32';
        Record2.Layer4th                      = 'L42';
        Record2.Layer5th                      = 'L52';
        ProductLotSerialNoTraceability2.Value = 'No tracing';
        Record2.LifeTime                      = 3;
        Record2.MCompany                      = '製造会社名';
        Record2.BaseUnitOfMeasure             = 'PC';
        Record2.BusinessSegmentName.Value     = 'G&R';
        Record2.To                            = 'OCN';
        Record2.CN_String1                    = eng; 
        
        Record2.MaterialClassificationCode.Value = '103410909020';
        QuantityPerSalesPackagingUnit.Value = '12';
        EANUPC.Category = 'G1';
        EANUPC.Value = '4042761051835';
        Record2.CN_SalesPackagingUnit = 'PCS';
        Record2.GrossWeight = '1';
        Record2.WeightUnit = 'KG';
        Record2.CN_LonerType = '消耗品';

        System.Test.startTest();
        MDMITMWebService.MDMITM(ItemMasters);
        System.Test.stopTest();
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                   MDM_Name__c, MDM_Model_No__c, Alternative_Products__c,
                                   Name, Asset_Model_No__c,
                                   Repair_Product_Code__c,
                                   BSSCategory__c, Family,
                                   Category2__c, Category3__c,
                                   Category4__c, Category5__c,
                                   Serial_Lot_No__c,
                                   Durable_Years__c,
                                   Brand_Name__c,
                                   Qty_Unit__c
                              from Product2
                             where ProductCode_Ext__c = :Record.MaterialNumber];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode);
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode_Ext__c);
            System.assertEquals('GIF-X0260:胃鏡', rslts[0].Name);
            System.assertEquals('GIF-X0260 : 停止2012.8 :: 胃鏡', rslts[0].MDM_Name__c);
            System.assertEquals('GIF-X0260', rslts[0].MDM_Model_No__c);
            System.assertEquals('停止2012.8', rslts[0].Alternative_Products__c);
            System.assertEquals('ABCD1234', rslts[0].Asset_Model_No__c);
            System.assertEquals('RC12345', rslts[0].Repair_Product_Code__c);
            System.assertEquals('GI', rslts[0].Family);
            System.assertEquals('L2', rslts[0].Category2__c);
            System.assertEquals('L3', rslts[0].Category3__c);
            System.assertEquals('L4', rslts[0].Category4__c);
            System.assertEquals('L5', rslts[0].Category5__c);
            System.assertEquals('No tracing', rslts[0].Serial_Lot_No__c);
            System.assertEquals(3, rslts[0].Durable_Years__c);
            System.assertEquals('製造会社名', rslts[0].Brand_Name__c);
            System.assertEquals('PC', rslts[0].Qty_Unit__c);
            System.assertEquals('G&R', rslts[0].BSSCategory__c);
        }
        rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                   MDM_Name__c, MDM_Model_No__c, Alternative_Products__c,
                                   Name, Asset_Model_No__c,
                                   Repair_Product_Code__c,
                                   BSSCategory__c, Family,
                                   Category2__c, Category3__c,
                                   Category4__c, Category5__c,
                                   Serial_Lot_No__c,
                                   Durable_Years__c,
                                   Brand_Name__c,
                                   Qty_Unit__c
                              from Product2
                             where ProductCode_Ext__c = :Record2.MaterialNumber];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('MDMITMTst2', rslts[0].ProductCode);
            System.assertEquals('MDMITMTst2', rslts[0].ProductCode_Ext__c);
            System.assertEquals('MDMITMTst2:胃鏡2', rslts[0].Name);
            System.assertEquals('：： 胃鏡2', rslts[0].MDM_Name__c);
            System.assertEquals(null, rslts[0].MDM_Model_No__c);
            System.assertEquals(null, rslts[0].Alternative_Products__c);
        }
    }
    
    static testMethod void testMDMITM_update() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        Product2 pro1 = new Product2(Name='MDMITMTst1',IsActive=true,Family='GI',Asset_Model_No__c='ABCD1234',Serial_Lot_No__c='No tracing',Manual_Entry__c=false);
        insert pro1;
        // null 更新、値の変更がない
        MDMITMWebService.ItemMaster ItemMaster = new MDMITMWebService.ItemMaster();
        MDMITMWebService.ItemMasters ItemMasters = new MDMITMWebService.ItemMasters();
        ItemMasters.ItemMaster = new MDMITMWebService.ItemMaster[] { ItemMaster };
        MDMITMWebService.Record Record = new MDMITMWebService.Record();
        ItemMaster.Record = new MDMITMWebService.Record[] { Record };
        MDMITMWebService.MaterialDescription MaterialDescription = new MDMITMWebService.MaterialDescription();
        Record.MaterialDescription = new MDMITMWebService.MaterialDescription[] { MaterialDescription };
        MDMITMWebService.ProductLotSerialNoTraceability ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability();
        Record.ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability[] { ProductLotSerialNoTraceability };
        Record.BusinessSegmentName = new MDMITMWebService.BusinessSegmentName();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ItemMasters.Monitoring = new NFMUtil.Monitoring();
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '21';
        List<String> eng = new List<String>();
        eng.add('ENG');
        // 品名以外全部 null
        Record.MaterialNumber                = 'MDMITMTst1';
        MaterialDescription.LanguageKey      = 'ZH';
        MaterialDescription.Description      = ' GIF-X0261 : 胃鏡 ';
        Record.ModelForSales                 = 'ABCD1234';
        Record.RepairCode                    = 'RC12345';
        Record.Layer1st                      = 'GI';
        Record.Layer2nd                      = 'L2';
        Record.Layer3rd                      = 'L3';
        Record.Layer4th                      = 'L4';
        Record.Layer5th                      = 'L5';
        ProductLotSerialNoTraceability.Value = 'No tracing';
        Record.LifeTime                      = 3;
        Record.MCompany                      = '製造会社名';
        Record.BaseUnitOfMeasure             = 'PC';
        Record.BusinessSegmentName.Value     = 'G&R';
        Record.To                            = 'OCN';
        Record.CN_String1                    = eng; 
        System.Test.startTest();
        MDMITMWebService.MDMITM(ItemMasters);
        System.Test.stopTest();
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                   MDM_Name__c, MDM_Model_No__c, Alternative_Products__c,
                                   Name, Asset_Model_No__c,
                                   Repair_Product_Code__c,
                                   BSSCategory__c, Family,
                                   Category2__c, Category3__c,
                                   Category4__c, Category5__c,
                                   Serial_Lot_No__c,
                                   Durable_Years__c,
                                   Brand_Name__c,
                                   Qty_Unit__c
                              from Product2
                             where ProductCode_Ext__c = :Record.MaterialNumber];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode);
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode_Ext__c);
            System.assertEquals('GIF-X0261:胃鏡', rslts[0].Name);
            System.assertEquals('GIF-X0261 : 胃鏡', rslts[0].MDM_Name__c);
            System.assertEquals('GIF-X0261', rslts[0].MDM_Model_No__c);
            System.assertEquals(null, rslts[0].Alternative_Products__c);
            System.assertEquals('ABCD1234', rslts[0].Asset_Model_No__c);
            System.assertEquals('RC12345', rslts[0].Repair_Product_Code__c);
            System.assertEquals('GI', rslts[0].Family);
            System.assertEquals('L2', rslts[0].Category2__c);
            System.assertEquals('L3', rslts[0].Category3__c);
            System.assertEquals('L4', rslts[0].Category4__c);
            System.assertEquals('L5', rslts[0].Category5__c);
            System.assertEquals('No tracing', rslts[0].Serial_Lot_No__c);
            System.assertEquals(3, rslts[0].Durable_Years__c);
            System.assertEquals('製造会社名', rslts[0].Brand_Name__c);
            System.assertEquals('PC', rslts[0].Qty_Unit__c);
            System.assertEquals('G&R', rslts[0].BSSCategory__c);
        }
   }

   static testMethod void testMDMITM_resend() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        Product2 pro1 = new Product2(Name='MDMITMTst1',IsActive=true,Family='GI',Asset_Model_No__c='ABCD1234',Serial_Lot_No__c='No tracing',Manual_Entry__c=false);
        insert pro1;
        // null 更新、値の変更がない
        MDMITMWebService.ItemMaster ItemMaster = new MDMITMWebService.ItemMaster();
        MDMITMWebService.ItemMasters ItemMasters = new MDMITMWebService.ItemMasters();
        ItemMasters.ItemMaster = new MDMITMWebService.ItemMaster[] { ItemMaster };
        MDMITMWebService.Record Record = new MDMITMWebService.Record();
        ItemMaster.Record = new MDMITMWebService.Record[] { Record };
        MDMITMWebService.MaterialDescription MaterialDescription = new MDMITMWebService.MaterialDescription();
        Record.MaterialDescription = new MDMITMWebService.MaterialDescription[] { MaterialDescription };
        MDMITMWebService.ProductLotSerialNoTraceability ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability();
        Record.ProductLotSerialNoTraceability = new MDMITMWebService.ProductLotSerialNoTraceability[] { ProductLotSerialNoTraceability };
        Record.BusinessSegmentName = new MDMITMWebService.BusinessSegmentName();

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ItemMasters.Monitoring = new NFMUtil.Monitoring();
        ItemMasters.Monitoring.MessageGroupNumber = nowStr + '21';
        List<String> eng = new List<String>();
        eng.add('ENG');
        // 品名以外全部 null
        Record.MaterialNumber                = 'MDMITMTst1';
        MaterialDescription.LanguageKey      = 'ZH';
        MaterialDescription.Description      = ' GIF-X0261 : 胃鏡 ';
        Record.ModelForSales                 = 'ABCD1234';
        Record.RepairCode                    = 'RC12345';
        Record.Layer1st                      = 'GI';
        Record.Layer2nd                      = 'L2';
        Record.Layer3rd                      = 'L3';
        Record.Layer4th                      = 'L4';
        Record.Layer5th                      = 'L5';
        ProductLotSerialNoTraceability.Value = 'No tracing';
        Record.LifeTime                      = 3;
        Record.MCompany                      = '製造会社名';
        Record.BaseUnitOfMeasure             = 'PC';
        Record.BusinessSegmentName.Value     = 'G&R';
        Record.To                            = 'OCN';
        Record.CN_String1                    = eng;  
        System.Test.startTest();
        MDMITMWebService.MDMITM(ItemMasters);
        System.Test.stopTest();
        Product2[] rslts = [select Id, ProductCode, ProductCode_Ext__c,
                                   MDM_Name__c, MDM_Model_No__c, Alternative_Products__c,
                                   Name, Asset_Model_No__c,
                                   Repair_Product_Code__c,
                                   BSSCategory__c, Family,
                                   Category2__c, Category3__c,
                                   Category4__c, Category5__c,
                                   Serial_Lot_No__c,
                                   Durable_Years__c,
                                   Brand_Name__c,
                                   Qty_Unit__c
                              from Product2
                             where ProductCode_Ext__c = :Record.MaterialNumber];
        System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode);
            System.assertEquals('MDMITMTst1', rslts[0].ProductCode_Ext__c);
            System.assertEquals('GIF-X0261:胃鏡', rslts[0].Name);
            System.assertEquals('GIF-X0261 : 胃鏡', rslts[0].MDM_Name__c);
            System.assertEquals('GIF-X0261', rslts[0].MDM_Model_No__c);
            System.assertEquals(null, rslts[0].Alternative_Products__c);
            System.assertEquals('ABCD1234', rslts[0].Asset_Model_No__c);
            System.assertEquals('RC12345', rslts[0].Repair_Product_Code__c);
            System.assertEquals('GI', rslts[0].Family);
            System.assertEquals('L2', rslts[0].Category2__c);
            System.assertEquals('L3', rslts[0].Category3__c);
            System.assertEquals('L4', rslts[0].Category4__c);
            System.assertEquals('L5', rslts[0].Category5__c);
            System.assertEquals('No tracing', rslts[0].Serial_Lot_No__c);
            System.assertEquals(3, rslts[0].Durable_Years__c);
            System.assertEquals('製造会社名', rslts[0].Brand_Name__c);
            System.assertEquals('PC', rslts[0].Qty_Unit__c);
            System.assertEquals('G&R', rslts[0].BSSCategory__c);
        }
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'MDMITM'
                                        order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

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