1   package com.sri.emo.controller;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import com.jcorporate.expresso.core.controller.ControllerException;
6   import com.jcorporate.expresso.core.controller.ExpressoResponse;
7   import com.jcorporate.expresso.core.controller.Input;
8   import com.jcorporate.expresso.core.controller.NonHandleableException;
9   import com.jcorporate.expresso.core.controller.Transition;
10  import com.jcorporate.expresso.core.controller.session.PersistentSession;
11  import com.jcorporate.expresso.core.db.DBException;
12  import com.jcorporate.expresso.services.test.ControllerTestFixture;
13  import com.jcorporate.expresso.services.test.TestSystemInitializer;
14  import com.sri.emo.dbobj.Node;
15  import com.sri.emo.test.DatabaseTestFixture;
16  import com.sri.emo.test.EmoTestSuite;
17  import com.sri.emo.test.EmoWizardDatabaseTestCase;
18  import com.sri.emo.wizard.Wizard;
19  import com.sri.emo.wizard.WizardPage;
20  import com.sri.emo.wizard.expressoimpl.WizardController;
21  import com.sri.emo.wizard.selection.WizardAction;
22  import com.sri.emo.wizard.selection.DisplaySummaryPage;
23  
24  /***
25   * @author Michael Rimov
26   */
27  public class TestWizardAction extends EmoWizardDatabaseTestCase {
28      private DatabaseTestFixture databaseTestFixture;
29      private ControllerTestFixture controllerTestFixture;
30  
31      protected void setUp() throws Exception {
32          super.setUp();
33  
34          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
35                  EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
36  
37          databaseTestFixture.setUp();
38          controllerTestFixture = new ControllerTestFixture();
39          controllerTestFixture.setUp();
40  
41      }
42  
43      protected void tearDown() throws Exception {
44          databaseTestFixture.tearDown();
45          controllerTestFixture.tearDown();
46          controllerTestFixture = null;
47          databaseTestFixture = null;
48          super.tearDown();
49      }
50  
51      protected ExpressoResponse runThroughWizardUpToSummaryPage(final int wizId, final String[] pageParams)
52              throws DBException, ControllerException, NonHandleableException {
53  
54  
55          Map params = new HashMap();
56          params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(wizId));
57          ExpressoResponse response = controllerTestFixture.invokeFacade(WizardAction.class
58                  , params, WizardController.STATE_BEGIN);
59  
60          assertTrue(response != null);
61          assertTrue(response.getErrors() == null
62                  || response.getErrors().getErrorCount() == 0);
63  
64          Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
65  
66          for (int i = 0; i < pageParams.length; i++) {
67              WizardPage page = wiz.getCurrentPage();
68              params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(wizId));
69              params.put(WizardController.WIZ_DATA_ID, pageParams[i]);
70              params.put(WizardController.WIZ_PAGE_PARAMETER, page.getId().toString());
71              response = controllerTestFixture.invokeFacade(WizardAction.class
72                      , params, WizardController.NEXT_STATE);
73  
74              assertTrue(response != null);
75              assertTrue(response.getErrors() == null
76                      || response.getErrors().getErrorCount() == 0);
77              assertEquals(wiz,
78                      (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID));
79  
80          }
81  
82  //        WizardPage page = wiz.getCurrentPage();
83  //        params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(wizId));
84  //        params.put(WizardController.WIZ_PAGE_PARAMETER, page.getId().toString());
85  //        params.put(WizardController.WIZ_DATA_ID, pageParams[pageParams.length - 1]);
86  //        response = controllerTestFixture.invokeFacade(WizardAction.class
87  //                , params, WizardController.FINISH_STATE);
88  
89          assertTrue(response != null);
90          assertTrue(response.getErrors() == null
91                  || response.getErrors().getErrorCount() == 0);
92  
93  
94          return response;
95      }
96  
97      protected Wizard getStoredWizard() {
98          return (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
99      }
100 
101     public void testWizardWithFoundNode() throws DBException, ControllerException, NonHandleableException {
102         ExpressoResponse response = runThroughWizardUpToSummaryPage(DatabaseTestFixture.DEFAULT_WIZ_ID,
103                 new String[]{"", "11", "17"});
104 
105         assertNotNull(response);
106         WizardPage currentPage = getStoredWizard().getCurrentPage();
107         assertTrue(currentPage instanceof DisplaySummaryPage);
108 
109         Node node = ((DisplaySummaryPage)currentPage).getSelectedNode();
110         assertTrue(node != null);
111         assertEquals(3, node.getFieldInt(Node.NODE_ID));
112         assertEquals("Mary Poppins", node.getNodeTitle());
113 
114     }
115 
116     public void testWizardWithNoNode() throws DBException, ControllerException, NonHandleableException {
117         ExpressoResponse response = runThroughWizardUpToSummaryPage(DatabaseTestFixture.DEFAULT_WIZ_ID,
118                 new String[]{"", "13", "17"});
119 
120         PersistentSession session = response.getExpressoRequest().getSession();
121         assertNotNull(session);
122 
123         WizardPage currentPage = getStoredWizard().getCurrentPage();
124         assertTrue(currentPage instanceof DisplaySummaryPage);
125 
126         Node n = ((DisplaySummaryPage)currentPage).getSelectedNode();
127         assertTrue(n == null);
128     }
129 
130     public void testWizardForwardBackForwardBack() throws Exception {
131         Map params = new HashMap();
132         params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
133         ExpressoResponse response = controllerTestFixture.invokeFacade(WizardAction.class
134                 , params, WizardController.STATE_BEGIN);
135 
136         assertTrue(response != null);
137         assertTrue(response.getErrors() == null
138                 || response.getErrors().getErrorCount() == 0);
139 
140         Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
141         assertEquals(21, ((Integer) wiz.getCurrentPage().getId()).intValue());
142         response = response.getTransition("Next").executeTransition(response.getExpressoRequest(), response, true);
143         wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
144         assertEquals(22, ((Integer) wiz.getCurrentPage().getId()).intValue());
145 
146         response = response.getTransition("Back").executeTransition(response.getExpressoRequest(), response, true);
147         wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
148         assertEquals(21, ((Integer) wiz.getCurrentPage().getId()).intValue());
149 
150         response = response.getTransition("Next").executeTransition(response.getExpressoRequest(), response, true);
151         wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
152         assertEquals(22, ((Integer) wiz.getCurrentPage().getId()).intValue());
153     }
154 
155     public void testCloneTemplate() throws Exception {
156         ExpressoResponse response = runThroughWizardUpToSummaryPage(DatabaseTestFixture.DEFAULT_WIZ_ID,
157                 new String[]{"", "11", "17"});
158 
159         Node n = new Node();
160         n.setField(Node.NODE_TITLE, "This is a test Template");
161         assertFalse(n.find());
162 
163         Transition t = response.getTransition("Next");
164         response = t.executeTransition(response.getExpressoRequest(), response, true);
165         assertTrue("Cannot have errors here.",
166             response.getErrors() == null || response.getErrors().getErrorCount() == 0);
167 
168         Input i = response.getInput(WizardController.WIZ_DATA_ID);
169         assertTrue(i != null);
170         assertEquals("", i.getDefaultValue());
171         t = response.getTransition("Finish");
172         assertTrue(t != null);
173         assertEquals(WizardAction.FINISH_STATE, t.getState());
174 
175         t.addParam(WizardController.WIZ_DATA_ID, "This is a test Template");
176         response = t.executeTransition(response.getExpressoRequest(), response, true);
177         assertTrue("Cannot have errors here.",
178             response.getErrors() == null || response.getErrors().getErrorCount() == 0);
179 
180         assertTrue(n.find());
181     }
182 
183 }