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