1   package com.sri.emo.controller;
2   
3   import java.util.HashMap;
4   import com.jcorporate.expresso.core.controller.ExpressoResponse;
5   import com.jcorporate.expresso.services.test.ControllerTestFixture;
6   import com.jcorporate.expresso.services.test.TestSystemInitializer;
7   import com.sri.common.taglib.TreeNode;
8   import com.sri.emo.wizard.wizardgateway.ListWizards;
9   import com.sri.emo.wizard.wizardgateway.WizardGatewayController;
10  import com.sri.emo.dbobj.Node;
11  import com.sri.emo.test.DatabaseTestFixture;
12  import com.sri.emo.test.EmoTestSuite;
13  import junit.framework.TestCase;
14  import com.sri.emo.wizard.completion.model.CompletionWizardTestBean;
15  
16  /***
17   * @author Michael Rimov
18   *
19   */
20  public class TestTreeViewVisitor extends TestCase {
21      private DatabaseTestFixture testFixture = null;
22      private ControllerTestFixture controllerFixture = null;
23  
24      private final int ID_WITH_RELATIONS = 19;
25  
26      private final int SW_NEW_HOPE = 4;
27  
28      private final int OPEN_RANGE =  12;
29  
30      private ExpressoResponse dummyControllerResponse = null;
31  
32      protected void setUp() throws Exception {
33          super.setUp();
34  
35          testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
36              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
37          controllerFixture = new ControllerTestFixture();
38          //We invoke this because it's a non-changing function and we need
39          //a legit controllerrequest and response for Transitions to operate properly.
40  
41          dummyControllerResponse = controllerFixture.invokeFacade(WizardGatewayController.class, new HashMap(), ListWizards.STATE_NAME);
42          testFixture.setUp();
43  
44      }
45  
46      protected void tearDown() throws Exception {
47          testFixture.tearDown();
48          testFixture = null;
49          controllerFixture = null;
50          dummyControllerResponse = null;
51          super.tearDown();
52      }
53  
54  
55  
56      public void testTraverseModelWithNoChildRelationNodes() throws Exception {
57          Node root = new Node();
58          root.setField(Node.NODE_ID, SW_NEW_HOPE);
59          root.retrieve();
60  
61  
62          TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
63          TreeNode rootNode = factory.buildTree();
64  
65          assertEquals("Star Wars: A New Hope",rootNode.getLabel());
66          assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
67          assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
68  
69          //Now check all children nodes to make sure they were properly constructed
70          TreeNode children[] =  rootNode.getNested();
71          assertNotSame(children, TreeNode.NO_CHILDREN);
72          //Additional filler nodes.
73          assertEquals(CompletionWizardTestBean.NUMBER_OF_PARTS + 3, children.length);  
74  
75          //Length -- should be a non-filled node
76          TreeNode movieLength = children[3];
77          assertTrue( movieLength.getLabel().indexOf("Length") > -1);
78          assertEquals(movieLength.getSelectedStyle(), TreeViewVisitor.EMPTY_SLOT_SELECTED);
79          assertEquals(movieLength.getUnselectedStyle(), TreeViewVisitor.EMPTY_SLOT_UNSELECTED);
80  
81          assertEquals(0, movieLength.getNested().length);
82  
83          //Movie Rating: filled
84          TreeNode movieRating = children[4];
85          assertTrue( movieRating.getLabel().indexOf("Rating") > -1);
86          assertEquals(movieRating.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
87          assertEquals(movieRating.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
88  
89          assertEquals(1, movieRating.getNested().length);
90          TreeNode ratingValue = movieRating.getNested()[0];
91          assertTrue( ratingValue.getLabel().indexOf("PG") > -1);
92          assertEquals(ratingValue.getSelectedStyle(), TreeViewVisitor.SINGLE_SELECTED);
93          assertEquals(ratingValue.getUnselectedStyle(), TreeViewVisitor.SINGLE_UNSELECTED);
94  
95  
96          //User Review: Filled node
97          TreeNode userReview = children[5];
98          assertTrue( userReview.getLabel().indexOf("Reviews") > -1);
99          assertEquals(userReview.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
100         assertEquals(userReview.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
101 
102     }
103 
104     public void testCompletedNodeWithNoChildRelationNodesShouldBeMarkedComplete() throws Exception {
105         Node root = new Node();
106         root.setField(Node.NODE_ID, OPEN_RANGE);
107         root.retrieve();
108 
109 
110         TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
111         TreeNode rootNode = factory.buildTree();
112 
113         //The folder should be black because all attributes are filled out.
114         assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
115         assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
116 
117 
118         //Length -- should be a filled node now.
119         //First 3 are Title, Summary, Comment.
120         TreeNode movieLength = rootNode.getNested()[3];
121         assertTrue("Failed Test: Label was: " + movieLength.getLabel(), movieLength.getLabel().indexOf("Length") > -1);
122         assertEquals(movieLength.getSelectedStyle(), TreeViewVisitor.PARTIAL_SLOT_OPEN);
123         assertEquals(movieLength.getUnselectedStyle(), TreeViewVisitor.PARTIAL_SLOT_CLOSED);
124     }
125 
126     public void testTraverseModelWithRelatedNodes() throws Exception {
127         Node root = new Node();
128         root.setField(Node.NODE_ID, ID_WITH_RELATIONS);
129         root.retrieve();
130 
131         TreeViewFactory factory = new TreeViewFactory(root,dummyControllerResponse,3);
132         TreeNode rootNode = factory.buildTree();
133 
134         assertEquals("Star Wars DVD Pack",rootNode.getLabel());
135         assertEquals(TreeViewVisitor.PARTIAL_NODE_OPEN,rootNode.getSelectedStyle());
136         assertEquals(TreeViewVisitor.PARTIAL_NODE_CLOSED,rootNode.getUnselectedStyle());
137 
138         TreeNode children[] =  rootNode.getNested();
139         assertNotSame(children, TreeNode.NO_CHILDREN);
140 
141         //Parts plus additions.
142         assertEquals(1 + 3, children.length);
143         children = children[3].getNested();  //Add Summary, Comment, Title Attributes.
144 
145         //Make sure there are  folders
146         int childNodeCount = 0;
147         for (int i = 0; i < children.length; i++) {
148             if (TreeViewVisitor.PARTIAL_NODE_OPEN.equals(children[i].getSelectedStyle())) {
149                 childNodeCount++;
150             }
151         }
152 
153         //Make sure we found two children.
154         assertEquals(2,childNodeCount);
155 
156     }
157 
158 
159 
160 }