View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation
3    *
4    * Licensed under the the Educational Community License, Version 1.0
5    * (the "License"); you may not use this file except in compliance
6    * with the License.  You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.r2.common.permutation;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.junit.runners.BlockJUnit4ClassRunner;
30  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
31  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
32  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
33  import org.kuali.student.r2.common.exceptions.MissingParameterException;
34  import org.kuali.student.r2.common.exceptions.OperationFailedException;
35  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
36  
37  /**
38   * Tests against the PermutationUtils class which provides the permutation generation support that is used 
39   * to Generate Unconstrained Registration Groups in @see {@link CourseOfferingService}.
40   * 
41   * @author ocleirig
42   *
43   */
44  @RunWith(BlockJUnit4ClassRunner.class)
45  public class TestPermutationUtils {
46  
47  	
48  	public TestPermutationUtils() {
49  	}
50  
51  	@Test
52  	public void testGeneratePermutations() throws DoesNotExistException,
53  			InvalidParameterException, MissingParameterException,
54  			OperationFailedException, PermissionDeniedException {
55  
56  		/*
57  		 * This tests the permutation generation logic that is used when generating registration groups.
58  		 * 
59  		 * We want to make a group of each ao for each activity type.
60  		 * 
61  		 * Such that each activity type is a certain column in the results list and each permutation exists only once.
62  		 * 
63  		 * e.g. Lec A, Lab X, Lab Y -> A, X and A, Y never X, A or Y, A.
64  		 * 
65  		 * So only 1/2 of the available permutations are
66  		 * 
67  		 */
68  		Map<String, List<String>> typeToListMap = new HashMap<String, List<String>>();
69  
70  		List<String> lecList = new ArrayList<String>(2);
71  
72  		lecList.add("A");
73  		lecList.add("B");
74  
75  		// lecture activity type
76  		typeToListMap.put("LEC", lecList);
77  
78  		List<String> labList = new ArrayList<String>(3);
79  
80  		labList.add("X");
81  		labList.add("Y");
82  		labList.add("Z");
83  
84  		// lab activity type
85  		typeToListMap.put("LAB", labList);
86  
87  		Set<String> nextStateSet = new HashSet<String>();
88  
89  		nextStateSet.addAll(typeToListMap.keySet());
90  
91  		int expectedPermutations = 0;
92  
93  		for (String key : typeToListMap.keySet()) {
94  
95  			int length = typeToListMap.get(key).size();
96  
97  			if (expectedPermutations == 0)
98  				expectedPermutations = length;
99  			else
100 				expectedPermutations *= length;
101 		}
102 
103 		assertEquals(6, expectedPermutations);
104 
105 		List<List<String>> permutations = new ArrayList<List<String>>();
106 
107 		ArrayList<String> keyList = new ArrayList<String>(
108 				typeToListMap.keySet());
109 
110 		PermutationUtils.generatePermutations(keyList, new ArrayList<String>(), typeToListMap,
111 				permutations);
112 
113 		assertEquals(6, permutations.size());
114 
115 		List<String> discussionList = new ArrayList<String>();
116 
117 		discussionList.add("Q");
118 		discussionList.add("R");
119 		discussionList.add("S");
120 		discussionList.add("T");
121 
122 		// now add discussion activity type
123 		typeToListMap.put("DIS", discussionList);
124 
125 		permutations.clear();
126 
127 		keyList = new ArrayList<String>(typeToListMap.keySet());
128 
129 		PermutationUtils.generatePermutations(keyList, new ArrayList<String>(), typeToListMap,
130 				permutations);
131 
132 		assertEquals(24, permutations.size());
133 
134 	}
135 }