liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@IsTest
class LexNewAndEditBaseControllerTest {
    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();
    @testSetup 
    static void testSetup() {
        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', RecordTypeId = rectAg, lastname='Plumber', AWS_Data_Id__c = 'TestContact', accountId = testRectHpAccounts.id);
        insert testContacts;
        PI_Policy_Configuration__c config1 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Contact');
        insert config1;
        PI_Policy_Configuration__c config2 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'ContactV2');
        insert config2;
        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;
    }
    @isTest
    static void testInitDataMethod1(){
        Account acct = [SELECT Id FROM Account WHERE Name='testRectHpAccount' LIMIT 1];
        Contact cont = [SELECT Name FROM Contact WHERE LastName ='Plumber' LIMIT 1];
        System.Test.startTest();
        LexNewAndEditBaseController.initData(acct.Id,rectCo,'Account');
        LexNewAndEditBaseController.initData(acct.Id,'','Account');
        LexNewAndEditBaseController.queryAccount('["Department_Class__c"]',acct.Id); 
        LexNewAndEditBaseController.searchContactInit(Null,Null); 
        LexNewAndEditBaseController.searchContactInit(Null,cont.Name); 
        LexNewAndEditBaseController.searchContactInit(acct.Id,cont.Name); 
        LexNewAndEditBaseController.checkNullString(Null); 
        LexNewAndEditBaseController.checkNullString('Test'); 
        System.Test.stopTest();
    }
 
    @isTest
    static void testInitDataMethod2(){
        Account acct = [SELECT Id FROM Account WHERE Name='testRectHpAccount' LIMIT 1];
        System.Test.startTest();
        List<Profile> 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/01G10000007ueSUEAY"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000005qJClAAM"},"Name":"販売店_20121225","TableEnumOrId":"Account"},"ProfileId":"'+systemAdminProfileId+'","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/'+systemAdminProfileId+'"},"Name":"系统管理员"},"RecordTypeId":"'+rectCo+'"}]}';
        Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200'));
        String record_type_id = rectCo;
        String objectType = 'Account';
        LexNewAndEditBaseController.initData(acct.Id,record_type_id,objectType);
        List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id,objectType);
        LexNewAndEditBaseController.reviseMetaLayouts(layout); 
        LexNewAndEditBaseController.reviseMetaLayouts(null); 
        System.debug('layout=' + layout);
        System.Test.stopTest();
    }
 
    @isTest
    static void testInitDataMethod3(){
        Contact cont = [SELECT Name,RecordTypeId FROM Contact WHERE LastName ='Plumber' LIMIT 1];
        PI_Policy_Configuration__c prc1 = [SELECT Sobject_Type__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c ='Contact' LIMIT 1];
        PI_Policy_Configuration__c prc2 = [SELECT Sobject_Type__c FROM PI_Policy_Configuration__c WHERE Sobject_Type__c ='ContactV2' LIMIT 1];
        PI_Field_Policy_Detail__c pfpd1 = [SELECT PI_Policy_Configuration_Name__c,PI_Policy_Configuration__c,PI_Policy_Configuration__r.Sobject_Type__c FROM PI_Field_Policy_Detail__c WHERE PI_Policy_Configuration_Name__c ='Contact' and Enable_Encrypt__c = true LIMIT 1];
        PI_Field_Policy_Detail__c pfpd2 = [SELECT PI_Policy_Configuration_Name__c,PI_Policy_Configuration__c,PI_Policy_Configuration__r.Sobject_Type__c FROM PI_Field_Policy_Detail__c WHERE PI_Policy_Configuration_Name__c ='ContactV2' and Enable_Encrypt__c = true LIMIT 1];
        System.Test.startTest();
        List<Profile> 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/00e10000000Y3o5AAC"},"Name":"系统管理员"},"RecordTypeId":"'+rectHp+'"}]}';
        Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200'));
        String record_type_id = rectHp;
        String objectType = prc1.Sobject_Type__c;
        String objectTypeV2 = prc2.Sobject_Type__c;
        List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id,objectType);
        PIHelper.PIIntegration piIntegration1 = PIHelper.getPIIntegrationInfo(objectType);
        PIHelper.PIIntegration piIntegration2 = PIHelper.getPIIntegrationInfo(objectTypeV2);
        Map<String, String> AWSToSobjectNonEncryptedMap = new Map<String, String>();
        List<String> AWSToSobjectNonEncryptedMapKeySet = new List<String>();
        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);
        }
        LexNewAndEditBaseController.initData(cont.Id,cont.RecordTypeId,objectType);
        lexNewAndEditMCController.initData(cont.Id,cont.RecordTypeId,objectType);
        Map<String, String> expected = new Map<String, String>();
        expected.put('phone1','Phone1');
        System.assertEquals(expected,AWSToSobjectNonEncryptedMap);
        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;
        }
    }
}