Created
September 1, 2014 17:36
-
-
Save coopernurse/7ca4bfbcd1cb3a6a4d94 to your computer and use it in GitHub Desktop.
rand dingle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dingle; | |
@RunWith(Theories.class) | |
public class DingleTest { | |
@DataPoints | |
public static DbDingle[] randDingles() { | |
long now = System.currentTimeMillis(); | |
RandGenerator rand = new RandGenerator(); | |
return rand.randDingles(100).toArray(new DbDingle[100]); | |
} | |
@Theory | |
public void nextDeliveryDateIsZeroIfAllDatesInPast(DbDingle c) { | |
assumeThat(c.getType(), is(DingleType.singleDate)); | |
String today = c.getSendDateFormat().print(System.currentTimeMillis()); | |
assumeFalse(Iterables.filter(c.getEvents(), Predicates.and(Events.isActive, Events.sendDateOnOrAfter(today))).iterator().hasNext()); | |
assertEquals(0L, Dingles.toDingle(c, System.currentTimeMillis()).getNextDeliveryDate().longValue()); | |
} | |
@Theory | |
public void nextDeliveryDateIsZeroIfAllEventsInactive(DbDingle c) { | |
assumeFalse(Iterables.filter(c.getEvents(), Events.isActive).iterator().hasNext()); | |
assertEquals(0L, Dingles.toDingle(c, System.currentTimeMillis()).getNextDeliveryDate().longValue()); | |
} | |
@Theory | |
public void dingleNextDeliveryDateIsMinEventDeliveryDate(DbDingle c) throws Exception { | |
assumeThat(c.getType(), is(DingleType.singleDate)); | |
assumeTrue(c.isActive()); | |
assumeFalse(c.isArchived()); | |
Date now = new Date(); | |
String today = c.getSendDateFormat().print(now.getTime()); | |
Iterable<DbEvent> events = Iterables.filter(c.getEvents(), | |
Predicates.and(Events.isActive, Events.sendDateOnOrAfter(today), Events.lastRunDateBefore(today))); | |
assumeTrue(events.iterator().hasNext()); | |
Dingle dingle = Dingles.toDingle(c, now.getTime()); | |
DbEvent min = null; | |
for (DbEvent event : events) { | |
if (min == null || event.getSendDate().compareTo(min.getSendDate()) < 0) { | |
min = event; | |
} | |
} | |
long expected = c.getSendDateFormat().parseMillis(min.getSendDate()); | |
assertEquals(expected, dingle.getNextDeliveryDate().longValue()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RandGenerator { | |
public RandGenerator(Random rand) { | |
this.rand = rand; | |
} | |
public DbDingleBuilder randDingleBuilder() { | |
return randDingleBuilder(rand.nextInt(20), rand.nextBoolean()); | |
} | |
public DbDingleBuilder randDingleBuilder(int eventCount, boolean randEventDeleted) { | |
boolean preBuilt = rand.nextBoolean(); | |
return new DbDingleBuilder() | |
.setExternalAppId((long)rand.nextInt()) | |
.setDateCreated(randTime()) | |
.setNextProcessTime(randTime()) | |
.setSyncCount((long)rand.nextInt()) | |
.setName(randStr(8)) | |
.setType((DingleType) randValue(DingleType.values())) | |
.setActive(rand.nextBoolean()) | |
.setArchived(rand.nextBoolean()) | |
.setUserId(String.valueOf(rand.nextInt() + 1000)) | |
.setUserDisplayName(randStr(10)) | |
.setUserEmail(randEmail()) | |
.setUserCanDelete(randBool()) | |
.setUserCanModify(randBool()) | |
.setPreBuilt(preBuilt) | |
.setImageUrl(randStr(8)) | |
.setParentDingleId(rand.nextBoolean() ? null : (long) rand.nextInt()) | |
.setTimeZoneId((String) randValue(timeZoneIds)) | |
.setContactIds(ImmutableList.copyOf(randIdSet(0, 20))) | |
.setGroupIds(ImmutableList.copyOf(randIdSet(0, 10))) | |
.setEvents(ImmutableList.copyOf(randEvents(0, eventCount, randEventDeleted))) | |
.setSubscriberCount(preBuilt ? new SubscriberCount.Builder().active(0L).all(0L).inactive(0L).build() : null); | |
} | |
public DbDingle randDingle() { | |
return randDingle(false); | |
} | |
public DbDingle randDingle(boolean randEventDeleted) { | |
return randDingle(rand.nextInt(20), randEventDeleted); | |
} | |
public DbDingle randDingle(int eventCount, boolean randEventDeleted) { | |
return randDingleBuilder(eventCount, randEventDeleted).createDbDingle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment