1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service;
17
18 import org.junit.Test;
19 import org.kuali.rice.krad.UserSession;
20 import org.kuali.rice.krad.test.document.AccountRequestDocument;
21 import org.kuali.rice.krad.test.document.AccountRequestDocumentWithCyclicalReference;
22 import org.kuali.rice.krad.util.GlobalVariables;
23 import org.kuali.rice.krad.util.MessageMap;
24 import org.kuali.test.KRADTestCase;
25
26 import static org.junit.Assert.assertEquals;
27
28
29
30
31
32
33
34 public class DictionaryValidationServiceTest extends KRADTestCase {
35
36 public DictionaryValidationServiceTest() {
37 }
38
39 @Override
40 public void setUp() throws Exception {
41 super.setUp();
42 GlobalVariables.setMessageMap(new MessageMap());
43 GlobalVariables.setUserSession(new UserSession("quickstart"));
44 }
45
46 @Override
47 public void tearDown() throws Exception {
48 GlobalVariables.setMessageMap(new MessageMap());
49 GlobalVariables.setUserSession(null);
50 super.tearDown();
51 }
52
53
54
55
56
57
58 @Test public void testRecursiveValidation() throws Exception {
59 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
60
61 travelDocument.getDocumentHeader().setDocumentDescription("test document");
62 travelDocument.setReason1("reason1");
63 travelDocument.setReason2("reason2");
64 travelDocument.setRequester("requester");
65
66 GlobalVariables.setMessageMap(new MessageMap());
67 KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
68 MessageMap errorMap = GlobalVariables.getMessageMap();
69 int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
70
71
72 assertEquals("Number of errors found is incorrect", 2, recursiveZeroMessageMapSize);
73 }
74
75
76
77
78
79
80 @Test public void testRecursiveValidationMultiple() throws Exception {
81 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
82
83 travelDocument.getDocumentHeader().setDocumentDescription("test document");
84 travelDocument.setReason1("reason1");
85 travelDocument.setReason2("reason2");
86 travelDocument.setRequester("requester");
87
88 GlobalVariables.setMessageMap(new MessageMap());
89 KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 0, true);
90 MessageMap errorMap = GlobalVariables.getMessageMap();
91 int recursiveZeroMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
92
93 GlobalVariables.setMessageMap(new MessageMap());
94 KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(travelDocument, 5, true);
95 MessageMap errorMap2 = GlobalVariables.getMessageMap();
96 int recursiveFiveMessageMapSize = errorMap2.getNumberOfPropertiesWithErrors();
97
98 assertEquals("We should get the same number of errors no matter how deeply we recursively validate for this document", recursiveZeroMessageMapSize, recursiveFiveMessageMapSize);
99 }
100
101 @Test public void testRecursiveValidationParentChildLoop() throws Exception {
102 AccountRequestDocumentWithCyclicalReference doc1 = (AccountRequestDocumentWithCyclicalReference) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest3");
103
104 doc1.getDocumentHeader().setDocumentDescription("test document 1");
105 doc1.setReason1("reason1");
106 doc1.setReason2("reason2");
107 doc1.setRequester("requester");
108
109 AccountRequestDocumentWithCyclicalReference doc2 = (AccountRequestDocumentWithCyclicalReference) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest3");
110 doc2.getDocumentHeader().setDocumentDescription("test document 2");
111 doc2.setReason1("reason1a");
112 doc2.setReason2("reason2a");
113 doc2.setRequester("requester2");
114 doc2.setParent(doc1);
115 doc1.setChild(doc2);
116
117 GlobalVariables.setMessageMap(new MessageMap());
118 KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(doc1, 5, true);
119 MessageMap errorMap = GlobalVariables.getMessageMap();
120 int recursiveFiveMessageMapSize = errorMap.getNumberOfPropertiesWithErrors();
121
122 GlobalVariables.setMessageMap(new MessageMap());
123 KRADServiceLocatorWeb.getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(doc1, 10, true);
124 MessageMap errorMap2 = GlobalVariables.getMessageMap();
125 int recursiveTenMessageMapSize = errorMap2.getNumberOfPropertiesWithErrors();
126
127 assertEquals("We should get the same number of errors no matter how deeply we recursively validate for this document", recursiveFiveMessageMapSize, recursiveTenMessageMapSize);
128 }
129
130 }