added service method to retrieve currently open application periods and unit test to confirm results

This commit is contained in:
Emil Siverhall 2012-07-30 10:34:32 +02:00
parent ccbcdb9d2f
commit 6902f96bc6
3 changed files with 124 additions and 0 deletions
src
main/java/se/su/dsv/scipro/springdata
test/java/se/su/dsv/scipro/springdata

@ -1,13 +1,22 @@
package se.su.dsv.scipro.springdata.serviceimpls;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mysema.query.types.expr.BooleanExpression;
import se.su.dsv.scipro.match.dataobject.ApplicationPeriod;
import se.su.dsv.scipro.match.dataobject.QApplicationPeriod;
import se.su.dsv.scipro.peer.data.dataobjects.PeerRequest;
import se.su.dsv.scipro.springdata.repos.ApplicationPeriodRepo;
import se.su.dsv.scipro.springdata.services.ApplicationPeriodService;
@ -25,5 +34,26 @@ public class ApplicationPeriodServiceImpl extends AbstractQueryService<Applicati
super(applicationPeriodRepo, applicationPeriodRepo);
System.out.println("ApplicationPeriodServiceImpl instantiating...");
}
@Override
public List<ApplicationPeriod> getCurrentPeriods(Date currentDate) {
Iterable<ApplicationPeriod> periods = applicationPeriodRepo.findAll(startedBefore(currentDate).and(endedAfter(currentDate)));
return constructList(periods);
}
private BooleanExpression startedBefore(Date currentDate){
return QApplicationPeriod.applicationPeriod.startDate.before(currentDate);
}
private BooleanExpression endedAfter(Date currentDate){
return QApplicationPeriod.applicationPeriod.endDate.after(currentDate);
}
private List<ApplicationPeriod> constructList(Iterable<ApplicationPeriod> periods) {
List<ApplicationPeriod> list = new ArrayList<ApplicationPeriod>();
for(ApplicationPeriod ap : periods) {
list.add(ap);
}
return list;
}
}

@ -1,7 +1,12 @@
package se.su.dsv.scipro.springdata.services;
import java.util.Date;
import java.util.List;
import se.su.dsv.scipro.match.dataobject.ApplicationPeriod;
public interface ApplicationPeriodService extends GenericService<ApplicationPeriod, Long>, QueryService<ApplicationPeriod, Long> {
List<ApplicationPeriod> getCurrentPeriods(final Date currentDate);
}

@ -0,0 +1,89 @@
package se.su.dsv.scipro.springdata;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.jsoup.select.Evaluator.AllElements;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import se.su.dsv.scipro.data.dataobjects.ProjectClass;
import se.su.dsv.scipro.match.dataobject.ApplicationPeriod;
import se.su.dsv.scipro.springdata.services.ApplicationPeriodService;
import se.su.dsv.scipro.springdata.services.ProjectClassService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(inheritLocations = false, locations = {
"classpath:test-applicationContext.xml"
})
public class TestApplicationPeriod {
@Autowired
private ApplicationPeriodService periodService;
@Autowired
private ProjectClassService projectClassService;
private SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
private ApplicationPeriod currentBachelor, currentMaster, futureBachelor, pastMaster;
private ProjectClass bachelor, master;
@Before
public void startTransaction() throws Exception {
bachelor = new ProjectClass(ProjectClass.BACHELOR, "Bachelor", "Bachelor degree thesis project");
bachelor = projectClassService.save(bachelor);
master = new ProjectClass(ProjectClass.MASTER, "Master", "Master degree thesis project");
master = projectClassService.save(master);
Set<ProjectClass> bachelorSet = new HashSet<ProjectClass>();
bachelorSet.add(bachelor);
Set<ProjectClass> masterSet = new HashSet<ProjectClass>();
masterSet.add(master);
currentBachelor = createPeriod(bachelorSet, date("2012-07-15"), date("2012-08-15"), date("2012-09-01"), "Current bachelor period");
currentMaster = createPeriod(masterSet, date("2012-07-10"), date("2012-08-10"), date("2012-08-25"), "Current master period");
futureBachelor = createPeriod(bachelorSet, date("2012-08-16"), date("2012-09-16"), date("2012-09-25"), "Future bachelor period");
pastMaster = createPeriod(masterSet, date("2012-05-25"), date("2012-07-01"), date("2012-07-15"), "Past master period");
}
@Test
@Transactional
@Rollback
public void testGetCurrentPeriods() {
Date currentDate = date("2012-07-30");
List<ApplicationPeriod> currentPeriods = periodService.getCurrentPeriods(currentDate);
Assert.assertEquals(Arrays.asList(new ApplicationPeriod[]{currentBachelor, currentMaster}), currentPeriods);
}
private Date date(String dateString) {
try {
return date.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
private ApplicationPeriod createPeriod(final Set<ProjectClass> projectClassSet, final Date startDate, final Date endDate, final Date courseStartDate, final String name) {
ApplicationPeriod appPeriod = new ApplicationPeriod();
appPeriod.setStartDate(startDate);
appPeriod.setEndDate(endDate);
appPeriod.setCourseStartDate(courseStartDate);
appPeriod.setProjectClass(projectClassSet);
appPeriod.setName(name);
return periodService.save(appPeriod);
}
}