View Javadoc

1   /*
2    * Copyright 2013 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 Lic+ense 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.enrollment.class1.roster.service.impl;
17  
18  
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.List;
23  import javax.annotation.Resource;
24  
25  import org.junit.After;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.kuali.student.common.test.util.RichTextTester;
29  import org.kuali.student.enrollment.roster.dto.LprRosterEntryInfo;
30  import org.kuali.student.enrollment.roster.dto.LprRosterInfo;
31  import org.kuali.student.r2.common.dto.TimeAmountInfo;
32  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
33  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
34  import org.kuali.student.r2.common.exceptions.MissingParameterException;
35  import org.kuali.student.r2.common.exceptions.OperationFailedException;
36  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
37  import org.kuali.student.r2.common.infc.HasId;
38  import org.kuali.student.r2.common.util.RichTextHelper;
39  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
40  import org.kuali.student.r2.core.constants.AtpServiceConstants;
41  import org.springframework.test.context.ContextConfiguration;
42  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
43  import static org.junit.Assert.assertEquals;
44  import static org.junit.Assert.assertFalse;
45  import static org.junit.Assert.assertNotNull;
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  
50  
51  @RunWith(SpringJUnit4ClassRunner.class)
52  @ContextConfiguration(locations = {"classpath:lprRoster-test-with-map-context.xml"})
53  public class TestLprRosterServiceImplConformanceExtendedCrud extends TestLprRosterServiceImplConformanceBaseCrud 
54  {
55  
56      @Resource
57      protected LprRosterDataLoader dataLoader;
58      private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
59  
60      @After
61      public void tearDownExtended() throws Exception {
62          dataLoader.afterTest();
63      }
64  	
65  	// ========================================
66  	// DTO FIELD SPECIFIC METHODS
67  	// ========================================
68  	
69  	// ****************************************************
70  	//           LprRosterInfo
71  	// ****************************************************
72  	
73  	/*
74  		A method to set the fields for a LprRoster in a 'test create' section prior to calling the 'create' operation.
75  	*/
76  	public void testCrudLprRoster_setDTOFieldsForTestCreate(LprRosterInfo expected) 
77  	{
78  		expected.setTypeKey("typeKey01");
79  		expected.setStateKey("stateKey01");
80  		expected.setName("name01");
81  		expected.setDescr(RichTextHelper.buildRichTextInfo("plain descr " + expected.getName(), "formatted descr " + expected.getName()));
82          List<String >associatedLuis = new ArrayList<String>();
83          associatedLuis.add("50");
84          associatedLuis.add("60");
85          associatedLuis.add("70");
86          associatedLuis.add("80");
87          associatedLuis.add("90");
88  		expected.setAssociatedLuiIds(associatedLuis);
89          expected.setMaximumCapacity(100);
90  		expected.setCheckInRequired(true);
91          TimeAmountInfo timeAmountInfo = new TimeAmountInfo();
92          timeAmountInfo.setTimeQuantity(5);
93          timeAmountInfo.setAtpDurationTypeKey(AtpServiceConstants.DURATION_WEEK_TYPE_KEY);
94  		expected.setCheckInFrequency(timeAmountInfo);
95  	}
96  	
97  	/*
98  		A method to test the fields for a LprRoster. This is called after:
99  		- creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
100 		- reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
101 		- 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
102 	*/
103 	public void testCrudLprRoster_testDTOFieldsForTestCreateUpdate(LprRosterInfo expected, LprRosterInfo actual) 
104 	{
105 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
106 		assertEquals (expected.getStateKey(), actual.getStateKey());
107 		assertEquals (expected.getName(), actual.getName());
108         new RichTextTester().check(expected.getDescr(), actual.getDescr());
109         if(expected.getAssociatedLuiIds() != null) {
110             assertEquals(expected.getAssociatedLuiIds().size(), actual.getAssociatedLuiIds().size());
111             for(String luiId : expected.getAssociatedLuiIds()) {
112                 assertTrue(actual.getAssociatedLuiIds().contains(luiId));
113             }
114         } else {
115             assertNull(actual.getAssociatedLuiIds());
116         }
117 
118 		assertEquals(expected.getMaximumCapacity(), actual.getMaximumCapacity());
119 		assertEquals(expected.getCheckInRequired(), actual.getCheckInRequired());
120         if(expected.getCheckInFrequency() != null) {
121             assertEquals(expected.getCheckInFrequency().getTimeQuantity(), actual.getCheckInFrequency().getTimeQuantity());
122             assertEquals(expected.getCheckInFrequency().getAtpDurationTypeKey(), actual.getCheckInFrequency().getAtpDurationTypeKey());
123         } else {
124             assertNull(actual.getCheckInFrequency());
125         }
126 	}
127 	
128 	/*
129 		A method to set the fields for a LprRoster in a 'test update' section prior to calling the 'update' operation.
130 	*/
131 	public void testCrudLprRoster_setDTOFieldsForTestUpdate(LprRosterInfo expected) 
132 	{
133 		expected.setStateKey("stateKey_Updated");
134 		expected.setName("name_Updated");
135 		expected.setDescr(RichTextHelper.buildRichTextInfo(expected.getDescr().getPlain() + "_Updated", expected.getDescr().getFormatted() + "_Updated"));
136         List<String >associatedLuis = new ArrayList<String>();
137         associatedLuis.add("100");
138         expected.setAssociatedLuiIds(associatedLuis);
139 		expected.setMaximumCapacity(0);
140 		expected.setCheckInRequired(false);
141         TimeAmountInfo timeAmountInfo = new TimeAmountInfo();
142         timeAmountInfo.setTimeQuantity(10);
143         timeAmountInfo.setAtpDurationTypeKey(AtpServiceConstants.DURATION_MONTH_TYPE_KEY);
144         expected.setCheckInFrequency(timeAmountInfo);
145 	}
146 	
147 	/*
148 		A method to test the fields for a LprRoster after an update operation, followed by a read operation,
149 		where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
150 	*/
151 	public void testCrudLprRoster_testDTOFieldsForTestReadAfterUpdate(LprRosterInfo expected, LprRosterInfo actual) 
152 	{
153 		assertEquals (expected.getId(), actual.getId());
154 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
155 		assertEquals (expected.getStateKey(), actual.getStateKey());
156 		assertEquals (expected.getName(), actual.getName());
157         new RichTextTester().check(expected.getDescr(), actual.getDescr());
158         if(expected.getAssociatedLuiIds() != null) {
159             assertEquals(expected.getAssociatedLuiIds().size(), actual.getAssociatedLuiIds().size());
160             for(String luiId : expected.getAssociatedLuiIds()) {
161                 assertTrue(actual.getAssociatedLuiIds().contains(luiId));
162             }
163         } else {
164             assertNull(actual.getAssociatedLuiIds());
165         }
166 
167         assertEquals(expected.getMaximumCapacity(), actual.getMaximumCapacity());
168         assertEquals(expected.getCheckInRequired(), actual.getCheckInRequired());
169         if(expected.getCheckInFrequency() != null) {
170             assertEquals(expected.getCheckInFrequency().getTimeQuantity(), actual.getCheckInFrequency().getTimeQuantity());
171             assertEquals(expected.getCheckInFrequency().getAtpDurationTypeKey(), actual.getCheckInFrequency().getAtpDurationTypeKey());
172         } else {
173             assertNull(actual.getCheckInFrequency());
174         }
175 	}
176 	
177 	/*
178 		A method to set the fields for a LprRoster in the 'test read after update' section.
179 		This dto is another (second) dto object being created for other tests.
180 	*/
181 	public void testCrudLprRoster_setDTOFieldsForTestReadAfterUpdate(LprRosterInfo expected) 
182 	{
183 		expected.setName("name_Updated");
184         List<String >associatedLuis = new ArrayList<String>();
185         associatedLuis.add("101");
186         associatedLuis.add("102");
187         associatedLuis.add("103");
188         expected.setAssociatedLuiIds(associatedLuis);
189         expected.setMaximumCapacity(11);
190         expected.setCheckInRequired(true);
191         TimeAmountInfo timeAmountInfo = new TimeAmountInfo();
192         timeAmountInfo.setTimeQuantity(5);
193         timeAmountInfo.setAtpDurationTypeKey(AtpServiceConstants.DURATION_MINUTES_TYPE_KEY);
194         expected.setCheckInFrequency(timeAmountInfo);
195 	}
196 	
197 	
198 	// ****************************************************
199 	//           LprRosterEntryInfo
200 	// ****************************************************
201 	
202 	/*
203 		A method to set the fields for a LprRosterEntry in a 'test create' section prior to calling the 'create' operation.
204 	*/
205 	public void testCrudLprRosterEntry_setDTOFieldsForTestCreate(LprRosterEntryInfo expected) {
206 		expected.setTypeKey("typeKey01");
207 		expected.setStateKey("stateKey01");
208         try {
209             expected.setEffectiveDate(dateFormat.parse("20130611"));
210             expected.setExpirationDate(dateFormat.parse("21000101"));
211         } catch (ParseException e) {
212             throw new RuntimeException("Failed to parse date", e);
213         }
214 		expected.setLprRosterId("A");
215 		expected.setLprId("100");
216 		expected.setPosition(5);
217 	}
218 	
219 	/*
220 		A method to test the fields for a LprRosterEntry. This is called after:
221 		- creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
222 		- reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
223 		- 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
224 	*/
225 	public void testCrudLprRosterEntry_testDTOFieldsForTestCreateUpdate(LprRosterEntryInfo expected, LprRosterEntryInfo actual) 
226 	{
227 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
228 		assertEquals (expected.getStateKey(), actual.getStateKey());
229 		assertEquals (expected.getEffectiveDate(), actual.getEffectiveDate());
230 		assertEquals (expected.getExpirationDate(), actual.getExpirationDate());
231 		assertEquals (expected.getLprRosterId(), actual.getLprRosterId());
232 		assertEquals (expected.getLprId(), actual.getLprId());
233 		assertEquals (expected.getPosition(), actual.getPosition());
234 	}
235 	
236 	/*
237 		A method to set the fields for a LprRosterEntry in a 'test update' section prior to calling the 'update' operation.
238 	*/
239 	public void testCrudLprRosterEntry_setDTOFieldsForTestUpdate(LprRosterEntryInfo expected) {
240 		expected.setStateKey("stateKey_Updated");
241         try {
242             expected.setEffectiveDate(dateFormat.parse("20120611"));
243             expected.setExpirationDate(dateFormat.parse("20130601"));
244         } catch (ParseException e) {
245             throw new RuntimeException("Failed to parse date", e);
246         }
247         expected.setLprRosterId("A");
248         expected.setLprId("100");
249         expected.setPosition(2);
250 	}
251 	
252 	/*
253 		A method to test the fields for a LprRosterEntry after an update operation, followed by a read operation,
254 		where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
255 	*/
256 	public void testCrudLprRosterEntry_testDTOFieldsForTestReadAfterUpdate(LprRosterEntryInfo expected, LprRosterEntryInfo actual) 
257 	{
258 		assertEquals (expected.getId(), actual.getId());
259 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
260 		assertEquals (expected.getStateKey(), actual.getStateKey());
261 		assertEquals (expected.getEffectiveDate(), actual.getEffectiveDate());
262 		assertEquals (expected.getExpirationDate(), actual.getExpirationDate());
263 		assertEquals (expected.getLprRosterId(), actual.getLprRosterId());
264 		assertEquals (expected.getLprId(), actual.getLprId());
265 		assertEquals (expected.getPosition(), actual.getPosition());
266 	}
267 	
268 	/*
269 		A method to set the fields for a LprRosterEntry in the 'test read after update' section.
270 		This dto is another (second) dto object being created for other tests.
271 	*/
272 	public void testCrudLprRosterEntry_setDTOFieldsForTestReadAfterUpdate(LprRosterEntryInfo expected) 
273 	{
274         expected.setLprRosterId("C");
275         expected.setLprId("222");
276         try {
277             expected.setEffectiveDate(dateFormat.parse("20120611"));
278             expected.setExpirationDate(dateFormat.parse("30300601"));
279         } catch (ParseException e) {
280             throw new RuntimeException("Failed to parse date", e);
281         }
282         expected.setPosition(1);
283 	}
284 	
285 	
286 	// ========================================
287 	// SERVICE OPS NOT TESTED IN BASE TEST CLASS
288 	// ========================================
289 	
290 	/* Method Name: getLprRostersByLui */
291 	@Test
292 	public void test_getLprRostersByLui()
293 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
294         loadData();
295 
296         List<LprRosterInfo> rosters = testService.getLprRostersByLui("4", contextInfo);
297         assertEquals(2, rosters.size());
298         assertContainsId("D", rosters);
299         assertContainsId("B", rosters);
300 
301         rosters = testService.getLprRostersByLui("DOES_NOT_EXIST", contextInfo);
302         assertEquals(0, rosters.size());
303 
304         rosters = testService.getLprRostersByLui("2", contextInfo);
305         assertEquals(1, rosters.size());
306         assertContainsId("B", rosters);
307 	}
308 	
309 	/* Method Name: getLprRostersByTypeAndLui */
310 	@Test
311 	public void test_getLprRostersByTypeAndLui() 
312 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
313         loadData();
314 
315         List<LprRosterInfo> rosters = testService.getLprRostersByTypeAndLui(LprServiceConstants.LPRROSTER_COURSE_MIDTERM_GRADE_TYPE_KEY, "4", contextInfo);
316         assertEquals(2, rosters.size());
317         assertContainsId("D", rosters);
318         assertContainsId("B", rosters);
319 
320         rosters = testService.getLprRostersByTypeAndLui(LprServiceConstants.LPRROSTER_COURSE_MIDTERM_GRADE_TYPE_KEY, "DOES_NOT_EXIST", contextInfo);
321         assertEquals(0, rosters.size());
322 
323         rosters = testService.getLprRostersByTypeAndLui(LprServiceConstants.LPRROSTER_COURSE_FINAL_GRADE_TYPE_KEY, "1", contextInfo);
324         assertEquals(1, rosters.size());
325         assertContainsId("A", rosters);
326 
327         rosters = testService.getLprRostersByTypeAndLui("SOME_TYPE_KEY_THAT_DOES_NOT_EXIST", "1", contextInfo);
328         assertEquals(0, rosters.size());
329 	}
330 	
331 	/* Method Name: searchForLprRosterIds */
332 	@Test
333 	public void test_searchForLprRosterIds() 
334 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
335 	}
336 	
337 	/* Method Name: searchForLprRosters */
338 	@Test
339 	public void test_searchForLprRosters() 
340 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
341 	}
342 	
343 	/* Method Name: validateLprRoster */
344 	@Test
345 	public void test_validateLprRoster() 
346 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
347 	}
348 	
349 	/* Method Name: getLprRosterEntriesByLprRoster */
350 	@Test
351 	public void test_getLprRosterEntriesByLprRoster() 
352 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
353 	}
354 	
355 	/* Method Name: getLprRosterEntriesByLpr */
356 	@Test
357 	public void test_getLprRosterEntriesByLpr() 
358 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
359         loadData();
360         List<LprRosterEntryInfo> entries = testService.getLprRosterEntriesByLprRoster("A", contextInfo);
361         assertEquals(9, entries.size());
362         for(int i = 0; i < entries.size(); i++) {
363             LprRosterEntryInfo entry = entries.get(i);
364             assertEquals(9 - i, Integer.parseInt(entry.getId()));
365         }
366 
367         entries = testService.getLprRosterEntriesByLprRoster("D", contextInfo);
368         assertEquals(0, entries.size());
369 	}
370 	
371 	/* Method Name: getLprRosterEntriesByLprRosterAndLpr */
372 	@Test
373 	public void test_getLprRosterEntriesByLprRosterAndLpr() 
374 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
375         loadData();
376         List<LprRosterEntryInfo> entries = testService.getLprRosterEntriesByLprRosterAndLpr("C", "19", contextInfo);
377         assertEquals(2, entries.size());
378         assertContainsId("19", entries);
379         assertContainsId("20", entries);
380 
381         entries = testService.getLprRosterEntriesByLprRosterAndLpr("C", "1", contextInfo);
382         assertEquals(0, entries.size());
383 	}
384 	
385 	/* Method Name: searchForLprRosterEntryIds */
386 	@Test
387 	public void test_searchForLprRosterEntryIds() 
388 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
389 	}
390 	
391 	/* Method Name: searchForLprRosterEntries */
392 	@Test
393 	public void test_searchForLprRosterEntries() 
394 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
395 	}
396 	
397 	/* Method Name: validateLprRosterEntry */
398 	@Test
399 	public void test_validateLprRosterEntry() 
400 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
401 	}
402 	
403 	/* Method Name: moveLprRosterEntryToPosition */
404 	@Test
405 	public void test_moveLprRosterEntryToPosition() 
406 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
407         loadData();
408         testService.moveLprRosterEntryToPosition("9", 9, contextInfo);
409         List<LprRosterEntryInfo> entries = testService.getLprRosterEntriesByLprRoster("A", contextInfo);
410         assertEquals(9, entries.size());
411         for(int i = 0; i < entries.size() - 1; i++) {
412             LprRosterEntryInfo entry = entries.get(i);
413             assertEquals(8 - i, Integer.parseInt(entry.getId()));
414         }
415         assertEquals("9", entries.get(8).getId());
416 
417         testService.moveLprRosterEntryToPosition("1", 1, contextInfo);
418         testService.moveLprRosterEntryToPosition("7", 7, contextInfo);
419         testService.moveLprRosterEntryToPosition("5", 5, contextInfo);
420         entries = testService.getLprRosterEntriesByLprRoster("A", contextInfo);
421         assertEquals(9, entries.size());
422         assertEquals("1", entries.get(0).getId());
423         assertEquals("8", entries.get(1).getId());
424         assertEquals("6", entries.get(2).getId());
425         assertEquals("4", entries.get(3).getId());
426         assertEquals("5", entries.get(4).getId());
427         assertEquals("3", entries.get(5).getId());
428         assertEquals("7", entries.get(6).getId());
429         assertEquals("2", entries.get(7).getId());
430         assertEquals("9", entries.get(8).getId());
431 	}
432 	
433 	/* Method Name: reorderLprRosterEntries */
434 	@Test
435 	public void test_reorderLprRosterEntries() 
436 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
437         loadData();
438         List<String> newOrder = new ArrayList<String>();
439         newOrder.add("3");
440         newOrder.add("2");
441         newOrder.add("1");
442         newOrder.add("7");
443         testService.reorderLprRosterEntries("A", newOrder, contextInfo);
444         List<LprRosterEntryInfo> entries = testService.getLprRosterEntriesByLprRoster("A", contextInfo);
445         assertEquals(9, entries.size());
446         assertEquals("3", entries.get(0).getId());
447         assertEquals("2", entries.get(1).getId());
448         assertEquals("1", entries.get(2).getId());
449         assertEquals("7", entries.get(3).getId());
450         assertEquals("9", entries.get(4).getId());
451         assertEquals("8", entries.get(5).getId());
452         assertEquals("6", entries.get(6).getId());
453         assertEquals("5", entries.get(7).getId());
454         assertEquals("4", entries.get(8).getId());
455 
456         newOrder = new ArrayList<String>();
457         newOrder.add("4");
458         testService.reorderLprRosterEntries("A", newOrder, contextInfo);
459         entries = testService.getLprRosterEntriesByLprRoster("A", contextInfo);
460         assertEquals(9, entries.size());
461 
462         assertEquals("4", entries.get(0).getId());
463         assertEquals("3", entries.get(1).getId());
464         assertEquals("2", entries.get(2).getId());
465         assertEquals("1", entries.get(3).getId());
466         assertEquals("7", entries.get(4).getId());
467         assertEquals("9", entries.get(5).getId());
468         assertEquals("8", entries.get(6).getId());
469         assertEquals("6", entries.get(7).getId());
470         assertEquals("5", entries.get(8).getId());
471 	}
472 
473     private void assertContainsId(String id, List<? extends HasId> list) {
474         for(HasId info: list) {
475             if(info.getId() != null && info.getId().equals(id)) {
476                 return;
477             }
478         }
479         fail("list does not contain id " + id);
480     }
481 
482     private void loadData() throws OperationFailedException{
483         try {
484             dataLoader.beforeTest();
485         } catch (Exception e) {
486             throw new OperationFailedException("failed to load data", e);
487         }
488     }
489 	
490 }
491 
492