@isTest private class NFM107WebServiceTest { @isTest static void testNFM107Repair() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { return; } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { return; } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM107TestCompany'; 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 = 'NFM107TestDepart'; 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 = 'NFM107Prd1'; prd1.ProductCode = 'NFM107Prd1'; prd1.Repair_Product_Code__c = 'NFM107Prd1_RP'; prd1.Name = 'NFM107Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM107Prd2'; prd2.ProductCode = 'NFM107Prd2'; prd2.Repair_Product_Code__c = 'NFM107Prd2_RP'; prd2.Name = 'NFM107Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM107Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM107SerialNumber'; 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 = 'NFM107Rpr1'; 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]; // そのままreturn // null NFM107WebService.NFM107(null); NFM107WebService.Invoice Invoice = new NFM107WebService.Invoice(); NFM107WebService.GeneralData GeneralData = new NFM107WebService.GeneralData(); Invoice.GeneralData = new NFM107WebService.GeneralData[] { GeneralData }; // Monitoringをセットしない NFM107WebService.NFM107(Invoice); Datetime nowDT = Datetime.now(); String nowStr = nowDT.format('yyyyMMddHHmm'); Invoice.Monitoring = new NFMUtil.Monitoring(); Invoice.Monitoring.MessageGroupNumber = nowStr + '01'; // 必須項目をセットしない NFM107WebService.NFM107(Invoice); // PaymentSource Undefined Invoice.Monitoring.MessageGroupNumber = nowStr + '02'; GeneralData.SFDCRepairNo = 'ABC'; NFM107WebService.NFM107(Invoice); List iflogList = [select Id, RowDataFlg__c, Log__c, ErrorLog__c from BatchIF_Log__c where MessageGroupNumber__c = :Invoice.Monitoring.MessageGroupNumber]; System.assertEquals(1, iflogList.size()); System.assertEquals(true, iflogList[0].RowDataFlg__c); System.assertEquals('[{"SFDCRepairNo":"ABC","ReverseFlag":null,"PayerNo":null,"InvoiceResource":null,"InvoiceAmount":null,"GoldenTaxInvoiceNo":null,"GoldenTaxDate":null,"BilltoPartyNo":null,"AccuTotalInvoiceAmount":null,"AccuTotalCost":null}]', iflogList[0].Log__c); // PaymentSource Undefined NFMUtil.MaxLogColumnLength = 2; Invoice.Monitoring.MessageGroupNumber = nowStr + '03'; GeneralData.SFDCRepairNo = 'ABC'; NFM107WebService.NFM107(Invoice); 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 = :Invoice.Monitoring.MessageGroupNumber]; System.debug('iflogList=' + iflogList); System.assertEquals(1, iflogList.size()); System.assertEquals(true, iflogList[0].RowDataFlg__c); System.assertEquals('[{"SFDCRepairNo":"ABC","ReverseFlag":null,"PayerNo":null,"InvoiceResource":null,"InvoiceAmount":null,"GoldenTaxInvoiceNo":null,"GoldenTaxDate":null,"BilltoPartyNo":null,"AccuTotalInvoiceAmount":null,"AccuTotalCost":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 より upsert テスト @isTest static void testExecute_upsert() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { return; } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { return; } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM107TestCompany'; upsert company; company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id]; 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 = 'NFM107TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id]; // 再取得 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 = 'NFM107Prd1'; prd1.ProductCode = 'NFM107Prd1'; prd1.Repair_Product_Code__c = 'NFM107Prd1_RP'; prd1.Name = 'NFM107Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM107Prd2'; prd2.ProductCode = 'NFM107Prd2'; prd2.Repair_Product_Code__c = 'NFM107Prd2_RP'; prd2.Name = 'NFM107Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM107Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM107SerialNumber'; 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 = 'NFM107Rpr1'; 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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1'; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null' + ',"PayerNo":"' + depart.Management_Code__c + '"' + ',"InvoiceResource":"维修单","InvoiceAmount":null' + ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"' + ',"GoldenTaxDate":null' + ',"BilltoPartyNo":"00' + company.Management_Code__c + '"' + ',"AccuTotalInvoiceAmount":null,"AccuTotalCost":null}]' ); insert rawData; // Test & assert Test.startTest(); NFM107WebService.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, PayerNo__c, PayerNoAccount__c, BilltoPartyNo__c, BilltoPartyNoAccount__c from Repair_receipt__c where UniqueKey__c = :strUniqueKey]; System.assertEquals('GoldenTaxInvoiceNo1', rc_Res.Name); System.assertEquals(depart.Management_Code__c, rc_Res.PayerNo__c); System.assertEquals('00' + company.Management_Code__c, rc_Res.BilltoPartyNo__c); System.assertEquals(depart.Id, rc_Res.PayerNoAccount__c); System.assertEquals(company.Id, rc_Res.BilltoPartyNoAccount__c); } // rawdata より upsert テスト // PayerNo not exist @isTest static void testExecute_upsert2() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { return; } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { return; } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM107TestCompany'; upsert company; company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id]; 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 = 'NFM107TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id]; // 再取得 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 = 'NFM107Prd1'; prd1.ProductCode = 'NFM107Prd1'; prd1.Repair_Product_Code__c = 'NFM107Prd1_RP'; prd1.Name = 'NFM107Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM107Prd2'; prd2.ProductCode = 'NFM107Prd2'; prd2.Repair_Product_Code__c = 'NFM107Prd2_RP'; prd2.Name = 'NFM107Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM107Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM107SerialNumber'; 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 = 'NFM107Rpr1'; 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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1'; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null' + ',"PayerNo":"1' + depart.Management_Code__c + '"' + ',"InvoiceResource":"维修单","InvoiceAmount":null' + ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"' + ',"GoldenTaxDate":null' + ',"BilltoPartyNo":"' + company.Management_Code__c + '"' + ',"AccuTotalInvoiceAmount":null,"AccuTotalCost":null}]' ); insert rawData; // Test & assert Test.startTest(); NFM107WebService.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.assert((iflogList[0].ErrorLog__c.indexOf('GoldenTaxInvoiceNo1:PayerNo[') >= 0), iflogList[0].ErrorLog__c); // System.assertEquals(null, iflogList[0].Log__c); Repair_receipt__c rc_Res = [select Id, Name, PayerNo__c, PayerNoAccount__c, BilltoPartyNo__c, BilltoPartyNoAccount__c from Repair_receipt__c where UniqueKey__c = :strUniqueKey]; System.assertEquals('GoldenTaxInvoiceNo1', rc_Res.Name); System.assertEquals('1' + depart.Management_Code__c, rc_Res.PayerNo__c); System.assertEquals(company.Management_Code__c, rc_Res.BilltoPartyNo__c); System.assertEquals(null, rc_Res.PayerNoAccount__c); System.assertEquals(company.Id, rc_Res.BilltoPartyNoAccount__c); } // rawdata より update テスト @isTest static void testExecute_update() { List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科']; if (rectSct.size() == 0) { return; } List rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { return; } // テストデータ Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM107TestCompany'; upsert company; company = [Select Id, Management_Code__c, Name from Account where Id = :company.Id]; 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 = 'NFM107TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; depart = [Select Id, Management_Code__c, Name from Account where Id = :depart.Id]; // 再取得 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 = 'NFM107Prd1'; prd1.ProductCode = 'NFM107Prd1'; prd1.Repair_Product_Code__c = 'NFM107Prd1_RP'; prd1.Name = 'NFM107Prd1'; prd1.Manual_Entry__c = false; prdList.add(prd1); Product2 prd2 = new Product2(); prd2.ProductCode_Ext__c = 'NFM107Prd2'; prd2.ProductCode = 'NFM107Prd2'; prd2.Repair_Product_Code__c = 'NFM107Prd2_RP'; prd2.Name = 'NFM107Prd2'; prd2.Manual_Entry__c = false; prdList.add(prd2); insert prdList; Asset ast = new Asset(); ast.Name = 'NFM107Ast1'; ast.AccountId = depart.Id; ast.Department_Class__c = section.Id; ast.Hospital__c = company.Id; ast.Product2Id = prd1.Id; ast.SerialNumber = 'NFM107SerialNumber'; 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 = 'NFM107Rpr1'; 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 strUniqueKey = rpr.Id + ':GoldenTaxInvoiceNo1'; // rawdata BatchIF_Log__c rawData = new BatchIF_Log__c( RowDataFlg__c = true, retry_cnt__c = 1, MessageGroupNumber__c = 'MG_1234567890', Log__c = '[{"SFDCRepairNo":"' + rpr.Name + '","ReverseFlag":null' + ',"PayerNo":null' + ',"InvoiceResource":"维修成本","InvoiceAmount":null' + ',"GoldenTaxInvoiceNo":"GoldenTaxInvoiceNo1"' + ',"GoldenTaxDate":"20160314"' + ',"BilltoPartyNo":"00' + company.Management_Code__c + '"' + ',"AccuTotalInvoiceAmount":null' + ',"AccuTotalCost":"6000.00"}]' ); insert rawData; // Test & assert Test.startTest(); NFM107WebService.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__c rc = [select Id, Name, Repair_cost__c, Repair_cost_date__c from Repair__c where Id = :rpr.Id]; System.assertEquals(6000, rc.Repair_cost__c); System.assertEquals(Date.newInstance(2016,3,14), rc.Repair_cost_date__c); } }