View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.core.atp;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Calendar;
25  import java.util.Date;
26  import java.util.List;
27  
28  import org.apache.log4j.Logger;
29  import org.junit.Test;
30  import org.kuali.student.common.test.spring.AbstractServiceTest;
31  import org.kuali.student.common.test.spring.Client;
32  import org.kuali.student.common.test.spring.Dao;
33  import org.kuali.student.common.test.spring.Daos;
34  import org.kuali.student.common.test.spring.PersistenceFileLocation;
35  import org.kuali.student.core.atp.dto.AtpInfo;
36  import org.kuali.student.core.atp.dto.AtpTypeInfo;
37  import org.kuali.student.core.atp.dto.DateRangeInfo;
38  import org.kuali.student.core.atp.dto.DateRangeTypeInfo;
39  import org.kuali.student.core.atp.dto.MilestoneInfo;
40  import org.kuali.student.core.atp.dto.MilestoneTypeInfo;
41  import org.kuali.student.core.atp.service.AtpService;
42  import org.kuali.student.core.dto.RichTextInfo;
43  import org.kuali.student.core.exceptions.DoesNotExistException;
44  import org.kuali.student.core.exceptions.InvalidParameterException;
45  import org.kuali.student.core.exceptions.MissingParameterException;
46  import org.kuali.student.core.exceptions.OperationFailedException;
47  import org.kuali.student.core.exceptions.VersionMismatchException;
48  
49  @Daos( { @Dao(value = "org.kuali.student.core.atp.dao.impl.AtpDaoImpl", testDataFile = "classpath:atp-test-beans.xml") })
50  @PersistenceFileLocation("classpath:META-INF/atp-persistence.xml")
51  public class TestAtpService extends AbstractServiceTest {
52  	final Logger LOG = Logger.getLogger(TestAtpService.class);
53  	
54  	@Client(value = "org.kuali.student.core.atp.service.impl.AtpServiceImpl")
55  	public AtpService client;
56  
57  	public static final String atpType_fallSemester = "atp.atpType.fallSemester";
58  	public static final String milestoneType_lastDateToDrop = "atp.milestoneType.lastDateToDrop";
59  	public static final String dateRangeType_finals = "atp.dateRangeType.finals";
60  	public static final String atpAttribute_notes = "atp.attribute.notes";
61  	public static final String dateRangeAttribute_notes = "atp.dateRangeAttribute.notes";
62  	public static final String milestoneAttribute_notes = "atp.milestoneAttribute.notes";
63  	public static final String atp_fall2008Semester = "atp.fall2008Semester";
64  	public static final String milestone_lastDateToDropFall2008 = "atp.milestone.lastDateToDropFall2008";
65  	public static final String dateRange_finalsFall2008 = "atp.dateRange.finalsFall2008";
66  
67  	public static final String atp_2009FallSemester = "atp.2009FallSemester";
68  	
69  	@Test
70  	public void TestFinds() throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, ParseException{
71  
72  		Calendar cal = Calendar.getInstance();
73  		cal.set(2009, 9, 1);
74  
75  		Calendar startCal = Calendar.getInstance();
76  		startCal.set(2008, 1, 1);
77  		
78  		Calendar endCal = Calendar.getInstance();
79  		endCal.set(2010, 1, 1);
80  
81  		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
82  		
83  		List<AtpTypeInfo> atpTypes = client.getAtpTypes();
84  		assertEquals(2,atpTypes.size());
85  		
86  		List<AtpInfo> atpsByAtpType = client.getAtpsByAtpType(atpType_fallSemester);
87  		assertEquals(1,atpsByAtpType.size());
88  		assertEquals(atp_2009FallSemester,atpsByAtpType.get(0).getId());
89  		
90  		List<AtpInfo> atpsByDate = client.getAtpsByDate(cal.getTime());
91  		assertEquals(1,atpsByDate.size());
92  		assertEquals(atp_2009FallSemester,atpsByDate.get(0).getId());
93  		
94  		List<AtpInfo> atpsByDates = client.getAtpsByDates(startCal.getTime(), endCal.getTime());
95  		assertEquals(2,atpsByDates.size());
96  		
97  		List<DateRangeInfo> dateRangesByAtp = client.getDateRangesByAtp(atp_2009FallSemester);
98  		assertEquals(1,dateRangesByAtp.size());
99  		
100 		List<DateRangeInfo> dateRangesByDate = client.getDateRangesByDate(df.parse("20091205"));
101 		assertEquals(1,dateRangesByDate.size());
102 		
103 		List<DateRangeTypeInfo> dateRangeTypesForAtpType = client.getDateRangeTypesForAtpType(atpType_fallSemester);
104 		assertEquals(1,dateRangeTypesForAtpType.size());
105 		
106 		List<MilestoneInfo> milestonesByAtp = client.getMilestonesByAtp(atp_2009FallSemester);
107 		assertEquals(1,milestonesByAtp.size());
108 		
109 		List<MilestoneInfo> milestonesByDates = client.getMilestonesByDates(startCal.getTime(), endCal.getTime());
110 		assertEquals(1,milestonesByDates.size());
111 		
112 		List<MilestoneInfo> milestonesByDatesAndType = client.getMilestonesByDatesAndType(milestoneType_lastDateToDrop, startCal.getTime(), endCal.getTime());
113 		assertEquals(1,milestonesByDatesAndType.size());
114 		
115 		List<MilestoneTypeInfo> milestoneTypesForAtpType = client.getMilestoneTypesForAtpType(atpType_fallSemester);
116 		assertEquals(1,milestoneTypesForAtpType.size());
117 	}
118 	
119 	@Test
120 	public void TestCreateUpdateDelete(){
121 		//Make an ATP
122 		AtpInfo atpInfo = new AtpInfo();
123 		atpInfo.setDesc(new RichTextInfo());
124 		atpInfo.getDesc().setFormatted("Atp for fall 2008 semester");
125 		atpInfo.getDesc().setPlain("Atp for fall 2008 semester");
126 		atpInfo.setName("Fall 2008 Semester");
127 		atpInfo.setStartDate(new Date());
128 		atpInfo.setEndDate(new Date());
129 		
130 		Date stDate = atpInfo.getStartDate();
131 		Date enDate = atpInfo.getEndDate();
132 		
133 		atpInfo.setState("new");
134 		
135 		atpInfo.getAttributes().put(atpAttribute_notes, "Notes for the Fall 2008 Semester");
136 		
137 		AtpInfo createdAtp=null;
138 		try {
139 			createdAtp = client.createAtp(atpType_fallSemester, atp_fall2008Semester, atpInfo);
140 		} catch (Exception e) {
141 			LOG.error(e);
142 			fail();
143 		}
144 		
145 		assertTrue(stDate.equals(createdAtp.getStartDate()));
146 		assertTrue(enDate.equals(createdAtp.getEndDate()));
147 		
148 		//Make a DateRange
149 		DateRangeInfo dateRangeInfo=new DateRangeInfo();
150 		dateRangeInfo.setDesc(new RichTextInfo());
151 		dateRangeInfo.getDesc().setFormatted("Date Range for fall 2008 semester finals");
152 		dateRangeInfo.setName("Finals Fall 2008 Semester");
153 		dateRangeInfo.setStartDate(new Date());
154 		dateRangeInfo.setEndDate(new Date());
155 		dateRangeInfo.setState("new");
156 		dateRangeInfo.setAtpId(atp_fall2008Semester);
157 		dateRangeInfo.setType(dateRangeType_finals);
158 
159 		dateRangeInfo.getAttributes().put(dateRangeAttribute_notes, "Notes for the Finals date range Fall 2008 Semester");
160 		
161 		DateRangeInfo createdDateRange=null;
162 		try {
163 			createdDateRange = client.addDateRange(atp_fall2008Semester, dateRange_finalsFall2008, dateRangeInfo);
164 		} catch (Exception e) {
165 			LOG.error(e);
166 			fail();
167 		} 
168 		
169 		//Make a Milestone
170 		MilestoneInfo milestoneInfo=new MilestoneInfo();
171 		milestoneInfo.setDesc(new RichTextInfo());
172 		milestoneInfo.getDesc().setFormatted("Milestone for fall 2008 semester last day to drop");
173 		milestoneInfo.setName("Last Day to Drop Fall 2008 Semester");
174 		milestoneInfo.setMilestoneDate(new Date());
175 		milestoneInfo.setState("new");
176 		milestoneInfo.setAtpId(atp_fall2008Semester);
177 		milestoneInfo.setType(milestoneType_lastDateToDrop);
178 
179 		milestoneInfo.getAttributes().put(milestoneAttribute_notes, "Notes for the Last Day to Drop Fall 2008 Semester");
180 		
181 		MilestoneInfo createdMilestone=null;
182 		try {
183 			createdMilestone = client.addMilestone(atp_fall2008Semester, milestone_lastDateToDropFall2008, milestoneInfo);
184 		} catch (Exception e) {
185 			LOG.error(e);
186 			fail();
187 		} 
188 		
189 		//Do some updates
190 		
191 		//Update Atp
192 		createdAtp.getDesc().setFormatted("Updated Atp for the Fall 2008 Semester");
193 		try {
194 			assertEquals("0",createdAtp.getMetaInfo().getVersionInd());
195 			AtpInfo updatedAtp = client.updateAtp(atp_fall2008Semester, createdAtp);
196 			assertEquals("1",updatedAtp.getMetaInfo().getVersionInd());
197 			assertEquals("Updated Atp for the Fall 2008 Semester", updatedAtp.getDesc().getFormatted());
198 		} catch (Exception e) {
199 			LOG.error(e);
200 			fail();
201 		}
202 		
203 		//now try to update again with the same version
204 		try {
205 			client.updateAtp(atp_fall2008Semester, createdAtp);
206 			fail("AtpService.updateAtp() should have thrown VersionMismatchException");
207 		} catch (VersionMismatchException vme) {
208 			// what we expect
209 		} catch (Exception e) {
210 			LOG.error(e);
211 			fail();
212 		}
213 		
214 		//Update Date Range
215 		createdDateRange.getDesc().setFormatted("Updated DateRange for the Finals date range Fall 2008 Semester");
216 		try {
217 			assertEquals("0",createdDateRange.getMetaInfo().getVersionInd());
218 			DateRangeInfo updatedDateRange = client.updateDateRange(dateRange_finalsFall2008, createdDateRange);
219 			assertEquals("1",updatedDateRange.getMetaInfo().getVersionInd());
220 			assertEquals("Updated DateRange for the Finals date range Fall 2008 Semester", updatedDateRange.getDesc().getFormatted());
221 		} catch (Exception e) {
222 			LOG.error(e);
223 			fail();
224 		}
225 		
226 		//Updating with the old version again should fail
227 		try {
228 			client.updateDateRange(dateRange_finalsFall2008, createdDateRange);
229 			fail("AtpService.updateDateRange() should have thrown VersionMismatchException");
230 		} catch (VersionMismatchException vme) {
231 			// what we expect
232 		} catch (Exception e) {
233 			LOG.error(e);
234 			fail();
235 		}
236 		
237 		//Update Milestone
238 		createdMilestone.getDesc().setFormatted("Updated Milestone for fall 2008 semester last day to drop");
239 		try {
240 			assertEquals("0",createdMilestone.getMetaInfo().getVersionInd());
241 			MilestoneInfo updatedMilestone = client.updateMilestone(milestone_lastDateToDropFall2008, createdMilestone);
242 			assertEquals("1",updatedMilestone.getMetaInfo().getVersionInd());
243 			assertEquals("Updated Milestone for fall 2008 semester last day to drop", updatedMilestone.getDesc().getFormatted());
244 		} catch (Exception e) {
245 			LOG.error(e);
246 		}
247 		
248 		//Try to update again should fail
249 		try {
250 			client.updateMilestone(milestone_lastDateToDropFall2008, createdMilestone);
251 			fail("AtpService.updateDateRange() should have thrown VersionMismatchException");
252 		} catch (VersionMismatchException vme) {
253 			// what we expect
254 		} catch (Exception e) {
255 			LOG.error(e);
256 			fail();
257 		}
258 		
259 		//Do some deletes that should cascade
260 		try {
261 			client.deleteAtp(atp_fall2008Semester);
262 		} catch (Exception e) {
263 			LOG.error(e);
264 			fail();
265 		}
266 	}
267 }