11
22package com .sparkpost .model ;
33
4+ import java .util .ArrayList ;
5+ import java .util .Collections ;
46import java .util .HashSet ;
7+ import java .util .List ;
8+ import java .util .Objects ;
59import java .util .Set ;
610
711import org .apache .commons .lang3 .StringUtils ;
1014
1115public class MessageEventsQueryBuilder {
1216
17+ private boolean sortOutput = false ;
18+
1319 private Set <BounceClass > bounceClasses = new HashSet <BounceClass >();
1420 private Set <String > campaignIds = new HashSet <String >();
1521 private Set <EventType > events = new HashSet <EventType >();
@@ -224,19 +230,19 @@ public void addTransmissionId(String tid) {
224230
225231 public void buildQuery (Endpoint endpoint ) {
226232 if (this .bounceClasses .size () > 0 ) {
227- endpoint .addParam ("bounce_classes" , StringUtils . join (this .bounceClasses , "," ));
233+ endpoint .addParam ("bounce_classes" , setAsString (this .bounceClasses , "," ));
228234 }
229235
230236 if (this .campaignIds .size () > 0 ) {
231- endpoint .addParam ("campaign_ids" , StringUtils . join (this .campaignIds , ',' ));
237+ endpoint .addParam ("campaign_ids" , setAsString (this .campaignIds , "," ));
232238 }
233239
234240 if (this .events .size () > 0 ) {
235- endpoint .addParam ("events" , StringUtils . join (this .events , ',' ));
241+ endpoint .addParam ("events" , setAsString (this .events , "," ));
236242 }
237243
238244 if (this .friendlyFroms .size () > 0 ) {
239- endpoint .addParam ("friendly_froms" , StringUtils . join (this .friendlyFroms , ',' ));
245+ endpoint .addParam ("friendly_froms" , setAsString (this .friendlyFroms , "," ));
240246 }
241247
242248 if (StringUtils .isNotEmpty (this .fromDateTime )) {
@@ -248,32 +254,61 @@ public void buildQuery(Endpoint endpoint) {
248254 }
249255
250256 if (this .messageIds .size () > 0 ) {
251- endpoint .addParam ("message_ids" , StringUtils . join (this .messageIds , ',' ));
257+ endpoint .addParam ("message_ids" , setAsString (this .messageIds , "," ));
252258 }
253259
254260 if (StringUtils .isNotEmpty (this .reason )) {
255261 endpoint .addParam ("reason" , this .reason );
256262 }
257263
258264 if (this .recipients .size () > 0 ) {
259- endpoint .addParam ("recipients" , StringUtils . join (this .recipients , ',' ));
265+ endpoint .addParam ("recipients" , setAsString (this .recipients , "," ));
260266 }
261267
262268 if (this .subaccounts .size () > 0 ) {
263- endpoint .addParam ("subaccounts" , StringUtils . join (this .subaccounts , ',' ));
269+ endpoint .addParam ("subaccounts" , setAsString (this .subaccounts , "," ));
264270 }
265271
266272 if (this .templateIds .size () > 0 ) {
267- endpoint .addParam ("template_ids" , StringUtils . join (this .templateIds , ',' ));
273+ endpoint .addParam ("template_ids" , setAsString (this .templateIds , "," ));
268274 }
269275
270276 if (StringUtils .isNotEmpty (this .timezone )) {
271277 endpoint .addParam ("timezone" , this .timezone );
272278 }
273279
274280 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 );
276299 }
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 ();
277312 }
278313
279314}
0 commit comments