001package org.kuali.student.enrollment.class1.hold.service.impl;
002
003import org.junit.Before;
004import org.junit.Test;
005import org.junit.runner.RunWith;
006import org.kuali.student.common.test.util.AttributeTester;
007import org.kuali.student.common.test.util.IdEntityTester;
008import org.kuali.student.common.test.util.ListOfStringTester;
009import org.kuali.student.common.test.util.MetaTester;
010import org.kuali.student.common.test.util.RelationshipTester;
011import org.kuali.student.common.test.util.RichTextTester;
012import org.kuali.student.r2.common.dto.ContextInfo;
013import org.kuali.student.r2.common.dto.StatusInfo;
014import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
015import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
016import org.kuali.student.r2.common.exceptions.DoesNotExistException;
017import org.kuali.student.r2.common.exceptions.InvalidParameterException;
018import org.kuali.student.r2.common.exceptions.MissingParameterException;
019import org.kuali.student.r2.common.exceptions.OperationFailedException;
020import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
021import org.kuali.student.r2.common.exceptions.ReadOnlyException;
022import org.kuali.student.r2.common.exceptions.VersionMismatchException;
023import org.kuali.student.r2.common.util.RichTextHelper;
024import org.kuali.student.r2.core.constants.HoldServiceConstants;
025import org.kuali.student.r2.core.hold.dto.AppliedHoldInfo;
026import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
027import org.kuali.student.r2.core.hold.service.HoldService;
028import org.springframework.test.context.ContextConfiguration;
029import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
030
031import javax.annotation.Resource;
032import java.util.ArrayList;
033import java.util.Date;
034import java.util.List;
035
036import static org.junit.Assert.assertEquals;
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertTrue;
039import static org.junit.Assert.fail;
040
041@RunWith(SpringJUnit4ClassRunner.class)
042@ContextConfiguration(locations = {"classpath:hold-mock-service-test-context.xml"})
043//@TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
044//@Transactional
045public class TestHoldServiceMockImpl {
046
047    public HoldService getHoldService() {
048        return holdService;
049    }
050
051    public void setHoldService(HoldService holdService) {
052        this.holdService = holdService;
053    }
054    @Resource
055    private HoldService holdService;
056    public static String principalId = "123";
057    public ContextInfo callContext = null;
058
059    @Before
060    public void setUp() {
061        principalId = "123";
062        callContext = new ContextInfo();
063        callContext.setPrincipalId(principalId);
064    }
065
066    @Test
067    public void testCrudIssue()
068            throws DataValidationErrorException,
069            DoesNotExistException,
070            InvalidParameterException,
071            MissingParameterException,
072            OperationFailedException,
073            PermissionDeniedException,
074            ReadOnlyException,
075            VersionMismatchException,
076            DependentObjectsExistException {
077        // test create
078        HoldIssueInfo expected = new HoldIssueInfo();
079        expected.setName("name of issue");
080        expected.setDescr(new RichTextHelper().fromPlain("description of issue"));
081        expected.setOrganizationId("org1");
082        expected.setTypeKey(HoldServiceConstants.ACADEMIC_PROGRESS_ISSUE_TYPE_KEY);
083        expected.setStateKey(HoldServiceConstants.ISSUE_INACTIVE_STATE_KEY);
084        new AttributeTester().add2ForCreate(expected.getAttributes());
085        HoldIssueInfo actual = holdService.createHoldIssue(expected.getTypeKey(), expected, callContext);
086        assertNotNull(actual.getId());
087        new IdEntityTester().check(expected, actual);
088        assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
089        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
090        new MetaTester().checkAfterCreate(actual.getMeta());
091
092        // test read
093        expected = actual;
094//        for (AttributeInfo itemInfo : expected.getAttributes()) {
095//            // clear out any id's set during the persistence
096//            // to let the checks work properly
097//            itemInfo.setId(null);
098//        }
099        actual = holdService.getHoldIssue(actual.getId(), 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().checkAfterGet(expected.getMeta(), actual.getMeta());
105
106        // test update
107        expected = actual;
108        expected.setName(expected.getName() + " updated");
109        expected.setDescr(new RichTextHelper().fromPlain(expected.getDescr().getPlain() + " updated"));
110        expected.setStateKey(HoldServiceConstants.ISSUE_ACTIVE_STATE_KEY);
111        expected.setOrganizationId("org2");
112        new AttributeTester().delete1Update1Add1ForUpdate(expected.getAttributes());
113        actual = holdService.updateHoldIssue(expected.getId(), expected, callContext);
114        assertEquals(expected.getId(), actual.getId());
115        new IdEntityTester().check(expected, actual);
116        assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
117        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
118        new MetaTester().checkAfterUpdate(expected.getMeta(), actual.getMeta());
119
120        // test read after update
121        expected = actual;
122        actual = holdService.getHoldIssue(actual.getId(), callContext);
123        assertEquals(expected.getId(), actual.getId());
124        new IdEntityTester().check(expected, actual);
125        assertEquals(expected.getOrganizationId(), actual.getOrganizationId());
126        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
127        new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
128
129        HoldIssueInfo acadIssue = actual;
130
131        // create a 2nd issue
132        HoldIssueInfo finAidIssue = new HoldIssueInfo();
133        finAidIssue.setName("fin aid issue");
134        finAidIssue.setDescr(new RichTextHelper().fromPlain("financial aid issue"));
135        finAidIssue.setOrganizationId("org2");
136        finAidIssue.setTypeKey(HoldServiceConstants.FINANCIAL_AID_ISSUE_TYPE_KEY);
137        finAidIssue.setStateKey(HoldServiceConstants.ISSUE_INACTIVE_STATE_KEY);
138        finAidIssue = holdService.createHoldIssue(finAidIssue.getTypeKey(), finAidIssue, callContext);
139
140        // test bulk get with no ids supplied
141        // test for jira KSENROLL-2949
142        List<String> ids = new ArrayList<String>();
143        List<HoldIssueInfo> issues = holdService.getHoldIssuesByIds(ids, callContext);
144        assertEquals(ids.size(), issues.size());
145        assertEquals(0, ids.size());
146
147         // test bulk get
148        ids = new ArrayList<String>();
149        ids.add(acadIssue.getId());
150        ids.add(finAidIssue.getId());
151        issues = holdService.getHoldIssuesByIds(ids, callContext);
152        assertEquals(ids.size(), issues.size());
153        for (HoldIssueInfo issue : issues) {
154            if (!ids.remove(issue.getId())) {
155                fail(issue.getId());
156            }
157        }
158        assertEquals(0, ids.size());
159        
160        
161        // test get by type
162        ids = holdService.getHoldIssueIdsByType(HoldServiceConstants.FINANCIAL_AID_ISSUE_TYPE_KEY, callContext);
163        assertEquals(1, ids.size());
164        assertEquals(finAidIssue.getId(), ids.get(0));
165
166        // test get by other type
167        ids = holdService.getHoldIssueIdsByType(HoldServiceConstants.ACADEMIC_PROGRESS_ISSUE_TYPE_KEY, callContext);
168        assertEquals(1, ids.size());
169        assertEquals(acadIssue.getId(), ids.get(0));
170
171        // test get by org1
172        issues = holdService.getHoldIssuesByOrg("org1", callContext);
173        assertEquals(0, issues.size());
174
175        // test get by org2
176        issues = holdService.getHoldIssuesByOrg("org2", callContext);
177        assertEquals(2, issues.size());
178        assertEquals(acadIssue.getId(), issues.get(0).getId());
179        ids = new ArrayList<String>();
180        ids.add(acadIssue.getId());
181        ids.add(finAidIssue.getId());
182        for (HoldIssueInfo issue : issues) {
183            if (!ids.remove(issue.getId())) {
184                fail(issue.getId());
185            }
186        }// here
187        assertEquals(0, ids.size());
188
189
190        //TODO: check that service throws dependent object exception propertly
191        AppliedHoldInfo holdInfo = this.testCrudHold(acadIssue, finAidIssue);
192
193        // test for circular dependency when delete issue with a hold
194        try {
195            StatusInfo status = holdService.deleteHoldIssue(actual.getId(), callContext);
196            fail("Did not receive DependentObjectsExistException when attempting to delete an issue with a hold that exists");
197        } catch (DependentObjectsExistException dnee) {
198            // expected
199        }
200
201        // now test delete of hold
202        StatusInfo status = holdService.deleteAppliedHold(holdInfo.getId(), callContext);
203        assertNotNull(status);
204        assertTrue(status.getIsSuccess());
205        try {
206            holdInfo = holdService.getAppliedHold(holdInfo.getId(), callContext);
207            fail("Did not receive DoesNotExistException when attempting to get already-deleted AppliedHoldEntity");
208        } catch (DoesNotExistException dnee) {
209            // expected
210        }
211
212        // test delete issue
213        status = holdService.deleteHoldIssue(finAidIssue.getId(), callContext);
214        assertNotNull(status);
215        assertTrue(status.getIsSuccess());
216        try {
217            actual = holdService.getHoldIssue(finAidIssue.getId(), callContext);
218            fail("Did not receive DoesNotExistException when attempting to get already-deleted IssueEntity");
219        } catch (DoesNotExistException dnee) {
220            // expected
221        }
222
223    }
224
225    private AppliedHoldInfo testCrudHold(HoldIssueInfo acadIssue,
226            HoldIssueInfo finAidIssue)
227            throws DataValidationErrorException,
228            DoesNotExistException,
229            InvalidParameterException,
230            MissingParameterException,
231            OperationFailedException,
232            PermissionDeniedException,
233            ReadOnlyException,
234            VersionMismatchException,
235            DependentObjectsExistException {
236        // test create
237        AppliedHoldInfo expected = new AppliedHoldInfo();
238        expected.setPersonId("student1");
239        expected.setHoldIssueId(acadIssue.getId());
240        expected.setName("name of hold");
241        expected.setDescr(new RichTextHelper().fromPlain("description of hold"));
242        expected.setEffectiveDate(new Date());
243        expected.setExpirationDate(new Date());
244        expected.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
245        expected.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
246        new AttributeTester().add2ForCreate(expected.getAttributes());
247        AppliedHoldInfo actual = holdService.createAppliedHold(expected.getPersonId(), expected.getHoldIssueId(), expected.getTypeKey(), expected,
248                callContext);
249        assertNotNull(actual.getId());
250        new RelationshipTester().check(expected, actual);
251        assertEquals(expected.getName(), actual.getName());
252        new RichTextTester().check(expected.getDescr(), actual.getDescr());
253        assertEquals(expected.getPersonId(), actual.getPersonId());
254        assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
255        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
256        new MetaTester().checkAfterCreate(actual.getMeta());
257
258        // test read
259        expected = actual;
260        actual = holdService.getAppliedHold(actual.getId(), callContext);
261        assertEquals(expected.getId(), actual.getId());
262        new RelationshipTester().check(expected, actual);
263        assertEquals(expected.getName(), actual.getName());
264        new RichTextTester().check(expected.getDescr(), actual.getDescr());
265        assertEquals(expected.getPersonId(), actual.getPersonId());
266        assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
267        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
268        new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
269
270        // test update
271        expected = actual;
272        expected.setName(expected.getName() + " updated");
273        expected.setDescr(new RichTextHelper().fromPlain(expected.getDescr().getPlain() + " updated"));
274        expected.setEffectiveDate(new Date(expected.getEffectiveDate().getTime() - 1000));
275        expected.setExpirationDate(new Date());
276        expected.setStateKey(HoldServiceConstants.HOLD_RELEASED_STATE_KEY);
277        new AttributeTester().delete1Update1Add1ForUpdate(expected.getAttributes());
278        actual = holdService.updateAppliedHold(expected.getId(), expected, callContext);
279        assertEquals(expected.getId(), actual.getId());
280        new RelationshipTester().check(expected, actual);
281        assertEquals(expected.getName(), actual.getName());
282        new RichTextTester().check(expected.getDescr(), actual.getDescr());
283        assertEquals(expected.getPersonId(), actual.getPersonId());
284        assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
285        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
286        new MetaTester().checkAfterUpdate(expected.getMeta(), actual.getMeta());
287
288        // test read
289        expected = actual;
290        actual = holdService.getAppliedHold(actual.getId(), callContext);
291        assertEquals(expected.getId(), actual.getId());
292        new RelationshipTester().check(expected, actual);
293        assertEquals(expected.getName(), actual.getName());
294        new RichTextTester().check(expected.getDescr(), actual.getDescr());
295        assertEquals(expected.getPersonId(), actual.getPersonId());
296        assertEquals(expected.getHoldIssueId(), actual.getHoldIssueId());
297        new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
298        new MetaTester().checkAfterCreate(actual.getMeta());
299
300        AppliedHoldInfo acadHoldReleasedStudent1 = actual;
301        // create 2nd
302        AppliedHoldInfo finAidHoldStudent1 = new AppliedHoldInfo();
303        finAidHoldStudent1.setPersonId("student1");
304        finAidHoldStudent1.setHoldIssueId(finAidIssue.getId());
305        finAidHoldStudent1.setName("name of hold");
306        finAidHoldStudent1.setDescr(new RichTextHelper().fromPlain("description of hold"));
307        finAidHoldStudent1.setEffectiveDate(new Date());
308        finAidHoldStudent1.setExpirationDate(null);
309        finAidHoldStudent1.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
310        finAidHoldStudent1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
311        finAidHoldStudent1 = holdService.createAppliedHold(finAidHoldStudent1.getPersonId(), 
312                finAidHoldStudent1.getHoldIssueId(),
313                finAidHoldStudent1.getTypeKey(), finAidHoldStudent1,
314                callContext);
315        // create a 3rd
316        AppliedHoldInfo acadHoldActiveStudent1 = new AppliedHoldInfo();
317        acadHoldActiveStudent1.setPersonId("student1");
318        acadHoldActiveStudent1.setHoldIssueId(acadIssue.getId());
319        acadHoldActiveStudent1.setName("name of hold");
320        acadHoldActiveStudent1.setDescr(new RichTextHelper().fromPlain("description of hold"));
321        acadHoldActiveStudent1.setEffectiveDate(new Date());
322        acadHoldActiveStudent1.setExpirationDate(null);
323        acadHoldActiveStudent1.setTypeKey(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY);
324        acadHoldActiveStudent1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
325        acadHoldActiveStudent1 = holdService.createAppliedHold(acadHoldActiveStudent1.getPersonId(), 
326                acadHoldActiveStudent1.getHoldIssueId(),
327                acadHoldActiveStudent1.getTypeKey(), acadHoldActiveStudent1,
328                callContext);
329
330        // create a 4th
331        AppliedHoldInfo acadHoldActiveInstructor1 = new AppliedHoldInfo();
332        acadHoldActiveInstructor1.setPersonId("instructor1");
333        acadHoldActiveInstructor1.setHoldIssueId(acadIssue.getId());
334        acadHoldActiveInstructor1.setName("name of hold");
335        acadHoldActiveInstructor1.setDescr(new RichTextHelper().fromPlain("description of hold"));
336        acadHoldActiveInstructor1.setEffectiveDate(new Date());
337        acadHoldActiveInstructor1.setExpirationDate(null);
338        acadHoldActiveInstructor1.setTypeKey(HoldServiceConstants.INTRUCTOR_HOLD_TYPE_KEY);
339        acadHoldActiveInstructor1.setStateKey(HoldServiceConstants.HOLD_ACTIVE_STATE_KEY);
340        acadHoldActiveInstructor1 = holdService.createAppliedHold(acadHoldActiveInstructor1.getPersonId(),
341                acadHoldActiveInstructor1.getHoldIssueId(), acadHoldActiveInstructor1.getTypeKey(), acadHoldActiveInstructor1,
342                callContext);
343
344        // test bulk get
345        List<String> expIds = new ArrayList<String>();
346        expIds.add(acadHoldReleasedStudent1.getId());
347        expIds.add(finAidHoldStudent1.getId());
348        expIds.add(acadHoldActiveStudent1.getId());
349        expIds.add(acadHoldActiveInstructor1.getId());
350        List<AppliedHoldInfo> holds = holdService.getAppliedHoldsByIds(expIds, callContext);
351        assertEquals(expIds.size(), holds.size());
352        for (AppliedHoldInfo issue : holds) {
353            if (!expIds.remove(issue.getId())) {
354                fail(issue.getId());
355            }
356        }
357        assertEquals(0, expIds.size());
358
359        // test get by type
360        List<String> actIds = holdService.getAppliedHoldIdsByType(HoldServiceConstants.INTRUCTOR_HOLD_TYPE_KEY, callContext);
361        assertEquals(1, actIds.size());
362        assertEquals(acadHoldActiveInstructor1.getId(), actIds.get(0));
363
364        // test get by other type
365        actIds = holdService.getAppliedHoldIdsByType(HoldServiceConstants.STUDENT_HOLD_TYPE_KEY, callContext);
366        expIds = new ArrayList<String>();
367        expIds.add(acadHoldReleasedStudent1.getId());
368        expIds.add(finAidHoldStudent1.getId());
369        expIds.add(acadHoldActiveStudent1.getId());
370        new ListOfStringTester().check(expIds, actIds);
371
372        // test get by student1
373        holds = holdService.getAppliedHoldsByPerson("student1", callContext);
374        expIds = new ArrayList<String>();
375        expIds.add(acadHoldReleasedStudent1.getId());
376        expIds.add(finAidHoldStudent1.getId());
377        expIds.add(acadHoldActiveStudent1.getId());
378        for (AppliedHoldInfo hold : holds) {
379            if ( ! expIds.remove(hold.getId())) {
380                fail(hold.getId());
381            }
382        }
383        assertEquals(0, expIds.size());
384
385        // test get by instructor1
386        holds = holdService.getAppliedHoldsByPerson("instructor1", callContext);
387        assertEquals(1, holds.size());
388        assertEquals(acadHoldActiveInstructor1.getId(), holds.get(0).getId());
389
390        // test get by student1
391        holds = holdService.getActiveAppliedHoldsByPerson("student1", callContext);
392        assertEquals(2, holds.size());
393
394        expIds = new ArrayList<String>();
395        expIds.add(finAidHoldStudent1.getId());
396        expIds.add(acadHoldActiveStudent1.getId());
397        for (AppliedHoldInfo hold : holds) {
398            if ( ! expIds.remove(hold.getId())) {
399                fail(hold.getId());
400            }
401        }
402        assertEquals(0, expIds.size());
403
404        //  getHoldsByIssue
405        actIds = holdService.getAppliedHoldIdsByIssue(acadIssue.getId(), callContext);
406        expIds = new ArrayList<String>();
407        expIds.add(acadHoldReleasedStudent1.getId());
408        expIds.add(acadHoldActiveStudent1.getId());
409        expIds.add(acadHoldActiveInstructor1.getId());
410        for (String id : actIds) {
411            if (!expIds.remove(id)) {
412                fail(id);
413            }
414        }
415        assertEquals(0, expIds.size());
416        
417//        getHoldsByIssueAndPerson
418        holds = holdService.getAppliedHoldsByIssueAndPerson(acadIssue.getId(), "student1", callContext);
419        expIds = new ArrayList<String>();
420        expIds.add(acadHoldReleasedStudent1.getId());
421        expIds.add(acadHoldActiveStudent1.getId());
422        for (AppliedHoldInfo hold : holds) {
423            if (!expIds.remove(hold.getId())) {
424                fail(hold.getId());
425            }
426        }
427        assertEquals(0, expIds.size());
428        
429//        getActiveHoldsByIssueAndPerson
430        holds = holdService.getActiveAppliedHoldsByIssueAndPerson(acadIssue.getId(), "student1", callContext);
431        expIds = new ArrayList<String>();
432        expIds.add(acadHoldActiveStudent1.getId());
433        for (AppliedHoldInfo hold : holds) {
434            if (!expIds.remove(hold.getId())) {
435                fail(hold.getId());
436            }
437        }
438        assertEquals(0, expIds.size());
439        
440        // now test delete all but one hold      
441        StatusInfo status = holdService.deleteAppliedHold(acadHoldActiveStudent1.getId(), callContext);
442        assertNotNull(status);
443        assertTrue(status.getIsSuccess());
444        try {
445            actual = holdService.getAppliedHold(acadHoldActiveStudent1.getId(), callContext);
446            fail("Did not receive DoesNotExistException when attempting to get already-deleted AppliedHoldEntity");
447        } catch (DoesNotExistException dnee) {
448            // expected
449        }
450        status = holdService.deleteAppliedHold(finAidHoldStudent1.getId(), callContext);
451        status = holdService.deleteAppliedHold(acadHoldActiveInstructor1.getId(), callContext);
452        
453        return acadHoldReleasedStudent1;
454    }
455}