1
1
2
2
package com .sparkpost .model ;
3
3
4
+ import java .util .ArrayList ;
5
+ import java .util .Collections ;
4
6
import java .util .HashSet ;
7
+ import java .util .List ;
8
+ import java .util .Objects ;
5
9
import java .util .Set ;
6
10
7
11
import org .apache .commons .lang3 .StringUtils ;
10
14
11
15
public class MessageEventsQueryBuilder {
12
16
17
+ private boolean sortOutput = false ;
18
+
13
19
private Set <BounceClass > bounceClasses = new HashSet <BounceClass >();
14
20
private Set <String > campaignIds = new HashSet <String >();
15
21
private Set <EventType > events = new HashSet <EventType >();
@@ -224,19 +230,19 @@ public void addTransmissionId(String tid) {
224
230
225
231
public void buildQuery (Endpoint endpoint ) {
226
232
if (this .bounceClasses .size () > 0 ) {
227
- endpoint .addParam ("bounce_classes" , StringUtils . join (this .bounceClasses , "," ));
233
+ endpoint .addParam ("bounce_classes" , setAsString (this .bounceClasses , "," ));
228
234
}
229
235
230
236
if (this .campaignIds .size () > 0 ) {
231
- endpoint .addParam ("campaign_ids" , StringUtils . join (this .campaignIds , ',' ));
237
+ endpoint .addParam ("campaign_ids" , setAsString (this .campaignIds , "," ));
232
238
}
233
239
234
240
if (this .events .size () > 0 ) {
235
- endpoint .addParam ("events" , StringUtils . join (this .events , ',' ));
241
+ endpoint .addParam ("events" , setAsString (this .events , "," ));
236
242
}
237
243
238
244
if (this .friendlyFroms .size () > 0 ) {
239
- endpoint .addParam ("friendly_froms" , StringUtils . join (this .friendlyFroms , ',' ));
245
+ endpoint .addParam ("friendly_froms" , setAsString (this .friendlyFroms , "," ));
240
246
}
241
247
242
248
if (StringUtils .isNotEmpty (this .fromDateTime )) {
@@ -248,32 +254,61 @@ public void buildQuery(Endpoint endpoint) {
248
254
}
249
255
250
256
if (this .messageIds .size () > 0 ) {
251
- endpoint .addParam ("message_ids" , StringUtils . join (this .messageIds , ',' ));
257
+ endpoint .addParam ("message_ids" , setAsString (this .messageIds , "," ));
252
258
}
253
259
254
260
if (StringUtils .isNotEmpty (this .reason )) {
255
261
endpoint .addParam ("reason" , this .reason );
256
262
}
257
263
258
264
if (this .recipients .size () > 0 ) {
259
- endpoint .addParam ("recipients" , StringUtils . join (this .recipients , ',' ));
265
+ endpoint .addParam ("recipients" , setAsString (this .recipients , "," ));
260
266
}
261
267
262
268
if (this .subaccounts .size () > 0 ) {
263
- endpoint .addParam ("subaccounts" , StringUtils . join (this .subaccounts , ',' ));
269
+ endpoint .addParam ("subaccounts" , setAsString (this .subaccounts , "," ));
264
270
}
265
271
266
272
if (this .templateIds .size () > 0 ) {
267
- endpoint .addParam ("template_ids" , StringUtils . join (this .templateIds , ',' ));
273
+ endpoint .addParam ("template_ids" , setAsString (this .templateIds , "," ));
268
274
}
269
275
270
276
if (StringUtils .isNotEmpty (this .timezone )) {
271
277
endpoint .addParam ("timezone" , this .timezone );
272
278
}
273
279
274
280
if (this .transmissionIds .size () > 0 ) {
275
- endpoint .addParam ("transmission_ids" , StringUtils .join (this .transmissionIds , ',' ));
281
+ endpoint .addParam ("transmission_ids" , setAsString (this .transmissionIds , "," ));
282
+ }
283
+ }
284
+
285
+ // To make test easier output can be sorted so value content is deterministic
286
+ public void setSortOutput (boolean sortOutput ) {
287
+ this .sortOutput = sortOutput ;
288
+ }
289
+
290
+ private String setAsString (@ SuppressWarnings ("rawtypes" ) Set set , String separator ) {
291
+
292
+ List <String > list = new ArrayList <String >();
293
+ for (Object obj : set ) {
294
+ list .add (Objects .toString (obj ));
295
+ }
296
+
297
+ if (this .sortOutput ) {
298
+ Collections .sort (list );
276
299
}
300
+
301
+ StringBuilder result = new StringBuilder ();
302
+ boolean isFirstElement = true ;
303
+ for (String val : list ) {
304
+ if (!isFirstElement ) {
305
+ result .append (separator );
306
+ }
307
+ result .append (val );
308
+ isFirstElement = false ;
309
+ }
310
+
311
+ return result .toString ();
277
312
}
278
313
279
314
}
0 commit comments