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 com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
14  import com.jcorporate.expresso.services.test.TestSystemInitializer;
15  import com.sri.common.dbobj.ObjectNotFoundException;
16  import com.sri.common.dbobj.RepositoryException;
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.CompletionWizardTestBean;
22  import junit.framework.TestCase;
23  import java.util.Iterator;
24  import java.util.List;
25  import com.sri.emo.wizard.completion.model.FieldCompletion;
26  import com.sri.emo.wizard.completion.model.CompletionPartsBean;
27  import com.jcorporate.expresso.core.security.SuperUser;
28  import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
29  
30  /***
31   * Tests the Expresso Completion Repository.
32   * @author Michael Rimov
33   * @version 1.0
34   */
35  public class TestExpressoCompletionRepository extends TestCase {
36  
37      //Repository instance
38      private ExpressoCompletionRepository testRepository = null;
39  
40      private CompletionDBObjTestModel dbobjTestModel = null;
41  
42      private DatabaseTestFixture testFixture = null;
43  
44  
45      private CompletionBean sampleBean;
46  
47      private WizDefinition sampleWizard;
48  
49      private CompletionWizardTestBean beanFixture = null;
50  
51      /***
52       * Allows for single class setup.
53       */
54      public TestExpressoCompletionRepository() {
55          super();
56          try {
57              TestSystemInitializer.setUp();
58              new MutableRequestRegistry(TestSystemInitializer.getTestContext(), SuperUser.INSTANCE);
59          } catch (Exception ex) {
60              throw new RuntimeException("Error setting things up",ex);
61          }
62      }
63  
64  
65      protected void setUp() throws Exception {
66          super.setUp();
67  
68          testRepository = new ExpressoCompletionRepository(new CompletionDBObjConverterImpl());
69          testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
70              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
71          testFixture.setUp();
72  
73          if (new WizDefinition().count() > 0) {
74              new WizDefinition().deleteAll(false);
75          }
76          if (new CompletionDefinition().count() > 0)  {
77              new CompletionDefinition().deleteAll(false);
78          }
79  
80          if (new CompletionDetails().count() > 0) {
81              new CompletionDetails().deleteAll(false);
82          }
83  
84          assert (new WizDefinition().count() == 0);
85          assert (new CompletionDefinition().count() == 0);
86          assert (new CompletionDetails().count() == 0);
87  
88  
89  
90          beanFixture = new CompletionWizardTestBean();
91          beanFixture.setUp();
92          sampleBean = beanFixture.getConstructedBean();
93  
94  
95  
96          dbobjTestModel = new CompletionDBObjTestModel();
97          dbobjTestModel.setUp();
98          sampleWizard = dbobjTestModel.getBuiltDefinition();
99      }
100 
101     protected void tearDown() throws Exception {
102         testRepository = null;
103         beanFixture.tearDown();
104         beanFixture = null;
105         sampleBean = null;
106         testFixture.tearDown();
107         testFixture = null;
108         sampleBean = null;
109         sampleWizard = null;
110 
111         super.tearDown();
112     }
113 
114     public void testAdd() throws Exception {
115         int beforeCountDefinition = new CompletionDefinition().count();
116         int beforeCountDetails = new CompletionDetails().count();
117 
118         int wizId = testRepository.add(sampleBean);
119         assertTrue(wizId > 0);
120         assertNotNull(sampleBean.getWizardId());
121         assertEquals(wizId, sampleBean.getWizardId().intValue());
122 
123         assertEquals(beforeCountDefinition + 1, new CompletionDefinition().count());
124         assertEquals(beforeCountDetails + CompletionWizardTestBean.NUMBER_OF_PARTS,
125             new CompletionDetails().count());
126 
127         WizDefinition wizDef = new WizDefinition();
128         wizDef.setId(wizId);
129         try {
130             wizDef.retrieve();
131         } catch (DBRecordNotFoundException ex) {
132             fail("Couldn't find wizard id that was supposed to be added. id: " + wizId);
133         }
134 
135     }
136 
137 
138     public void testDelete() throws Exception {
139         int beforeCountDefinition = new CompletionDefinition().count();
140         int beforeCountDetails = new CompletionDetails().count();
141         sampleWizard.add();
142         int wizardId = Integer.parseInt(sampleWizard.getId());
143         testRepository.delete(wizardId);
144         WizDefinition wizdef = new WizDefinition();
145         wizdef.setId(wizardId);
146 
147         if (wizdef.find()) {
148             fail("Found wizard definition that was already deleted from repository.");
149         }
150 
151         assertEquals(beforeCountDefinition,new CompletionDefinition().count());
152         assertEquals(beforeCountDetails,new CompletionDetails().count());
153 
154     }
155 
156     public void testFindById() throws Exception {
157         sampleWizard.add();
158         int wizardId = Integer.parseInt(sampleWizard.getId());
159         assertTrue(wizardId > 0);
160 
161         CompletionBean cb = testRepository.findById(wizardId);
162         assertTrue(cb != null);
163         //Adjust id for equality check.
164         sampleBean.setWizardId(new Integer(wizardId));
165         assertEquals(sampleBean, cb);
166     }
167 
168     public void testFindInvalidIdThrowsObjectNotFoundException() throws Exception {
169         try {
170             testRepository.findById(10010110);
171             fail("Finding a repository with a bogus id should have thrown ObjectNotFoundException");
172         } catch (ObjectNotFoundException ex) {
173         	assertNotNull(ex.getMessage());
174             //a-ok
175         }
176     }
177 
178     public void testUpdate() throws RepositoryException, ObjectNotFoundException {
179         int id = testRepository.add(sampleBean);
180         sampleBean.setWizardTitle("This is a new title");
181         testRepository.update(sampleBean);
182 
183         CompletionBean wizardBean = testRepository.findById(id);
184         assertNotNull(wizardBean);
185         assertEquals(sampleBean, wizardBean);
186     }
187 
188     public void testFindByIdWithObjects() throws Exception {
189         sampleWizard.add();
190         Object object = sampleWizard.getId();
191         assertNotNull(object);
192 
193         CompletionBean cb = testRepository.findById(object);
194         assertNotNull(cb);
195         CompletionBean cb2 = testRepository.findById(Integer.parseInt(object.toString()));
196         assertEquals(cb,cb2);
197     }
198 
199 
200     public void testFindByIdWithBadObjectThrowsNumberFormatException() throws Exception {
201         try {
202             testRepository.findById("18kfas");
203             fail("Completion Repository Should have thrown Exception");
204         } catch (NumberFormatException ex) {
205         	assertNotNull(ex.getMessage());
206             //a-ok
207         }
208 
209     }
210 
211     public void testIncompletePartsDataMayBeRepaired() throws Exception {
212         int key = testRepository.add(sampleBean);
213         CompletionDefinition def = new CompletionDefinition();
214         def.setField(CompletionDefinition.FLD_ID, key);
215         def.retrieve();
216 
217         List l = def.getCompletionDetails();
218         int deletedDetailsId = -1;
219         int deletedPartId = -1;
220         for (Iterator i = l.iterator(); i.hasNext();) {
221             CompletionDetails oneDetail = (CompletionDetails)i.next();
222             if (FieldCompletion.WIZARD.toString().equals(oneDetail.getField(CompletionDetails.FLD_COMPLETION))) {
223                 deletedDetailsId = oneDetail.getFieldInt(CompletionDetails.FLD_SURROGATE_KEY);
224                 deletedPartId = oneDetail.getFieldInt(CompletionDetails.FLD_PART_ID);
225                 break;
226             }
227         }
228 
229         assertTrue("Didn't find any suitable completion details id.", deletedDetailsId > -1);
230         assertTrue("Part Id should be set as well", deletedPartId > -1);
231 
232         CompletionDetails toDelete = new CompletionDetails();
233         toDelete.setField(CompletionDetails.FLD_SURROGATE_KEY, deletedDetailsId);
234         toDelete.delete();
235 
236 
237 
238         CompletionBean cb = testRepository.findById(key);
239 
240         testRepository.update(cb);
241 
242         cb = testRepository.findById(key);
243 
244         boolean foundDeletedPart = false;
245         for (Iterator it = cb.getCompletionParts().iterator(); it.hasNext();) {
246             CompletionPartsBean onePart = (CompletionPartsBean)it.next();
247             if (Integer.parseInt(onePart.getPart().getId()) == deletedPartId) {
248                 foundDeletedPart = true;
249                 assertTrue(FieldCompletion.FIXED == onePart.getFieldCompletion());
250             }
251         }
252 
253         assertTrue("Must find a recreated missing part", foundDeletedPart);
254 
255 
256     }
257 
258 }