1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.sys.document.validation;
20
21 import groovy.util.logging.Log;
22
23 import java.util.List;
24
25 import org.apache.log4j.Logger;
26 import org.kuali.kfs.sys.ConfigureContext;
27 import org.kuali.kfs.sys.KFSConstants;
28 import org.kuali.kfs.sys.KFSKeyConstants;
29 import org.kuali.kfs.sys.context.KualiTestBase;
30 import org.kuali.kfs.sys.context.SpringContext;
31 import org.kuali.rice.kew.api.exception.WorkflowException;
32 import org.kuali.rice.kns.document.MaintenanceDocument;
33 import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
34 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
35 import org.kuali.rice.kns.rules.MaintenanceDocumentRule;
36 import org.kuali.rice.kns.service.DictionaryValidationService;
37 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
38 import org.kuali.rice.krad.bo.PersistableBusinessObject;
39 import org.kuali.rice.krad.service.DocumentService;
40 import org.kuali.rice.krad.util.ErrorMessage;
41 import org.kuali.rice.krad.util.GlobalVariables;
42
43 @ConfigureContext
44 public abstract class MaintenanceRuleTestBase extends KualiTestBase {
45
46
47
48
49
50
51
52 protected MaintenanceDocument newMaintDoc(PersistableBusinessObject newBo) {
53 return newMaintDoc(null, newBo);
54 }
55
56
57
58
59
60
61
62
63
64 protected MaintenanceDocument newMaintDoc(PersistableBusinessObject oldBo, PersistableBusinessObject newBo) {
65
66
67 if (null == newBo) {
68 throw new IllegalArgumentException("Invalid value (null) for newBo. " + "This must always be a valid, populated BusinessObject instance.");
69 }
70
71
72 MaintenanceDocument document = null;
73 try {
74 document = (MaintenanceDocument) SpringContext.getBean(DocumentService.class).getNewDocument(SpringContext.getBean(MaintenanceDocumentDictionaryService.class).getDocumentTypeName(newBo.getClass()));
75 }
76 catch (WorkflowException e) {
77 throw new RuntimeException(e);
78 }
79
80
81 document.getDocumentHeader().setDocumentDescription("test");
82 if (null == oldBo) {
83 document.setOldMaintainableObject(new KualiMaintainableImpl());
84 }
85 else {
86 document.setOldMaintainableObject(new KualiMaintainableImpl(oldBo));
87 document.getOldMaintainableObject().setBoClass(oldBo.getClass());
88 }
89 document.setNewMaintainableObject(new KualiMaintainableImpl(newBo));
90 document.getNewMaintainableObject().setBoClass(newBo.getClass());
91 return document;
92 }
93
94
95
96
97
98
99
100
101
102 protected MaintenanceDocumentRule setupMaintDocRule(PersistableBusinessObject newBo, Class ruleClass) {
103 MaintenanceDocument maintDoc = newMaintDoc(newBo);
104 return setupMaintDocRule(maintDoc, ruleClass);
105 }
106
107
108
109
110
111
112
113
114
115
116
117 protected MaintenanceDocumentRule setupMaintDocRule(PersistableBusinessObject oldBo, PersistableBusinessObject newBo, Class ruleClass) {
118
119 MaintenanceDocument maintDoc = newMaintDoc(oldBo, newBo);
120
121 return setupMaintDocRule(maintDoc, ruleClass);
122 }
123
124
125
126
127
128
129
130
131
132 protected MaintenanceDocumentRule setupMaintDocRule(MaintenanceDocument maintDoc, Class ruleClass) {
133
134 MaintenanceDocumentRule rule;
135 try {
136 rule = (MaintenanceDocumentRule) ruleClass.newInstance();
137 }
138 catch (InstantiationException e) {
139 throw new RuntimeException(e);
140 }
141 catch (IllegalAccessException e) {
142 throw new RuntimeException(e);
143 }
144
145 rule.setupBaseConvenienceObjects(maintDoc);
146
147
148 assertEquals(0, GlobalVariables.getMessageMap().getNumberOfPropertiesWithErrors());
149
150 return rule;
151 }
152
153 protected void testDefaultExistenceCheck(PersistableBusinessObject bo, String fieldName, boolean shouldFail) {
154
155
156 GlobalVariables.getMessageMap().addToErrorPath("document.newMaintainableObject");
157
158
159 SpringContext.getBean(DictionaryValidationService.class).validateDefaultExistenceChecks(bo);
160
161
162 GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
163
164
165 assertFieldErrorExistence(fieldName, KFSKeyConstants.ERROR_EXISTENCE, shouldFail);
166
167 }
168
169
170
171
172
173
174
175 protected void assertErrorCount(int expectedErrorCount) {
176 assertEquals(expectedErrorCount, GlobalVariables.getMessageMap().getErrorCount());
177 }
178
179
180
181
182
183
184
185
186 protected boolean doesFieldErrorExist(String fieldName, String errorKey) {
187 return GlobalVariables.getMessageMap().fieldHasMessage(MaintenanceDocumentRuleBase.MAINTAINABLE_ERROR_PREFIX + fieldName, errorKey);
188 }
189
190
191
192
193
194
195
196
197
198 protected void assertFieldErrorExistence(String fieldName, String errorKey, boolean expectedResult) {
199 boolean result = doesFieldErrorExist(fieldName, errorKey);
200 assertEquals("Existence check for Error on fieldName/errorKey: " + fieldName + "/" + errorKey + ". " + GlobalVariables.getMessageMap(), expectedResult, result);
201 }
202
203
204
205
206
207
208
209
210
211 protected void assertFieldErrorDoesNotExist(String fieldName, String errorKey) {
212 boolean result = doesFieldErrorExist(fieldName, errorKey);
213 assertTrue("FieldName (" + fieldName + ") should NOT contain errorKey: " + errorKey, !result);
214 }
215
216
217
218
219
220
221
222
223
224 protected void assertFieldErrorExists(String fieldName, String errorKey) {
225 boolean result = GlobalVariables.getMessageMap().fieldHasMessage(MaintenanceDocumentRuleBase.MAINTAINABLE_ERROR_PREFIX + fieldName, errorKey);
226 if ( !result ) {
227 Logger.getLogger(getClass()).info("Messages in MessageMap: " + GlobalVariables.getMessageMap());
228 }
229 assertTrue("FieldName (" + fieldName + ") should contain errorKey: " + errorKey, result);
230 }
231
232
233
234
235
236
237
238 protected void assertGlobalErrorExists(String errorKey) {
239 boolean result = GlobalVariables.getMessageMap().fieldHasMessage(KFSConstants.DOCUMENT_ERRORS, errorKey);
240 assertTrue("Document should contain errorKey: " + errorKey, result);
241 }
242 }