// TODO サビコンのテスト @isTest private class NFM108WebServiceTest { // そのままreturn のパターン // rawdataの確認 static testMethod void testNFM108Repair_void() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw new ControllerUtil.myException('not found 病院 recodetype'); } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype'); } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { throw new ControllerUtil.myException('not found 診療科 消化科 recodetype'); } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM108TestCompany'; upsert company; Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id]; Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM108TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; // 再取得 List accList = new List(); company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id]; accList.add(company); section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id]; accList.add(section); depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id]; accList.add(depart); List prdList = new List(); Product2 prd1 = new Product2(); prd1.ProductCode_Ext__c = 'NFM108Prd1'; prd1.ProductCode = 'NFM108Prd1'; prd1.Repair_Product_Code__c = 'NFM108Prd1_RP'; prd1.Name = 'NFM108Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM108Prd2'; prd2.ProductCode = 'NFM108Prd2'; prd2.Repair_Product_Code__c = 'NFM108Prd2_RP'; prd2.Name = 'NFM108Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM108Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM108SerialNumber'; ast.Guarantee_period_for_products__c = Date.today(); ast.InstallDate = Date.today(); insert ast; ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber from Asset where Id = :ast.Id]; Repair__c rpr = new Repair__c(); rpr.SAPRepairNo__c = 'NFM108Rpr1'; rpr.Account__c = depart.Id; rpr.Department_Class__c = section.Id; rpr.Hospital__c = company.Id; rpr.Delivered_Product__c = ast.Id; rpr.SalesOfficeCode_selection__c = '北京'; insert rpr; rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id]; // そのままreturn // null NFM108WebService.NFM108(null); NFM108WebService.Payment Payment = new NFM108WebService.Payment(); NFM108WebService.GeneralData GeneralData = new NFM108WebService.GeneralData(); Payment.GeneralData = new NFM108WebService.GeneralData[] { GeneralData }; // Monitoringをセットしない NFM108WebService.NFM108(Payment); Datetime nowDT = Datetime.now(); String nowStr = nowDT.format('yyyyMMddHHmm'); Payment.Monitoring = new NFMUtil.Monitoring(); Payment.Monitoring.MessageGroupNumber = nowStr + '01'; // 必須項目をセットしない NFM108WebService.NFM108(Payment); // PaymentSource Undefined Payment.Monitoring.MessageGroupNumber = nowStr + '02'; GeneralData.SFDCRepairNo = 'ABC'; NFM108WebService.NFM108(Payment); List iflogList = [select Id, RowDataFlg__c, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = :Payment.Monitoring.MessageGroupNumber]; System.assertEquals(1, iflogList.size()); System.assertEquals(true, iflogList[0].RowDataFlg__c); System.assertEquals('[{"SFDCRepairNo":"ABC","PaymentSource":null,"PaymentDate":null,"PaymentAmount":null,"BaddebtsFlg":null,"ArrearsBasePaymentTerm":null,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]', iflogList[0].Log__c); // PaymentSource Undefined NFMUtil.MaxLogColumnLength = 2; Payment.Monitoring.MessageGroupNumber = nowStr + '03'; GeneralData.SFDCRepairNo = 'ABC'; NFM108WebService.NFM108(Payment); iflogList = [select Id, RowDataFlg__c, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c from BatchIF_Log__c where MessageGroupNumber__c = :Payment.Monitoring.MessageGroupNumber]; System.debug('iflogList=' + iflogList); System.assertEquals(1, iflogList.size()); System.assertEquals(true, iflogList[0].RowDataFlg__c); System.assertEquals('[{"SFDCRepairNo":"ABC","PaymentSource":null,"PaymentDate":null,"PaymentAmount":null,"BaddebtsFlg":null,"ArrearsBasePaymentTerm":null,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]', iflogList[0].Log__c + iflogList[0].Log2__c + iflogList[0].Log3__c + iflogList[0].Log4__c + iflogList[0].Log5__c + iflogList[0].Log6__c + iflogList[0].Log7__c + iflogList[0].Log8__c + iflogList[0].Log9__c + iflogList[0].Log10__c + iflogList[0].Log11__c + iflogList[0].Log12__c + iflogList[0].ErrorLog__c); } // rawdata より Insert テスト static testMethod void testExecute_insert() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw new ControllerUtil.myException('not found 病院 recodetype'); } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype'); } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { throw new ControllerUtil.myException('not found 診療科 消化科 recodetype'); } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM108TestCompany'; upsert company; Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id]; Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM108TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; // 再取得 List accList = new List(); company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id]; accList.add(company); section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id]; accList.add(section); depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id]; accList.add(depart); List prdList = new List(); Product2 prd1 = new Product2(); prd1.ProductCode_Ext__c = 'NFM108Prd1'; prd1.ProductCode = 'NFM108Prd1'; prd1.Repair_Product_Code__c = 'NFM108Prd1_RP'; prd1.Name = 'NFM108Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM108Prd2'; prd2.ProductCode = 'NFM108Prd2'; prd2.Repair_Product_Code__c = 'NFM108Prd2_RP'; prd2.Name = 'NFM108Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM108Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM108SerialNumber'; ast.Guarantee_period_for_products__c = Date.today(); ast.InstallDate = Date.today(); insert ast; ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber from Asset where Id = :ast.Id]; Repair__c rpr = new Repair__c(); rpr.SAPRepairNo__c = 'NFM108SAP'; rpr.Account__c = depart.Id; rpr.Department_Class__c = section.Id; rpr.Hospital__c = company.Id; rpr.Delivered_Product__c = ast.Id; rpr.SalesOfficeCode_selection__c = '北京'; insert rpr; rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id]; String strSFDCRepairNo = rpr.Name + ':123'; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"' + strSFDCRepairNo + '","PaymentSource":"维修单","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]' ); insert rawData; // Test & assert Test.startTest(); NFM108WebService.execute(rawData.Id); Test.stopTest(); List iflogList = [select Id, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = 'MG_1234567890' and RowDataFlg__c = false]; System.assertEquals(1, iflogList.size()); System.assertEquals(null, iflogList[0].ErrorLog__c); // System.assertEquals(null, iflogList[0].Log__c); Repair_receipt__c rc_Res = [select Id, Name, SFDCRepairNo__c, PaymentVoucher__c from Repair_receipt__c where UniqueKey__c = :strSFDCRepairNo]; System.assertEquals(strSFDCRepairNo, rc_Res.Name); System.assertEquals(rpr.Name, rc_Res.SFDCRepairNo__c); System.assertEquals('123', rc_Res.PaymentVoucher__c); rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c, Received_Date__c, BaddebtsDate__c from Repair__c where Id = :rpr.Id]; System.assertEquals(Date.newInstance(2016, 1, 1), rpr.Received_Date__c); System.assertEquals(Date.newInstance(2016, 1, 1), rpr.BaddebtsDate__c); } // rawdata より Resend テスト static testMethod void testExecute_resend() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw new ControllerUtil.myException('not found 病院 recodetype'); } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype'); } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { throw new ControllerUtil.myException('not found 診療科 消化科 recodetype'); } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM108TestCompany'; upsert company; Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id]; Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM108TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; // 再取得 List accList = new List(); company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id]; accList.add(company); section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id]; accList.add(section); depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id]; accList.add(depart); List prdList = new List(); Product2 prd1 = new Product2(); prd1.ProductCode_Ext__c = 'NFM108Prd1'; prd1.ProductCode = 'NFM108Prd1'; prd1.Repair_Product_Code__c = 'NFM108Prd1_RP'; prd1.Name = 'NFM108Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM108Prd2'; prd2.ProductCode = 'NFM108Prd2'; prd2.Repair_Product_Code__c = 'NFM108Prd2_RP'; prd2.Name = 'NFM108Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM108Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM108SerialNumber'; ast.Guarantee_period_for_products__c = Date.today(); ast.InstallDate = Date.today(); insert ast; ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber from Asset where Id = :ast.Id]; Repair__c rpr = new Repair__c(); rpr.SAPRepairNo__c = 'NFM108SAP'; rpr.Account__c = depart.Id; rpr.Department_Class__c = section.Id; rpr.Hospital__c = company.Id; rpr.Delivered_Product__c = ast.Id; rpr.SalesOfficeCode_selection__c = '北京'; insert rpr; rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id]; String strSFDCRepairNo = rpr.Name + ':123'; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, retry_cnt__c = 1, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"' + strSFDCRepairNo + '","PaymentSource":"维修单","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]' ); insert rawData; // Test & assert Test.startTest(); NFM108WebService.execute(rawData.Id); Test.stopTest(); List bl = [Select Id, Is_Error__c, Type__c, Log__c, ErrorLog__c,retry_cnt__c from BatchIF_Log__c where Id = :rawData.id]; System.assertEquals(0, bl[0].retry_cnt__c); List iflogList = [select Id, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = 'MG_1234567890' and RowDataFlg__c = false]; System.assertEquals(1, iflogList.size()); System.assertEquals(null, iflogList[0].ErrorLog__c); // System.assertEquals(null, iflogList[0].Log__c); Repair_receipt__c rc_Res = [select Id, Name, SFDCRepairNo__c, PaymentVoucher__c from Repair_receipt__c where UniqueKey__c = :strSFDCRepairNo]; System.assertEquals(strSFDCRepairNo, rc_Res.Name); System.assertEquals(rpr.Name, rc_Res.SFDCRepairNo__c); System.assertEquals('123', rc_Res.PaymentVoucher__c); rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c, Received_Date__c, BaddebtsDate__c from Repair__c where Id = :rpr.Id]; System.assertEquals(Date.newInstance(2016, 1, 1), rpr.Received_Date__c); System.assertEquals(Date.newInstance(2016, 1, 1), rpr.BaddebtsDate__c); } static testMethod void testExecute2() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw new ControllerUtil.myException('not found 病院 recodetype'); } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype'); } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { throw new ControllerUtil.myException('not found 診療科 消化科 recodetype'); } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM108TestCompany'; upsert company; Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id]; Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM108TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; // 再取得 List accList = new List(); company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id]; accList.add(company); section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id]; accList.add(section); depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id]; accList.add(depart); List prdList = new List(); Product2 prd1 = new Product2(); prd1.ProductCode_Ext__c = 'NFM108Prd1'; prd1.ProductCode = 'NFM108Prd1'; prd1.Repair_Product_Code__c = 'NFM108Prd1_RP'; prd1.Name = 'NFM108Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM108Prd2'; prd2.ProductCode = 'NFM108Prd2'; prd2.Repair_Product_Code__c = 'NFM108Prd2_RP'; prd2.Name = 'NFM108Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM108Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM108SerialNumber'; ast.Guarantee_period_for_products__c = Date.today(); ast.InstallDate = Date.today(); insert ast; ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber from Asset where Id = :ast.Id]; Repair__c rpr = new Repair__c(); rpr.SAPRepairNo__c = 'NFM108SAP'; rpr.Account__c = depart.Id; rpr.Department_Class__c = section.Id; rpr.Hospital__c = company.Id; rpr.Delivered_Product__c = ast.Id; rpr.SalesOfficeCode_selection__c = '北京'; insert rpr; rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id]; String strSFDCRepairNo = rpr.Name + ':123'; Maintenance_Contract__c mc1 = new Maintenance_Contract__c(); mc1.Name = 'mc1'; mc1.Past_update_contract__c = true; mc1.RecordTypeId = '01210000000QjeFAAS'; mc1.CurrencyIsoCode = 'CNY'; mc1.Status__c = '契約'; mc1.Hospital__c = company.Id; mc1.Department_Class__c = section.Id; mc1.Department__c = depart.Id; mc1.Maintenance_Contract_No__c = 'mc1'; mc1.Contract_Conclusion_Date__c = Date.today(); mc1.Contract_End_Date__c = Date.today().addDays(1); mc1.SalesOfficeCode_selection__c = '北京RC'; insert mc1; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"mc1","PaymentSource":"维修合同","PaymentDate":20160101,"PaymentAmount":12345,"BaddebtsFlg":"X","ArrearsBasePaymentTerm":0,"ArrearsBaseInvoice":null,"ARAmountBasePaymentTerm":null,"ARAmountBaseInvoice":null,"ARAmountBaseAverage":null,"AccumulativeTotalPaymentAmount":null,"AccumulativeARAmountBaseAverage":null}]' ); insert rawData; // Test & assert Test.startTest(); NFM108WebService.execute(rawData.Id); Test.stopTest(); } }