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  
11  package com.sri.emo.wizard.completion.persistence;
12  
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Set;
16  import com.jcorporate.expresso.services.test.TestSystemInitializer;
17  import com.sri.emo.dbobj.WizDefinition;
18  import com.sri.emo.test.DatabaseTestFixture;
19  import com.sri.emo.test.EmoTestSuite;
20  import com.sri.emo.wizard.completion.model.CompletionBean;
21  import com.sri.emo.wizard.completion.model.CompletionPartsBean;
22  import com.sri.emo.wizard.completion.model.CompletionWizardTestBean;
23  import junit.framework.TestCase;
24  import com.sri.emo.wizard.completion.model.FieldCompletion;
25  import com.jcorporate.expresso.core.security.SuperUser;
26  import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
27  
28  /***
29   *
30   * @author Michael Rimov
31   * @version 1.0
32   */
33  public class TestEmoCompletionDBObjConverter extends TestCase {
34  
35      CompletionDBObjTestModel dbobjTestModel = new CompletionDBObjTestModel();
36  
37      private DatabaseTestFixture testFixture = null;
38  
39      private CompletionDBObjConverterImpl emoCompletionDBObjConverter = null;
40  
41      private CompletionBean sampleBean;
42  
43      private WizDefinition sampleWizard;
44  
45      private CompletionWizardTestBean beanFixture = null;
46  
47  
48      public TestEmoCompletionDBObjConverter() throws Exception {
49          super();
50          try {
51              TestSystemInitializer.setUp();
52              new MutableRequestRegistry(TestSystemInitializer.getTestContext(), SuperUser.INSTANCE);
53          } catch (Exception ex) {
54              throw new RuntimeException("Error setting things up",ex);
55          }
56      }
57  
58      protected void setUp() throws Exception {
59          super.setUp();
60  
61          //Clear out the tables.
62          new WizDefinition().deleteAll(true);
63          new CompletionDefinition().deleteAll(true);
64          new CompletionDetails().deleteAll(true);
65  
66          assert (new WizDefinition().count() == 0);
67          assert (new CompletionDefinition().count() == 0);
68          assert (new CompletionDetails().count() == 0);
69  
70  
71          testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
72              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
73          testFixture.setUp();
74  
75          beanFixture = new CompletionWizardTestBean();
76          beanFixture.setUp();
77          sampleBean = beanFixture.getConstructedBean();
78  
79  
80  
81          dbobjTestModel = new CompletionDBObjTestModel();
82          dbobjTestModel.setUp();
83          sampleWizard = dbobjTestModel.getBuiltDefinition();
84  
85          emoCompletionDBObjConverter = new CompletionDBObjConverterImpl();
86  
87      }
88  
89      protected void tearDown() throws Exception {
90          beanFixture.tearDown();
91          beanFixture = null;
92          sampleBean = null;
93          testFixture.tearDown();
94          testFixture = null;
95          sampleBean = null;
96          sampleWizard = null;
97          emoCompletionDBObjConverter = null;
98          super.tearDown();
99      }
100 
101 
102 
103     public void testConvertToDBObjectHasAdditionalInfo() throws Exception {
104         WizDefinition result= emoCompletionDBObjConverter.convertToDBObject(sampleBean);
105         assertEquals(CompletionDefinition.class, result.getAdditionalInfoClass());
106         assertNotNull(result.getAdditionalInfo());
107     }
108 
109     public void testConvertToDBObjectHasDBObjectSteps() throws Exception {
110         WizDefinition result= emoCompletionDBObjConverter.convertToDBObject(sampleBean);
111         CompletionDefinition completion = (CompletionDefinition)result.getAdditionalInfo();
112         List details = completion.getCompletionDetails();
113         assertNotNull(details);
114         assertEquals(CompletionWizardTestBean.NUMBER_OF_PARTS, details.size());
115     }
116 
117     public void testConvertToBeanHasSteps() throws Exception {
118         CompletionBean result = emoCompletionDBObjConverter.convertToBean(sampleWizard);
119         Set resultingParts = result.getCompletionParts();
120         assertNotNull(resultingParts);
121         assertEquals(CompletionWizardTestBean.NUMBER_OF_PARTS, resultingParts.size());
122     }
123 
124     public void testConversionToBeanMaintainsMinMaxProperties() throws Exception {
125         CompletionBean result = emoCompletionDBObjConverter.convertToBean(sampleWizard);
126         Set resultingParts = result.getCompletionParts();
127         boolean foundAttribute = false;
128         for (Iterator i = resultingParts.iterator(); i.hasNext(); ) {
129             CompletionPartsBean onePart = (CompletionPartsBean)i.next();
130             if (Integer.parseInt(onePart.getPart().getId()) == CompletionWizardTestBean.ACTORS) {
131                 foundAttribute = true;
132                 assertEquals(CompletionWizardTestBean.MIN_ACTORS, onePart.getMinEntries().intValue());
133                 assertEquals(CompletionWizardTestBean.MAX_ACTORS, onePart.getMaxEntries().intValue());
134                 break;
135             }
136         }
137 
138         assertEquals("Must find an 'actors' attribute",true, foundAttribute);
139     }
140 
141     public void testConversionToDBObjectMaintainsMinMaxProperties() throws Exception {
142         WizDefinition result= emoCompletionDBObjConverter.convertToDBObject(sampleBean);
143         boolean foundAttribute = false;
144         for (Iterator i = ((CompletionDefinition)result.getAdditionalInfo()).getCompletionDetails().iterator(); i.hasNext(); ) {
145             CompletionDetails onePart = (CompletionDetails)i.next();
146             if (Integer.parseInt(onePart.getPart().getId()) == CompletionWizardTestBean.ACTORS) {
147                 foundAttribute = true;
148                 assertEquals(CompletionWizardTestBean.MIN_ACTORS, Integer.parseInt(onePart.getField(CompletionDetails.FLD_MIN_ENTRIES)));
149                 assertEquals(CompletionWizardTestBean.MAX_ACTORS, Integer.parseInt(onePart.getField(CompletionDetails.FLD_MAX_ENTRIES)));
150                 break;
151             }
152         }
153 
154         assertEquals("Must find an 'actors' attribute",true, foundAttribute);
155 
156     }
157 
158     public void testConvertFromDBObjectMaintainsFieldCompletion() throws Exception {
159         WizDefinition result= emoCompletionDBObjConverter.convertToDBObject(sampleBean);
160         CompletionDefinition completion = (CompletionDefinition)result.getAdditionalInfo();
161         List details = completion.getCompletionDetails();
162         for (Iterator i = details.iterator(); i.hasNext(); ) {
163             CompletionDetails oneDetail = (CompletionDetails)i.next();
164             switch(oneDetail.getFieldInt(CompletionDetails.FLD_PART_ID)) {
165                 case CompletionWizardTestBean.PART_RATING:
166                     assertEquals(FieldCompletion.FIXED.toString(), oneDetail.getField(CompletionDetails.FLD_COMPLETION));
167                     break;
168 
169                 case CompletionWizardTestBean.USER_REVIEWS:
170                     assertEquals(FieldCompletion.WIZARD.toString(), oneDetail.getField(CompletionDetails.FLD_COMPLETION));
171                     break;
172 
173                 case CompletionWizardTestBean.LENGTH:
174                     assertEquals(FieldCompletion.WIZARD.toString(), oneDetail.getField(CompletionDetails.FLD_COMPLETION));
175                     break;
176 
177                 case CompletionWizardTestBean.ACTORS:
178                     assertEquals(FieldCompletion.FIXED.toString(), oneDetail.getField(CompletionDetails.FLD_COMPLETION));
179                     break;
180 
181                 case CompletionWizardTestBean.CRITIQUES:
182                     assertEquals(FieldCompletion.WIZARD.toString(), oneDetail.getField(CompletionDetails.FLD_COMPLETION));
183                     break;
184 
185                 default:
186                     fail("Caught unknown part.");
187 
188             }
189         }
190 
191 
192     }
193 
194 
195     /***
196      * Overall test that makes sure that dbobj to bean gets the bean we expect
197      * @throws Exception
198      */
199     public void testConvertToBean() throws Exception {
200       WizDefinition source = sampleWizard;
201       CompletionBean expectedReturn = sampleBean;
202       CompletionBean actualReturn = emoCompletionDBObjConverter.convertToBean(source);
203       assertEquals("return value", expectedReturn, actualReturn);
204   }
205 
206   public void testExploratoryWizardEqualsWorks() throws Exception {
207       WizDefinition otherWizard = (WizDefinition)sampleWizard.clone();
208       assertEquals(sampleWizard,otherWizard);
209   }
210 
211 
212   /***
213    * Overall test that makes sure that bean to dbobj gets the dbobj we expect.
214    * @throws Exception
215    */
216   public void testConvertToDBObject() throws Exception {
217       CompletionBean source = sampleBean;
218       WizDefinition expectedReturn = sampleWizard;
219       WizDefinition actualReturn = emoCompletionDBObjConverter.convertToDBObject(source);
220       assertEquals("return value", expectedReturn, actualReturn);
221   }
222 
223 
224     /***
225      * General smoke test to make sure that Bean - WizardDefinition - Bean
226      * gets the same results out.
227      * @throws Exception
228      */
229     public void testBeanRoundTripConvert() throws Exception {
230         CompletionBean expectedReturn = sampleBean;
231         WizDefinition intermediateReturn= emoCompletionDBObjConverter.convertToDBObject(sampleBean);
232         assertNotNull(intermediateReturn);
233         CompletionBean actualReturn = emoCompletionDBObjConverter.convertToBean(intermediateReturn);
234         assertEquals(expectedReturn, actualReturn);
235     }
236 
237     /***
238      * General smoke test to make sure that WizardDefinition - Bean - WizardDefinition
239      * gets the same results as the original definition.
240      * @throws Exception
241      */
242     public void testDBObjectRoundTripConvert() throws Exception {
243         WizDefinition expectedReturn = sampleWizard;
244         CompletionBean intermediateReturn = emoCompletionDBObjConverter.convertToBean(sampleWizard);
245         WizDefinition actualReturn = emoCompletionDBObjConverter.convertToDBObject(intermediateReturn);
246 
247         assertEquals(expectedReturn, actualReturn);
248         assertEquals(CompletionDefinition.class, actualReturn.getAdditionalInfoClass());
249         assertEquals(expectedReturn.getAdditionalInfo(), actualReturn.getAdditionalInfo());
250     }
251 
252 
253 
254 }