1   /* ===================================================================
2    * Copyright 2002-05 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.completion.management;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  import com.jcorporate.expresso.core.controller.ExpressoResponse;
15  import com.jcorporate.expresso.services.test.ControllerTestFixture;
16  import com.jcorporate.expresso.services.test.TestSystemInitializer;
17  import com.sri.emo.dbobj.Node;
18  import com.sri.emo.test.DatabaseTestFixture;
19  import com.sri.emo.test.EmoTestSuite;
20  import com.sri.emo.wizard.completion.model.CompletionBean;
21  
22  /***
23   *
24   * @author Michael Rimov
25   * @version 1.0
26   */
27  public class TestDoChooseNodeStep extends ChooseCompletionTestBase {
28      private ControllerTestFixture testFixture;
29  
30      private DatabaseTestFixture databaseFixture = null;
31  
32      protected void setUp() throws Exception {
33          super.setUp();
34          testFixture = new ControllerTestFixture();
35          testFixture.setUp();
36  
37          databaseFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
38              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
39          databaseFixture.setUp();
40  
41      }
42  
43      protected void tearDown() throws Exception {
44          testFixture.tearDown();
45          testFixture = null;
46          databaseFixture.tearDown();
47          databaseFixture = null;
48          super.tearDown();
49      }
50  
51  
52      public void testBadInput() throws Exception {
53          Map parameters = new HashMap();
54          parameters.put("targetId","ABCDEFG");
55          ExpressoResponse response = testFixture.invokeFacade(CompletionEditor.class, parameters, DoChooseNodeStep.STATE_NAME);
56          assertTrue(response.getErrors() == null || response.getErrors().size() > 0);
57          assertTrue(getCompletionBean(response) != null);
58  
59          //Check for redirect back to prompt
60          assertEquals(PromptChooseNode.STATE_NAME, response.getCurrentState().getName());
61          assertEquals(CompletionEditor.class.getName(), response.getControllerClass());
62      }
63  
64  
65      public void testInputLoadsNodeData() throws Exception {
66          Map parameters = new HashMap();
67          parameters.put("Title","Test Wizard");
68          parameters.put("targetId","2");
69          parameters.put("WizardClass",com.sri.emo.wizard.completion.EmoCompletionWizard.class.getName());
70          ExpressoResponse response = testFixture.invokeFacade(CompletionEditor.class, parameters, DoChooseNodeStep.STATE_NAME);
71          assertTrue(response.getErrors() == null || response.getErrors().size() == 0);
72  
73          //Check for redirect to next prompt
74          assertEquals(PromptChooseParts.STATE_NAME, response.getCurrentState().getName());
75          assertEquals(CompletionEditor.class.getName(), response.getControllerClass());
76  
77          //Check that node was loaded.
78          CompletionBean bean = this.getCompletionBean(response);
79  
80          assertEquals(new Integer(2), bean.getTargetId());
81  
82          assertTrue (bean.getCurrentNode() != null);
83          Node n = new Node();
84          n.setField(Node.NODE_ID, 2);
85          n.retrieve();
86          assertEquals(n, bean.getCurrentNode());
87  
88      }
89  
90      /***
91       * Test case against entering one node, going back and entering
92       * another should cause reinitialization of the beans.
93       */
94      public void testInputTwiceGetsTwoDifferentNodes() throws Exception {
95          Map parameters = new HashMap();
96          parameters.put("Title","Test Wizard");
97          parameters.put("targetId","2");
98          parameters.put("WizardClass",com.sri.emo.wizard.completion.EmoCompletionWizard.class.getName());
99          ExpressoResponse response = testFixture.invokeFacade(CompletionEditor.class, parameters, DoChooseNodeStep.STATE_NAME);
100 
101         //Check that node was loaded.
102         CompletionBean bean = this.getCompletionBean(response);
103         assertTrue(bean != null);
104 
105         parameters.put("targetId","3");
106         response = testFixture.invokeFacade(CompletionEditor.class, parameters, DoChooseNodeStep.STATE_NAME);
107         assertTrue(response.getErrors() == null || response.getErrors().size() == 0);
108         bean = this.getCompletionBean(response);
109 
110         assertEquals(new Integer(3), bean.getTargetId());
111         assertTrue (bean.getCurrentNode() != null);
112         Node n = new Node();
113         n.setField(Node.NODE_ID, 3);
114         n.retrieve();
115         assertEquals(n, bean.getCurrentNode());
116 
117     }
118 
119 
120 }