View Javadoc
1   /*
2    * Copyright 2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
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.ap.service;
17  
18  
19  import org.apache.commons.lang.mutable.MutableLong;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.kuali.student.ap.academicplan.constants.AcademicPlanServiceConstants;
23  import org.kuali.student.ap.academicplan.dto.LearningPlanInfo;
24  import org.kuali.student.ap.academicplan.dto.PlanItemInfo;
25  import org.kuali.student.ap.academicplan.infc.LearningPlan;
26  import org.kuali.student.ap.framework.config.KsapFrameworkServiceLocator;
27  import org.kuali.student.common.test.util.AttributeTester;
28  import org.kuali.student.common.test.util.IdEntityTester;
29  import org.kuali.student.common.test.util.MetaTester;
30  import org.kuali.student.common.test.util.RichTextTester;
31  import org.kuali.student.r2.common.dto.MetaInfo;
32  import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
33  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
34  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
35  import org.kuali.student.r2.common.exceptions.MissingParameterException;
36  import org.kuali.student.r2.common.exceptions.OperationFailedException;
37  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
38  import org.kuali.student.r2.common.util.RichTextHelper;
39  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
40  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41  import org.springframework.test.context.transaction.TransactionConfiguration;
42  import org.springframework.transaction.annotation.Transactional;
43  
44  import java.math.BigDecimal;
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  import static org.junit.Assert.assertEquals;
52  import static org.junit.Assert.assertNotNull;
53  import static org.junit.Assert.assertTrue;
54  
55  
56  @RunWith(SpringJUnit4ClassRunner.class)
57  @TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
58  @Transactional
59  public abstract class TestAcademicPlanServiceImplConformanceExtendedCrud extends TestAcademicPlanServiceImplConformanceBaseCrud
60  {
61  	
62  	// ========================================
63  	// DTO FIELD SPECIFIC METHODS
64  	// ========================================
65  	
66  	// ****************************************************
67  	//           LearningPlanInfo
68  	// ****************************************************
69  	
70  	/*
71  		A method to set the fields for a LearningPlan in a 'test create' section prior to calling the 'create' operation.
72  	*/
73  	public void testCrudLearningPlan_setDTOFieldsForTestCreate(LearningPlanInfo expected) 
74  	{
75          expected.setStudentId("student1");
76          expected.setTypeKey(AcademicPlanServiceConstants.LEARNING_PLAN_TYPE_PLAN);
77          expected.setStateKey(AcademicPlanServiceConstants.LEARNING_PLAN_ACTIVE_STATE_KEY);
78          expected.setDescr(RichTextHelper.buildRichTextInfo("My Plan", "<span>My Plan</span>"));
79          expected.setName("Test Plan Name");
80          expected.setMeta(new MetaInfo());
81          expected.setShared(false);
82  	}
83  	
84  	/*
85  		A method to test the fields for a LearningPlan. This is called after:
86  		- creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
87  		- reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
88  		- updating a DTO, where actual is DTO returned by the update operation, and expected is the dto that was passed in to the update operation
89  	*/
90  	public void testCrudLearningPlan_testDTOFieldsForTestCreateUpdate(LearningPlanInfo expected, LearningPlanInfo actual) 
91  	{
92  		assertEquals (expected.getStudentId(), actual.getStudentId());
93  		assertEquals (expected.getTypeKey(), actual.getTypeKey());
94  		assertEquals (expected.getStateKey(), actual.getStateKey());
95  		assertEquals (expected.getShared(), actual.getShared());
96          assertEquals (expected.getName(), actual.getName());
97  		new RichTextTester().check(expected.getDescr(), actual.getDescr());
98  	}
99  	
100 	/*
101 		A method to set the fields for a LearningPlan in a 'test update' section prior to calling the 'update' operation.
102 	*/
103 	public void testCrudLearningPlan_setDTOFieldsForTestUpdate(LearningPlanInfo expected) 
104 	{
105 		expected.setShared(false);
106         expected.setDescr(RichTextHelper.buildRichTextInfo("My Plan updated", "<span>My Plan updated</span>"));
107         expected.setName("Test Plan Name updated");
108 	}
109 	
110 	/*
111 		A method to test the fields for a LearningPlan after an update operation, followed by a read operation,
112 		where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
113 	*/
114 	public void testCrudLearningPlan_testDTOFieldsForTestReadAfterUpdate(LearningPlanInfo expected, LearningPlanInfo actual) 
115 	{
116 		assertEquals (expected.getStudentId(), actual.getStudentId());
117 		assertEquals (expected.getId(), actual.getId());
118 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
119 		assertEquals (expected.getStateKey(), actual.getStateKey());
120 		assertEquals (expected.getShared(), actual.getShared());
121         assertEquals (expected.getName(), actual.getName());
122 		new RichTextTester().check(expected.getDescr(), actual.getDescr());
123 	}
124 	
125 	/*
126 		A method to set the fields for a LearningPlan in the 'test read after update' section.
127 		This dto is another (second) dto object being created for other tests.
128 	*/
129 	public void testCrudLearningPlan_setDTOFieldsForTestReadAfterUpdate(LearningPlanInfo expected) 
130 	{
131         expected.setShared(true);
132         expected.setDescr(RichTextHelper.buildRichTextInfo("My Plan updated again", "<span>My Plan updated again</span>"));
133         expected.setName("Test Plan Name updated again");
134 	}
135 	
136 	
137 	// ****************************************************
138 	//           PlanItemInfo
139 	// ****************************************************
140 	
141 	/*
142 		A method to set the fields for a PlanItem in a 'test create' section prior to calling the 'create' operation.
143 	*/
144 	public void testCrudPlanItem_setDTOFieldsForTestCreate(PlanItemInfo expected) 
145 	{
146 		expected.setRefObjectId("ENGL101ind");
147 		expected.setRefObjectType(CluServiceConstants.CREDIT_COURSE_LU_TYPE_KEY);
148         List<String> planTermIds= new ArrayList<String>();
149         planTermIds.add("planTermId01");
150         planTermIds.add("planTermid02");
151 		expected.setPlanTermIds(planTermIds);
152         expected.setCredit(new BigDecimal("3.10"));
153 		expected.setCategory(AcademicPlanServiceConstants.ItemCategory.PLANNED);
154 		expected.setTypeKey(AcademicPlanServiceConstants.LEARNING_PLAN_ITEM_TYPE);
155 		expected.setStateKey("stateKey01");
156         expected.setName("item name 01");
157 		expected.setDescr(RichTextHelper.buildRichTextInfo("descr01", "<span>descr01</span>"));
158 	}
159 	
160 	/*
161 		A method to test the fields for a PlanItem. This is called after:
162 		- creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
163 		- reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
164 		- updating a DTO, where actual is DTO returned by the update operation, and expected is the dto that was passed in to the update operation
165 	*/
166 	public void testCrudPlanItem_testDTOFieldsForTestCreateUpdate(PlanItemInfo expected, PlanItemInfo actual) 
167 	{
168 		assertEquals (expected.getRefObjectId(), actual.getRefObjectId());
169 		assertEquals (expected.getRefObjectType(), actual.getRefObjectType());
170 		assertEquals (expected.getLearningPlanId(), actual.getLearningPlanId());
171         Collections.sort(expected.getPlanTermIds());
172         Collections.sort(actual.getPlanTermIds());
173 		assertEquals (expected.getPlanTermIds(), actual.getPlanTermIds());
174         assertEquals(expected.getCredits(), actual.getCredits());
175 		assertEquals(expected.getCategory(), actual.getCategory());
176 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
177 		assertEquals (expected.getStateKey(), actual.getStateKey());
178         assertEquals (expected.getName(), actual.getName());
179 		new RichTextTester().check(expected.getDescr(), actual.getDescr());
180 	}
181 	
182 	/*
183 		A method to set the fields for a PlanItem in a 'test update' section prior to calling the 'update' operation.
184 	*/
185 	public void testCrudPlanItem_setDTOFieldsForTestUpdate(PlanItemInfo expected) 
186 	{
187 		expected.setRefObjectId("CHEM131ind");
188         expected.setRefObjectType(CluServiceConstants.CREDIT_COURSE_LU_TYPE_KEY);
189         expected.getPlanTermIds().remove("planTermId01");
190         expected.getPlanTermIds().add("planTermId03");
191 		expected.setCredit(new BigDecimal("4.00"));
192         expected.setCategory(AcademicPlanServiceConstants.ItemCategory.BACKUP);
193 		expected.setStateKey("stateKeyUpdated");
194         expected.setName("item name 01  updated");
195 		expected.setDescr(RichTextHelper.buildRichTextInfo("descr_Updated", "<span>descr_Updated</span>"));
196 	}
197 	
198 	/*
199 		A method to test the fields for a PlanItem after an update operation, followed by a read operation,
200 		where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
201 	*/
202 	public void testCrudPlanItem_testDTOFieldsForTestReadAfterUpdate(PlanItemInfo expected, PlanItemInfo actual) 
203 	{
204 		assertEquals (expected.getRefObjectId(), actual.getRefObjectId());
205 		assertEquals (expected.getRefObjectType(), actual.getRefObjectType());
206 		assertEquals (expected.getLearningPlanId(), actual.getLearningPlanId());
207         Collections.sort(expected.getPlanTermIds());
208         Collections.sort(actual.getPlanTermIds());
209 		assertEquals (expected.getPlanTermIds(), actual.getPlanTermIds());
210 		assertEquals (expected.getId(), actual.getId());
211 		assertEquals (expected.getCredits(), actual.getCredits());
212 		assertEquals (expected.getCategory(), actual.getCategory());
213 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
214 		assertEquals (expected.getStateKey(), actual.getStateKey());
215         assertEquals (expected.getName(), actual.getName());
216 		new RichTextTester().check(expected.getDescr(), actual.getDescr());
217 	}
218 	
219 	/*
220 		A method to set the fields for a PlanItem in the 'test read after update' section.
221 		This dto is another (second) dto object being created for other tests.
222 	*/
223 	public void testCrudPlanItem_setDTOFieldsForTestReadAfterUpdate(PlanItemInfo expected) 
224 	{
225         expected.setRefObjectId("CHEM131ind");
226         expected.setRefObjectType(CluServiceConstants.CREDIT_COURSE_LU_TYPE_KEY);
227 		expected.setLearningPlanId("learningPlanId_Updated");
228         expected.getPlanTermIds().add("planTermId02");
229         expected.getPlanTermIds().add("planTermId03");
230         expected.setCredit(new BigDecimal("4.00"));
231         expected.setName("item name 01 updated again");
232         expected.setDescr(RichTextHelper.buildRichTextInfo("descr_Updated2", "<span>descr_Updated2</span>"));
233         expected.setCategory(AcademicPlanServiceConstants.ItemCategory.BACKUP);
234 	}
235 
236 
237 	// ========================================
238 	// SERVICE OPS NOT TESTED IN BASE TEST CLASS
239 	// ========================================
240 
241 	/* Method Name: getPlanItemsInPlanByCategory */
242 	@Test
243 	public void test_getPlanItemsInPlanByCategory()
244 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	{
245 	}
246 
247     /* Method Name: getPlanItemsInPlanByCategories */
248     @Test
249     public void test_getPlanItemsByPlanTermAndCategories()
250             throws DoesNotExistException, InvalidParameterException, MissingParameterException,
251                    OperationFailedException, PermissionDeniedException {
252 
253         String studentId = "student1";
254         List<LearningPlanInfo> learningPlans;
255         learningPlans = KsapFrameworkServiceLocator.getAcademicPlanService().getLearningPlansForStudentByType(
256                 studentId,
257                 AcademicPlanServiceConstants.LEARNING_PLAN_TYPE_PLAN,
258                 KsapFrameworkServiceLocator.getContext().getContextInfo());
259         assertNotNull(learningPlans);
260         assertTrue(String.format("no learning plans found for studenId=%s to test",studentId),
261                 learningPlans.size()>1); //"should be > 1
262         int plansThatHaveItems=0;
263         String useThisTerm=null;
264         for (LearningPlan plan : learningPlans) {
265             int categoryCount=0;
266             boolean planHasMultipleCategories=false;
267             HashMap<String,HashMap<AcademicPlanServiceConstants.ItemCategory,MutableLong>> termCategoryMap = new
268                     HashMap<>();
269 
270             List<PlanItemInfo> items = KsapFrameworkServiceLocator.getAcademicPlanService()
271                     .getPlanItemsInPlan(plan.getId(),KsapFrameworkServiceLocator.getContext().getContextInfo());
272             //Look for non-empty learning plans
273             if (items.size()>0)
274                 ++plansThatHaveItems;
275 
276             //Look for and count distinct items per categor per term
277             if (items.size()>1) {
278                 for (PlanItemInfo item : items) {
279                     for (String term : item.getPlanTermIds()) {
280                         if (!termCategoryMap.containsKey(term)) {
281                             termCategoryMap.put(term,new HashMap<AcademicPlanServiceConstants.ItemCategory,MutableLong>());
282                         }
283                         if (!termCategoryMap.get(term).containsKey(item.getCategory())) {
284                             termCategoryMap.get(term).put(item.getCategory(),new MutableLong(0L));
285                         }
286                         termCategoryMap.get(term).get(item.getCategory()).add(1L);
287                     }
288                 }
289             }
290             //No look for terms that use more that one category
291             for (String term : termCategoryMap.keySet()) {
292                 if (useThisTerm==null && termCategoryMap.get(term).size()>1 ) {
293                         planHasMultipleCategories=true;
294                         useThisTerm=term;
295                         continue;
296                 }
297             }
298 
299             //No check that  getPlanItemsByPlanTermAndCategories returns the appropriate # of items
300             //for categories
301             if (planHasMultipleCategories) {
302                 int totalItemsPerTermSoFar=0;
303                 List<AcademicPlanServiceConstants.ItemCategory> categories = new ArrayList<>();
304                 //Get items for 1st category of term, then 1st+2nd categories then 1st+2nd+3rd categories...
305                 // .... & check count ea. time
306                 for (AcademicPlanServiceConstants.ItemCategory category : termCategoryMap.get(useThisTerm).keySet()) {
307                     totalItemsPerTermSoFar+=termCategoryMap.get(useThisTerm).get(category).longValue(); //sum counts
308                     categories.add(category);
309                     List<PlanItemInfo> itemsSoFar = getAcademicPlanService().
310                             getPlanItemsByPlanTermAndCategories(plan.getId(), useThisTerm, categories, contextInfo);
311 
312                     //compare sum of map counts to items returned b getPlanItemsByPlanTermAndCategories
313                     assertEquals("wrong item count returned by getPlanItemsByPlanTermAndCategories",
314                             totalItemsPerTermSoFar,itemsSoFar.size());
315                 }
316             }
317         }
318         assertTrue(String.format("this test expects multiple plans to exist for student %s",studentId),
319                 plansThatHaveItems > 1);
320         assertTrue("this test expects a learning plan having items in more than one category in a term",
321             useThisTerm!=null);
322     }
323 
324     /* Method Name: getPlanItemsInPlan */
325 	@Test
326 	public void test_getPlanItemsInPlan() 
327 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	{
328 	}
329 	
330 	/* Method Name: getPlanItemsInPlanByTermIdByCategory */
331 	@Test
332 	public void test_getPlanItemsInPlanByAtp() 
333 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	{
334 	}
335 	
336 	/* Method Name: getPlanItemsInPlanByRefObjectIdByRefObjectType */
337 	@Test
338 	public void test_getPlanItemsInPlanByRefObjectIdByRefObjectType()
339             throws DoesNotExistException, InvalidParameterException, MissingParameterException,
340                    OperationFailedException, PermissionDeniedException {
341 	}
342 	
343 	/* Method Name: validateLearningPlan */
344 	@Test
345 	public void test_validateLearningPlan() 
346 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	{
347 	}
348 	
349 	/* Method Name: validatePlanItem */
350 	@Test
351 	public void test_validatePlanItem() 
352 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,AlreadyExistsException {
353 	}
354 	
355 }
356 
357