IFeed feed = fFactory.createFeed(null, new URI("http://www.link.com"));
feed.setTitle("Feed Name");
DynamicDAO.save(feed);
INews news1 = fFactory.createNews(null, feed, new Date());
news1.setTitle("News 1");
news1.setPublishDate(fToday);
INews news2 = fFactory.createNews(null, feed, new Date());
news2.setTitle("News 2");
news2.setPublishDate(fYesterday);
INews news3 = fFactory.createNews(null, feed, new Date());
news3.setTitle("News 3");
news3.setPublishDate(fEarlierThisWeek);
INews news4 = fFactory.createNews(null, feed, new Date());
news4.setTitle("News 4");
news4.setPublishDate(fLastWeek);
INews news5 = fFactory.createNews(null, feed, new Date());
news5.setTitle("News 5");
news5.setPublishDate(new Date(0));
INews news6 = fFactory.createNews(null, feed, new Date());
news6.setTitle("News 6");
news6.setPublishDate(new Date(0));
INews news7 = fFactory.createNews(null, feed, new Date());
news7.setTitle("News 7");
news7.setPublishDate(new Date(0));
List<INews> input = new ArrayList<INews>();
input.add(news1);
input.add(news2);
input.add(news3);
input.add(news4);
input.add(news5);
input.add(news6);
input.add(news7);
/* Group by Date */
{
fGrouping.setType(NewsGrouping.Type.GROUP_BY_DATE);
List<EntityGroup> group = fGrouping.group(input);
assertEquals(fTodayIsFirstDayOfWeek || fYesterdayIsFirstDayOfWeek ? 4 : 5, group.size());
assertEquals(input.size(), countEntities(group));
for (EntityGroup entityGroup : group) {
if (entityGroup.getId() == NewsGrouping.Group.TODAY.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(fTodayIsFirstDayOfWeek ? 2 : 1, entities.size());
if (!fTodayIsFirstDayOfWeek)
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news1 })));
else
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news1, news3 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.YESTERDAY.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(fYesterdayIsFirstDayOfWeek ? 2 : 1, entities.size());
if (!fYesterdayIsFirstDayOfWeek)
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news2 })));
else
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news2, news3 })));
}
else if (!fTodayIsFirstDayOfWeek && !fYesterdayIsFirstDayOfWeek && entityGroup.getId() == NewsGrouping.Group.EARLIER_THIS_WEEK.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news3 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.LAST_WEEK.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news4 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.OLDER.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(3, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news5, news6, news7 })));
}
}
}
/* Group by State */
{
news1.setState(INews.State.UNREAD);
news2.setState(INews.State.READ);
news3.setState(INews.State.HIDDEN);
news4.setState(INews.State.DELETED);
news5.setState(INews.State.UPDATED);
fGrouping.setType(NewsGrouping.Type.GROUP_BY_STATE);
List<EntityGroup> group = fGrouping.group(input);
assertEquals(4, group.size());
assertEquals(5, countEntities(group));
for (EntityGroup entityGroup : group) {
if (entityGroup.getId() == NewsGrouping.Group.NEW.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(2, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news6, news7 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.UNREAD.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news1 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.UPDATED.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news5 })));
}
else if (entityGroup.getId() == NewsGrouping.Group.READ.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news2 })));
}
}
news3.setState(INews.State.NEW);
news4.setState(INews.State.NEW);
}
/* Group by Author */
{
IPerson author1 = fFactory.createPerson(null, news1);
author1.setName("Author 1");
IPerson author2 = fFactory.createPerson(null, news2);
author2.setName("Author 2");
fGrouping.setType(NewsGrouping.Type.GROUP_BY_AUTHOR);
List<EntityGroup> group = fGrouping.group(input);
assertEquals(3, group.size());
assertEquals(input.size(), countEntities(group));
for (EntityGroup entityGroup : group) {
if (entityGroup.getId() == NewsGrouping.Group.UNKNOWN_AUTHOR.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(5, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news3, news4, news5, news6, news7 })));
}
else if (entityGroup.getName().equals("Author 1")) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news1 })));
}
else if (entityGroup.getName().equals("Author 2")) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news2 })));
}
}
}
/* Group by Category */
{
fFactory.createCategory(null, news1).setName("Category 1");
fFactory.createCategory(null, news2).setName("Category 2");
fGrouping.setType(NewsGrouping.Type.GROUP_BY_CATEGORY);
List<EntityGroup> group = fGrouping.group(input);
assertEquals(3, group.size());
assertEquals(input.size(), countEntities(group));
for (EntityGroup entityGroup : group) {
if (entityGroup.getId() == NewsGrouping.Group.UNKNOWN_CATEGORY.ordinal()) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(5, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news3, news4, news5, news6, news7 })));
}
else if (entityGroup.getName().equals("Category 1")) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news1 })));
}
else if (entityGroup.getName().equals("Category 2")) {
List<IEntity> entities = entityGroup.getEntities();
assertEquals(1, entities.size());
assertEquals(true, entities.containsAll(Arrays.asList(new IEntity[] { news2 })));
}
}
}
/* Group by Label */
{
ILabel label1 = fFactory.createLabel(null, "Label 1");
ILabel label2 = fFactory.createLabel(null, "Label 2");
news6.setLabel(label1);
news7.setLabel(label2);
fGrouping.setType(NewsGrouping.Type.GROUP_BY_LABEL);
List<EntityGroup> group = fGrouping.group(input);
assertEquals(3, group.size());