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.r2.lum.coursebundle.service.impl;
17  
18  
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Date;
24  import java.util.List;
25  import javax.annotation.Resource;
26  
27  import org.junit.After;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.kuali.student.common.test.util.IdEntityTester;
33  import org.kuali.student.common.test.util.KeyEntityTester;
34  import org.kuali.student.common.test.util.RichTextTester;
35  import org.kuali.student.lum.coursebundle.dto.CourseBundleInfo;
36  import org.kuali.student.lum.coursebundle.service.CourseBundleService;
37  import org.kuali.student.r2.common.dto.ContextInfo;
38  import org.kuali.student.r2.common.dto.IdEntityInfo;
39  import org.kuali.student.r2.common.dto.StatusInfo;
40  import org.kuali.student.r2.common.dto.TypeStateEntityInfo;
41  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
42  import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
43  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
44  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
45  import org.kuali.student.r2.common.exceptions.MissingParameterException;
46  import org.kuali.student.r2.common.exceptions.OperationFailedException;
47  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
48  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
49  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
50  import org.kuali.student.r2.common.util.RichTextHelper;
51  import org.springframework.test.context.ContextConfiguration;
52  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
53  import org.springframework.transaction.annotation.Transactional;
54  
55  import static org.junit.Assert.assertEquals;
56  import static org.junit.Assert.assertFalse;
57  import static org.junit.Assert.assertNotNull;
58  import static org.junit.Assert.assertNull;
59  import static org.junit.Assert.assertTrue;
60  import static org.junit.Assert.fail;
61  
62  
63  @Transactional
64  public abstract class TestCourseBundleServiceImplConformanceExtendedCrud extends TestCourseBundleServiceImplConformanceBaseCrud
65  {
66      private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
67  
68      @Resource
69      private CourseBundleDataLoader dataLoader;
70  
71      @After
72      public void tearDownExtended() throws Exception {
73          dataLoader.afterTest();
74      }
75  
76  	// ========================================
77  	// DTO FIELD SPECIFIC METHODS
78  	// ========================================
79  	
80  	// ****************************************************
81  	//           CourseBundleInfo
82  	// ****************************************************
83  	
84  	/*
85  		A method to set the fields for a CourseBundle in a 'test create' section prior to calling the 'create' operation.
86  	*/
87  	public void testCrudCourseBundle_setDTOFieldsForTestCreate(CourseBundleInfo expected) 
88  	{
89  		expected.setTypeKey("typeKey01");
90  		expected.setStateKey("stateKey01");
91  		expected.setName("name01");
92  		expected.setDescr(RichTextHelper.buildRichTextInfo("descr01", "descr01"));
93  
94          try {
95              expected.setEffectiveDate(dateFormat.parse("20130611"));
96              expected.setExpirationDate(dateFormat.parse("20500101"));
97          } catch (ParseException e) {
98              throw new RuntimeException("Failed to parse date", e);
99          }
100 		expected.setCourseBundleCode("courseBundleCode01");
101 		expected.setStartTermId("startTermId01");
102 		expected.setEndTermId("endTermId01");
103 		expected.setSubjectAreaOrgId("subjectAreaOrgId01");
104 		expected.setCourseBundleCodeSuffix("courseBundleCodeSuffix01");
105         expected.setAdminOrgIds(Arrays.asList(new String[] {"1", "2", "3", "4", "5", "6"}));
106         expected.setCourseIds(Arrays.asList(new String[] {"A", "B", "C"}));
107 	}
108 	
109 	/*
110 		A method to test the fields for a CourseBundle. This is called after:
111 		- creating a DTO, where actual is the DTO returned by the create operation, and expected is the dto passed in to the create operation
112 		- reading a DTO after creating it, and actual is the read DTO, and expected is the dto that was created
113 		- 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
114 	*/
115 	public void testCrudCourseBundle_testDTOFieldsForTestCreateUpdate(CourseBundleInfo expected, CourseBundleInfo actual) 
116 	{
117 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
118 		assertEquals (expected.getStateKey(), actual.getStateKey());
119 		assertEquals (expected.getName(), actual.getName());
120 		new RichTextTester().check(expected.getDescr(), actual.getDescr());
121 		assertEquals (expected.getEffectiveDate(), actual.getEffectiveDate());
122 		assertEquals (expected.getExpirationDate(), actual.getExpirationDate());
123 		assertEquals (expected.getCourseBundleCode(), actual.getCourseBundleCode());
124 		assertEquals (expected.getStartTermId(), actual.getStartTermId());
125 		assertEquals (expected.getEndTermId(), actual.getEndTermId());
126 		assertEquals (expected.getSubjectAreaOrgId(), actual.getSubjectAreaOrgId());
127 		assertEquals (expected.getCourseBundleCodeSuffix(), actual.getCourseBundleCodeSuffix());
128         assertEquals(expected.getAdminOrgIds().size(), actual.getAdminOrgIds().size());
129         for(String orgId : expected.getAdminOrgIds())  {
130             assertTrue(actual.getAdminOrgIds().contains(orgId));
131         }
132         assertEquals(expected.getCourseIds().size(), actual.getCourseIds().size());
133         for(String id : expected.getCourseIds())  {
134             assertTrue(actual.getCourseIds().contains(id));
135         }
136 	}
137 	
138 	/*
139 		A method to set the fields for a CourseBundle in a 'test update' section prior to calling the 'update' operation.
140 	*/
141 	public void testCrudCourseBundle_setDTOFieldsForTestUpdate(CourseBundleInfo expected) 
142 	{
143 		expected.setTypeKey("typeKey_Updated");
144 		expected.setStateKey("stateKey_Updated");
145 		expected.setName("name_Updated");
146 		expected.setDescr(RichTextHelper.buildRichTextInfo("descr_Updated", "descr_Updated"));
147         try {
148             expected.setEffectiveDate(dateFormat.parse("20200611"));
149             expected.setExpirationDate(dateFormat.parse("20550101"));
150         } catch (ParseException e) {
151         throw new RuntimeException("Failed to parse date", e);
152         }
153 		expected.setCourseBundleCode("courseBundleCode_Updated");
154 		expected.setStartTermId("startTermId_Updated");
155 		expected.setEndTermId("endTermId_Updated");
156 		expected.setSubjectAreaOrgId("subjectAreaOrgId_Updated");
157 		expected.setCourseBundleCodeSuffix("courseBundleCodeSuffix_Updated");
158         expected.setAdminOrgIds(Arrays.asList(new String[] {"SINGLE_ID"}));
159         expected.setCourseIds(Arrays.asList(new String[] {"1", "2", "3"}));
160 	}
161 	
162 	/*
163 		A method to test the fields for a CourseBundle after an update operation, followed by a read operation,
164 		where actual is the DTO returned by the read operation, and expected is the dto returned by the update operation.
165 	*/
166 	public void testCrudCourseBundle_testDTOFieldsForTestReadAfterUpdate(CourseBundleInfo expected, CourseBundleInfo actual) 
167 	{
168 		assertEquals (expected.getId(), actual.getId());
169 		assertEquals (expected.getTypeKey(), actual.getTypeKey());
170 		assertEquals (expected.getStateKey(), actual.getStateKey());
171 		assertEquals (expected.getName(), actual.getName());
172 		new RichTextTester().check(expected.getDescr(), actual.getDescr());
173         assertEquals (expected.getEffectiveDate(), actual.getEffectiveDate());
174         assertEquals (expected.getExpirationDate(), actual.getExpirationDate());
175 		assertEquals (expected.getCourseBundleCode(), actual.getCourseBundleCode());
176 		assertEquals (expected.getStartTermId(), actual.getStartTermId());
177 		assertEquals (expected.getEndTermId(), actual.getEndTermId());
178 		assertEquals (expected.getSubjectAreaOrgId(), actual.getSubjectAreaOrgId());
179 		assertEquals (expected.getCourseBundleCodeSuffix(), actual.getCourseBundleCodeSuffix());
180         assertEquals(expected.getAdminOrgIds().size(), actual.getAdminOrgIds().size());
181         for(String orgId : expected.getAdminOrgIds())  {
182             assertTrue(actual.getAdminOrgIds().contains(orgId));
183         }
184         assertEquals(expected.getCourseIds().size(), actual.getCourseIds().size());
185         for(String id : expected.getCourseIds())  {
186             assertTrue(actual.getCourseIds().contains(id));
187         }
188 	}
189 	
190 	/*
191 		A method to set the fields for a CourseBundle in the 'test read after update' section.
192 		This dto is another (second) dto object being created for other tests.
193 	*/
194 	public void testCrudCourseBundle_setDTOFieldsForTestReadAfterUpdate(CourseBundleInfo expected) 
195 	{
196 		expected.setName("name_Updated");
197         try {
198             expected.setEffectiveDate(dateFormat.parse("19000611"));
199             expected.setExpirationDate(dateFormat.parse("30300101"));
200         } catch (ParseException e) {
201             throw new RuntimeException("Failed to parse date", e);
202         }
203 		expected.setCourseBundleCode("courseBundleCode_Updated");
204 		expected.setStartTermId("startTermId_Updated");
205 		expected.setEndTermId("endTermId_Updated");
206 		expected.setSubjectAreaOrgId("subjectAreaOrgId_Updated");
207 		expected.setCourseBundleCodeSuffix("courseBundleCodeSuffix_Updated");
208 
209         expected.setAdminOrgIds(Arrays.asList(new String[] {"SINGLE_ID_UPDATED"}));
210         expected.setCourseIds(Arrays.asList(new String[] {"X", "Y", "Z"}));
211 	}
212 	
213 	
214 	// ========================================
215 	// SERVICE OPS NOT TESTED IN BASE TEST CLASS
216 	// ========================================
217 	
218 	/* Method Name: searchForCourseBundleIds */
219 	@Test
220 	public void test_searchForCourseBundleIds() 
221 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
222 	}
223 	
224 	/* Method Name: searchForCourseBundles */
225 	@Test
226 	public void test_searchForCourseBundles() 
227 	throws 	InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
228 	}
229 	
230 	/* Method Name: validateCourseBundle */
231 	@Test
232 	public void test_validateCourseBundle() 
233 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
234 	}
235 	
236 	/* Method Name: changeCourseBundleState */
237 	@Test
238 	public void test_changeCourseBundleState() 
239 	throws 	DoesNotExistException	,InvalidParameterException	,MissingParameterException	,OperationFailedException	,PermissionDeniedException	{
240         loadData();
241         List<String> infos = getCourseBundleService().getCourseBundleIdsByType(CourseBundleDataLoader.COURSE_BUNDLE_TYPE_KEY, contextInfo);
242         assertTrue(infos.size() > 0);
243         CourseBundleInfo original = getCourseBundleService().getCourseBundle(infos.get(0), contextInfo);
244         assertEquals(CourseBundleDataLoader.COURSE_BUNDLE_ACTIVE_STATE_KEY,original.getStateKey());
245         original.setStateKey(CourseBundleDataLoader.COURSE_BUNDLE_INACTIVE_STATE_KEY);
246 
247         try {
248             getCourseBundleService().updateCourseBundle(original.getId(), original, contextInfo);
249         } catch (Exception e){
250             throw new OperationFailedException(e);
251         }
252         CourseBundleInfo updated = getCourseBundleService().getCourseBundle(original.getId(), contextInfo);
253         assertEquals(CourseBundleDataLoader.COURSE_BUNDLE_INACTIVE_STATE_KEY,updated.getStateKey());
254 	}
255 
256 
257     private void loadData() throws OperationFailedException {
258         try {
259             dataLoader.beforeTest();
260         } catch (Exception e) {
261             throw new OperationFailedException("failed to load data", e);
262         }
263     }
264 }
265 
266