View Javadoc
1   /**
2    * Copyright 2013 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   * Created by Charles on 6/13/13
16   */
17  package org.kuali.student.enrollment.class2.acal.service.impl;
18  
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingServiceTestDataLoader;
24  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
25  import org.kuali.student.r2.common.dto.ContextInfo;
26  import org.kuali.student.r2.common.dto.StatusInfo;
27  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
28  import org.kuali.student.r2.common.exceptions.OperationFailedException;
29  import org.kuali.student.r2.core.acal.dto.AcademicCalendarInfo;
30  import org.kuali.student.r2.core.acal.dto.TermInfo;
31  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
32  import org.kuali.student.r2.core.acal.service.facade.AcademicCalendarServiceFacade;
33  import org.kuali.student.r2.core.atp.service.AtpService;
34  import org.kuali.student.r2.core.constants.AtpServiceConstants;
35  import org.kuali.student.r2.lum.course.service.CourseService;
36  import org.kuali.student.r2.lum.lrc.service.LRCService;
37  import org.springframework.test.context.ContextConfiguration;
38  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
39  
40  import javax.annotation.Resource;
41  import java.util.Date;
42  
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.assertTrue;
47  import static org.junit.Assert.fail;
48  
49  /**
50   * Tests AcademicCalendarServiceFacadeImpl
51   *
52   * @author Kuali Student Team (cclin@umd.edu)
53   */
54  @RunWith(SpringJUnit4ClassRunner.class)
55  @ContextConfiguration(locations = {"classpath:acal-service-facade-test-class2-mock-context.xml"})
56  public class TestAcademicCalendarServiceFacadeImpl {
57      @Resource(name="CourseOfferingService")
58      protected CourseOfferingService coService;
59  
60      @Resource
61      protected CourseService courseService;
62  
63      @Resource
64      protected CourseOfferingServiceTestDataLoader dataLoader;
65  
66      @Resource(name = "LrcService")
67      protected LRCService lrcService;
68  
69      @Resource
70      protected AcademicCalendarService acalService;
71  
72      @Resource
73      protected AtpService atpService;
74  
75      @Resource
76      private AcademicCalendarServiceFacade acalServiceFacade;
77  
78      // Local instance variables
79      private ContextInfo contextInfo;
80      private AcademicCalendarInfo cal;
81      private TermInfo parentTerm;
82      private TermInfo childTerm;
83      private TermInfo childTerm2;
84  
85      public TestAcademicCalendarServiceFacadeImpl() {
86      }
87  
88      @Before
89      public void beforeTest() throws Exception {
90          dataLoader.beforeTest();
91  
92          contextInfo = new ContextInfo();
93  
94          contextInfo.setCurrentDate(new Date());
95  
96          contextInfo.setPrincipalId("test");
97          contextInfo.setAuthenticatedPrincipalId("test");
98  
99          // Create an acal, plus a term and subterm
100         cal = new AcademicCalendarInfo();
101         cal.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
102         cal.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY);
103         cal = acalService.createAcademicCalendar(cal.getTypeKey(), cal, contextInfo);
104         // Then, create a term
105         parentTerm = new TermInfo();
106         parentTerm.setTypeKey(AtpServiceConstants.ATP_FALL_TYPE_KEY);
107         parentTerm.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
108         parentTerm = acalService.createTerm(parentTerm.getTypeKey(), parentTerm, contextInfo);
109         // Attach term to calendar
110         acalService.addTermToAcademicCalendar(cal.getId(), parentTerm.getId(), contextInfo);
111         // Create child term
112         childTerm = new TermInfo();
113         childTerm.setTypeKey(AtpServiceConstants.ATP_HALF_FALL_1_TYPE_KEY);
114         childTerm.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
115         childTerm = acalService.createTerm(childTerm.getTypeKey(), childTerm, contextInfo);
116         // Attach term to calendar
117         acalService.addTermToTerm(parentTerm.getId(), childTerm.getId(), contextInfo);
118         // Create child term (second)
119         childTerm2 = new TermInfo();
120         childTerm2.setTypeKey(AtpServiceConstants.ATP_HALF_FALL_2_TYPE_KEY);
121         childTerm2.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
122         childTerm2 = acalService.createTerm(childTerm2.getTypeKey(), childTerm2, contextInfo);
123         // Attach term to calendar
124         acalService.addTermToTerm(parentTerm.getId(), childTerm2.getId(), contextInfo);
125     }
126 
127     @After
128     public void afterTest() throws Exception {
129         dataLoader.afterTest();
130     }
131 
132     @Test
133     public void testMakeTermOfficialCascaded() throws Exception {
134         // KSENROLL-7251 (testing making a term offiical)
135         acalServiceFacade.makeTermOfficialCascaded(childTerm.getId(), contextInfo);
136         // Refetch all relevant parts
137         cal = acalService.getAcademicCalendar(cal.getId(), contextInfo);
138         parentTerm = acalService.getTerm(parentTerm.getId(), contextInfo);
139         childTerm = acalService.getTerm(childTerm.getId(), contextInfo);
140         childTerm2 = acalService.getTerm(childTerm2.getId(), contextInfo);
141         assertEquals(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, cal.getStateKey());
142         assertEquals(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, parentTerm.getStateKey());
143         assertEquals(AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, childTerm.getStateKey());
144         // The only one that should stay draft
145         assertEquals(AtpServiceConstants.ATP_DRAFT_STATE_KEY, childTerm2.getStateKey());
146     }
147 
148     @Test
149     public void testDeleteTermSucceed() throws Exception {
150         // KSENROLL-7701
151         StatusInfo statusInfo = acalServiceFacade.deleteTermCascaded(parentTerm.getId(), contextInfo);
152         assertTrue(statusInfo.getIsSuccess());
153         try {
154             acalService.getTerm(parentTerm.getId(), contextInfo);
155             fail("DoesNotExistException should have been thrown");
156         } catch (DoesNotExistException e) {
157             assertNotNull(e.getMessage());
158             assertEquals(parentTerm.getId(), e.getMessage());
159         }
160 
161         try {
162             acalService.getTerm(childTerm.getId(), contextInfo);
163             fail("DoesNotExistException should have been thrown");
164         } catch (DoesNotExistException e) {
165             assertNotNull(e.getMessage());
166             assertEquals(childTerm.getId(), e.getMessage());
167         }
168 
169         try {
170             acalService.getTerm(childTerm2.getId(), contextInfo);
171             fail("DoesNotExistException should have been thrown");
172         } catch (DoesNotExistException e) {
173             assertNotNull(e.getMessage());
174             assertEquals(childTerm2.getId(), e.getMessage());
175         }
176     }
177 
178     @Test
179     public void testDeleteTermFailed() throws Exception {
180         acalServiceFacade.makeTermOfficialCascaded(parentTerm.getId(), contextInfo);
181         try {
182             acalServiceFacade.deleteTermCascaded(parentTerm.getId(), contextInfo);
183             fail("OperationFailedException should have been thrown");
184         } catch (OperationFailedException e) {
185             assertNotNull(e.getMessage());
186             assertEquals("Can't delete term that is official", e.getMessage());
187         }
188 
189         acalService.getTerm(parentTerm.getId(), contextInfo);
190 
191         // KSENROLL-7927
192         // The code below fails because mocks don't do rollbacks.  deleteTermCascaded deletes child
193         // terms prior to the parent node.  Since child terms can be draft, and parents need not be,
194         // we have no good mechanism to do this.
195 
196 //        threwException = false;
197 //        try {
198 //            acalService.getTerm(childTerm.getId(), contextInfo);
199 //        } catch (DoesNotExistException e) {
200 //            threwException = true;
201 //        }
202 //        assert(!threwException);
203 //
204 //        threwException = false;
205 //        try {
206 //            acalService.getTerm(childTerm2.getId(), contextInfo);
207 //        } catch (DoesNotExistException e) {
208 //            threwException = true;
209 //        }
210 //        assert(!threwException);
211     }
212 
213     private TermInfo _setup2(int level) throws Exception {
214         // Initial data setup
215         TermInfo subSubTerm = new TermInfo();
216         subSubTerm.setTypeKey(AtpServiceConstants.ATP_HALF_FALL_2_TYPE_KEY);
217         subSubTerm.setStateKey(AtpServiceConstants.ATP_DRAFT_STATE_KEY);
218         subSubTerm = acalService.createTerm(subSubTerm.getTypeKey(), subSubTerm, contextInfo);
219         acalService.addTermToTerm(childTerm2.getId(), subSubTerm.getId(), contextInfo);
220         if (level == 0) {
221             acalService.changeTermState(parentTerm.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
222         } else if (level == 1) {
223             acalService.changeTermState(childTerm.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
224         } else if (level == 2) {
225             acalService.changeTermState(childTerm2.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
226         } else if (level == 3) {
227             acalService.changeTermState(subSubTerm.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
228         }
229         return subSubTerm;
230     }
231 
232     @Test
233     public void testValidateTerm0() throws Exception {
234         _setup2(0);
235         // Now test for invalidity
236         assertTrue(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
237 
238     }
239 
240     @Test
241     public void testValidateTerm1() throws Exception {
242         _setup2(1);
243         // Now test for invalidity
244         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
245     }
246 
247     @Test
248     public void testValidateTerm2() throws Exception {
249         _setup2(2);
250         // Now test for invalidity
251         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
252     }
253 
254     @Test
255     public void testValidateTerm3() throws Exception {
256         _setup2(3);
257         // Now test for invalidity
258         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
259     }
260 
261     @Test
262     public void testValidateCalendar0() throws Exception {
263         _setup2(0);
264 
265         // Now test for invalidity
266         assertFalse(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
267         acalService.changeAcademicCalendarState(cal.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
268         assertTrue(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
269     }
270 
271     @Test
272     public void testValidateCalendar1() throws Exception {
273         _setup2(1);
274         // Now test for invalidity
275         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
276         acalService.changeAcademicCalendarState(cal.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
277         assertFalse(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
278     }
279 
280     @Test
281     public void testValidateCalendar2() throws Exception {
282         _setup2(2);
283         // Now test for invalidity
284         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
285         acalService.changeAcademicCalendarState(cal.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
286         assertFalse(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
287     }
288 
289     @Test
290     public void testValidateCalendar3() throws Exception {
291         _setup2(3);
292         // Now test for invalidity
293         assertFalse(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
294         acalService.changeAcademicCalendarState(cal.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
295         assertFalse(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
296     }
297 
298     @Test
299     public void testValidateCalendar4() throws Exception {
300         _setup2(4);  // all terms draft
301         // Now test for invalidity
302         assertTrue(acalServiceFacade.validateTerm(parentTerm.getId(), contextInfo));
303         acalService.changeAcademicCalendarState(cal.getId(), AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, contextInfo);
304         assertTrue(acalServiceFacade.validateCalendar(cal.getId(), contextInfo));
305     }
306 }