1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.selection.management;
11  
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  import com.jcorporate.expresso.core.controller.Block;
17  import com.jcorporate.expresso.core.controller.ControllerException;
18  import com.jcorporate.expresso.core.controller.ExpressoResponse;
19  import com.jcorporate.expresso.core.controller.Input;
20  import com.jcorporate.expresso.core.controller.NonHandleableException;
21  import com.jcorporate.expresso.core.controller.Output;
22  import com.jcorporate.expresso.core.controller.Transition;
23  import com.jcorporate.expresso.core.db.DBException;
24  import com.jcorporate.expresso.services.test.ControllerTestFixture;
25  import com.jcorporate.expresso.services.test.TestSystemInitializer;
26  import com.sri.emo.controller.SelectionWizardManager;
27  import com.sri.emo.dbobj.WizDefinition;
28  import com.sri.emo.dbobj.WizStep;
29  import com.sri.emo.test.DatabaseTestFixture;
30  import com.sri.emo.test.EmoTestSuite;
31  import com.sri.emo.wizard.expressoimpl.WizardController;
32  import junit.framework.TestCase;
33  
34  public class TestWizardStepController extends TestCase {
35      private ControllerTestFixture testFixture = null;
36      private DatabaseTestFixture dbTestFixture = null;
37  
38      protected void setUp() throws Exception {
39          super.setUp();
40          testFixture = new ControllerTestFixture();
41          testFixture.setUp();
42          dbTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(), EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
43          dbTestFixture.setUp();
44      }
45  
46      protected void tearDown() throws Exception {
47  
48          dbTestFixture.tearDown();
49          dbTestFixture = null;
50  
51          testFixture.tearDown();
52          testFixture = null;
53          super.tearDown();
54      }
55  
56      /***
57       * Checks that there are no errors defined in the error collection.
58       *
59       * @param response The ExpressoResponse object.
60       * @throws ControllerException
61       */
62      protected static void checkNoErrorsInErrorCollection(ExpressoResponse response) throws
63              ControllerException {
64          assertTrue(response.getErrors() == null
65                  || response.getErrors().getErrorCount() == 0);
66      }
67  
68  
69      public void testPromptTextEntryInputs() throws ControllerException, NonHandleableException, DBException {
70  
71          String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
72          Map params = new HashMap();
73          params.put(WizStep.FLD_WIZID, wizId);
74  
75          ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params, PromptTextEntry.STATE_NAME);
76  
77          assertTrue(response != null);
78          checkNoErrorsInErrorCollection(response);
79          assertTrue(response.getBlock("wizstep") != null);
80  
81          Block step = response.getBlock("wizstep");
82  
83          //Check default values
84          Input title = step.getInput(WizStep.FLD_TITLE);
85          assertTrue(title != null);
86  
87      }
88  
89      public void testPromptTextEntryTransitions() throws ControllerException, NonHandleableException, DBException {
90          String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
91          Map params = new HashMap();
92          params.put(WizStep.FLD_WIZID, wizId);
93  
94          ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
95                  PromptTextEntry.STATE_NAME);
96  
97          assertTrue(response != null);
98          checkNoErrorsInErrorCollection(response);
99  
100         Transition cancel = response.getTransition("cancel");
101         assertTrue(cancel != null);
102         assertEquals(SelectionWizardManager.class.getName(), cancel.getControllerObject());
103         assertEquals(SelectionWizardManager.STATE_PROMPT_EDIT, cancel.getState());
104         assertEquals(cancel.getParam(WizardController.WIZ_PARAMETER_ID), wizId);
105 
106         Transition save = response.getTransition("save");
107         assertTrue(save != null);
108         assertEquals(WizardStepController.class.getName(), save.getControllerObject());
109         assertEquals(WizardStepController.STATE_DO_ADD, save.getState());
110         assertEquals(wizId, save.getParam(WizStep.FLD_WIZID));
111     }
112 
113 
114     /***
115      * @throws ControllerException    upon error
116      * @throws NonHandleableException upon fatal error
117      * @throws DBException            upon database error.
118      */
119     public void testRunDeleteState() throws ControllerException, NonHandleableException, DBException {
120 
121         String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
122         WizStep oneStep = new WizStep();
123         oneStep.setField(WizStep.FLD_WIZID, wizId);
124         int numberSteps = oneStep.count();
125 
126         Map params = new HashMap();
127         params.put(WizStep.FLD_ID, wizId);
128         WizDefinition wizard = new WizDefinition();
129         wizard.setId(wizId);
130         wizard.retrieve();
131 
132         WizStep deleteStep = (WizStep)wizard.getPageDefinitions().get(0);
133         params.put("StepId", deleteStep.getId());
134 
135         ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class,  params,
136                 WizardStepController.STATE_DO_DELETE);
137         assertTrue(response != null);
138         checkNoErrorsInErrorCollection(response);
139 
140         //Check for the correct number of remaining steps.
141         int afterNumberSteps = oneStep.count();
142         assertEquals((numberSteps - 1), afterNumberSteps);
143 
144         //
145         //Check to make sure that our deleted id no longer exists in the
146         //defined steps.
147         //
148         for (Iterator i = wizard.getPageDefinitions().iterator();
149              i.hasNext();) {
150             WizStep aStep = (WizStep) i.next();
151             assertNotSame(deleteStep.getId(), aStep.getId());
152         }
153 
154     }
155 
156     /***
157      * @throws ControllerException    upon error
158      * @throws NonHandleableException upon fatal error
159      * @throws DBException            upon database error.
160      */
161     public void testRunOrderDownState() throws ControllerException, NonHandleableException, DBException {
162         WizDefinition wizard = new WizDefinition();
163         wizard.setId(DatabaseTestFixture.DEFAULT_WIZ_ID);
164         wizard.retrieve();
165 
166         List allSteps = wizard.getPageDefinitions();
167         int beforeIds[] = new int[allSteps.size()];
168         for (int i = 0; i < allSteps.size(); i++) {
169             WizStep oneStep = (WizStep) allSteps.get(i);
170             beforeIds[i] = oneStep.getFieldInt(WizStep.FLD_ID);
171         }
172 
173         int expectedAfterIds[] = new int[allSteps.size()];
174 
175         for (int i = 0; i < allSteps.size(); i++) {
176             if (i == 0) {
177                 expectedAfterIds[0] = beforeIds[1];
178                 expectedAfterIds[1] = beforeIds[0];
179                 i++;
180                 continue;
181             }
182 
183             expectedAfterIds[i] = beforeIds[i];
184         }
185 
186         Map params = new HashMap();
187 
188         WizStep moveStep = (WizStep) wizard.getPageDefinitions().get(0);
189         params.put(WizStep.FLD_ID, moveStep.getId());
190 
191         ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class,  params,
192                 WizardStepController.STATE_ORDERDOWN);
193 
194         assertTrue(response != null);
195         checkNoErrorsInErrorCollection(response);
196 
197         //Gotta have the same number as before
198         assertEquals(allSteps.size(),
199                 wizard.getPageDefinitions().size());
200 
201         //Now check for proper ordering.
202         List finalSteps = wizard.getPageDefinitions();
203         for (int i = 0; i < allSteps.size(); i++) {
204             WizStep oneStep = (WizStep) finalSteps.get(i);
205             assertEquals(expectedAfterIds[i],
206                     oneStep.getFieldInt(WizStep.FLD_ID));
207         }
208 
209     }
210 
211 
212     /***
213      * This is nearly identical to order down, EXCEPT that we order up step #1,
214      * which has the same effect as order down of step #0
215      *
216      * @throws ControllerException    upon error
217      * @throws NonHandleableException upon fatal error
218      * @throws DBException            upon database error.
219      */
220     public void testRunOrderUpState() throws ControllerException, NonHandleableException, DBException {
221         WizDefinition wizard = new WizDefinition();
222         wizard.setId(DatabaseTestFixture.DEFAULT_WIZ_ID);
223         wizard.retrieve();
224 
225         List allSteps = wizard.getPageDefinitions();
226         int beforeIds[] = new int[allSteps.size()];
227         for (int i = 0; i < allSteps.size(); i++) {
228             WizStep oneStep = (WizStep) allSteps.get(i);
229             beforeIds[i] = oneStep.getFieldInt(WizStep.FLD_ID);
230         }
231 
232         int expectedAfterIds[] = new int[allSteps.size()];
233 
234         for (int i = 0; i < allSteps.size(); i++) {
235             if (i == 0) {
236                 expectedAfterIds[0] = beforeIds[1];
237                 expectedAfterIds[1] = beforeIds[0];
238                 i++;
239                 continue;
240             }
241 
242             expectedAfterIds[i] = beforeIds[i];
243         }
244 
245         final String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
246         Map params = new HashMap();
247         params.put(WizardController.WIZ_PARAMETER_ID, wizId);
248 
249         WizStep moveStep = (WizStep) wizard.getPageDefinitions().get(1); //Only difference
250         params.put("StepId", moveStep.getId());
251 
252         ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class,  params,
253                 WizardStepController.STATE_ORDERUP);
254 
255         assertTrue(response != null);
256         checkNoErrorsInErrorCollection(response);
257 
258         //Gotta have the same number as before
259         assertEquals(allSteps.size(),
260                 wizard.getPageDefinitions().size());
261 
262         //Now check for proper ordering.
263         List finalSteps = wizard.getPageDefinitions();
264         for (int i = 0; i < allSteps.size(); i++) {
265             WizStep oneStep = (WizStep) finalSteps.get(i);
266             assertEquals(expectedAfterIds[i],
267                     oneStep.getFieldInt(WizStep.FLD_ID));
268         }
269     }
270 
271 
272 //    public void testRunSelectModelState() throws Exception {
273 //        DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
274 //            EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
275 //
276 //        dtf.setUp();
277 //        try {
278 //            Map params = new HashMap();
279 //            params.put(WizardController.WIZ_PARAMETER_ID, "12");
280 //            params.put(WizardStepController.PARAM_STEPID,"23");
281 //            params.put("DefaultModel","-2");
282 //            params.put("NextStep", WizardStepController.STATE_PROMPT_ADD);
283 //
284 //            ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class,
285 //                User.getAdminId(TestSystemInitializer.
286 //                getTestContext()), params,
287 //                WizardStepController.STATE_PROMPT_SELECT_MODEL);
288 //
289 //            checkNoErrorsInErrorCollection(response);
290 //
291 //            Input input = response.getInput(WizardStepController.PARAM_MODEL_ID);
292 //            assertTrue(input != null);
293 //            assertTrue(input.getValidValues().size() > 0);
294 //            Vector vec = input.getValidValues();
295 //            ValidValue vv = (ValidValue) vec.get(0);
296 //            assertTrue(vv.getKey().equals("-2"));
297 //
298 //            vv = (ValidValue) vec.get(1);
299 //            assertTrue(vv.getKey().equals("-1"));
300 //
301 //            for (int i = 2; i < vec.size(); i++) {
302 //                vv = (ValidValue) vec.get(i);
303 //                String nodeTypeId = vv.getDescription();
304 //                NodeType nt = new NodeType();
305 //                nt.setId(vv.getKey());
306 //
307 //                try {
308 //                    nt.retrieve();
309 //                } catch (DBException ex) {
310 //                    fail("Unable to find nodetype of: " + vv.getKey());
311 //                }
312 //
313 //                assertEquals(nt.getField(NodeType.DISPLAY_TITLE), vv.getDescription());
314 //            }
315 //        } finally {
316 //            dtf.tearDown();
317 //        }
318 //    }
319 
320     public void testRunPromptDeleteState() throws Exception {
321         DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
322                 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
323 
324         dtf.setUp();
325         try {
326             WizStep ws = new WizStep();
327             ws.setId(DatabaseTestFixture.DEFAULT_WELCOME_STEP);
328             ws.retrieve();
329 
330             Map params = new HashMap();
331             params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
332             params.put(WizStep.FLD_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WELCOME_STEP));
333 
334             ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
335                     WizardStepController.STATE_PROMPT_DELETE);
336 
337             checkNoErrorsInErrorCollection(response);
338 
339             Output o = response.getOutput("prompt");
340             assertEquals(ws.getTitle(), o.getContent());
341 
342             Transition yes = response.getTransition("yes");
343             assertTrue(yes != null);
344             assertEquals(ws.getId(), yes.getParam(WizStep.FLD_ID));
345             assertEquals(WizardStepController.STATE_DO_DELETE, yes.getState());
346 
347             Transition no = response.getTransition("no");
348             assertTrue(no != null);
349             assertEquals(SelectionWizardManager.STATE_PROMPT_EDIT, no.getState());
350             assertEquals(ws.getWizId(), no.getParam(WizardController.WIZ_PARAMETER_ID));
351         } finally {
352             dtf.tearDown();
353         }
354 
355     }
356 
357     /***
358      * @todo finish me
359      */
360     public void testRunAddCompleteState() throws Exception {
361 //        DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
362 //            EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
363 //
364 //        dtf.setUp();
365 //        try {
366 //            WizStep ws = new WizStep();
367 //            ws.setField(WizStep.FLD_WIZID, 12);
368 //            int numSteps = ws.count();
369 //            final String TITLE = "This is a new Step";
370 //            final String DIRECTIVE = "Enter some text";
371 //            final String HELPTEXT = "This is some helpful text";
372 //            Map params = new HashMap();
373 //            params.put(WizardController.WIZ_PARAMETER_ID, "12");
374 //            params.put("Title", TITLE);
375 //            params.put("Directive",DIRECTIVE);
376 //            params.put("HelpText", HELPTEXT);
377 //            params.put(WizardStepController.PARAM_MODEL_ID, "-2");
378 //
379 //            ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class,
380 //                User.getAdminId(TestSystemInitializer.
381 //                getTestContext()), params,
382 //                WizardStepController.STATE_DO_ADD);
383 //
384 //            checkNoErrorsInErrorCollection(response);
385 //
386 //            //Check added parameters
387 //            assertEquals( (numSteps + 1), ws.count());
388 //            ws.setField(WizStep.FLD_TITLE, TITLE);
389 //            ws.setField(WizStep.FLD_DIRECTIVE, DIRECTIVE);
390 //            ws.setField(WizStep.FLD_HELPTEXT,HELPTEXT);
391 //            ws.setField(WizStep.FLD_MODEL,"-2");
392 //            assertTrue(ws.find());
393 //        } finally {
394 //            dtf.tearDown();
395 //        }
396 //
397     }
398 
399     /***
400      * @todo finish me
401      */
402     public void testRunEditState() throws Exception {
403         DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
404                 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
405 
406         dtf.setUp();
407         try {
408             Map params = new HashMap();
409             params.put(WizStep.FLD_WIZID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
410             params.put(WizStep.FLD_ID, Integer.toString(DatabaseTestFixture.DEFAULT_USER_RATING_STEP));
411 
412             ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
413                     WizardStepController.STATE_PROMPT_EDIT);
414 
415             checkNoErrorsInErrorCollection(response);
416 
417             Block b = response.getBlock("wizstep");
418             assertTrue(b != null);
419 
420 
421             Input i = b.getInput("Title");
422             assertTrue(i != null);
423             assertEquals("User Rating", i.getDefaultValue());
424             assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
425 
426             i = b.getInput("Directive");
427             assertTrue(i != null);
428             assertEquals("Choose the user rating you want to see.", i.getDefaultValue());
429             assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
430 
431             i = b.getInput("HelpText");
432             assertTrue(i != null);
433             assertEquals("If you want a really bad movie, check the one star movie.", i.getDefaultValue());
434             assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
435 
436         } finally {
437             dtf.tearDown();
438         }
439     }
440 
441 }