From 68f76ae00b3c3112512c9917fe587ad0e1ac1a55 Mon Sep 17 00:00:00 2001
From: Fredrik Friis <fred-fri@dsv.su.se>
Date: Fri, 13 Apr 2012 21:38:02 +0900
Subject: [PATCH] testing, one method doesnt seem to work

---
 .../dsv/scipro/springdata/TestChecklist.java  | 206 ++++++++++++++++++
 1 file changed, 206 insertions(+)
 create mode 100644 src/test/java/se/su/dsv/scipro/springdata/TestChecklist.java

diff --git a/src/test/java/se/su/dsv/scipro/springdata/TestChecklist.java b/src/test/java/se/su/dsv/scipro/springdata/TestChecklist.java
new file mode 100644
index 0000000000..05342fa4ad
--- /dev/null
+++ b/src/test/java/se/su/dsv/scipro/springdata/TestChecklist.java
@@ -0,0 +1,206 @@
+package se.su.dsv.scipro.springdata;
+
+
+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.dao.interfaces.CheckListAnswerDao;
+import se.su.dsv.scipro.data.dao.interfaces.CheckListQuestionDao;
+import se.su.dsv.scipro.data.dao.interfaces.ProjectDao;
+import se.su.dsv.scipro.data.dataobjects.*;
+import se.su.dsv.scipro.data.enums.CheckListQuestionAnswer;
+import se.su.dsv.scipro.match.facade.ApplicationPeriodFacade;
+import se.su.dsv.scipro.springdata.services.*;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(inheritLocations = false, locations = {
+        "classpath:test-applicationContext.xml"
+})
+public class TestChecklist {
+
+    @Autowired
+    private UserService userService;
+    @Autowired
+    private ProjectIdeaService projectIdeaService;
+    @Autowired
+    private ProjectClassService projectClassService;
+    @Autowired
+    private RoleService roleService;
+    @Autowired
+    private ApplicationPeriodFacade applicationPeriodFacade;
+    @Autowired
+    private MatchService matchService;
+
+    @Autowired
+    private ProjectDao projectDao;
+    @Autowired
+    private ChecklistService checklistService;
+    @Autowired
+    private CheckListQuestionDao checkListQuestionDao;
+    @Autowired
+    private CheckListAnswerDao checkListAnswerDao;
+    
+    private User employee1;
+    private Employee employee1Role;
+    private ProjectClass bachelor;
+    private Project project;
+
+    private List<CheckList> checkLists;
+    private CheckList checkList;
+
+    private List<CheckListQuestion> checkListQuestions;
+    private CheckListQuestion checkListQuestion;
+    
+    private List<CheckListAnswer> checkListAnswers;
+    private CheckListAnswer checkListAnswer;
+
+    @Before
+    public void startTransaction() throws Exception {
+
+        employee1 = new User();
+        employee1 = userService.save(employee1);
+
+        employee1Role = new Employee();
+        employee1Role.setUser(employee1);
+        employee1Role = (Employee) roleService.save(employee1Role);
+        
+        bachelor = new ProjectClass(ProjectClass.BACHELOR, "Bachelor", "Bachelor degree thesis project");
+        bachelor = projectClassService.save(bachelor);
+
+        project = new Project();
+        project.setHeadSupervisor(employee1Role);
+        project.setTitle("project");
+        project.setProjectClass(bachelor);
+        project = projectDao.save(project);
+
+        checkListAnswers = new ArrayList<CheckListAnswer>();
+        checkListAnswer = new CheckListAnswer();
+        checkListAnswer.setAnswer(CheckListQuestionAnswer.GREEN);
+        checkListAnswer.setUser(employee1);
+        checkListAnswer = checkListAnswerDao.save(checkListAnswer);
+        checkListAnswers.add(checkListAnswer);
+
+        checkListQuestions = new ArrayList<CheckListQuestion>();
+        checkListQuestion = new CheckListQuestion();
+        checkListQuestion.setQuestion("question?");
+        checkListQuestion.setQuestionNumber(1);
+        checkListQuestion.setAnswers(checkListAnswers);
+        checkListQuestion = checkListQuestionDao.save(checkListQuestion);
+        checkListQuestions.add(checkListQuestion);
+
+        checkLists = new ArrayList<CheckList>();
+        checkList = new CheckList();
+        checkList.setName("checklist");
+        checkList.setProject(project);
+        checkList.setQuestions(checkListQuestions);
+        checkList = checklistService.save(checkList);
+    }
+
+    @Test
+    @Transactional
+    @Rollback
+    public void testStuff() {
+        Assert.assertTrue(
+        checkList.getQuestions()!=null
+        && !checkList.getQuestions().isEmpty()
+        && checkList.getQuestions().get(0)!=null
+        && checkList.getQuestions().get(0).getAnswers() != null
+        && !checkList.getQuestions().get(0).getAnswers().isEmpty()
+        && checkList.getQuestions().get(0).getAnswers().get(0) != null
+        && checkList.getQuestions().get(0).getAnswers().get(0).getAnswer() != null
+        && checkList.getQuestions().get(0).getAnswers().get(0).getAnswer().equals(CheckListQuestionAnswer.GREEN)
+        && checkList.getUserLastOpenDate().get(employee1) == null
+        );
+    }
+
+    /**
+     * If we run method, the date should get updated.
+     */
+    @Test
+    @Transactional
+    @Rollback
+    public void testupdateUserLastOpenDate() {
+        /**
+         * Since the date is null ATM, we need to put a new date and save the checklist.
+         */
+        Date oldDate = new Date();
+        checkList.getUserLastOpenDate().put(employee1, oldDate);
+        checkList = checklistService.save(checkList);
+        /**
+         * Then we run the method, which should update the date to a newer date.
+         */
+        checkList = checklistService.updateUserLastOpenDate(checkList, employee1);
+        Date newDate = checkList.getUserLastOpenDate().get(employee1);
+        /**
+         * The old date should now be before the new date.
+         */
+        Assert.assertTrue(oldDate.before(newDate));
+    }
+
+    /**
+     * If we run method with user, getUserLastOpenUpdate for user should no longer be null.
+     */
+    @Test
+    @Transactional
+    @Rollback
+    public void testhasChangedSinceUserLastOpenDate1() {
+        checklistService.hasChangedSinceUserLastOpenDate(checkList, employee1);
+        Assert.assertNotSame(null, checkList.getUserLastOpenDate().get(employee1));
+    }
+    
+    /**
+     * User has not yet opened the checklist with the new functionality. We have no way of knowing
+     * when the user last opened the checklist, so method should return false.
+     */
+    @Test
+    @Transactional
+    @Rollback
+    public void testhasChangedSinceUserLastOpenDate2() {
+        Assert.assertFalse(checklistService.hasChangedSinceUserLastOpenDate(checkList, employee1));
+    }
+
+    /**
+     * We run the update method. Since the updated date will be newer than the lastModified of
+     * the checklistanswer, the method should return false.
+     */
+    @Test
+    @Transactional
+    @Rollback
+    public void testhasChangedSinceUserLastOpenDate3() {
+        checkList = checklistService.updateUserLastOpenDate(checkList, employee1);
+        Assert.assertFalse(checklistService.hasChangedSinceUserLastOpenDate(checkList, employee1));
+    }
+
+    @Test
+    @Transactional
+    @Rollback
+    public void testhasChangedSinceUserLastOpenDate4() {
+//        Assert.assertTrue(checkList.getUserLastOpenDate().get(employee1)==null); true
+
+//        Date lastModified = checkListAnswer.getLastModified();
+//        Assert.assertTrue(lastModified!=null); true
+
+//        checkList = checklistService.updateUserLastOpenDate(checkList, employee1);
+//        Assert.assertTrue(checkList.getUserLastOpenDate().get(employee1)!=null); true
+        
+        checkList = checklistService.updateUserLastOpenDate(checkList, employee1);
+        Date newDate = new Date();
+        checkListAnswer.setLastModified(newDate);
+
+//        Assert.assertTrue(checkList.getUserLastOpenDate().get(employee1).before(newDate)); true
+
+        Assert.assertFalse(checklistService.hasChangedSinceUserLastOpenDate(checkList, employee1));
+
+    }
+    
+}