1   package com.sri.emo.wizard.completion.management;
2   
3   import java.util.HashSet;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.Set;
8   
9   import com.jcorporate.expresso.core.controller.ExpressoResponse;
10  import com.jcorporate.expresso.core.db.DBException;
11  import com.sri.emo.dbobj.WizDefinition;
12  import com.sri.emo.wizard.completion.model.CompletionBean;
13  import com.sri.emo.wizard.completion.model.CompletionPartsBean;
14  import com.sri.emo.wizard.completion.persistence.CompletionDBObjConverterImpl;
15  import com.sri.emo.wizard.completion.persistence.ExpressoCompletionRepository;
16  
17  /***
18   *
19   * @author Michael Rimov
20   * @version 1.0
21   */
22  public class TestDoChooseCriteria extends ChooseCompletionTestBase {
23      final int starwarsId = 4;
24  
25      private NewCompletionFixtureTemplate completionTemplate;
26  
27      private static class DoChooseCriteriaCompletionFixture extends NewCompletionFixtureTemplate {
28          public DoChooseCriteriaCompletionFixture(String stateName, int targetNodeId) {
29              super(stateName, targetNodeId);
30          }
31  
32          protected void modifyParameters(Map parameters) {
33  
34              //Length
35              parameters.put("0_freeText", "checkbox");
36              parameters.put("0_minEntries", "1");
37              parameters.put("0_maxEntries", "1");
38              parameters.put("0_directive", "");
39              parameters.put("0_helpText", "");
40  
41              //User Rating
42              parameters.put("2_minEntries", "1");
43              parameters.put("2_maxEntries", "1");
44              parameters.put("2_directive", "Please select a value for User Rating");
45              parameters.put("2_helpText",
46                  "Ratings are 1-5 stars. Since this is your own movie, We'd strongly suggest that you give it a '5'.");
47  
48              //User Reviews
49              parameters.put("3_minEntries", "3");
50              parameters.put("3_maxEntries", "5");
51              parameters.put("3_directive", "Please enter some user reviews.");
52              parameters.put("3_helpText",
53                  "See imdb.com for some examples.");
54  
55              //Actors
56              parameters.put("4_minEntries", "0");
57              parameters.put("4_maxEntries", "0");
58              parameters.put("4_directive", "Please select the top three actors to receive billing.");
59              parameters.put("4_helpText", "");
60          }
61  
62  
63      }
64  
65  
66      protected void setUp() throws Exception {
67          super.setUp();
68  
69          completionTemplate = new DoChooseCriteriaCompletionFixture(DoChooseCriteria.STATE_NAME, starwarsId);
70      }
71  
72      protected void tearDown() throws Exception {
73          super.tearDown();
74          completionTemplate = null;
75      }
76  
77      public void testHandleAddRequest() throws Exception {
78          int beforeCount = new WizDefinition().count();
79  
80          ExpressoResponse response = completionTemplate.invokeController();
81          assertNotNull(response);
82          assertTrue("Received Errors: " + response.getErrors(),
83              response.getErrors() == null || response.getErrors().size() == 0);
84  
85          //Verify something added.
86          assertEquals((beforeCount + 1), new WizDefinition().count());
87      }
88  
89  
90      public void testHandleAddRequestGeneratesProperValues() throws Exception {
91          int oneId = this.createAddedWizardAndGetId();
92  
93          ExpressoCompletionRepository completion = new ExpressoCompletionRepository(new CompletionDBObjConverterImpl());
94          CompletionBean cb = completion.findById(oneId);
95  
96          assertTrue(cb != null);
97          assertEquals(oneId, cb.getWizardId().intValue());
98  
99          HashSet completionParts = new HashSet(cb.getWizardCompletionParts());
100         assertNotNull(completionParts);
101         assertTrue(completionParts.size() == 3);
102 
103         for (Iterator i = completionParts.iterator(); i.hasNext(); ) {
104             CompletionPartsBean onePart = (CompletionPartsBean) i.next();
105             assertNotNull(onePart);
106             assertNotNull(onePart.getPart());
107             assertNotNull(onePart.getMinEntries());
108             assertNotNull(onePart.getMaxEntries());
109 
110             //Verify Min/Max values
111             switch (onePart.getPart().getPartNumInt()) {
112                 case 0:
113                     assertEquals(1, onePart.getMinEntries().intValue());
114                     assertEquals(1, onePart.getMaxEntries().intValue());
115                     assertEquals("", onePart.getDirective());
116                     assertEquals("", onePart.getHelpText());
117                     break;
118 
119                 case 2:
120                     assertEquals(1, onePart.getMinEntries().intValue());
121                     assertEquals(1, onePart.getMaxEntries().intValue());
122                     assertEquals("Please select a value for User Rating", onePart.getDirective());
123                     assertEquals("Ratings are 1-5 stars. Since this is your own movie, We'd strongly suggest "
124                         +"that you give it a '5'.", onePart.getHelpText());
125                     break;
126 
127                 case 3:
128                     assertEquals(3, onePart.getMinEntries().intValue());
129                     assertEquals(5, onePart.getMaxEntries().intValue());
130                     break;
131 
132                 case 4:
133                     assertEquals(0, onePart.getMinEntries().intValue());
134                     assertEquals(0, onePart.getMaxEntries().intValue());
135                     break;
136 
137 
138                 default:
139                     fail("Encountered unknown part");
140                     break;
141 
142             }
143         }
144     }
145 
146     private Set loadAllWizardIds() throws DBException {
147         Set allPreviousIds = new HashSet();
148         List allWizards = new WizDefinition().searchAndRetrieveList();
149         for (Iterator i = allWizards.iterator(); i.hasNext(); ) {
150             WizDefinition oneWizDef = (WizDefinition) i.next();
151             allPreviousIds.add(oneWizDef.getId());
152         }
153 
154         return allPreviousIds;
155     }
156 
157 
158     public void testHandleBadMinMaxEntries() throws Exception {
159         completionTemplate = new DoChooseCriteriaCompletionFixture(DoChooseCriteria.STATE_NAME, starwarsId) {
160             protected void modifyParameters(final Map parameters) {
161                 super.modifyParameters(parameters);
162 
163                 //Length
164                 parameters.put("0_freeText", "checkbox");
165                 parameters.put("0_minEntries", "able");
166                 parameters.put("0_maxEntries", "baker");
167                 parameters.put("0_directive", "");
168                 parameters.put("0_helpText", "");
169             }
170         };
171 
172         ExpressoResponse response = completionTemplate.invokeController();
173         assertNotNull("Should be errors in submitted form.", response.getErrors());
174         assertTrue("Should be one error for each bad min/max entry (2).  Instead got: "
175             + response.getErrors().getErrorCount() + " with data: " + response.getErrors().toString(),
176             response.getErrors().getErrorCount() == 2);
177     }
178 
179 
180     /***
181      * 'Fixture' that creates a wizard with the default values from the completion template,
182      * saves it, and returns the created id by checking what's in the database
183      * afterwards.
184      * @return int the created wizard id.
185      * @throws Exception upon error.
186      */
187     private int createAddedWizardAndGetId() throws Exception {
188         Set allPreviousIds = loadAllWizardIds();
189         ExpressoResponse response = completionTemplate.invokeController();
190         assertTrue("Received Errors: " + response.getErrors(),
191             response.getErrors() == null || response.getErrors().size() == 0);
192         Set allAfterIds = loadAllWizardIds();
193 
194         assertTrue(allAfterIds.removeAll(allPreviousIds));
195         assertTrue(allAfterIds.size() == 1);
196 
197         //Get the wizard id
198         String oneId = (String) allAfterIds.iterator().next();
199         return Integer.parseInt(oneId);
200     }
201 
202     public void testHandleUpdateRequest() throws Exception {
203         //Prep -- add a wizard and get its id
204 
205         int oneId = createAddedWizardAndGetId();
206         //Now construct our existing wizard template with this id.
207         ExistingWizardFixtureTemplate existingTemplate = new ExistingWizardFixtureTemplate(DoChooseCriteria.STATE_NAME,
208             oneId) {
209             protected void modifyParameters(Map parameters) {
210 
211                 //Length
212                 parameters.put("0_freeText", "checkbox");
213                 parameters.put("0_minEntries", "1");
214                 parameters.put("0_maxEntries", "1");
215                 parameters.put("0_directive", "New Directive");
216                 parameters.put("0_helpText", "New Helptext");
217 
218                 //User Rating
219                 parameters.put("2_minEntries", "1");
220                 parameters.put("2_maxEntries", "1");
221                 parameters.put("2_directive", "Please select a value for User Rating");
222                 parameters.put("2_helpText", "Ratings are 1-5 stars. Since this is your own movie, We'd "
223                     +"strongly suggest that you give it a '5'.");
224 
225 
226                 //User Reviews
227                 parameters.put("3_minEntries", "0");
228                 parameters.put("3_maxEntries", "10");
229                 parameters.put("3_directive", "Please enter some user reviews.");
230                 parameters.put("3_helpText",
231                     "See imdb.com for some examples.");
232 
233                 //Actors
234                 parameters.put("4_minEntries", "0");
235                 parameters.put("4_maxEntries", "0");
236                 parameters.put("4_directive", "Please select the top three actors to receive billing.");
237                 parameters.put("4_helpText", "");
238             }
239         };
240 
241         ExpressoResponse response = existingTemplate.invokeController();
242         assertNotNull(response);
243 
244         assertTrue("Should have no errors updating.",
245             response.getErrors() == null || response.getErrors().getErrorCount() == 0);
246 
247         ExpressoCompletionRepository completion = new ExpressoCompletionRepository(new CompletionDBObjConverterImpl());
248         CompletionBean cb = completion.findById(oneId);
249         assertNotNull("Should have found completion bean", cb);
250 
251         assertTrue(cb != null);
252         assertEquals(oneId, cb.getWizardId().intValue());
253 
254         HashSet completionParts = new HashSet(cb.getWizardCompletionParts());
255         assertNotNull(completionParts);
256         assertTrue(completionParts.size() == 3);
257 
258         for (Iterator i = completionParts.iterator(); i.hasNext(); ) {
259             CompletionPartsBean onePart = (CompletionPartsBean) i.next();
260             assertNotNull(onePart);
261             assertNotNull(onePart.getPart());
262             assertNotNull(onePart.getMinEntries());
263             assertNotNull(onePart.getMaxEntries());
264 
265             //Verify Individual parts.
266             switch (onePart.getPart().getPartNumInt()) {
267                 case 0:
268                     assertEquals(1, onePart.getMinEntries().intValue());
269                     assertEquals(1, onePart.getMaxEntries().intValue());
270                     assertEquals("New Directive", onePart.getDirective());
271                     assertEquals("New Helptext", onePart.getHelpText());
272                     break;
273 
274                 case 2:
275                     assertEquals(1, onePart.getMinEntries().intValue());
276                     assertEquals(1, onePart.getMaxEntries().intValue());
277                     assertEquals("Please select a value for User Rating", onePart.getDirective());
278                     assertEquals("Ratings are 1-5 stars. Since this is your own movie, We'd strongly suggest "
279                         +"that you give it a '5'.", onePart.getHelpText());
280                     break;
281 
282                 case 3:
283                     assertEquals(0, onePart.getMinEntries().intValue());
284                     assertEquals(10, onePart.getMaxEntries().intValue());
285                     break;
286 
287                 case 4:
288                     assertEquals(0, onePart.getMinEntries().intValue());
289                     assertEquals(0, onePart.getMaxEntries().intValue());
290                     break;
291 
292 
293                 default:
294                     fail("Encountered unknown part");
295                     break;
296             }
297         }
298 
299     }
300 
301 
302     public void testBeanGetsClearedAfterSave() throws Exception {
303         ExpressoResponse response = completionTemplate.invokeController();
304         assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
305 
306         assertNull("Completion Bean should have been cleared from session after save.",
307             response.getExpressoRequest().getSession().getPersistentAttribute(CompletionBeanManager.BEAN_NAME));
308 
309     }
310 
311 
312 }