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.exam.service.impl;
17  
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import javax.annotation.Resource;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.kuali.student.common.test.util.IdEntityTester;
27  import org.kuali.student.enrollment.exam.dto.ExamInfo;
28  import org.kuali.student.enrollment.exam.service.ExamService;
29  import org.kuali.student.r2.common.dto.ContextInfo;
30  import org.kuali.student.r2.common.dto.DtoConstants;
31  import org.kuali.student.r2.common.dto.StatusInfo;
32  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
33  import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
34  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
35  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
36  import org.kuali.student.r2.common.exceptions.MissingParameterException;
37  import org.kuali.student.r2.common.exceptions.OperationFailedException;
38  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
39  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
40  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
41  import org.kuali.student.common.test.util.AttributeTester;
42  import org.kuali.student.common.test.util.RichTextTester;
43  import org.kuali.student.common.test.util.MetaTester;
44  import org.kuali.student.r2.common.util.constants.ExamServiceConstants;
45  import org.springframework.test.context.ContextConfiguration;
46  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertNotNull;
50  import static org.junit.Assert.assertNull;
51  import static org.junit.Assert.assertTrue;
52  import static org.junit.Assert.fail;
53  
54  
55  @RunWith(SpringJUnit4ClassRunner.class)
56  @ContextConfiguration(locations = {"classpath:exam-test-with-map-context.xml"})
57  public abstract class TestExamServiceImplConformanceBaseCrud {
58  
59      // ====================
60      // SETUP
61      // ====================
62  
63      @Resource(name = "testExamService")
64      public ExamService testService;
65      public ExamService getExamService() { return testService; }
66      public void setExamService(ExamService service) { testService = service; }
67  
68      public ContextInfo contextInfo = null;
69      public static String principalId = "123";
70  
71      @Before
72      public void setUp()
73      {
74          principalId = "123";
75          contextInfo = new ContextInfo();
76          contextInfo.setPrincipalId(principalId);
77      }
78  
79      // ====================
80      // TESTING
81      // ====================
82  
83      // ****************************************************
84      //           ExamInfo
85      // ****************************************************
86      @Test
87      public void testCrudExam()
88              throws DataValidationErrorException,
89              DoesNotExistException,
90              InvalidParameterException,
91              MissingParameterException,
92              OperationFailedException,
93              PermissionDeniedException,
94              ReadOnlyException,
95              VersionMismatchException,
96              DependentObjectsExistException
97      {
98          // -------------------------------------
99          // test create
100         // -------------------------------------
101         ExamInfo expected = new ExamInfo ();
102 
103         // METHOD TO SET DTO FIELDS HERE FOR TEST CREATE
104         testCrudExam_setDTOFieldsForTestCreate (expected);
105 
106         new AttributeTester().add2ForCreate(expected.getAttributes());
107 
108         // code to create actual
109         ExamInfo actual = testService.createExam ( expected.getTypeKey(), expected, contextInfo);
110 
111         assertNotNull(actual.getId());
112         new IdEntityTester().check(expected, actual);
113 
114         // METHOD TO TEST DTO FIELDS HERE FOR TEST CREATE
115         testCrudExam_testDTOFieldsForTestCreateUpdate (expected, actual);
116 
117         new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
118         new MetaTester().checkAfterCreate(actual.getMeta());
119 
120         // -------------------------------------
121         // test read
122         // -------------------------------------
123         expected = actual;
124         actual = testService.getExam ( actual.getId(), contextInfo);
125         assertEquals(expected.getId(), actual.getId());
126         new IdEntityTester().check(expected, actual);
127 
128         // INSERT CODE FOR TESTING MORE DTO FIELDS HERE
129         testCrudExam_testDTOFieldsForTestCreateUpdate (expected, actual);
130 
131         new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
132         new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
133 
134         // -------------------------------------
135         // test update
136         // -------------------------------------
137         expected = actual;
138 
139         expected.setStateKey(expected.getState() + "_Updated");
140 
141         // METHOD TO INSERT CODE TO UPDATE DTO FIELDS HERE
142         testCrudExam_setDTOFieldsForTestUpdate (expected);
143 
144         new AttributeTester().delete1Update1Add1ForUpdate(expected.getAttributes());
145         // code to update
146         actual = testService.updateExam ( expected.getId(), expected, contextInfo);
147 
148         assertEquals(expected.getId(), actual.getId());
149         new IdEntityTester().check(expected, actual);
150 
151         // METHOD TO INSERT CODE FOR TESTING DTO FIELDS HERE
152         testCrudExam_testDTOFieldsForTestCreateUpdate (expected, actual);
153 
154         new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
155         new MetaTester().checkAfterUpdate(expected.getMeta(), actual.getMeta());
156 
157         // -------------------------------------
158         // test read after update
159         // -------------------------------------
160 
161         expected = actual;
162         // code to get actual
163         actual = testService.getExam ( actual.getId(), contextInfo);
164 
165         assertEquals(expected.getId(), actual.getId());
166         new IdEntityTester().check(expected, actual);
167 
168         // INSERT METHOD CODE FOR TESTING DTO FIELDS HERE
169         testCrudExam_testDTOFieldsForTestReadAfterUpdate (expected, actual);
170 
171         new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
172         new MetaTester().checkAfterGet(expected.getMeta(), actual.getMeta());
173 
174         ExamInfo alphaDTO = actual;
175 
176         // create a 2nd DTO
177         ExamInfo betaDTO = new ExamInfo ();
178 
179         // METHOD TO INSERT CODE TO SET MORE DTO FIELDS HERE
180         testCrudExam_setDTOFieldsForTestReadAfterUpdate (betaDTO);
181 
182         betaDTO.setTypeKey("typeKeyBeta");
183         betaDTO.setStateKey("stateKeyBeta");
184         betaDTO = testService.createExam ( betaDTO.getTypeKey(), betaDTO, contextInfo);
185 
186         // -------------------------------------
187         // test bulk get with no ids supplied
188         // -------------------------------------
189 
190         List<String> examIds = new ArrayList<String>();
191         // code to get DTO by Ids
192         List<ExamInfo> records = testService.getExamsByIds ( examIds, contextInfo);
193 
194         assertEquals(examIds.size(), records.size());
195         assertEquals(0, examIds.size());
196 
197         // -------------------------------------
198         // test bulk get
199         // -------------------------------------
200         examIds = new ArrayList<String>();
201         examIds.add(alphaDTO.getId());
202         examIds.add(betaDTO.getId());
203         // code to get DTO by Ids
204         records = testService.getExamsByIds ( examIds, contextInfo);
205 
206         assertEquals(examIds.size(), records.size());
207         for (ExamInfo record : records)
208         {
209             if (!examIds.remove(record.getId()))
210             {
211                 fail(record.getId());
212             }
213         }
214         assertEquals(0, examIds.size());
215 
216         // -------------------------------------
217         // test get by type
218         // -------------------------------------
219         ExamInfo infoByType = new ExamInfo();
220         testCrudExam_setDTOFieldsForTestCreate(infoByType);
221         infoByType.setStateKey(DtoConstants.STATE_ACTIVE);
222         infoByType.setTypeKey("firstByType");
223         infoByType = testService.createExam( infoByType.getTypeKey(), infoByType, contextInfo);
224         ExamInfo infoByType2 = new ExamInfo();
225         testCrudExam_setDTOFieldsForTestCreate(infoByType2);
226         infoByType2.setStateKey(DtoConstants.STATE_ACTIVE);
227         infoByType2.setTypeKey("otherByType");
228         infoByType2 = testService.createExam(infoByType2.getTypeKey(), infoByType2, contextInfo);
229 
230 
231 
232         // code to get by specific type "firstByType"
233         examIds = testService.getExamIdsByType ("firstByType", contextInfo);
234 
235         assertEquals(1, examIds.size());
236         assertEquals(infoByType.getId(), examIds.get(0));
237 
238         // test get by other type
239         // code to get by specific type "otherByType"
240         examIds = testService.getExamIdsByType ("otherByType", contextInfo);
241 
242         assertEquals(1, examIds.size());
243         assertEquals(infoByType2.getId(), examIds.get(0));
244 
245         // -------------------------------------
246         // test delete
247         // -------------------------------------
248 
249         StatusInfo status = testService.deleteExam ( actual.getId(), contextInfo);
250 
251         assertNotNull(status);
252         assertTrue(status.getIsSuccess());
253         try
254         {
255             ExamInfo record = testService.getExam ( actual.getId(), contextInfo);
256             fail("Did not receive DoesNotExistException when attempting to get already-deleted entity");
257         }
258         catch (DoesNotExistException dnee)
259         {
260             // expected
261         }
262 
263     }
264 
265     /*
266         A method to set the fields for a Exam in a 'test create' section prior to calling the 'create' operation.
267     */
268     public abstract void testCrudExam_setDTOFieldsForTestCreate(ExamInfo expected);
269 
270     /*
271         A method to test the fields for a Exam. This is called after:
272         - creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
273         - reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
274         - updating a DTO, where actual is DTO returned by the update operation, and expected is the dto that was passed in to the update operation
275     */
276     public abstract void testCrudExam_testDTOFieldsForTestCreateUpdate(ExamInfo expected, ExamInfo actual);
277 
278     /*
279         A method to set the fields for a Exam in a 'test update' section prior to calling the 'update' operation.
280     */
281     public abstract void testCrudExam_setDTOFieldsForTestUpdate(ExamInfo expected);
282 
283     /*
284         A method to test the fields for a Exam after an update operation, followed by a read operation,
285         where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
286     */
287     public abstract void testCrudExam_testDTOFieldsForTestReadAfterUpdate(ExamInfo expected, ExamInfo actual);
288 
289     /*
290         A method to set the fields for a Exam in the 'test read after update' section.
291         This dto is another (second) dto object being created for other tests.
292     */
293     public abstract void testCrudExam_setDTOFieldsForTestReadAfterUpdate(ExamInfo expected);
294 
295 
296     // ========================================
297     // SERVICE OPS TESTED IN BASE TEST CLASS
298     // ========================================
299 	
300 	/*
301 		The following methods are tested as part of CRUD operations for this service's DTOs:
302 			createExam
303 			getExam
304 			updateExam
305 			deleteExam
306 			getExamsByIds
307 			getExamIdsByType
308 	*/
309 
310     // ========================================
311     // SERVICE OPS NOT TESTED IN BASE TEST CLASS
312     // ========================================
313 
314     /* Method Name: validateExam */
315     @Test
316     public abstract void test_validateExam()
317             throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	;
318 
319     /* Method Name: searchForExamIds */
320     @Test
321     public abstract void test_searchForExamIds()
322             throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	;
323 
324     /* Method Name: searchForExams */
325     @Test
326     public abstract void test_searchForExams()
327             throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	;
328 
329 }
330 
331