@IsTest class LexNewAndEditReportPIPLControllerTest { public static String rectCo = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId(); public static String rectHp = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(); public static String rectAg = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId(); public static String rNTC = Schema.SObjectType.Report__c.getRecordTypeInfosByDeveloperName().get('NTC').getRecordTypeId(); public static User thisUser = [ SELECT Id from User WHERE Id = :UserInfo.getUserId()]; @testSetup static void testSetup() { System.runAs(thisUser){ Account testRectcoAccounts = new Account(Name = 'testRectcoAccount', RecordTypeId = rectHp); insert testRectcoAccounts; Account testRectHpAccounts = new Account(Name = 'testRectHpAccount', RecordTypeId = rectCo, Hospital__c = testRectcoAccounts.id); insert testRectHpAccounts; Contact testContacts = new Contact(email='jplumber@salesforce.com', firstname='Joe', lastname='Plumber',RecordTypeId = rectAg, AWS_Data_Id__c = 'TestContact', accountId = testRectHpAccounts.id); insert testContacts; PI_Policy_Configuration__c config1 = new PI_Policy_Configuration__c(Sobject_Type__c = 'Report__c'); insert config1; PI_Policy_Configuration__c config2 = new PI_Policy_Configuration__c(Sobject_Type__c = 'Report__cV2'); insert config2; PI_Policy_Configuration__c config3 = new PI_Policy_Configuration__c(Sobject_Type__c = 'Contact'); insert config3; PI_Field_Policy_Detail__c pfpd1 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone1', SF_Field_API_Name__c = 'Phone1',PI_Policy_Configuration__c = config1.Id, Enable_Encrypt__c=true); insert pfpd1; PI_Field_Policy_Detail__c pfpd2 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config2.Id, Enable_Encrypt__c=true); insert pfpd2; PI_Field_Policy_Detail__c pfpd3 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config3.Id, Enable_Encrypt__c=true); insert pfpd3; Report__c rep1 = new Report__c(); rep1.CurrencyIsoCode = 'CNY'; rep1.recordtypeId = rNTC; rep1.Manual_Name__c = '内镜中心'; rep1.Status__c = ''; rep1.Purpose1__c = 'OPD'; rep1.Purpose2__c = '引合追及活動'; rep1.Reason__c = '実施背景'; rep1.Appeal_Point__c = '実施目的・訴求点'; rep1.Strategic_Department_Class__c = '1.消化科'; rep1.Technical_Category1__c = '消化道检查'; rep1.Technical_Category2__c = '普通胃镜检查'; rep1.Patient_Status__c = '患者状況'; rep1.Operation_Process__c = '手術プロセス'; rep1.Operation_Task__c = '手術上の課題'; rep1.Product_Evaluation_Advantage__c = '製品評価利点'; rep1.Product_Evaluation_Faults__c = '製品評価欠点'; rep1.OPD_Summarize__c = 'OPD実施総括'; rep1.Activity_Plan__c = '今後の活動計画'; rep1.Opportunity_Situation__c = '引合無'; rep1.OPD_ProductCategory1__c ='3D System'; rep1.OPD_ProductCategory2__c ='OR Imaging Products'; rep1.Hospital_Reference__c = testRectcoAccounts.Id; rep1.Status__c = '草案中'; rep1.Date__c= Date.today().addDays(-1); insert rep1; } } @isTest static void tesMethod1(){ System.runAs(thisUser){ Account acct = [SELECT Id FROM Account WHERE RecordTypeId =:rectCo LIMIT 1]; Contact cont = [SELECT Name FROM Contact WHERE RecordTypeId =:rectAg LIMIT 1]; Report__c rept = [SELECT Id FROM Report__c WHERE RecordTypeId =:rNTC LIMIT 1]; PI_Policy_Configuration__c prc1 = [SELECT Sobject_Type__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c ='Report__c' LIMIT 1]; PI_Policy_Configuration__c prc2 = [SELECT Sobject_Type__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c ='Report__cV2' LIMIT 1]; List p = [SELECT Id FROM Profile WHERE Name = '系统管理员']; if(p.size() == 0) { p = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; } Id systemAdminProfileId = p[0].Id; String body='{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G10000007udmsEAA"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000005q7duAAA"},"Name":"NTC/TTC","TableEnumOrId":"01I10000000SWfeEAG"},"ProfileId":"'+systemAdminProfileId+'","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/'+systemAdminProfileId+'"},"Name":"系统管理员"},"RecordTypeId":"'+rectCo+'"}]}'; String objectType = prc1.Sobject_Type__c; String objectTypeV2 = prc2.Sobject_Type__c; Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200')); System.Test.startTest(); List layout = MetaDataUtility.GetRecordTypePageLayout(rNTC,objectType); PIHelper.PIIntegration piIntegration1 = PIHelper.getPIIntegrationInfo(objectType); PIHelper.PIIntegration piIntegration2 = PIHelper.getPIIntegrationInfo(objectTypeV2); Map AWSToSobjectNonEncryptedMap = new Map(); List AWSToSobjectNonEncryptedMapKeySet = new List(); for (PI_Field_Policy_Detail__c PIDetail : piIntegration1.PIDetails) { AWSToSobjectNonEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c); AWSToSobjectNonEncryptedMapKeySet.add(PIDetail.AWS_Field_API__c); } LexNewAndEditBasePIPLController.initData(rept.Id,rNTC,objectType); Map expected = new Map(); expected.put('phone1','Phone1'); System.assertEquals(expected,AWSToSobjectNonEncryptedMap); LexNewAndEditReportPIPLController.initData(rept.Id, rNTC,objectType); LexNewAndEditReportPIPLController.queryAccount('["Department_Class__c"]',acct.Id); LexNewAndEditReportPIPLController.searchContactInit(acct.Id,cont.Name); System.Test.stopTest(); } } @isTest static void tesMethod2(){ User thisUser = [ select Id from User where Id = :UserInfo.getUserId()]; System.runAs(thisUser){ Contact cont = [SELECT Id FROM Contact WHERE LastName ='Plumber' LIMIT 1]; PI_Policy_Configuration__c prc3 = [SELECT Sobject_Type__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c ='Contact' LIMIT 1]; List p = [SELECT Id FROM Profile WHERE Name = '系统管理员']; if(p.size() == 0) { p = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; } Id systemAdminProfileId = p[0].Id; String body='{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G10000007vQmsEAE"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000005qCPIAA2"},"Name":"販売店","TableEnumOrId":"Contact"},"ProfileId":"'+systemAdminProfileId+'","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/'+systemAdminProfileId+'"},"Name":"系统管理员"},"RecordTypeId":"'+rectHp+'"}]}'; Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200')); System.Test.startTest(); String objectTypeCon= prc3.Sobject_Type__c; List layoutCon = MetaDataUtility.GetRecordTypePageLayout(rectAg,objectTypeCon); PIHelper.PIIntegration piIntegration3 = PIHelper.getPIIntegrationInfo(objectTypeCon); Map AWSToSobjectNonEncryptedMap = new Map(); List AWSToSobjectNonEncryptedMapKeySet = new List(); for (PI_Field_Policy_Detail__c PIDetail : piIntegration3.PIDetails) { AWSToSobjectNonEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c); AWSToSobjectNonEncryptedMapKeySet.add(PIDetail.AWS_Field_API__c); } LexNewAndEditBasePIPLController.initData(cont.Id, rectAg,objectTypeCon); Map expected = new Map(); expected.put('phone2','Phone2'); System.assertEquals(expected,AWSToSobjectNonEncryptedMap); LexNewAndEditRentalApplyPIPLController.initData(cont.Id, rectAg,objectTypeCon); System.Test.stopTest(); } } public class BaseHttpMock implements HttpCalloutMock { String body =''; String status = ''; String statusCode = ''; public BaseHttpMock(String body,String status,String statusCode){ this.body = body; this.status = status; this.statusCode = statusCode; } public HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody(body); res.setStatus('OK'); res.setStatusCode(200); return res; } } }