buli
2023-07-11 80a3f59e2d3df07805bc67e329300b8de90a5b3a
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Author: Guo, Aline Yaying
 * Created Date: 03/22/2022
 * Purpose: Test Class
 * History:
 *      03/22/2022 - Guo, Aline Yaying - Initial Code.
 *
 * */
@isTest
private class NewAndEditLeadControllerTest {
    @TestSetup
    static void makeData() {
        List<String> strList = new List<String>();
        strList.add('Lead');
        strList.add('Contact');
        strList.add('Inquiry_form__c');
        strList.add('LeadV2');
        TestDataUtility.CreatePIPolicyConfigurations(strList);
    }
    static testMethod void testMethod1() {
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());
        Test.startTest();
        List<RecordType> rectCo = [
            SELECT Id
            FROM RecordType
            WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
        ];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectAcc = [
            SELECT Id
            FROM RecordType
            WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '戦略科室分類 呼吸科'
        ];
        if (rectAcc.size() == 0) {
            return;
        }
        Account accountItem1 = new Account(Name = 'testaccount001', Dealer_discount__c = 10, RecordTypeId = rectCo[0].Id);
        insert accountItem1;
        Account accountItem3 = new Account(
            Name = 'testaccount001',
            Dealer_discount__c = 10,
            RecordTypeId = rectAcc[0].Id,
            ParentId = accountItem1.Id
        );
        insert accountItem3;
        Account accountItem2 = new Account(
            Name = 'testaccount002',
            Dealer_discount__c = 10,
            RecordTypeId = rectCo[0].Id,
            Hospital__c = accountItem1.Id,
            Department_Class__c = accountItem3.Id
        );
        insert accountItem2;
 
        Contact contact2 = new Contact();
        contact2.AccountId = accountItem2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.AWS_Data_Id__c = 'Test123';
        insert contact2;
 
        Lead leadTest = new Lead();
        leadTest.LastName = 'test';
        leadTest.Company = 'test';
        leadTest.hospital__c = accountItem2.Hospital__c;
        leadTest.Contact_Name__c = contact2.Id;
        insert leadTest;
 
        Inquiry_form__c inquiryform = new Inquiry_form__c();
        inquiryform.Name = '2023062001';
        inquiryform.Request1__c = '需要报价';
        inquiryform.Product1__c = '超声';
        inquiryform.Hospital__c = accountItem2.Hospital__c;
        inquiryform.Contact_Name__c = contact2.Id;
 
        inquiryform.Opportunity_Division__c = '询价';
        insert inquiryform;
 
        String contactDataId = contact2.AWS_Data_Id__c;
 
        Boolean isClone = true;
        Boolean isNew = true;
        Map<string, string> AWSToSobjectEncryptedMap = new Map<String, String>();
        String AWSToSobjectEncryptedMapJson = JSON.serialize(AWSToSobjectEncryptedMap);
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Lead');
        for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
            AWSToSobjectEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c);
        }
        Map<string, string> mso = ApexPages.currentPage().getParameters();
        mso.put('newclone', 'newclone');
        mso.put('CF00N1000000962np_lkid', inquiryform.Id);
        mso.put('CF00N10000006ps6f_lkid', contact2.Id);
        mso.put('CF00N10000002CvC5_lkid', accountItem2.Hospital__c);
        mso.put('CF00N10000002CvC5', 'Company');
        mso.put('LeadSource', 'LeadSource');
 
        String InquiryFormId = mso.get('CF00N1000000962np_lkid');
        String contactId = mso.get('CF00N10000006ps6f_lkid');
        Boolean isNewMode = false;
        String recordId = leadTest.Id;
        String rid = '';
        ApexPages.StandardController con = new ApexPages.StandardController(leadTest);
        NewAndEditLeadController lead = new NewAndEditLeadController(con);
        Inquiry_form__c ifc = lead.ifc;
        String token = lead.awsToken;
        String AWSDataId = lead.AWSDataId;
        String AWSDataIdInquiryForm = lead.AWSDataIdInquiryForm;
        contactId = lead.contactId; //For Lookup field
        String contactsInfo = lead.contactsInfo; //key sfid;value awsid
        Map<String, String> sfIdToAWSIdMap = new Map<String, String>();
 
        SObject obj = con.getRecord();
        obj.put('Hospital_Name__c', mso.get('CF00N10000002CvC5_lkid'));
        String hospitalId = mso.get('CF00N10000002CvC5_lkid');
        if (obj.Id != null) {
            Lead leadData = [
                SELECT Id, RecordTypeId, AWS_Data_Id__c, Contact_Name__r.Id, Contact_Name__r.AWS_Data_Id__c
                FROM Lead
                WHERE id = :obj.Id
            ];
            String rtTypeId = leadData.RecordTypeId;
            AWSDataId = leadData.AWS_Data_Id__c;
            if (leadData.Contact_Name__r.Id != null) {
                sfIdToAWSIdMap.put(String.valueof(leadData.Contact_Name__r.Id).subString(0, 15), leadData.Contact_Name__r.AWS_Data_Id__c);
            }
            contactsInfo = JSON.serialize(sfIdToAWSIdMap);
        }
 
        String leadJson = '{"RecordTypeId":"01210000000QiRaAAK","Lead_No__c":"IN-JS-2022040127183","Status":"未跟进","Hospital_Name__c":"0011000000VAPEt","Department_Class__c":"0011000000V9bh5","Salutation":"","LastName":"***","Contact_Name__c":"0031m00000DHrkV","Cancel_Reason__c":"","Phone":"","Opp_Name_Search__c":"000000000000000","Email":"","Opp_Name__c":"Test","owner_not_automatically_update__c":false,"Purchase_Plan__c":"有(无预算)","Lead_Inquiry_form__c":"000000000000000","Opportunity_Division__c":"询价","LeadSource":"电话","Campaign__c":"000000000000000","urgent__c":false,"SI_PromoteInquiry__c":false,"Request__c":"","Request_Detail__c":"","Opportunity_stage__c":"预算没有批准","Close_Forecasted_Date__c":"2022/04/01","Purchase_Reason__c":"更新","Sales_Method__c":"单一来源采购","Fund_Basis__c":"銀行資金","Purchase_Type__c":"一般引合","Company":"江苏省人民医院 呼吸科 呼吸科","Name_Encrypted__c":"48a2693f6bf73d16e0abd48caf7fb756","Phone_Encrypted__c":"","Email_Encrypted__c":"","AWS_Data_Id__c":"959584917346320385"}';
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', false);
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', true);
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', isNew);
 
        Test.stopTest();
    }
 
    static testMethod void testMethod2() {
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());
        Test.startTest();
        List<RecordType> rectCo = [
            SELECT Id
            FROM RecordType
            WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
        ];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectAcc = [
            SELECT Id
            FROM RecordType
            WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '戦略科室分類 呼吸科'
        ];
        if (rectAcc.size() == 0) {
            return;
        }
        Account accountItem1 = new Account(Name = 'testaccount001', Dealer_discount__c = 10, RecordTypeId = rectCo[0].Id);
        insert accountItem1;
        Account accountItem3 = new Account(
            Name = 'testaccount001',
            Dealer_discount__c = 10,
            RecordTypeId = rectAcc[0].Id,
            ParentId = accountItem1.Id
        );
        insert accountItem3;
        Account accountItem2 = new Account(
            Name = 'testaccount002',
            Dealer_discount__c = 10,
            RecordTypeId = rectCo[0].Id,
            Hospital__c = accountItem1.Id,
            Department_Class__c = accountItem3.Id
        );
        insert accountItem2;
 
        Contact contact2 = new Contact();
        contact2.AccountId = accountItem2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.AWS_Data_Id__c = 'Test123';
        insert contact2;
 
        Lead leadTest = new Lead();
        leadTest.LastName = 'test';
        leadTest.Company = 'test';
        leadTest.hospital__c = accountItem2.Hospital__c;
        leadTest.Contact_Name__c = contact2.Id;
        insert leadTest;
 
        Inquiry_form__c inquiryform = new Inquiry_form__c();
        inquiryform.Name = '2023062001';
        inquiryform.Request1__c = '需要报价';
        inquiryform.Product1__c = '超声';
        inquiryform.Hospital__c = accountItem2.Hospital__c;
        inquiryform.Contact_Name__c = contact2.Id;
 
        inquiryform.Opportunity_Division__c = '询价';
        insert inquiryform;
 
        String contactDataId = contact2.AWS_Data_Id__c;
 
        Boolean isClone = true;
        Boolean isNew = true;
        Map<string, string> AWSToSobjectEncryptedMap = new Map<String, String>();
        String AWSToSobjectEncryptedMapJson = JSON.serialize(AWSToSobjectEncryptedMap);
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Lead');
        for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
            AWSToSobjectEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c);
        }
        Map<string, string> mso = ApexPages.currentPage().getParameters();
        mso.put('newclone', 'newclone');
        mso.put('CF00N1000000962np_lkid', null);
        mso.put('CF00N10000006ps6f_lkid', contact2.Id);
        mso.put('CF00N10000002CvC5_lkid', accountItem2.Hospital__c);
        mso.put('CF00N10000002CvC5', 'Company');
        mso.put('LeadSource', 'LeadSource');
 
        String InquiryFormId = mso.get('CF00N1000000962np_lkid');
        String contactId = mso.get('CF00N10000006ps6f_lkid');
        Boolean isNewMode = false;
        String recordId = leadTest.Id;
        String rid = '';
        ApexPages.StandardController con = new ApexPages.StandardController(leadTest);
        NewAndEditLeadController lead = new NewAndEditLeadController(con);
        Inquiry_form__c ifc = lead.ifc;
        String token = lead.awsToken;
        String AWSDataId = lead.AWSDataId;
        String AWSDataIdInquiryForm = lead.AWSDataIdInquiryForm;
        String contactsInfo = lead.contactsInfo; //key sfid;value awsid
        Map<String, String> sfIdToAWSIdMap = new Map<String, String>();
        SObject obj = con.getRecord();
        obj.put('Hospital_Name__c', mso.get('CF00N10000002CvC5_lkid'));
        String hospitalId = mso.get('CF00N10000002CvC5_lkid');
 
        String leadJson = '{"RecordTypeId":"01210000000QiRaAAK","Lead_No__c":"IN-JS-2022040127183","Status":"未跟进","Hospital_Name__c":"0011000000VAPEt","Department_Class__c":"0011000000V9bh5","Salutation":"","LastName":"***","Contact_Name__c":"0031m00000DHrkV","Cancel_Reason__c":"","Phone":"","Opp_Name_Search__c":"000000000000000","Email":"","Opp_Name__c":"Test","owner_not_automatically_update__c":false,"Purchase_Plan__c":"有(无预算)","Lead_Inquiry_form__c":"000000000000000","Opportunity_Division__c":"询价","LeadSource":"电话","Campaign__c":"000000000000000","urgent__c":false,"SI_PromoteInquiry__c":false,"Request__c":"","Request_Detail__c":"","Opportunity_stage__c":"预算没有批准","Close_Forecasted_Date__c":"2022/04/01","Purchase_Reason__c":"更新","Sales_Method__c":"单一来源采购","Fund_Basis__c":"銀行資金","Purchase_Type__c":"一般引合","Company":"江苏省人民医院 呼吸科 呼吸科","Name_Encrypted__c":"48a2693f6bf73d16e0abd48caf7fb756","Phone_Encrypted__c":"","Email_Encrypted__c":"","AWS_Data_Id__c":"959584917346320385"}';
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', false);
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', true);
        NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', isNew);
        Test.stopTest();
    }
 
    static testMethod void testMethod3() {
        Lead leadTest = new Lead();
        String url = ApexPages.currentPage().getParameters().put('CF00N10000006ps6f_lkid', '000000000000000');
        url = ApexPages.currentPage().getParameters().put('RecordType', '01210000000QiRf');
        Test.startTest();
        try {
            NewAndEditLeadController.ControllerUtil();
            ApexPages.StandardController con = new ApexPages.StandardController(leadTest);
            NewAndEditLeadController lc = new NewAndEditLeadController(con);
            String jsonString = '["Department_Class__c"]';
            system.debug('jsonString:' + JSON.deserializeUntyped(jsonString));
            String token = lc.awsToken;
            String AWSDataId = lc.AWSDataId;
            String AWSDataIdInquiryForm = lc.AWSDataIdInquiryForm;
            String contactId = lc.contactId; //For Lookup field
            String contactsInfo = lc.contactsInfo; //key sfid;value awsid
            Inquiry_form__c ifc = lc.ifc;
            NewAndEditLeadController.queryAccount(jsonString, '000000000000000');
        } catch (Exception e) {
            system.debug('Exception from query account:' + e.getMessage());
        }
        Test.stopTest();
    }
}