View Javadoc

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