From 0595121bc9cb74d578f74054fdd6a99348371a2c Mon Sep 17 00:00:00 2001
From: Tom Vahlman <tom@dsv.su.se>
Date: Thu, 1 Mar 2012 19:10:22 +0100
Subject: [PATCH] Added a test to verify that the match algorithm considers
 availability (both per project class and "total availability")and "project
 class" when sorting matches according to relevance.

---
 .../match/TestGreedyMatchingAlgorithm.java    | 147 +++++++++++++++---
 1 file changed, 125 insertions(+), 22 deletions(-)

diff --git a/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java b/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
index 1ff2d347d3..a6cdcaf326 100644
--- a/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
+++ b/src/test/java/se/su/dsv/scipro/match/TestGreedyMatchingAlgorithm.java
@@ -468,62 +468,162 @@ public class TestGreedyMatchingAlgorithm {
     }
 
 
+// We now perform three tests to verify the correctness of the method GreedyMatchingAlgorithm # Pair # compareTo,
+// which sorts the list of matches, i. e. Pairs according to specific criterias
     @Test
     @Transactional
     @Rollback
-    /*  test that the supervisor who has the highest total slot available will be chosen */
-    public void testHighestAvailableSlot() {
+    /*  1.
+     *  ==================================================================================
+     *
+     *  Prerequisites: two supervisors are matched against one bachelor project idea,
+     *  both supervisors have the SAME "total availability" (9)
+     *  and they both have a calculated match of 10 points,
+     *
+     *  Tests:  in the first test we verify that "henrikH" is found, because "davidH"
+     *  has less availability slots (4) for bachelor project ideas
+     *
+     * */
+    public void testDavidH_Has_Less_Available_Slots_For_Bachelor() {
+        List<ProjectIdea> projectIdeaList = new ArrayList<ProjectIdea>();
+        List<Employee> supervisorList = new ArrayList<Employee>();
         Employee davidH = createEmployee("David", "Hallberg", languages);
+        supervisorAvailability.add(new Availability(davidH, 1L, 5, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(davidH, 0L, 5, masterProjectClass));
+
         Employee henrikH = createEmployee("Henrik", "Hansson", languages);
-        supervisorAvailability.add(new Availability(henrikH, 2L, 3, bachelorProjectClass)); // total slot = 2
-        supervisorAvailability.add(new Availability(henrikH, 3L, 4, masterProjectClass));
-        supervisorAvailability.add(new Availability(davidH, 1L, 1, bachelorProjectClass));          // total slot = 1
-        supervisorAvailability.add(new Availability(davidH, 0L, 1, masterProjectClass));
-        unmatchedProjectIdeas.add(masterProjectIdea);
-        unmatchedProjectIdeas.add(bachelorProjectIdea);
+        supervisorAvailability.add(new Availability(henrikH, 0L, 5, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(henrikH, 1L, 5, masterProjectClass));
+        supervisorList.add(henrikH);
+
+        ProjectIdea secondBachelorIdea = createProjectIdea(bachelorProjectClass, applicationPeriod);
+        projectIdeaList.clear();
+        projectIdeaList.add(secondBachelorIdea); // David Hallberg is preferred supervisor for this project idea
+        secondBachelorIdea.setPreferredSupervisor(davidH);
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development", false));
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development__", false));
+
+        unmatchedProjectIdeas.add(secondBachelorIdea);
 
         Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
-        assertTrue(result.matches.size() > 0);
-        assertTrue(result.matches.get(0).getProjectIdea().equals(masterProjectIdea));
+        assertTrue(result.matches.size() == 1);
+        assertTrue(result.matches.get(0).getPoints() == 10);
         assertTrue(result.matches.get(0).getSupervisor().equals(henrikH));
-        assertTrue(result.matches.get(1).getProjectIdea().equals(bachelorProjectIdea));
-        assertTrue(result.matches.get(1).getSupervisor().equals(henrikH));
-        assertTrue(result.unmatched.size() == 0);
+        assertTrue(result.matches.get(0).getProjectIdea().equals(secondBachelorIdea));
     }
 
     @Test
     @Transactional
     @Rollback
-    /*  three supervisors are matched against three project ideas  */
-    public void testThreeSupervisorsAndThreeProjectIdeas() {
+   /* .
+   *  2.
+   *  ==================================================================================
+   *  Prerequisites: two supervisors are matched against one bachelor project idea,
+   *  both supervisors have the SAME "total availability" (9)
+   *  and they both have a calculated match of 10 points,
+   *
+   *  Tests: In the second test we verify that "davidH" is found, because "henrikH"
+   *  has less availability slots (4) for bachelor project ideas
+   * */
+    public void testHenrikH_Has_Less_Available_Slots_For_Bachelor() {
+        List<ProjectIdea> projectIdeaList = new ArrayList<ProjectIdea>();
+        List<Employee> supervisorList = new ArrayList<Employee>();
+        Employee davidH = createEmployee("David", "Hallberg", languages);
+        supervisorAvailability.add(new Availability(davidH, 0L, 5, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(davidH, 1L, 5, masterProjectClass));
+
+        Employee henrikH = createEmployee("Henrik", "Hansson", languages);
+        supervisorAvailability.add(new Availability(henrikH, 1L, 5, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(henrikH, 0L, 5, masterProjectClass));
+        supervisorList.add(henrikH);
+
+        ProjectIdea secondBachelorIdea = createProjectIdea(bachelorProjectClass, applicationPeriod);
+        projectIdeaList.clear();
+        projectIdeaList.add(secondBachelorIdea); // David Hallberg is preferred supervisor for this project idea
+        secondBachelorIdea.setPreferredSupervisor(davidH);
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development", false));
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development__", false));
+
+        unmatchedProjectIdeas.add(secondBachelorIdea);
+
+        Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
+        assertTrue(result.matches.size() == 1);
+        assertTrue(result.matches.get(0).getPoints() == 10);
+        assertTrue(result.matches.get(0).getSupervisor().equals(davidH));
+        assertTrue(result.matches.get(0).getProjectIdea().equals(secondBachelorIdea));
+    }
+
+    @Test
+    @Transactional
+    @Rollback
+  /* 3.
+   * ==================================================================================
+   *  Prerequisites: two supervisors are matched against one bachelor project idea,
+   *  both supervisors have "open" availability slots for both master and bachelor projects
+   *  the "total availability" is DIFFERENT 8 slots vs. 9 slots for the supervisors but they have both a calculated match of 10 points,
+   *
+   *  Tests: We now perform a third test "testDavidH_Has_Less_Total_Available_Slots", here we test that "henrikH" is found, because "davidH"
+   *  has less "total available" slots (8) compared to 9 for "henrikH" for project ideas (master + bachelor)
+   * */
+    public void testDavidH_Has_Less_Total_Available_Slots() {
+        List<ProjectIdea> projectIdeaList = new ArrayList<ProjectIdea>();
+        List<Employee> supervisorList = new ArrayList<Employee>();
+        Employee davidH = createEmployee("David", "Hallberg", languages);
+        supervisorAvailability.add(new Availability(davidH, 0L, 4, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(davidH, 1L, 5, masterProjectClass));
+
+        Employee henrikH = createEmployee("Henrik", "Hansson", languages);
+        supervisorAvailability.add(new Availability(henrikH, 1L, 5, bachelorProjectClass));
+        supervisorAvailability.add(new Availability(henrikH, 0L, 5, masterProjectClass));
+        supervisorList.add(henrikH);
+
+        ProjectIdea secondBachelorIdea = createProjectIdea(bachelorProjectClass, applicationPeriod);
+        projectIdeaList.clear();
+        projectIdeaList.add(secondBachelorIdea); // David Hallberg is preferred supervisor for this project idea
+        secondBachelorIdea.setPreferredSupervisor(davidH);
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development", false));
+        addKeyWords(supervisorList, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development__", false));
+
+        unmatchedProjectIdeas.add(secondBachelorIdea);
+
+        Result result = new GreedyMatchingAlgorithm().match(supervisorAvailability, unmatchedProjectIdeas, weights);
+        assertTrue(result.matches.size() == 1);
+        assertTrue(result.matches.get(0).getPoints() == 10);
+        assertTrue(result.matches.get(0).getSupervisor().equals(henrikH));
+        assertTrue(result.matches.get(0).getProjectIdea().equals(secondBachelorIdea));
+    }
+
+    @Test
+    @Transactional
+    @Rollback
+    /*  three supervisors are matched against three project ideas
+     *  tests that project classes are regarded when matching project ideas and supervisors
+     *  when available slots are exactly the same
+     *  this test, which may be a duplicate, is done only to test if the matching algorithm can handle a more "complicated" match
+      * */
+    public void testThreeSupervisorsAndThreeProjectIdeas_Available_Slots_Are_The_Same() {
         List<ProjectIdea> projectIdeaList = new ArrayList<ProjectIdea>();
         List<Employee> supervisorList = new ArrayList<Employee>();
         Employee davidH = createEmployee("David", "Hallberg", languages);
         supervisorAvailability.add(new Availability(davidH, 0L, 5, bachelorProjectClass));
         supervisorAvailability.add(new Availability(davidH, 0L, 5, masterProjectClass));
-
         supervisorList.clear();
         supervisorList.add(davidH);
-        addKeyWords(supervisorList, null, createKeyword(keywordTypeArea, "IT för lärande", false));
-        addKeyWords(supervisorList, null, createKeyword(keywordTypeWord, "Flexibelt lärande", false));
 
         Employee henrikH = createEmployee("Henrik", "Hansson", languages);
         supervisorAvailability.add(new Availability(henrikH, 0L, 5, bachelorProjectClass));
         supervisorAvailability.add(new Availability(henrikH, 0L, 5, masterProjectClass));
-
         supervisorList.clear();
         supervisorList.add(henrikH);
         addKeyWords(supervisorList, null, createKeyword(keywordTypeArea, "ICT for Development", false));
-        addKeyWords(supervisorList, null, createKeyword(keywordTypeWord, "Flexibelt lärande", false));
+        addKeyWords(supervisorList, null, createKeyword(keywordTypeArea, "ICT for Development__", false));
 
         Employee henrikBergstr = createEmployee("Henrik", "Bergström", languages);        // login henrikbe
         supervisorAvailability.add(new Availability(henrikBergstr, 0L, 5, bachelorProjectClass));
         supervisorAvailability.add(new Availability(henrikBergstr, 0L, 5, masterProjectClass));
-
         supervisorList.clear();
         supervisorList.add(henrikBergstr);
         addKeyWords(supervisorList, null, createKeyword(keywordTypeWord, "System development", false));
-        addKeyWords(supervisorList, null, createKeyword(keywordTypeWord, "IT-architectures", false));
 
         ProjectIdea firstBachelorIdea = createProjectIdea(bachelorProjectClass, applicationPeriod);
         projectIdeaList.clear();
@@ -541,6 +641,8 @@ public class TestGreedyMatchingAlgorithm {
         projectIdeaList.clear();
         projectIdeaList.add(secondBachelorIdea); // David Hallberg is preferred supervisor for this project idea
         secondBachelorIdea.setPreferredSupervisor(davidH);
+        addKeyWords(null, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development", false));
+        addKeyWords(null, projectIdeaList, createKeyword(keywordTypeArea, "ICT for Development__", false));
 
         unmatchedProjectIdeas.add(firstBachelorIdea);
         unmatchedProjectIdeas.add(secondBachelorIdea);
@@ -561,4 +663,5 @@ public class TestGreedyMatchingAlgorithm {
         assertTrue(result.matches.get(2).getSupervisor().equals(henrikH));
         assertTrue(result.matches.get(2).getProjectIdea().equals(masterIdea));
     }
+
 }