1 package org.kuali.student.enrollment.class1.hold.service.impl;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6 import javax.annotation.Resource;
7 import static org.junit.Assert.*;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.kuali.student.enrollment.test.util.AttributeTester;
12 import org.kuali.student.enrollment.test.util.IdEntityTester;
13 import org.kuali.student.enrollment.test.util.ListOfStringTester;
14 import org.kuali.student.enrollment.test.util.MetaTester;
15 import org.kuali.student.enrollment.test.util.TimeTester;
16 import org.kuali.student.r2.common.dto.ContextInfo;
17 import org.kuali.student.r2.common.dto.StatusInfo;
18 import org.kuali.student.r2.common.exceptions.*;
19 import org.kuali.student.r2.common.util.RichTextHelper;
20 import org.kuali.student.r2.core.constants.HoldServiceConstants;
21 import org.kuali.student.r2.core.hold.dto.AppliedHoldInfo;
22 import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
23 import org.kuali.student.r2.core.hold.service.HoldService;
24 import org.springframework.test.context.ContextConfiguration;
25 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26
27 @RunWith(SpringJUnit4ClassRunner.class)
28 @ContextConfiguration(locations = {"classpath:hold-mock-service-test-context.xml"})
29
30
31 public class TestHoldServiceMockImpl {
32
33 public HoldService getHoldService() {
34 return holdService;
35 }
36
37 public void setHoldService(HoldService holdService) {
38 this.holdService = holdService;
39 }
40 @Resource
41 private HoldService holdService;
42 public static String principalId = "123";
43 public ContextInfo callContext = null;
44
45 @Before
46 public void setUp() {
47 principalId = "123";
48 callContext = new ContextInfo();
49 callContext.setPrincipalId(principalId);
50 }
51
52 @Test
53 public void testCrudIssue()
54 throws DataValidationErrorException,
55 DoesNotExistException,
56 InvalidParameterException,
57 MissingParameterException,
58 OperationFailedException,
59 PermissionDeniedException,
60 ReadOnlyException,
61 VersionMismatchException,
62 DependentObjectsExistException {
63
64 HoldIssueInfo expected = new HoldIssueInfo();
65 expected.setName("name of issue");
66 expected.setDescr(new RichTextHelper().fromPlain("description of issue"));
67 expected.setOrganizationId("org1");
68 expected.setTypeKey(HoldServiceConstants.ACADEMIC_PROGRESS_ISSUE_TYPE_KEY);
69 expected.setStateKey(HoldServiceConstants.ISSUE_INACTIVE_STATE_KEY);
70 new AttributeTester().add2ForCreate(expected.getAttributes());
71 HoldIssueInfo actual = holdService.createHoldIssue(expected.getTypeKey(), expected, callContext);
72 assertNotNull(actual.getId());
73 new IdEntityTester().check(expected, actual);
74 assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
75 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
76 new MetaTester().checkAfterCreate(actual.getMeta());
77
78
79 expected = actual;
80
81
82
83
84
85 actual = holdService.getHoldIssue(actual.getId(), callContext);
86 assertEquals(expected.getId(), actual.getId());
87 new IdEntityTester().check(expected, actual);
88 assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
89 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
90 new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
91
92
93 expected = actual;
94 expected.setName(expected.getName() + " updated");
95 expected.setDescr(new RichTextHelper().fromPlain(expected.getDescr().getPlain() + " updated"));
96 expected.setStateKey(HoldServiceConstants.ISSUE_ACTIVE_STATE_KEY);
97 expected.setOrganizationId("org2");
98 new AttributeTester().delete1Update1Add1ForUpdate(expected.getAttributes());
99 actual = holdService.updateHoldIssue(expected.getId(), expected, callContext);
100 assertEquals(expected.getId(), actual.getId());
101 new IdEntityTester().check(expected, actual);
102 assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
103 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
104 new MetaTester().checkAfterUpdate(expected.getMeta(), actual.getMeta());
105
106
107 expected = actual;
108 actual = holdService.getHoldIssue(actual.getId(), callContext);
109 assertEquals(expected.getId(), actual.getId());
110 new IdEntityTester().check(expected, actual);
111 assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
112 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
113 new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
114
115 HoldIssueInfo acadIssue = actual;
116
117
118 HoldIssueInfo finAidIssue = new HoldIssueInfo();
119 finAidIssue.setName("fin aid issue");
120 finAidIssue.setDescr(new RichTextHelper().fromPlain("financial aid issue"));
121 finAidIssue.setOrganizationId("org2");
122 finAidIssue.setTypeKey(HoldServiceConstants.FINANCIAL_AID_ISSUE_TYPE_KEY);
123 finAidIssue.setStateKey(HoldServiceConstants.ISSUE_INACTIVE_STATE_KEY);
124 finAidIssue = holdService.createHoldIssue(finAidIssue.getTypeKey(), finAidIssue, callContext);
125
126
127 List<String> ids = new ArrayList<String>();
128 ids.add(acadIssue.getId());
129 ids.add(finAidIssue.getId());
130 List<HoldIssueInfo> issues = holdService.getHoldIssuesByIds(ids, callContext);
131 assertEquals(ids.size(), issues.size());
132 for (HoldIssueInfo issue : issues) {
133 if (!ids.remove(issue.getId())) {
134 fail(issue.getId());
135 }
136 }
137 assertEquals(0, ids.size());
138
139
140 ids = holdService.getHoldIssueIdsByType(HoldServiceConstants.FINANCIAL_AID_ISSUE_TYPE_KEY, callContext);
141 assertEquals(1, ids.size());
142 assertEquals(finAidIssue.getId(), ids.get(0));
143
144
145 ids = holdService.getHoldIssueIdsByType(HoldServiceConstants.ACADEMIC_PROGRESS_ISSUE_TYPE_KEY, callContext);
146 assertEquals(1, ids.size());
147 assertEquals(acadIssue.getId(), ids.get(0));
148
149
150 issues = holdService.getHoldIssuesByOrg("org1", callContext);
151 assertEquals(0, issues.size());
152
153
154 issues = holdService.getHoldIssuesByOrg("org2", callContext);
155 assertEquals(2, issues.size());
156 assertEquals(acadIssue.getId(), issues.get(0).getId());
157 ids = new ArrayList<String>();
158 ids.add(acadIssue.getId());
159 ids.add(finAidIssue.getId());
160 for (HoldIssueInfo issue : issues) {
161 if (!ids.remove(issue.getId())) {
162 fail(issue.getId());
163 }
164 }
165 assertEquals(0, ids.size());
166
167
168
169 AppliedHoldInfo holdInfo = this.testCrudHold(acadIssue, finAidIssue);
170
171
172 try {
173 StatusInfo status = holdService.deleteHoldIssue(actual.getId(), callContext);
174 fail("Did not receive DependentObjectsExistException when attempting to delete an issue with a hold that exists");
175 } catch (DependentObjectsExistException dnee) {
176
177 }
178
179
180 StatusInfo status = holdService.deleteAppliedHold(holdInfo.getId(), callContext);
181 assertNotNull(status);
182 assertTrue(status.getIsSuccess());
183 try {
184 holdInfo = holdService.getAppliedHold(holdInfo.getId(), callContext);
185 fail("Did not receive DoesNotExistException when attempting to get already-deleted AppliedHoldEntity");
186 } catch (DoesNotExistException dnee) {
187
188 }
189
190
191 status = holdService.deleteHoldIssue(finAidIssue.getId(), callContext);
192 assertNotNull(status);
193 assertTrue(status.getIsSuccess());
194 try {
195 actual = holdService.getHoldIssue(finAidIssue.getId(), callContext);
196 fail("Did not receive DoesNotExistException when attempting to get already-deleted IssueEntity");
197 } catch (DoesNotExistException dnee) {
198
199 }
200
201 }
202
203 private AppliedHoldInfo testCrudHold(HoldIssueInfo acadIssue,
204 HoldIssueInfo finAidIssue)
205 throws DataValidationErrorException,
206 DoesNotExistException,
207 InvalidParameterException,
208 MissingParameterException,
209 OperationFailedException,
210 PermissionDeniedException,
211 ReadOnlyException,
212 VersionMismatchException,
213 DependentObjectsExistException {
214
215 AppliedHoldInfo expected = new AppliedHoldInfo();
216 expected.setPersonId("student1");
217 expected.setHoldIssueId(acadIssue.getId());
218 expected.setName("name of hold");
219 expected.setDescr(new RichTextHelper().fromPlain("description of hold"));
220 expected.setEffectiveDate(new Date());
221 expected.setReleasedDate(new Date());
222 expected.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
223 expected.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
224 new AttributeTester().add2ForCreate(expected.getAttributes());
225 AppliedHoldInfo actual = holdService.createAppliedHold(expected.getPersonId(), expected.getHoldIssueId(), expected.getTypeKey(), expected,
226 callContext);
227 assertNotNull(actual.getId());
228 new IdEntityTester().check(expected, actual);
229 assertEquals(expected.getPersonId(), actual.getPersonId());
230 assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
231 new TimeTester().check(expected.getEffectiveDate(), actual.getEffectiveDate());
232 new TimeTester().check(expected.getReleasedDate(), actual.getReleasedDate());
233 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
234 new MetaTester().checkAfterCreate(actual.getMeta());
235
236
237 expected = actual;
238 actual = holdService.getAppliedHold(actual.getId(), callContext);
239 assertEquals(expected.getId(), actual.getId());
240 new IdEntityTester().check(expected, actual);
241 assertEquals(expected.getPersonId(), actual.getPersonId());
242 assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
243 new TimeTester().check(expected.getEffectiveDate(), actual.getEffectiveDate());
244 new TimeTester().check(expected.getReleasedDate(), actual.getReleasedDate());
245 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
246 new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
247
248
249 expected = actual;
250 expected.setName(expected.getName() + " updated");
251 expected.setDescr(new RichTextHelper().fromPlain(expected.getDescr().getPlain() + " updated"));
252 expected.setEffectiveDate(new Date(expected.getEffectiveDate().getTime() - 1000));
253 expected.setReleasedDate(new Date());
254 expected.setStateKey(HoldServiceConstants.HOLD_RELEASED_STATE_KEY);
255 new AttributeTester().delete1Update1Add1ForUpdate(expected.getAttributes());
256 actual = holdService.updateAppliedHold(expected.getId(), expected, callContext);
257 assertEquals(expected.getId(), actual.getId());
258 new IdEntityTester().check(expected, actual);
259 assertEquals(expected.getPersonId(), actual.getPersonId());
260 assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
261 new TimeTester().check(expected.getEffectiveDate(), actual.getEffectiveDate());
262 new TimeTester().check(expected.getReleasedDate(), actual.getReleasedDate());
263 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
264 new MetaTester().checkAfterUpdate(expected.getMeta(), actual.getMeta());
265
266
267 expected = actual;
268 actual = holdService.getAppliedHold(actual.getId(), callContext);
269 assertEquals(expected.getId(), actual.getId());
270 new IdEntityTester().check(expected, actual);
271 assertEquals(expected.getPersonId(), actual.getPersonId());
272 assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
273 new TimeTester().check(expected.getEffectiveDate(), actual.getEffectiveDate());
274 new TimeTester().check(expected.getReleasedDate(), actual.getReleasedDate());
275 new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
276 new MetaTester().checkAfterCreate(actual.getMeta());
277
278 AppliedHoldInfo acadHoldReleasedStudent1 = actual;
279
280 AppliedHoldInfo finAidHoldStudent1 = new AppliedHoldInfo();
281 finAidHoldStudent1.setPersonId("student1");
282 finAidHoldStudent1.setHoldIssueId(finAidIssue.getId());
283 finAidHoldStudent1.setName("name of hold");
284 finAidHoldStudent1.setDescr(new RichTextHelper().fromPlain("description of hold"));
285 finAidHoldStudent1.setEffectiveDate(new Date());
286 finAidHoldStudent1.setReleasedDate(null);
287 finAidHoldStudent1.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
288 finAidHoldStudent1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
289 finAidHoldStudent1 = holdService.createAppliedHold(finAidHoldStudent1.getPersonId(),
290 finAidHoldStudent1.getHoldIssueId(),
291 finAidHoldStudent1.getTypeKey(), finAidHoldStudent1,
292 callContext);
293
294 AppliedHoldInfo acadHoldActiveStudent1 = new AppliedHoldInfo();
295 acadHoldActiveStudent1.setPersonId("student1");
296 acadHoldActiveStudent1.setHoldIssueId(acadIssue.getId());
297 acadHoldActiveStudent1.setName("name of hold");
298 acadHoldActiveStudent1.setDescr(new RichTextHelper().fromPlain("description of hold"));
299 acadHoldActiveStudent1.setEffectiveDate(new Date());
300 acadHoldActiveStudent1.setReleasedDate(null);
301 acadHoldActiveStudent1.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
302 acadHoldActiveStudent1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
303 acadHoldActiveStudent1 = holdService.createAppliedHold(acadHoldActiveStudent1.getPersonId(),
304 acadHoldActiveStudent1.getHoldIssueId(),
305 acadHoldActiveStudent1.getTypeKey(), acadHoldActiveStudent1,
306 callContext);
307
308
309 AppliedHoldInfo acadHoldActiveInstructor1 = new AppliedHoldInfo();
310 acadHoldActiveInstructor1.setPersonId("instructor1");
311 acadHoldActiveInstructor1.setHoldIssueId(acadIssue.getId());
312 acadHoldActiveInstructor1.setName("name of hold");
313 acadHoldActiveInstructor1.setDescr(new RichTextHelper().fromPlain("description of hold"));
314 acadHoldActiveInstructor1.setEffectiveDate(new Date());
315 acadHoldActiveInstructor1.setReleasedDate(null);
316 acadHoldActiveInstructor1.setTypeKey(HoldServiceConstants.INTRUCTOR_HOLD_TYPE_KEY);
317 acadHoldActiveInstructor1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
318 acadHoldActiveInstructor1 = holdService.createAppliedHold(acadHoldActiveInstructor1.getPersonId(),
319 acadHoldActiveInstructor1.getHoldIssueId(), acadHoldActiveInstructor1.getTypeKey(), acadHoldActiveInstructor1,
320 callContext);
321
322
323 List<String> expIds = new ArrayList<String>();
324 expIds.add(acadHoldReleasedStudent1.getId());
325 expIds.add(finAidHoldStudent1.getId());
326 expIds.add(acadHoldActiveStudent1.getId());
327 expIds.add(acadHoldActiveInstructor1.getId());
328 List<AppliedHoldInfo> holds = holdService.getAppliedHoldsByIds(expIds, callContext);
329 assertEquals(expIds.size(), holds.size());
330 for (AppliedHoldInfo issue : holds) {
331 if (!expIds.remove(issue.getId())) {
332 fail(issue.getId());
333 }
334 }
335 assertEquals(0, expIds.size());
336
337
338 List<String> actIds = holdService.getAppliedHoldIdsByType(HoldServiceConstants.INTRUCTOR_HOLD_TYPE_KEY, callContext);
339 assertEquals(1, actIds.size());
340 assertEquals(acadHoldActiveInstructor1.getId(), actIds.get(0));
341
342
343 actIds = holdService.getAppliedHoldIdsByType(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY, callContext);
344 expIds = new ArrayList<String>();
345 expIds.add(acadHoldReleasedStudent1.getId());
346 expIds.add(finAidHoldStudent1.getId());
347 expIds.add(acadHoldActiveStudent1.getId());
348 new ListOfStringTester().check(expIds, actIds);
349
350
351 holds = holdService.getAppliedHoldsByPerson("student1", callContext);
352 expIds = new ArrayList<String>();
353 expIds.add(acadHoldReleasedStudent1.getId());
354 expIds.add(finAidHoldStudent1.getId());
355 expIds.add(acadHoldActiveStudent1.getId());
356 for (AppliedHoldInfo hold : holds) {
357 if ( ! expIds.remove(hold.getId())) {
358 fail(hold.getId());
359 }
360 }
361 assertEquals(0, expIds.size());
362
363
364 holds = holdService.getAppliedHoldsByPerson("instructor1", callContext);
365 assertEquals(1, holds.size());
366 assertEquals(acadHoldActiveInstructor1.getId(), holds.get(0).getId());
367
368
369 holds = holdService.getActiveAppliedHoldsByPerson("student1", callContext);
370 assertEquals(2, holds.size());
371
372 expIds = new ArrayList<String>();
373 expIds.add(finAidHoldStudent1.getId());
374 expIds.add(acadHoldActiveStudent1.getId());
375 for (AppliedHoldInfo hold : holds) {
376 if ( ! expIds.remove(hold.getId())) {
377 fail(hold.getId());
378 }
379 }
380 assertEquals(0, expIds.size());
381
382
383 actIds = holdService.getAppliedHoldIdsByIssue(acadIssue.getId(), callContext);
384 expIds = new ArrayList<String>();
385 expIds.add(acadHoldReleasedStudent1.getId());
386 expIds.add(acadHoldActiveStudent1.getId());
387 expIds.add(acadHoldActiveInstructor1.getId());
388 for (String id : actIds) {
389 if (!expIds.remove(id)) {
390 fail(id);
391 }
392 }
393 assertEquals(0, expIds.size());
394
395
396 holds = holdService.getAppliedHoldsByIssueAndPerson(acadIssue.getId(), "student1", callContext);
397 expIds = new ArrayList<String>();
398 expIds.add(acadHoldReleasedStudent1.getId());
399 expIds.add(acadHoldActiveStudent1.getId());
400 for (AppliedHoldInfo hold : holds) {
401 if (!expIds.remove(hold.getId())) {
402 fail(hold.getId());
403 }
404 }
405 assertEquals(0, expIds.size());
406
407
408 holds = holdService.getActiveAppliedHoldsByIssueAndPerson(acadIssue.getId(), "student1", callContext);
409 expIds = new ArrayList<String>();
410 expIds.add(acadHoldActiveStudent1.getId());
411 for (AppliedHoldInfo hold : holds) {
412 if (!expIds.remove(hold.getId())) {
413 fail(hold.getId());
414 }
415 }
416 assertEquals(0, expIds.size());
417
418
419 StatusInfo status = holdService.deleteAppliedHold(acadHoldActiveStudent1.getId(), callContext);
420 assertNotNull(status);
421 assertTrue(status.getIsSuccess());
422 try {
423 actual = holdService.getAppliedHold(acadHoldActiveStudent1.getId(), callContext);
424 fail("Did not receive DoesNotExistException when attempting to get already-deleted AppliedHoldEntity");
425 } catch (DoesNotExistException dnee) {
426
427 }
428 status = holdService.deleteAppliedHold(finAidHoldStudent1.getId(), callContext);
429 status = holdService.deleteAppliedHold(acadHoldActiveInstructor1.getId(), callContext);
430
431 return acadHoldReleasedStudent1;
432 }
433 }