View Javadoc

1   package org.kuali.student.lum.course.service.impl;
2   
3   import org.junit.Test;
4   import org.kuali.student.common.test.mock.MockProxyFactoryBean;
5   import org.kuali.student.common.test.util.ContextInfoTestUtility;
6   import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
7   import org.kuali.student.r1.common.dictionary.service.impl.DictionaryTesterHelper;
8   import org.kuali.student.r1.common.validator.ServerDateParser;
9   import org.kuali.student.r2.common.dto.AttributeInfo;
10  import org.kuali.student.r2.common.dto.ContextInfo;
11  import org.kuali.student.r2.common.dto.RichTextInfo;
12  import org.kuali.student.r2.common.dto.ValidationResultInfo;
13  import org.kuali.student.r2.common.exceptions.OperationFailedException;
14  import org.kuali.student.r2.common.validator.DefaultValidatorImpl;
15  import org.kuali.student.r2.common.validator.Validator;
16  import org.kuali.student.r2.common.validator.ValidatorFactory;
17  import org.kuali.student.r2.core.atp.dto.AtpInfo;
18  import org.kuali.student.r2.core.atp.service.AtpService;
19  import org.kuali.student.r2.lum.clu.dto.AffiliatedOrgInfo;
20  import org.kuali.student.r2.lum.course.dto.CourseExpenditureInfo;
21  import org.kuali.student.r2.lum.course.dto.CourseInfo;
22  import org.kuali.student.r2.lum.course.dto.CourseRevenueInfo;
23  import org.kuali.student.r2.lum.course.dto.LoDisplayInfo;
24  import org.kuali.student.r2.lum.course.service.utils.ActiveDatesValidator;
25  import org.kuali.student.r2.lum.course.service.utils.ActivityTypeValidator;
26  import org.kuali.student.r2.lum.course.service.utils.ExpenditurePercentValidator;
27  import org.kuali.student.r2.lum.course.service.utils.RevenuePercentValidator;
28  import org.kuali.student.r2.lum.course.service.utils.SubjectAreaUnitOwnerValidator;
29  import org.kuali.student.r2.lum.lo.dto.LoCategoryInfo;
30  import org.springframework.context.ConfigurableApplicationContext;
31  import org.springframework.context.support.ClassPathXmlApplicationContext;
32  
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.Date;
36  import java.util.HashMap;
37  import java.util.LinkedHashSet;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import static org.junit.Assert.assertEquals;
43  import static org.junit.Assert.assertTrue;
44  import static org.junit.Assert.fail;
45  
46  public class TestCourseInfoDictionary {
47  	
48  	ContextInfo contextInfo = ContextInfoTestUtility.getEnglishContextInfo();
49  
50  	@Test
51  	public void testLoadCourseInfoDictionary() {
52  		Set<String> startingClasses = new LinkedHashSet<String>();
53  		startingClasses.add(CourseInfo.class.getName ());
54  		// startingClasses.add (StatementTreeViewInfo.class);
55  		String contextFile = "ks-courseInfo-dictionary-context";
56  		String outFile = "target/" + contextFile + ".txt";
57  		DictionaryTesterHelper helper = new DictionaryTesterHelper(outFile,
58  				startingClasses, contextFile + ".xml", true);
59  	 List<String> errors = helper.doTest ();
60    if (errors.size () > 0)
61    {
62     fail ("failed dictionary validation:\n" + formatAsString (errors));
63    }
64   }
65  
66   private String formatAsString (List<String> errors)
67   {
68    int i = 0;
69    StringBuilder builder = new StringBuilder ();
70    for (String error : errors)
71    {
72     i ++;
73     builder.append (i + ". " + error + "\n");
74    }
75    return builder.toString ();
76   }
77  
78  	@Test
79  	public void testCourseInfoValidation() throws OperationFailedException {
80  		System.out.println("h1. Test Validation");
81  		DefaultValidatorImpl val = new DefaultValidatorImpl();
82  		ValidatorFactory vf = new ValidatorFactory();
83  		SubjectAreaUnitOwnerValidator saVal = new SubjectAreaUnitOwnerValidator();
84  		
85  		List<Validator> vList = new ArrayList<Validator>();
86  		
87  		saVal.setSearchDispatcher(new MockSearchDispatcher());
88  		
89  		vList.add(new RevenuePercentValidator() );
90  		vList.add(new ExpenditurePercentValidator());
91  		vList.add(saVal);
92  		vList.add(getActiveDatesValidator());
93  		vList.add(new ActivityTypeValidator());
94  		vf.setValidatorList(vList);
95  		
96  		val.setValidatorFactory(vf);
97  		val.setDateParser(new ServerDateParser());
98  		val.setSearchDispatcher(new MockSearchDispatcher());
99  		// TODO KSCM Do we need a mock for COurseInfo?
100 		CourseInfo info = new CourseInfo();
101 
102         ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("classpath:ks-courseInfo-dictionary-context.xml");
103 		ObjectStructureDefinition os =(ObjectStructureDefinition) ac.getBean(info.getClass().getName());
104         ac.close();
105 
106 		List<ValidationResultInfo> validationResults =  val.validateObject(info,	os, contextInfo);
107 		System.out.println("h3. With just a blank Course");
108 		for (ValidationResultInfo vr : validationResults) {
109 			System.out.println(vr.getElement() + " " + vr.getMessage());
110 		}
111 		assertEquals(3, validationResults.size());
112 
113 		try {
114 			info = new CourseDataGenerator().getCourseTestData();
115 		} catch (Exception ex) {
116 			throw new RuntimeException(ex);
117 		}
118 
119 		// Clean up revenues because of custom validation  
120 		info.setRevenues(new ArrayList<CourseRevenueInfo>());
121 		info.setExpenditure(null);
122 		
123 		validationResults = val.validateObject(info, os, contextInfo);
124 		System.out.println("h3. With generated data");
125 		for (ValidationResultInfo vr : validationResults) {
126 			System.out.println(vr.getElement() + " " + vr.getMessage());
127 		}
128 		assertEquals(0, validationResults.size());
129 
130 		System.out.println("testCourseDescrRequiredBasedOnState");
131 		info.setStateKey("DRAFT");
132 		info.setDescr(null);
133 		validationResults = val.validateObject(info, os, contextInfo);
134 		assertEquals(0, validationResults.size());
135 
136 		info.setStateKey("ACTIVE");
137 		info.setDescr(null);
138 		validationResults = val.validateObject(info, os, contextInfo);
139 		for (ValidationResultInfo vr : validationResults) {
140 			System.out.println(vr.getElement() + " " + vr.getMessage());
141 		}
142 		assertEquals(3, validationResults.size());
143 
144 		System.out.println("test validation on dynamic attributes");
145 		
146 		info.getAttributes().add(new AttributeInfo("finalExamStatus", "123"));
147 		validationResults = val.validateObject(info, os, contextInfo);
148 		for (ValidationResultInfo vr : validationResults) {
149 			System.out.println(vr.getElement() + " " + vr.getMessage());
150 		}
151 		assertEquals(5, validationResults.size());
152 
153 		LoDisplayInfo loInfo = new LoDisplayInfo();
154 		LoCategoryInfo loCatInfo = new LoCategoryInfo();
155 		loInfo.setLoCategoryInfoList(Arrays.asList(loCatInfo));
156 		RichTextInfo rtInfo = new RichTextInfo();
157 		rtInfo.setPlain("The ability to use sensory cues to guide motor activity.  This ranges from sensory stimulation, through cue selection, to translation.");
158 		rtInfo.setFormatted(rtInfo.getPlain());
159 		loCatInfo.setDescr(rtInfo);
160 		info.setCourseSpecificLOs(Arrays.asList(loInfo));
161 		info.setRevenues(new ArrayList<CourseRevenueInfo>());
162 		validationResults = val.validateObject(info, os, contextInfo);
163 		for (ValidationResultInfo vr : validationResults) {
164 			System.out.println(vr.getElement() + " " + vr.getMessage());
165 		}
166 		assertTrue(rtInfo.getPlain().matches("[A-Za-z0-9.\\\\\\-;:&#34;,'&amp;%$#@!\t\n\r ]*"));
167 //courseSpecificLOs/0/loCategoryInfoList/0/name validation.required
168 //courseSpecificLOs/0/loInfo validation.required
169 ///descr validation.required
170 ///finalExamStatus validation.validCharsFailed
171 ///finalExamRationale validation.required
172 		assertEquals(7, validationResults.size());
173 
174 		
175 		// Test custom validation 
176         try {
177             info = new CourseDataGenerator().getCourseTestData();
178         } catch (Exception ex) {
179             throw new RuntimeException(ex);
180         }
181         
182         AffiliatedOrgInfo af1 = new AffiliatedOrgInfo();
183         af1.setOrgId("orgId");
184         af1.setPercentage(15l);
185 
186         AffiliatedOrgInfo af2 = new AffiliatedOrgInfo();
187         af2.setOrgId("orgId");
188         af2.setPercentage(15l);
189         
190         List<AffiliatedOrgInfo> afList = new ArrayList<AffiliatedOrgInfo>();
191         afList.add(af1);
192         afList.add(af2);
193         
194         CourseRevenueInfo cr = new CourseRevenueInfo();
195         cr.setFeeType("REVENUE");
196         cr.setAffiliatedOrgs(afList);
197                 
198 
199         AffiliatedOrgInfo af3 = new AffiliatedOrgInfo();
200         af3.setOrgId("orgId");
201         af3.setPercentage(55l);
202 
203         AffiliatedOrgInfo af4 = new AffiliatedOrgInfo();
204         af4.setOrgId("orgId");
205         af4.setPercentage(45l);
206         
207         List<AffiliatedOrgInfo> afList2 = new ArrayList<AffiliatedOrgInfo>();
208         afList2.add(af3);
209         afList2.add(af4);
210         
211         CourseRevenueInfo cr2 = new CourseRevenueInfo();
212         cr2.setFeeType("REVENUE");
213         cr2.setAffiliatedOrgs(afList2);
214         
215         List<CourseRevenueInfo> revenues = new ArrayList<CourseRevenueInfo>();
216         revenues.add(cr);
217         revenues.add(cr2);
218         
219         info.setRevenues(revenues);
220         
221         
222         CourseExpenditureInfo cei = new CourseExpenditureInfo();
223         cei.setAffiliatedOrgs(afList);
224         
225         List<ValidationResultInfo> validationResults1 = val.validateObject(info, os, contextInfo);
226         System.out.println("h3. With just a custom validations");
227 
228         assertEquals(2, validationResults1.size());
229         
230         for(ValidationResultInfo vr : validationResults1) {
231             System.out.println(vr.getElement());
232            assertTrue("/revenues".equals(vr.getElement()) || "/expenditure/affiliatedOrgs".equals(vr.getElement())
233                     ||"revenues/0/stateKey".equals(vr.getElement()) ||"revenues/0/typeKey".equals(vr.getElement())||"revenues/1/stateKey".equals(vr.getElement()) ||"revenues/1/typeKey".equals(vr.getElement()) ||"/stateKey".equals(vr.getElement()) ||"/typeKey".equals(vr.getElement()));
234         }
235 
236 	}
237 
238 	private Validator getActiveDatesValidator() {
239 		ActiveDatesValidator adv = new ActiveDatesValidator();
240 		MockProxyFactoryBean b = new MockProxyFactoryBean();
241 		Map<String,Object> methodReturnMap = new HashMap<String,Object>(); 
242 		AtpInfo atpInfo = new AtpInfo();
243 		atpInfo.setStartDate(new Date());
244 		atpInfo.setEndDate(new Date());
245 		methodReturnMap.put("getAtp", atpInfo);
246 		b.setMethodReturnMap(methodReturnMap);
247 		
248 		b.setInterfaceClass(AtpService.class);
249 		try {
250 			adv.setAtpService((AtpService) b.getObject());
251 		} catch (Exception e) {
252 			throw new RuntimeException(e);
253 		}
254 		
255 		return adv;
256 	}
257 	
258 }