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.SuccessFailureMessageBuilder;
15 import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
16
17 public class SuccessFailureMessageBuilderTest {
18 private final static CommonTreeAdaptor adapter = new CommonTreeAdaptor();
19 private SuccessFailureMessageBuilder successFailureMessageBuilder;
20
21 @Before
22 public void setUp() throws Exception {
23 BooleanOperators bo = new BooleanOperators();
24 successFailureMessageBuilder = new SuccessFailureMessageBuilder(bo);
25 }
26
27 @Test
28 public void testBuildFailureMessage() throws Exception {
29
30 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
31 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
32 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
33 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
34 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
35 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
36 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
37
38
39 BooleanNode andNode = new BooleanNode(and);
40
41 BooleanNode orNode1 = new BooleanNode(or1);
42 orNode1.setParent(andNode);
43 orNode1.setValue(Boolean.TRUE);
44
45 BooleanNode orNode2 = new BooleanNode(or2);
46 orNode2.setParent(andNode);
47 orNode2.setValue(Boolean.TRUE);
48
49 BooleanNode aNode = new BooleanNode(a);
50 aNode.setParent(orNode1);
51 aNode.setValue(Boolean.TRUE);
52 aNode.setNodeMessage("MATH101");
53
54 BooleanNode bNode = new BooleanNode(b);
55 bNode.setParent(orNode1);
56 bNode.setValue(Boolean.FALSE);
57 bNode.setNodeMessage("MATH201");
58
59 BooleanNode cNode = new BooleanNode(c);
60 cNode.setParent(orNode2);
61 cNode.setValue(Boolean.FALSE);
62 cNode.setNodeMessage("MATH301");
63
64 BooleanNode dNode = new BooleanNode(d);
65 dNode.setParent(orNode2);
66 dNode.setValue(Boolean.TRUE);
67 dNode.setNodeMessage("MATH401");
68
69 orNode1.addChild(aNode);
70 orNode1.addChild(bNode);
71 orNode2.addChild(cNode);
72 orNode2.addChild(dNode);
73 andNode.addChild(orNode1);
74 andNode.addChild(orNode2);
75
76
77 List<BooleanNode> list = new ArrayList<BooleanNode>();
78 list.add(aNode);
79 list.add(bNode);
80 list.add(cNode);
81 list.add(dNode);
82 list.add(orNode1);
83 list.add(orNode2);
84 list.add(andNode);
85
86 String failureMsg = successFailureMessageBuilder.buildMessage(list);
87
88 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
89 Assert.assertEquals("MATH101 AND MATH401", failureMsg);
90 }
91
92 @Test
93 public void testBuildSuccessMessageForListOfNodes_Complex() throws Exception {
94
95 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
96 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
97 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
98 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
99 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
100 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
101 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
102
103
104 BooleanNode andNode = new BooleanNode(and);
105
106 BooleanNode orNode1 = new BooleanNode(or1);
107 orNode1.setParent(andNode);
108 orNode1.setValue(Boolean.TRUE);
109
110 BooleanNode orNode2 = new BooleanNode(or2);
111 orNode2.setParent(andNode);
112 orNode2.setValue(Boolean.TRUE);
113
114 BooleanNode aNode = new BooleanNode(a);
115 aNode.setParent(orNode1);
116 aNode.setValue(Boolean.TRUE);
117 aNode.setNodeMessage("MATH101");
118
119 BooleanNode bNode = new BooleanNode(b);
120 bNode.setParent(orNode1);
121 bNode.setValue(Boolean.FALSE);
122 bNode.setNodeMessage("MATH201");
123
124 BooleanNode cNode = new BooleanNode(c);
125 cNode.setParent(orNode2);
126 cNode.setValue(Boolean.FALSE);
127 cNode.setNodeMessage("MATH301");
128
129 BooleanNode dNode = new BooleanNode(d);
130 dNode.setParent(orNode2);
131 dNode.setValue(Boolean.TRUE);
132 dNode.setNodeMessage("MATH401");
133
134 orNode1.addChild(aNode);
135 orNode1.addChild(bNode);
136 orNode2.addChild(cNode);
137 orNode2.addChild(dNode);
138 andNode.addChild(orNode1);
139 andNode.addChild(orNode2);
140
141
142 List<BooleanNode> list = new ArrayList<BooleanNode>();
143 list.add(aNode);
144 list.add(bNode);
145 list.add(cNode);
146 list.add(dNode);
147 list.add(orNode1);
148 list.add(orNode2);
149 list.add(andNode);
150
151 String failureMsg = successFailureMessageBuilder.buildMessage(list);
152
153 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
154 Assert.assertEquals("MATH101 AND MATH401", failureMsg);
155 }
156
157 @Test
158 public void testBuildSuccessMessageForListOfNodes_MultipleRootNodes() throws Exception {
159 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
160 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
161
162
163 BooleanNode orNode = new BooleanNode(or);
164
165 BooleanNode andNode = new BooleanNode(and);
166
167 List<BooleanNode> list = new ArrayList<BooleanNode>();
168 list.add(orNode);
169 list.add(andNode);
170
171 try {
172 successFailureMessageBuilder.buildMessage(list);
173 Assert.fail("buildMessage should have failed since list has 2 root nodes");
174 } catch (MessageBuilderException e) {
175 Assert.assertTrue(e.getMessage() != null);
176 Assert.assertTrue(e.getMessage().contains("label=+"));
177 Assert.assertTrue(e.getMessage().contains("label=*"));
178 }
179 }
180 }