1   package com.sri.emo.wizard.wizardgateway;
2   
3   import java.util.HashMap;
4   import java.util.Vector;
5   import com.jcorporate.expresso.core.controller.Block;
6   import com.jcorporate.expresso.core.controller.Controller;
7   import com.jcorporate.expresso.core.controller.ControllerException;
8   import com.jcorporate.expresso.core.controller.ControllerResponse;
9   import com.jcorporate.expresso.core.controller.ExpressoRequest;
10  import com.jcorporate.expresso.core.controller.ExpressoResponse;
11  import com.jcorporate.expresso.core.controller.NonHandleableException;
12  import com.jcorporate.expresso.core.controller.Output;
13  import com.jcorporate.expresso.core.controller.Transition;
14  import com.jcorporate.expresso.core.db.DBException;
15  import com.jcorporate.expresso.core.misc.ConfigManager;
16  import com.jcorporate.expresso.services.test.ControllerTestFixture;
17  import com.jcorporate.expresso.services.test.TestSystemInitializer;
18  import com.sri.emo.wizard.expressoimpl.SelectionWizDefinitionRepository;
19  import com.sri.emo.dbobj.WizDefinition;
20  import com.sri.emo.wizard.WizDefinitionRepository;
21  import com.sri.emo.test.DatabaseTestFixture;
22  import com.sri.emo.test.EmoTestSuite;
23  import com.sri.emo.wizard.expressoimpl.WizardController;
24  import junit.framework.TestCase;
25  
26  /***
27   * Tests the list wizards.
28   * @author Michael Rimov
29   * @version 1.0
30   */
31  public class TestListWizards extends TestCase {
32  
33      /***
34       * DBUnit wrapping test fixture.
35       */
36      private DatabaseTestFixture databaseTestFixture;
37  
38      /***
39       * Controller test fixture.  Used for invoking controllers.
40       */
41      private ControllerTestFixture testFixture;
42  
43      protected void setUp() throws Exception {
44          super.setUp();
45  
46          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
47              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
48  
49          databaseTestFixture.setUp();
50  
51          testFixture = new ControllerTestFixture();
52          testFixture.setUp();
53      }
54  
55      protected void tearDown() throws Exception {
56          databaseTestFixture.tearDown();
57          databaseTestFixture = null;
58  
59          testFixture.tearDown();
60          testFixture = null;
61          super.tearDown();
62      }
63  
64      /***
65       * Checks to make sure that if the user doesn't have add permissions then
66       * no add link is displayed.
67       * @throws DBException
68       * @throws ControllerException
69       */
70      public void testNoPermissionToAddCreatesNoAddTransition() throws DBException, ControllerException {
71  
72          //Easiest way to set up -- anonymous extension fo expressi wiz definition repository.
73          WizDefinitionRepository repository = new SelectionWizDefinitionRepository() {
74              public boolean canRequesterAdd() throws NonHandleableException {
75                  return false;
76              }
77          };
78  
79          Controller controller = ConfigManager.getControllerFactory().getController(
80              WizardGatewayController.class.getName());
81  
82          assert controller != null;
83  
84          ListWizards listHandler = new ListWizards(controller, repository);
85          ExpressoRequest request = testFixture.buildControllerRequest(new HashMap(), ListWizards.STATE_NAME);
86          ExpressoResponse response = new ControllerResponse();
87          response.setRequest(request);
88          listHandler.handleRequest(request, response);
89  
90          assertTrue(response.getTransition("add") == null);
91      }
92  
93      /***
94       * Checks the list state.
95       *
96       * @throws ControllerException
97       * @throws NonHandleableException
98       * @throws DBException
99       */
100     public void testRunListState() throws ControllerException, DBException {
101 
102         ExpressoResponse response = testFixture.invokeFacade(WizardGatewayController.class,
103             new HashMap(), null);
104 
105         assertTrue(response != null);
106         assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
107 
108         Transition add = response.getTransition("add");
109         assertTrue(add != null);
110         assertEquals(WizardGatewayController.class.getName(), add.getControllerObject());
111         assertEquals(PromptAddWizard.STATE_NAME, add.getState());
112 
113         Block wizardList = response.getBlock("WizardList");
114         assertTrue(wizardList != null);
115         Vector allRows = wizardList.getNested();
116 
117         WizDefinition wizdef = new WizDefinition();
118         assertEquals(wizdef.count(), allRows.size());
119 
120         String wizardParameter = getDefinedWizard().getId();
121 
122         //Validate nested rows.
123         for (int i = 0; i < allRows.size(); i++) {
124             Block oneRow = (Block) allRows.get(0);
125             Output name = (Output) oneRow.getNested("name");
126             assertTrue(name != null);
127             assertTrue(name.getContent() != null);
128 
129             Transition edit = (Transition) oneRow.getNested("edit");
130             assertTrue(edit != null);
131             assertEquals(WizardGatewayController.class.getName(),
132                 edit.getControllerObject());
133 
134             assertEquals(EditWizard.STATE_NAME, edit.getState());
135             assertEquals(wizardParameter,
136                 edit.getParam(WizardController.WIZ_PARAMETER_ID));
137 
138             Transition delete = (Transition) oneRow.getNested("delete");
139             assertTrue(delete != null);
140             assertEquals(WizardGatewayController.class.getName(),
141                 delete.getControllerObject());
142 
143             assertEquals(PromptDeleteWizard.STATE_NAME, delete.getState());
144             assertEquals(wizardParameter,
145                 delete.getParam(WizardController.WIZ_PARAMETER_ID));
146         }
147     }
148 
149     /***
150      * Retrieve the default defined wizard.
151      * @return WizDefinition
152      * @throws DBException
153      */
154     private WizDefinition getDefinedWizard() throws DBException {
155         WizDefinition def = new WizDefinition();
156         def.setField(WizDefinition.FLD_ID, DatabaseTestFixture.DEFAULT_WIZ_ID);
157         def.retrieve();
158         return def;
159     }
160 
161     /***
162      * Tests the list state when nothing is defined.
163      *
164      * @throws ControllerException
165      * @throws NonHandleableException
166      * @throws DBException
167      */
168     public void testRunListStateWithNoWizardsDefined() throws
169         ControllerException, DBException {
170         WizDefinition wizdef = new WizDefinition();
171         wizdef.deleteAll(true);
172 
173         WizDefinition def = new WizDefinition();
174         int totals = def.count();
175         if (totals > 0) {
176             def.deleteAll();
177         }
178         ExpressoResponse response = testFixture.invokeFacade(WizardGatewayController.class,
179             new HashMap(), null);
180 
181         assertTrue(response != null);
182         assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
183 
184         Transition add = response.getTransition("add");
185         assertTrue(add != null);
186         assertEquals(WizardGatewayController.class.getName(), add.getControllerObject());
187         assertEquals(PromptAddWizard.STATE_NAME, add.getState());
188 
189         Block wizardList = response.getBlock("WizardList");
190         assertTrue(wizardList != null);
191         Vector allRows = wizardList.getNested();
192         assertEquals(0, allRows.size());
193     }
194 
195 }