3
3
import com .fasterxml .jackson .core .JsonProcessingException ;
4
4
import com .fasterxml .jackson .core .type .TypeReference ;
5
5
import com .fasterxml .jackson .databind .ObjectMapper ;
6
+
6
7
import net .glenmazza .sfclient .TestApplication ;
7
8
import net .glenmazza .sfclient .model .AccountCreateRecord ;
9
+ import net .glenmazza .sfclient .model .AccountInsertCompositeRecord ;
8
10
import net .glenmazza .sfclient .model .AccountMultipleEntityRecord ;
9
11
import net .glenmazza .sfclient .model .AccountQueryRecord ;
12
+ import net .glenmazza .sfclient .model .AccountUpdateCompositeRecord ;
10
13
import net .glenmazza .sfclient .model .AccountUpdateRecord ;
11
14
import net .glenmazza .sfclient .model .ApexAccountRecord ;
15
+ import net .glenmazza .sfclient .model .CompositeEntityRecord ;
16
+ import net .glenmazza .sfclient .model .CompositeEntityRecordRequest ;
17
+ import net .glenmazza .sfclient .model .CompositeEntityRecordResponse ;
12
18
import net .glenmazza .sfclient .model .MultipleEntityRecord ;
13
19
import net .glenmazza .sfclient .model .MultipleEntityRecord201Response ;
14
20
import net .glenmazza .sfclient .model .MultipleEntityRecord400ResponseException ;
30
36
31
37
import java .time .LocalDate ;
32
38
import java .time .temporal .ChronoUnit ;
39
+ import java .util .ArrayList ;
33
40
import java .util .HashMap ;
34
41
import java .util .List ;
35
42
import java .util .Map ;
36
43
37
44
import static org .junit .jupiter .api .Assertions .assertEquals ;
38
45
import static org .junit .jupiter .api .Assertions .assertTrue ;
39
46
40
-
41
47
/**
42
48
* Test cases cover the three AbstractRESTService subclasses supporting Salesforce entity calls, Apex endpoints,
43
49
* and SOQLQueries.
44
- *
45
50
* Will need to have your test Salesforce instance configured in application-test.properties~template. Also,
46
51
* the Apex REST test requires a certain Apex endpoint to be installed see the header for that
47
52
* test for more info.
@@ -61,6 +66,9 @@ public class RESTServicesTest {
61
66
@ Autowired
62
67
private SalesforceMultipleRecordInserter smri ;
63
68
69
+ @ Autowired
70
+ private SalesforceCompositeRequestService scrs ;
71
+
64
72
@ Autowired
65
73
private ApexRESTCaller arc ;
66
74
@@ -78,7 +86,7 @@ public static void initialize() {
78
86
void testSalesforceRecordManagerInsertsGetsAndSOQLQueries () throws JsonProcessingException {
79
87
// insert first Account via Class & SOQL query
80
88
AccountCreateRecord acr = new AccountCreateRecord ();
81
- String account1Name = "Test Account " + LocalDate .now ();
89
+ String account1Name = "ACR Test Account " + LocalDate .now ();
82
90
acr .setName (account1Name );
83
91
acr .setSite ("Philadelphia" );
84
92
acr .setNumberOfEmployees (25 );
@@ -98,7 +106,7 @@ void testSalesforceRecordManagerInsertsGetsAndSOQLQueries() throws JsonProcessin
98
106
99
107
// create via Map
100
108
Map <String , Object > accountViaMap = new HashMap <>();
101
- String account2Name = "Test Account 2 " + LocalDate .now ();
109
+ String account2Name = "ACR Test Account 2 " + LocalDate .now ();
102
110
accountViaMap .put ("Name" , account2Name );
103
111
accountViaMap .put ("Site" , "Baltimore" );
104
112
accountViaMap .put ("NumberOfEmployees" , 30 );
@@ -188,8 +196,11 @@ void testSalesforceRecordManagerUpdates() throws JsonProcessingException {
188
196
srm .deleteObject ("Account" , rcr1 .getId ());
189
197
}
190
198
199
+
200
+ // Note if this test fails, may be necessary to delete Test MER Account1 and 2 that it creates
201
+ // from Salesforce CRM. This test requires that neither exist in SF prior to running.
191
202
@ Test
192
- void testMultipleEntityRecordInsertions () throws JsonProcessingException {
203
+ void testMultipleEntityRecordInsertionsAndCompositeCalls () throws JsonProcessingException {
193
204
AccountMultipleEntityRecord amer = new AccountMultipleEntityRecord ("111" );
194
205
amer .setName ("Test MER Account1" );
195
206
amer .setSite ("Raleigh" );
@@ -212,11 +223,77 @@ void testMultipleEntityRecordInsertions() throws JsonProcessingException {
212
223
MultipleEntityRecord201Response response = smri .bulkInsert ("Account" , merr );
213
224
assertEquals (2 , response .getResults ().size ());
214
225
215
- // method also deletes objects once done comparing
216
- queryAndConfirmValues (response , amer );
217
- queryAndConfirmValues (response , amer2 );
218
-
219
- // now test exception handling: will activate by having both accounts have the same reference ID.
226
+ String acct1SfId = queryAndConfirmMultipleRecordValues (response , amer );
227
+ String acct2SfId = queryAndConfirmMultipleRecordValues (response , amer2 );
228
+
229
+ // use the Composite objects to update the values
230
+ AccountUpdateCompositeRecord aucr1 = new AccountUpdateCompositeRecord (acct1SfId );
231
+ aucr1 .getBody ().setNumberOfEmployees (30 );
232
+ aucr1 .getBody ().setSite ("Asheville" );
233
+
234
+ AccountUpdateCompositeRecord aucr2 = new AccountUpdateCompositeRecord (acct2SfId );
235
+ aucr2 .getBody ().setNumberOfEmployees (40 );
236
+ aucr2 .getBody ().setSite ("Greensboro" );
237
+
238
+ // ...and add a third company, note SF ID not known yet, so just choosing a unique ref ID
239
+ AccountInsertCompositeRecord aicr = new AccountInsertCompositeRecord ("mythirdcompany" );
240
+ aicr .getBody ().setName ("Test MER Account3" );
241
+ aicr .getBody ().setSite ("Winston-Salem" );
242
+ aicr .getBody ().setNumberOfEmployees (25 );
243
+
244
+ CompositeEntityRecordRequest cerReq = new CompositeEntityRecordRequest (false );
245
+ List <? extends CompositeEntityRecord > compList = new ArrayList <>(List .of (aucr1 , aucr2 , aicr ));
246
+ cerReq .setCompositeRequest (compList );
247
+
248
+ CompositeEntityRecordResponse cerr = scrs .bulkProcess (cerReq );
249
+
250
+ queryAndConfirmCompositeValues (acct1SfId , aucr1 );
251
+ queryAndConfirmCompositeValues (acct2SfId , aucr2 );
252
+
253
+ // test that create worked fine
254
+ String acct3SfId = (String ) cerr .getCompositeResponse ().get (2 ).getSuccessResultsMap ().get ("id" );
255
+
256
+ AccountCreateRecord acr = srm .getObject ("Account" ,
257
+ acct3SfId , AccountCreateRecord .class );
258
+
259
+ List <CompositeEntityRecordResponse .Result > results = cerr .getCompositeResponse ();
260
+ assertEquals (201 , results .get (2 ).getHttpStatusCode ());
261
+ assertEquals ("mythirdcompany" , results .get (2 ).getReferenceId ());
262
+ Map <String , Object > body = results .get (2 ).getSuccessResultsMap ();
263
+ assertEquals (true , body .get ("success" ));
264
+ assertEquals (aicr .getBody ().getName (), acr .getName ());
265
+ assertEquals (aicr .getBody ().getSite (), acr .getSite ());
266
+ assertEquals (aicr .getBody ().getNumberOfEmployees (), acr .getNumberOfEmployees ());
267
+ srm .deleteObject ("Account" , acct3SfId );
268
+
269
+ // now test composite errors, will attempt to update Acct 1 even though it doesn't exist anymore
270
+ srm .deleteObject ("Account" , acct1SfId );
271
+ aucr2 .getBody ().setNumberOfEmployees (50 );
272
+ aucr2 .getBody ().setSite ("Durham" );
273
+
274
+ cerr = scrs .bulkProcess (cerReq );
275
+
276
+ results = cerr .getCompositeResponse ();
277
+ assertEquals (400 , results .get (0 ).getHttpStatusCode ());
278
+ assertEquals (acct1SfId , results .get (0 ).getReferenceId ());
279
+ List <Map <String , Object >> errors = results .get (0 ).getErrorResultsList ();
280
+ assertEquals ("ENTITY_IS_DELETED" , errors .get (0 ).get ("errorCode" ));
281
+ assertEquals ("entity is deleted" , errors .get (0 ).get ("message" ));
282
+ assertEquals (0 , ((List <?>) errors .get (0 ).get ("fields" )).size ());
283
+
284
+ assertEquals (200 , results .get (1 ).getHttpStatusCode ());
285
+ assertEquals (acct2SfId , results .get (1 ).getReferenceId ());
286
+ body = results .get (1 ).getSuccessResultsMap ();
287
+ assertEquals (acct2SfId , body .get ("id" ));
288
+ assertEquals (true , body .get ("success" ));
289
+ assertEquals (false , body .get ("created" ));
290
+
291
+ // check update occurred for success case
292
+ queryAndConfirmCompositeValues (acct2SfId , aucr2 );
293
+
294
+ srm .deleteObject ("Account" , acct2SfId );
295
+
296
+ // now test Multiple Entry exception handling: will activate by having both accounts have the same reference ID.
220
297
amer2 .setAttributes (new MultipleEntityRecord .Attributes ("Account" , "111" ));
221
298
222
299
/* Error response should be:
@@ -261,7 +338,7 @@ void testMultipleEntityRecordInsertions() throws JsonProcessingException {
261
338
assertEquals ("Duplicate ReferenceId provided in the request." , message );
262
339
}
263
340
264
- private void queryAndConfirmValues (MultipleEntityRecord201Response response , AccountMultipleEntityRecord amer )
341
+ private String queryAndConfirmMultipleRecordValues (MultipleEntityRecord201Response response , AccountMultipleEntityRecord amer )
265
342
throws JsonProcessingException {
266
343
String accountSfId = response .getResults ().stream ()
267
344
.filter (r -> amer .getAttributes ().getReferenceId ().equals (r .getReferenceId ()))
@@ -275,7 +352,16 @@ private void queryAndConfirmValues(MultipleEntityRecord201Response response, Acc
275
352
assertEquals (acr .getRating ().name (), amer .getRating ().name ());
276
353
assertEquals (acr .getLeadDate (), amer .getLeadDate ());
277
354
278
- srm .deleteObject ("Account" , accountSfId );
355
+ return accountSfId ;
356
+ }
357
+
358
+ private void queryAndConfirmCompositeValues (String accountSfId , AccountUpdateCompositeRecord aucr )
359
+ throws JsonProcessingException {
360
+
361
+ // query and check values
362
+ AccountCreateRecord acr = srm .getObject ("Account" , accountSfId , AccountCreateRecord .class );
363
+ assertEquals (aucr .getBody ().getSite (), acr .getSite ());
364
+ assertEquals (aucr .getBody ().getNumberOfEmployees (), acr .getNumberOfEmployees ());
279
365
}
280
366
281
367
@ Test
0 commit comments