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