1 package org.kuali.student.common.messagebuilder.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.antlr.runtime.Token;
7 import org.antlr.runtime.tree.CommonTreeAdaptor;
8 import org.junit.Assert;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode;
12 import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionParser;
13 import org.kuali.student.common.messagebuilder.impl.BooleanOperators;
14 import org.kuali.student.common.messagebuilder.impl.SuccessMessageBuilder;
15 import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
16
17 public class SuccessMessageBuilderTest {
18 private final static CommonTreeAdaptor adapter = new CommonTreeAdaptor();
19 private SuccessMessageBuilder successMessageBuilder;
20
21 @Before
22 public void setUp() throws Exception {
23 BooleanOperators bo = new BooleanOperators();
24 successMessageBuilder = new SuccessMessageBuilder(bo);
25 }
26
27 @Test
28 public void testBuildSuccessMessageForSingleAndNode() throws Exception {
29
30 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
31 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
32 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
33
34 BooleanNode andNode = new BooleanNode(and);
35
36 BooleanNode aNode = new BooleanNode(a);
37 aNode.setParent(andNode);
38 aNode.setValue(Boolean.TRUE);
39 aNode.setNodeMessage("MATH101");
40
41 BooleanNode bNode = new BooleanNode(b);
42 bNode.setParent(andNode);
43 bNode.setValue(Boolean.TRUE);
44 bNode.setNodeMessage("MATH201");
45
46 andNode.addChild(aNode);
47 andNode.addChild(bNode);
48
49 String msg = successMessageBuilder.buildSuccessMessage(andNode);
50
51 Assert.assertEquals("MATH101 AND MATH201", msg);
52 }
53
54 @Test
55 public void testBuildSuccessMessageForSingleOr1Node() throws Exception {
56
57 Token and = adapter.createToken(BooleanFunctionParser.OR, "+");
58 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
59 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
60
61
62 BooleanNode andNode = new BooleanNode(and);
63
64 BooleanNode aNode = new BooleanNode(a);
65 aNode.setParent(andNode);
66 aNode.setValue(Boolean.TRUE);
67 aNode.setNodeMessage("MATH101");
68
69 BooleanNode bNode = new BooleanNode(b);
70 bNode.setParent(andNode);
71 bNode.setValue(Boolean.FALSE);
72 bNode.setNodeMessage("MATH201");
73
74 andNode.addChild(aNode);
75 andNode.addChild(bNode);
76
77 String msg = successMessageBuilder.buildSuccessMessage(andNode);
78
79 Assert.assertEquals("MATH101", msg);
80 }
81
82 @Test
83 public void testBuildSuccessMessageForSingleOr2Node() throws Exception {
84
85 Token and = adapter.createToken(BooleanFunctionParser.OR, "+");
86 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
87 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
88
89
90 BooleanNode andNode = new BooleanNode(and);
91
92 BooleanNode aNode = new BooleanNode(a);
93 aNode.setParent(andNode);
94 aNode.setValue(Boolean.TRUE);
95 aNode.setNodeMessage("MATH101");
96
97 BooleanNode bNode = new BooleanNode(b);
98 bNode.setParent(andNode);
99 bNode.setValue(Boolean.TRUE);
100 bNode.setNodeMessage("MATH201");
101
102 andNode.addChild(aNode);
103 andNode.addChild(bNode);
104
105 String msg = successMessageBuilder.buildSuccessMessage(andNode);
106
107 Assert.assertEquals("MATH101 OR MATH201", msg);
108 }
109
110 @Test
111 public void testBuildSuccessMessageForListOfNodes() throws Exception {
112
113 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
114 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
115 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
116 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
117 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
118
119
120 BooleanNode andNode = new BooleanNode(and);
121 BooleanNode orNode = new BooleanNode(or);
122 orNode.setParent(andNode);
123 orNode.setValue(Boolean.TRUE);
124
125 BooleanNode aNode = new BooleanNode(a);
126 aNode.setParent(andNode);
127 aNode.setValue(Boolean.TRUE);
128 aNode.setNodeMessage("MATH101");
129
130 BooleanNode bNode = new BooleanNode(b);
131 bNode.setParent(orNode);
132 bNode.setValue(Boolean.TRUE);
133 bNode.setNodeMessage("MATH201");
134
135 BooleanNode cNode = new BooleanNode(c);
136 cNode.setParent(orNode);
137 cNode.setValue(Boolean.TRUE);
138 cNode.setNodeMessage("MATH301");
139
140 orNode.addChild(bNode);
141 orNode.addChild(cNode);
142 andNode.addChild(aNode);
143 andNode.addChild(orNode);
144
145
146 List<BooleanNode> list = new ArrayList<BooleanNode>();
147 list.add(cNode);
148 list.add(bNode);
149 list.add(orNode);
150 list.add(aNode);
151 list.add(andNode);
152
153 successMessageBuilder.buildSuccessMessage(list);
154
155 Assert.assertEquals("MATH101 AND (MATH201 OR MATH301)", andNode.getNodeMessage());
156 }
157
158 @Test
159 public void testBuildSuccessMessageForListOfNodes_Complex() throws Exception {
160
161 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
162 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
163 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
164 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
165 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
166 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
167 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
168
169
170 BooleanNode andNode = new BooleanNode(and);
171
172 BooleanNode orNode1 = new BooleanNode(or1);
173 orNode1.setParent(andNode);
174 orNode1.setValue(Boolean.TRUE);
175
176 BooleanNode orNode2 = new BooleanNode(or2);
177 orNode2.setParent(andNode);
178 orNode2.setValue(Boolean.TRUE);
179
180 BooleanNode aNode = new BooleanNode(a);
181 aNode.setParent(orNode1);
182 aNode.setValue(Boolean.TRUE);
183 aNode.setNodeMessage("MATH101");
184
185 BooleanNode bNode = new BooleanNode(b);
186 bNode.setParent(orNode1);
187 bNode.setValue(Boolean.FALSE);
188 bNode.setNodeMessage("MATH201");
189
190 BooleanNode cNode = new BooleanNode(c);
191 cNode.setParent(orNode2);
192 cNode.setValue(Boolean.FALSE);
193 cNode.setNodeMessage("MATH301");
194
195 BooleanNode dNode = new BooleanNode(d);
196 dNode.setParent(orNode2);
197 dNode.setValue(Boolean.TRUE);
198 dNode.setNodeMessage("MATH401");
199
200 orNode1.addChild(aNode);
201 orNode1.addChild(bNode);
202 orNode2.addChild(cNode);
203 orNode2.addChild(dNode);
204 andNode.addChild(orNode1);
205 andNode.addChild(orNode2);
206
207
208 List<BooleanNode> list = new ArrayList<BooleanNode>();
209 list.add(aNode);
210 list.add(bNode);
211 list.add(cNode);
212 list.add(dNode);
213 list.add(orNode1);
214 list.add(orNode2);
215 list.add(andNode);
216
217 String successMsg = successMessageBuilder.buildSuccessMessage(list);
218 System.out.println("\n\nmsg="+successMsg);
219
220 Assert.assertEquals(successMsg, andNode.getNodeMessage());
221 Assert.assertEquals("MATH101 AND MATH401", successMsg);
222 }
223
224 @Test
225 public void testBuildSuccessMessageForListOfNodes_MultipleRootNodes() throws Exception {
226 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
227 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
228
229
230 BooleanNode orNode = new BooleanNode(or);
231
232 BooleanNode andNode = new BooleanNode(and);
233
234 List<BooleanNode> list = new ArrayList<BooleanNode>();
235 list.add(orNode);
236 list.add(andNode);
237
238 try {
239 successMessageBuilder.buildSuccessMessage(list);
240 Assert.fail("buildSuccessMessage should have failed since list has 2 root nodes");
241 } catch (MessageBuilderException e) {
242 Assert.assertTrue(e.getMessage() != null);
243 Assert.assertTrue(e.getMessage().contains("label=+"));
244 Assert.assertTrue(e.getMessage().contains("label=*"));
245 }
246 }
247 }