CampaignFacadeEjb implements CampaignFacade { @Override public CampaignReferenceDto getLastStartedCampaign() { final CriteriaBuilder cb = em.getCriteriaBuilder(); final CriteriaQuery<Campaign> query = cb.createQuery(Campaign.class); final Root<Campaign> from = query.from(Campaign.class); query.select(from); query.where(cb.lessThanOrEqualTo(from.get(Campaign.START_DATE), new Date())); query.orderBy(cb.desc(from.get(Campaign.START_DATE))); final TypedQuery<Campaign> q = em.createQuery(query); final Campaign lastStartedCampaign = q.getResultList().stream().findFirst().orElse(null); return toReferenceDto(lastStartedCampaign); }  @Override List<CampaignIndexDto> getIndexList(CampaignCriteria campaignCriteria, Integer first, Integer max, List<SortProperty> sortProperties); @Override List<CampaignReferenceDto> getAllCampaignsAsReference(); @Override CampaignReferenceDto getLastStartedCampaign(); @Override long count(CampaignCriteria campaignCriteria); @Override CampaignDto saveCampaign(CampaignDto dto); Campaign fromDto(@NotNull CampaignDto source); static CampaignDto toDto(Campaign source); @Override CampaignDto getByUuid(String uuid); @Override List<CampaignDashboardElement> getCampaignDashboardElements(String campaignUuid); @Override boolean isArchived(String uuid); @Override void deleteCampaign(String campaignUuid); @Override void archiveOrDearchiveCampaign(String campaignUuid, boolean archive); static CampaignReferenceDto toReferenceDto(Campaign entity);  }
@Test public void testGetLastStartedCampaign() { final TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility"); final UserDto user = creator.createUser(rdcf, UserRole.SURVEILLANCE_SUPERVISOR); final CampaignDto campaign1 = creator.createCampaign(user); campaign1.setStartDate(new Date(System.currentTimeMillis() - 7 * ONE_DAY_IN_MILLIS)); getCampaignFacade().saveCampaign(campaign1); final CampaignDto campaign2 = creator.createCampaign(user); campaign2.setStartDate(new Date(System.currentTimeMillis() - ONE_DAY_IN_MILLIS)); getCampaignFacade().saveCampaign(campaign2); final CampaignDto campaign3 = creator.createCampaign(user); campaign3.setStartDate(new Date(System.currentTimeMillis() + ONE_DAY_IN_MILLIS)); getCampaignFacade().saveCampaign(campaign3); CampaignReferenceDto lastStartedCampaign = getCampaignFacade().getLastStartedCampaign(); Assert.assertEquals(campaign2.getUuid(), lastStartedCampaign.getUuid()); }