Should not be possible to edit start date (or end date) of an application period to a date before today. + Added static import of org.junit.Assert.

This commit is contained in:
Tom Vahlman 2012-02-21 11:56:18 +01:00
parent 7de1c5e60d
commit cb05b984e9
2 changed files with 103 additions and 88 deletions
src
main/java/se/su/dsv/scipro/match/facade
test/java/se/su/dsv/scipro/match/dao

@ -24,15 +24,15 @@ public class ApplicationPeriodFacade {
/**
* Returns true when the application period has been removed successfully,
* it should only be possible to remove/edit a period only if the start date NOT IS BEFORE present date,
* That is because the history should be kept.
* it should only be allowed to remove a period if the start date NOT IS BEFORE present date,
* ie. the same prerequisites as for editing the start date of a period.
* @param appPeriod the period that should be removed
* @return boolean the value is true when success false when failure
* @return boolean the value is true if success false if failure
*/
@Transactional
public boolean removeApplicationPeriod(ApplicationPeriod appPeriod){
appPeriod = applicationPeriodDao.reLoad(appPeriod);
if(!appPeriod.getStartDate().before(Calendar.getInstance().getTime())) {
if(editStartDatePeriod(appPeriod)) {
applicationPeriodDao.delete(appPeriod);
return true;
} else {
@ -40,6 +40,27 @@ public class ApplicationPeriodFacade {
}
}
/**
* Returns true if it is allowed to edit the application period. ,
* It should be allowed to edit the start date a period only if the start date NOT IS BEFORE present date,
* @param editedAppPeriod the period that should be edited
* @return boolean true if success false if failure
*/
@Transactional
public boolean editStartDatePeriod(final ApplicationPeriod editedAppPeriod){
return !editedAppPeriod.getStartDate().before(Calendar.getInstance().getTime());
}
/**
* Returns true if it is allowed to edit the end date of the application period.
* It should be allowed to edit the end date of a period only if the end date NOT IS BEFORE present date,
* @param editedAppPeriod the period which end date should be edited
* @return boolean true if success false if failure
*/
public boolean editEndDatePeriod(final ApplicationPeriod editedAppPeriod){
return !editedAppPeriod.getEndDate().before(Calendar.getInstance().getTime());
}
/**
* Returns true when an open application period exists for the submitted project class
* @param projectClass the project class that should be tested

@ -4,7 +4,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -28,6 +28,7 @@ import se.su.dsv.scipro.match.dao.interfaces.ProjectIdeaDao;
import se.su.dsv.scipro.match.dataobject.ApplicationPeriod;
import se.su.dsv.scipro.match.dataobject.ProjectIdea;
import se.su.dsv.scipro.match.facade.ApplicationPeriodFacade;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ -80,12 +81,12 @@ public class TestApplicationPeriodDao {
projectIdea.setApplicationPeriod(applicationPeriod1);
return projectIdeaDao.save(projectIdea);
}
private ProjectClass createProjectClass(final String projectCode, final String name, final String desc) {
ProjectClass projectClass = new ProjectClass(projectCode, name, desc);
return projectClassDao.save(projectClass);
ProjectClass projectClass = new ProjectClass(projectCode, name, desc);
return projectClassDao.save(projectClass);
}
private Project createProject(final String title, final ProjectClass projectClass, final Role role) {
Project project = new Project();
project.setTitle(title);
@ -93,14 +94,14 @@ public class TestApplicationPeriodDao {
project.addProjectParticipant((Student) role);
return projectDao.save(project);
}
private Role createRole(final User user) {
Role student1Role = new Student();
student1Role.setUser(user);
return roleDao.save(student1Role);
}
@Before
public void startTransaction() throws Exception {
User student1 = new User();
@ -135,7 +136,7 @@ public class TestApplicationPeriodDao {
projectIdea1.setDateCreated(date("2011-07-02"));
projectIdea2.setDateCreated(date("2011-06-12"));
List<ProjectIdea> projectIdeas = applicationPeriodDao.getProjectIdeas(applicationPeriod);
Assert.assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea2 }), projectIdeas);
assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea2 }), projectIdeas);
}
@Test
@ -149,7 +150,7 @@ public class TestApplicationPeriodDao {
projectIdea3.setProjectClass(phd);
applicationPeriod.getProjectClass().add(phd);
List<ProjectIdea> projectIdeas = applicationPeriodDao.getProjectIdeas(applicationPeriod);
Assert.assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea1, projectIdea3 }), projectIdeas);
assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea1, projectIdea3 }), projectIdeas);
}
@Test
@ -160,7 +161,7 @@ public class TestApplicationPeriodDao {
projectIdea1.setProject(project);
projectIdea2.setDateCreated(date("2011-07-01"));
List<ProjectIdea> projectIdeas = applicationPeriodDao.getProjectIdeas(applicationPeriod);
Assert.assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea2 }), projectIdeas);
assertEquals(Arrays.asList(new ProjectIdea[] { projectIdea2 }), projectIdeas);
}
@Test
@ -169,16 +170,16 @@ public class TestApplicationPeriodDao {
public void testShouldOnlyReturnCurrentPeriods() {
Date currentTime = date("2011-07-20");
List<ApplicationPeriod> periods = applicationPeriodDao.getCurrentPeriods(currentTime);
Assert.assertEquals(Arrays.asList(new ApplicationPeriod[] { applicationPeriod2 }), periods);
assertEquals(Arrays.asList(new ApplicationPeriod[] { applicationPeriod2 }), periods);
}
@Test
@Transactional
@Rollback
public void testShouldReturnNextPeriod() {
Date currentTime = date("2011-07-02");
ApplicationPeriod ap = applicationPeriodDao.getNextPeriodByClass(bachelor, currentTime);
Assert.assertEquals(applicationPeriod2, ap);
assertEquals(applicationPeriod2, ap);
}
private void cleanUpDatabase() {
@ -194,7 +195,7 @@ public class TestApplicationPeriodDao {
@Test
@Transactional
@Rollback
public void notAbleToRemovePeriod() {
public void notPossibleToRemovePeriod() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -205,7 +206,7 @@ public class TestApplicationPeriodDao {
final Date endDate1 = cal.getTime();
ApplicationPeriod applicationPeriod = applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate1, endDate1, "name1");
boolean periodRemoved = applicationPeriodFacade.removeApplicationPeriod(applicationPeriod);
Assert.assertFalse(periodRemoved); // should be false
assertFalse(periodRemoved); // should be false
}
@ -214,7 +215,7 @@ public class TestApplicationPeriodDao {
@Test
@Transactional
@Rollback
public void canRemovePeriod() {
public void testRemovePeriod() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -225,16 +226,59 @@ public class TestApplicationPeriodDao {
final Date endDate1 = cal.getTime();
ApplicationPeriod applicationPeriod = applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate1, endDate1, "name1");
boolean periodRemoved = applicationPeriodFacade.removeApplicationPeriod(applicationPeriod);
Assert.assertTrue(periodRemoved); // should be true
assertTrue(periodRemoved); // should be true
}
/* create an application period, then try to edit the start date of the period to a date before TODAY
but the end date is irrelevant, it should NOT be possible to edit the start date of the application period, */
@Test
@Transactional
@Rollback
public void testEditStartDate() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
Calendar cal = Calendar.getInstance();
final Date startDate1 = cal.getTime();
cal.add(Calendar.MONTH, 1);
final Date endDate1 = cal.getTime();
ApplicationPeriod applicationPeriod = applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate1, endDate1, "name1");
cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
applicationPeriod.setStartDate(cal.getTime());
boolean allowedToEdit = applicationPeriodFacade.editStartDatePeriod(applicationPeriod);
assertFalse(allowedToEdit); // allowedToEdit should be false
}
/* create an application period, then try to edit the end date of the period to a date before TODAY
but the start date is irrelevant, it should NOT be possible to edit end date of the application period, */
@Test
@Transactional
@Rollback
public void testEditEndDate() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
Calendar cal = Calendar.getInstance();
final Date startDate1 = cal.getTime();
cal.add(Calendar.MONTH, 1);
final Date endDate1 = cal.getTime();
ApplicationPeriod applicationPeriod = applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate1, endDate1, "name1");
cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
applicationPeriod.setEndDate(cal.getTime());
boolean allowedToEdit = applicationPeriodFacade.editEndDatePeriod(applicationPeriod);
assertFalse(allowedToEdit); // allowedToEdit should be false
}
/* create a "bachelor" application period from TODAY and a month forward,,
there should exist an OPEN "bachelor"application period */
@Test
@Transactional
@Rollback
public void applicationPeriodExists() {
public void testPeriodExists() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -244,7 +288,7 @@ there should exist an OPEN "bachelor"application period */
final Date endDate1 = cal.getTime();
applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate1, endDate1, "name1");
boolean exists = applicationPeriodFacade.openApplicationPeriodsExists(bachelor);
Assert.assertTrue(exists); // should exist
assertTrue(exists); // should exist
}
/* create a "master" application period from TODAY and two months forward,
@ -252,38 +296,24 @@ there should exist an OPEN "bachelor"application period */
@Test
@Transactional
@Rollback
public void missingApplicationPeriod_1() {
public void testPeriodMissing() {
cleanUpDatabase();
Set<ProjectClass> myMasterSet = new HashSet<ProjectClass>();
myMasterSet.add(master);
Calendar cal = Calendar.getInstance();
final Date startDate1 = cal.getTime();
cal.add(Calendar.MONTH, 2);
cal.add(Calendar.MONTH, 1);
final Date endDate1 = cal.getTime();
applicationPeriodFacade.createApplicationPeriodDates(myMasterSet, startDate1, endDate1, "name1");
boolean exists = applicationPeriodFacade.openApplicationPeriodsExists(bachelor);
Assert.assertFalse(exists); // should NOT exist
assertFalse(exists); // should NOT exist
}
/* the same project classes, the same date, the first period: 10.00 - 11.59,
the second period: 12.00 - 13.59,
should NOT overlap. */
@Test
@Transactional
@Rollback
public void notOverlappingBasedOnTimeLimits() {
// Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
// myBachelorSet.add(bachelor);
// applicationPeriodDao.save(createApplicationPeriod(myBachelorSet, createStartDate_1(), createEndDate_1()));
// boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(createApplicationPeriod(myBachelorSet, createStartDate_2(), createEndDate_2()));
// Assert.assertFalse(periodOverlap); // should NOT overlap
}
/* the same project classes, the date of the first time period ends when the date of the second time period starts, should NOT overlap */
@Test
@Transactional
@Rollback
public void notOverlappingBasedOnDatePeriods() {
public void testOverlapBasedOnDates() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -298,8 +328,9 @@ should NOT overlap. */
final Date startDate2 = cal.getTime();
cal.set(2012, Calendar.MAY, 2);
final Date endDate2 = cal.getTime();
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate2, endDate2, "name1"));
Assert.assertFalse(periodOverlap); // should NOT overlap
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriodFacade.
createApplicationPeriodDates(myBachelorSet, startDate2, endDate2, "name2"));
assertFalse(periodOverlap); // should NOT overlap
}
/* Test for checking if it possible to edit an application period: first create bachelor-application period and save it,
@ -308,7 +339,7 @@ should NOT overlap. */
@Test
@Transactional
@Rollback
public void notOverlapWhenEditing() {
public void testOverlapWhenEditing() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -326,14 +357,14 @@ should NOT overlap. */
// save the changes
applicationPeriodDao.save(applicationPeriod1);
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriod1);
Assert.assertFalse(periodOverlap); // should NOT overlap
assertFalse(periodOverlap); // should NOT overlap
}
/* same time periods, with different project classes, should NOT overlap */
@Test
@Transactional
@Rollback
public void notOverlappingDifferentProjectClasses() {
public void testOverlapDifferentProjectClasses() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -349,7 +380,7 @@ should NOT overlap. */
Set<ProjectClass> myMasterSet = new HashSet<ProjectClass>();
myMasterSet.add(master);
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriodFacade.createApplicationPeriodDates(myMasterSet, startDate, endDate2, "name1"));
Assert.assertFalse(periodOverlap); // should NOT overlap
assertFalse(periodOverlap); // should NOT overlap
}
@ -357,7 +388,7 @@ should NOT overlap. */
@Test
@Transactional
@Rollback
public void overlappingPeriods() {
public void testOverlappingPeriods() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -372,7 +403,7 @@ should NOT overlap. */
cal.set(2012, Calendar.JUNE, 2);
final Date endDate2 = cal.getTime();
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate2, endDate2, "name1"));
Assert.assertTrue(periodOverlap); // should overlap
assertTrue(periodOverlap); // should overlap
}
@ -380,7 +411,7 @@ should NOT overlap. */
@Test
@Transactional
@Rollback
public void overlappingMultipleProjectClasses() {
public void testOverlappingMultipleProjectClasses() {
cleanUpDatabase();
Set<ProjectClass> myBachelorSet = new HashSet<ProjectClass>();
myBachelorSet.add(bachelor);
@ -396,44 +427,7 @@ should NOT overlap. */
multipleProjectClasses.add(bachelor);
multipleProjectClasses.add(master);
boolean periodOverlap = applicationPeriodFacade.doesPeriodOverlap(applicationPeriodFacade.createApplicationPeriodDates(myBachelorSet, startDate, endDate2, "name1"));
Assert.assertTrue(periodOverlap); // should overlap
assertTrue(periodOverlap); // should overlap
}
Date createStartDate_1() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
Date createEndDate_1() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
Date createStartDate_2() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
Date createEndDate_2() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 13);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}