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.class2.examoffering.service.facade;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingServiceTestDataLoader;
23  import org.kuali.student.enrollment.class2.examoffering.service.facade.ExamOfferingServiceFacade;
24  import org.kuali.student.enrollment.class2.examoffering.service.impl.ExamOfferingServiceTestDataLoader;
25  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
26  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
27  import org.kuali.student.enrollment.examoffering.dto.ExamOfferingRelationInfo;
28  import org.kuali.student.enrollment.examoffering.infc.ExamOffering;
29  import org.kuali.student.enrollment.examoffering.service.ExamOfferingService;
30  import org.kuali.student.lum.lrc.service.util.MockLrcTestDataLoader;
31  import org.kuali.student.r2.common.dto.AttributeInfo;
32  import org.kuali.student.r2.common.dto.ContextInfo;
33  import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
34  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
35  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
36  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
37  import org.kuali.student.r2.common.exceptions.MissingParameterException;
38  import org.kuali.student.r2.common.exceptions.OperationFailedException;
39  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
40  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
41  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
42  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
43  import org.kuali.student.r2.common.util.constants.ExamOfferingServiceConstants;
44  import org.kuali.student.r2.common.util.constants.LuServiceConstants;
45  import org.kuali.student.r2.lum.course.service.CourseService;
46  import org.kuali.student.r2.lum.lrc.service.LRCService;
47  import org.springframework.test.context.ContextConfiguration;
48  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49  
50  import javax.annotation.Resource;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import static org.junit.Assert.assertEquals;
55  import static org.junit.Assert.assertNotNull;
56  import static org.junit.Assert.assertNull;
57  import static org.junit.Assert.fail;
58  
59  @RunWith(SpringJUnit4ClassRunner.class)
60  @ContextConfiguration(locations = {"classpath:eo-businesslogic-test-with-mocks-context.xml"})
61  public class TestExamOfferingServiceFacadeImpl {
62  
63      @Resource
64      private ExamOfferingServiceFacade examOfferingBusinessLogic;
65  
66      @Resource
67      private CourseService courseService;
68  
69      @Resource
70      private CourseOfferingService courseOfferingService;
71  
72      @Resource
73      private LRCService lrcService;
74  
75      @Resource
76      private ExamOfferingService examOfferingService;
77  
78      @Resource
79      private CourseOfferingServiceTestDataLoader coDataLoader;
80  
81      @Resource
82      private ExamOfferingServiceTestDataLoader eoDataLoader;
83  
84      public ContextInfo contextInfo = null;
85      public static String principalId = "123";
86  
87      @Before
88      public void setUp() throws Exception {
89          new MockLrcTestDataLoader(this.lrcService).loadData();
90          coDataLoader.createStateTestData();
91          coDataLoader.beforeTest(false);
92  
93          eoDataLoader.beforeTest();
94  
95          principalId = "123";
96          contextInfo = new ContextInfo();
97          contextInfo.setPrincipalId(principalId);
98      }
99  
100     @After
101     public void afterTest() throws Exception {
102         coDataLoader.afterTest();
103         eoDataLoader.afterTest();
104     }
105 
106     @Test
107     public void testGenerateFinalExamOffering() throws InvalidParameterException, AlreadyExistsException,
108             OperationFailedException, MissingParameterException, PermissionDeniedException, ReadOnlyException,
109             DataValidationErrorException, DoesNotExistException, VersionMismatchException {
110 
111         String coId = CourseOfferingServiceTestDataLoader.CHEM123_COURSE_OFFERING_ID;
112 
113         CourseOfferingInfo co = this.getCourseOfferingService().getCourseOffering(coId, contextInfo);
114         co.setTermId(ExamOfferingServiceTestDataLoader.TERM_ONE_ID);
115         co.getAttributes().add(new AttributeInfo(CourseOfferingServiceConstants.FINAL_EXAM_DRIVER_ATTR, LuServiceConstants.LU_EXAM_DRIVER_AO_KEY));
116         co = this.getCourseOfferingService().updateCourseOffering(co.getId(), co, contextInfo);
117 
118         List<String> optionKeys = new ArrayList<String>();
119         String ePID = this.getExamOfferingBusinessLogic().getExamPeriodId(co.getTermId(),contextInfo);
120         this.getExamOfferingBusinessLogic().generateFinalExamOffering(coId, co.getTermId(), ePID, optionKeys, contextInfo);
121 
122         List<ExamOfferingRelationInfo> eoRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
123                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
124         assertEquals(2, eoRelations.size());
125         for(ExamOfferingRelationInfo eoRelation : eoRelations){
126             assertEquals(1, eoRelation.getActivityOfferingIds().size());
127         }
128 
129         co.getAttributes().get(0).setValue(LuServiceConstants.LU_EXAM_DRIVER_CO_KEY);
130         this.getCourseOfferingService().updateCourseOffering(co.getId(), co, contextInfo);
131 
132         this.getExamOfferingBusinessLogic().generateFinalExamOffering(coId, co.getTermId(), ePID, optionKeys, contextInfo);
133 
134         eoRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
135                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
136         assertEquals(3, eoRelations.size());
137         int counter = 0;
138         for(ExamOfferingRelationInfo eoRelation : eoRelations){
139             ExamOffering eo = this.getExamOfferingService().getExamOffering(eoRelation.getExamOfferingId(), contextInfo);
140             if(!ExamOfferingServiceConstants.EXAM_OFFERING_CANCELED_STATE_KEY.equals(eo.getStateKey())){
141                 assertEquals(5, eoRelation.getActivityOfferingIds().size());
142             } else {
143                 counter++;
144             }
145         }
146         assertEquals(2, counter);
147     }
148 
149     @Test
150     public void testGenerateFinalExamOfferingsPerCO() throws MissingParameterException, PermissionDeniedException,
151             InvalidParameterException, OperationFailedException, DoesNotExistException, ReadOnlyException, DataValidationErrorException {
152 
153         List<String> optionKeys = new ArrayList<String>();
154         this.getExamOfferingBusinessLogic().generateFinalExamOfferingsPerFO(CourseOfferingServiceTestDataLoader.CHEM123_COURSE_OFFERING_ID,
155                 ExamOfferingServiceTestDataLoader.TERM_ONE_ID, ExamOfferingServiceTestDataLoader.PERIOD_ONE_ID, optionKeys, contextInfo);
156 
157         List<ExamOfferingRelationInfo> eoRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
158                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
159         assertEquals(1, eoRelations.size());
160         for(ExamOfferingRelationInfo eoRelation : eoRelations){
161             assertEquals(5, eoRelation.getActivityOfferingIds().size());
162             assertNotNull(eoRelation.getFormatOfferingId());
163             assertNotNull(eoRelation.getExamOfferingId());
164 
165             ExamOffering eo = this.getExamOfferingService().getExamOffering(eoRelation.getExamOfferingId(), contextInfo);
166             assertNotNull(eo);
167         }
168 
169         // Test delete of co exam relationships
170         this.getExamOfferingBusinessLogic().removeFinalExamOfferingsFromCO(CourseOfferingServiceTestDataLoader.CHEM123_COURSE_OFFERING_ID,
171                 contextInfo);
172 
173         List<ExamOfferingRelationInfo> newRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
174                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
175         assertEquals(0, newRelations.size());
176 
177         for(ExamOfferingRelationInfo eoRelation : eoRelations){
178             try{
179                 ExamOffering eo = this.getExamOfferingService().getExamOffering(eoRelation.getExamOfferingId(), contextInfo);
180                 fail("Service should throw does noet exist exception");
181             } catch (DoesNotExistException dne){
182                 // do nothing.
183             }
184         }
185     }
186 
187     @Test
188     public void testGenerateFinalExamOfferingsPerAO() throws MissingParameterException, PermissionDeniedException,
189             InvalidParameterException, OperationFailedException, DoesNotExistException, ReadOnlyException, DataValidationErrorException {
190 
191         List<String> optionKeys = new ArrayList<String>();
192         this.getExamOfferingBusinessLogic().generateFinalExamOfferingsPerAO(CourseOfferingServiceTestDataLoader.CHEM123_COURSE_OFFERING_ID,
193                 ExamOfferingServiceTestDataLoader.TERM_ONE_ID, ExamOfferingServiceTestDataLoader.PERIOD_ONE_ID, optionKeys, contextInfo);
194 
195         List<ExamOfferingRelationInfo> eoRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
196                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
197         assertEquals(2, eoRelations.size());
198         for(ExamOfferingRelationInfo eoRelation : eoRelations){
199             assertEquals(1, eoRelation.getActivityOfferingIds().size());
200             assertNotNull(eoRelation.getFormatOfferingId());
201             assertNotNull(eoRelation.getExamOfferingId());
202 
203             ExamOffering eo = this.getExamOfferingService().getExamOffering(eoRelation.getExamOfferingId(), contextInfo);
204             assertNotNull(eo);
205         }
206 
207         // Test delete of ao exam relationships.
208         this.getExamOfferingBusinessLogic().removeFinalExamOfferingsFromCO(CourseOfferingServiceTestDataLoader.CHEM123_COURSE_OFFERING_ID,
209                 contextInfo);
210 
211         List<ExamOfferingRelationInfo> newRelations = this.getExamOfferingService().getExamOfferingRelationsByFormatOffering(
212                 CourseOfferingServiceTestDataLoader.CHEM123_LEC_AND_LAB_FORMAT_OFFERING_ID, contextInfo);
213         assertEquals(0, newRelations.size());
214 
215         for(ExamOfferingRelationInfo eoRelation : eoRelations){
216             try{
217                 ExamOffering eo = this.getExamOfferingService().getExamOffering(eoRelation.getExamOfferingId(), contextInfo);
218                 fail("Service should throw does noet exist exception");
219             } catch (DoesNotExistException dne){
220                 // do nothing.
221             }
222         }
223     }
224 
225     public ExamOfferingServiceFacade getExamOfferingBusinessLogic() {
226         return examOfferingBusinessLogic;
227     }
228 
229     public void setExamOfferingBusinessLogic(ExamOfferingServiceFacade examOfferingBusinessLogic) {
230         this.examOfferingBusinessLogic = examOfferingBusinessLogic;
231     }
232 
233     public CourseService getCourseService() {
234         return courseService;
235     }
236 
237     public void setCourseService(CourseService courseService) {
238         this.courseService = courseService;
239     }
240 
241     public CourseOfferingService getCourseOfferingService() {
242         return courseOfferingService;
243     }
244 
245     public void setCourseOfferingService(CourseOfferingService courseOfferingService) {
246         this.courseOfferingService = courseOfferingService;
247     }
248 
249     public LRCService getLrcService() {
250         return lrcService;
251     }
252 
253     public void setLrcService(LRCService lrcService) {
254         this.lrcService = lrcService;
255     }
256 
257     public ExamOfferingService getExamOfferingService() {
258         return examOfferingService;
259     }
260 
261     public void setExamOfferingService(ExamOfferingService examOfferingService) {
262         this.examOfferingService = examOfferingService;
263     }
264 
265     public CourseOfferingServiceTestDataLoader getCoDataLoader() {
266         return coDataLoader;
267     }
268 
269     public void setCoDataLoader(CourseOfferingServiceTestDataLoader coDataLoader) {
270         this.coDataLoader = coDataLoader;
271     }
272 
273     public ExamOfferingServiceTestDataLoader getEoDataLoader() {
274         return eoDataLoader;
275     }
276 
277     public void setEoDataLoader(ExamOfferingServiceTestDataLoader eoDataLoader) {
278         this.eoDataLoader = eoDataLoader;
279     }
280 }