001 package org.kuali.student.common.messagebuilder.impl; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import org.antlr.runtime.Token; 007 import org.antlr.runtime.tree.CommonTreeAdaptor; 008 import org.junit.Assert; 009 import org.junit.Before; 010 import org.junit.Test; 011 import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode; 012 import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionParser; 013 import org.kuali.student.common.messagebuilder.impl.BooleanOperators; 014 import org.kuali.student.common.messagebuilder.impl.SuccessMessageBuilder; 015 import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException; 016 017 public class SuccessMessageBuilderTest { 018 private final static CommonTreeAdaptor adapter = new CommonTreeAdaptor(); 019 private SuccessMessageBuilder successMessageBuilder; 020 021 @Before 022 public void setUp() throws Exception { 023 BooleanOperators bo = new BooleanOperators(); 024 successMessageBuilder = new SuccessMessageBuilder(bo); 025 } 026 027 @Test 028 public void testBuildSuccessMessageForSingleAndNode() throws Exception { 029 // Rule: A AND B 030 Token and = adapter.createToken(BooleanFunctionParser.AND, "*"); 031 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A"); 032 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B"); 033 034 BooleanNode andNode = new BooleanNode(and); 035 036 BooleanNode aNode = new BooleanNode(a); 037 aNode.setParent(andNode); 038 aNode.setValue(Boolean.TRUE); 039 aNode.setNodeMessage("MATH101"); 040 041 BooleanNode bNode = new BooleanNode(b); 042 bNode.setParent(andNode); 043 bNode.setValue(Boolean.TRUE); 044 bNode.setNodeMessage("MATH201"); 045 046 andNode.addChild(aNode); // left node 047 andNode.addChild(bNode); // right node 048 049 String msg = successMessageBuilder.buildSuccessMessage(andNode); 050 051 Assert.assertEquals("MATH101 AND MATH201", msg); 052 } 053 054 @Test 055 public void testBuildSuccessMessageForSingleOr1Node() throws Exception { 056 // Rule: A OR B 057 Token and = adapter.createToken(BooleanFunctionParser.OR, "+"); 058 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A"); 059 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B"); 060 061 // root node 062 BooleanNode andNode = new BooleanNode(and); 063 064 BooleanNode aNode = new BooleanNode(a); 065 aNode.setParent(andNode); 066 aNode.setValue(Boolean.TRUE); 067 aNode.setNodeMessage("MATH101"); 068 069 BooleanNode bNode = new BooleanNode(b); 070 bNode.setParent(andNode); 071 bNode.setValue(Boolean.FALSE); 072 bNode.setNodeMessage("MATH201"); 073 074 andNode.addChild(aNode); // left node 075 andNode.addChild(bNode); // right node 076 077 String msg = successMessageBuilder.buildSuccessMessage(andNode); 078 079 Assert.assertEquals("MATH101", msg); 080 } 081 082 @Test 083 public void testBuildSuccessMessageForSingleOr2Node() throws Exception { 084 // Rule: A OR B 085 Token and = adapter.createToken(BooleanFunctionParser.OR, "+"); 086 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A"); 087 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B"); 088 089 // root node 090 BooleanNode andNode = new BooleanNode(and); 091 092 BooleanNode aNode = new BooleanNode(a); 093 aNode.setParent(andNode); 094 aNode.setValue(Boolean.TRUE); 095 aNode.setNodeMessage("MATH101"); 096 097 BooleanNode bNode = new BooleanNode(b); 098 bNode.setParent(andNode); 099 bNode.setValue(Boolean.TRUE); 100 bNode.setNodeMessage("MATH201"); 101 102 andNode.addChild(aNode); // left node 103 andNode.addChild(bNode); // right node 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 // Rule: A AND (B OR C) 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 // root node 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); // left node 141 orNode.addChild(cNode); // right node 142 andNode.addChild(aNode); // left node 143 andNode.addChild(orNode); // right node 144 145 // List of nodes order is important for building success message 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 // Rule: (A OR B) AND (C OR D) 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 // root node 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); // left node 201 orNode1.addChild(bNode); // right node 202 orNode2.addChild(cNode); // left node 203 orNode2.addChild(dNode); // right node 204 andNode.addChild(orNode1); // left node 205 andNode.addChild(orNode2); // right node 206 207 // List of nodes order is important for building success message 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 // root node1 230 BooleanNode orNode = new BooleanNode(or); 231 // root node1 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 }