1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.Locale;
4   
5   import org.apache.log4j.Logger;
6   
7   import org.sourceforge.vlibrary.Constants;
8   import org.sourceforge.vlibrary.VlibraryActionTestCase;
9   import org.sourceforge.vlibrary.user.dao.ReaderDAOSpringJDBCImpl;
10  import org.sourceforge.vlibrary.user.domain.Reader;
11  import org.sourceforge.vlibrary.user.logic.LibraryManager;
12  
13  
14  /**
15   * Tests for SaveReaderAction
16   *
17   */
18  public class SaveReaderActionTest extends VlibraryActionTestCase {
19      
20      private static Logger logger =
21          Logger.getLogger(VlibraryActionTestCase.class.getName());
22         
23         private ReaderDAOSpringJDBCImpl readerInstance = null;
24         private LibraryManager libraryManagerInstance = null;
25         
26         private Reader newbie = new Reader(
27                 "Newbie",          // last name
28                 "Joe",             // first name
29                 "joe@schmoe.com",  // email
30                 "48012345678",     // desk phone
31                 "60212345678",     // mobile
32                 "123",             // pager
33                 "TheExplorer",     // screen name
34                 "123",             // im service
35                 "newbie",          // uid
36                 "sdsdsd",          // pwd
37                 false);            // admin?
38         
39         private Reader changed = new Reader(
40                 "Changer",         // last name
41                 "Moe",             // first name
42                 "moe@schmoe.com",  // email
43                 "48012345679",     // desk phone
44                 "60212345679",     // mobile
45                 "123",             // pager
46                 "TheExplorer",     // screen name
47                 "123",             // im service
48                 "newbie",         // uid
49                 "sdsdsd",          // pwd
50                 false);            // admin?
51      
52      public SaveReaderActionTest(String testName) {
53          super(testName);
54      }
55      
56      public void setUp() throws Exception {
57          super.setUp();
58          readerInstance = (ReaderDAOSpringJDBCImpl) springContext.getBean(
59              "ReaderDAO", ReaderDAOSpringJDBCImpl.class);
60          libraryManagerInstance = 
61              (LibraryManager) springContext.getBean(
62              "LibraryManager", LibraryManager.class);
63          
64      }
65      
66      protected void tearDown() throws Exception {
67          super.tearDown();
68      }
69      
70      /**
71       * Tests a successful reader CREATE action.
72       * 
73       */
74      public void testSuccessfulCreate() throws Exception {
75       
76          logger.info(" in testSuccessfulCheckout");  
77        
78          // Set up and submit the http request
79          setRequestPathInfo("/a/saveReader");
80          addRequestParameter(Constants.ACTION, Constants.CREATE);
81          insertBeanAsParameters(newbie);
82          addRequestParameter("confirmPwd", newbie.getPwd());
83          actionPerform();
84          
85          // Check for no errors
86          verifyForward("success");
87          verifyNoActionErrors();
88          
89          // Verify that the reader was added to the database
90          Reader newbie2 = libraryManagerInstance.retrieveByUid("newbie");
91          // Make sure a valid ID was assigned
92          assertTrue(newbie2.getId() > 0);
93          // Make IDs same, so equals just tests other fields
94          newbie2.setId(newbie.getId());
95    
96          assertEquals(newbie2, newbie);
97      }
98      
99      public void testCreateValidationFailure() throws Exception {
100         
101         logger.info(" in testCreateValidationFailure");  
102         
103         // Set up and submit the http request
104         setRequestPathInfo("/a/saveReader");
105         addRequestParameter(Constants.ACTION, Constants.CREATE);
106         insertBeanAsParameters(newbie);
107         addRequestParameter("confirmPwd", newbie.getPwd() + "junk");
108         actionPerform();
109         
110         // Forward back to input, with mismatch error
111         verifyInputForward();
112         verifyActionErrors(new String[] {"error.reader.confirmPwdFailed"});
113     }
114     
115     public void testCreateMissingUidFailure() throws Exception {
116         
117         logger.info(" in testCreateValidationFailure");  
118         
119         // Set up and submit the http request
120         setRequestPathInfo("/a/saveReader");
121         addRequestParameter(Constants.ACTION, Constants.CREATE);
122         newbie.setUid(null); // missing uid should blow up
123         insertBeanAsParameters(newbie);
124         addRequestParameter("confirmPwd", newbie.getPwd());
125         actionPerform();
126         
127         // Forward back to input, with missing data error
128         verifyInputForward();
129         verifyActionErrors(new String[] {"error.reader.uid.required"});
130     }
131     
132     //  TODO:  Add similar test cases for all other required fields
133     
134     public void testCreateDuplicateFailure() throws Exception {
135         
136         logger.info(" in testCreateDuplicateUidFailure"); 
137         
138         // Insert first, so result is duplicate
139         readerInstance.insert(newbie);
140         
141         // Set up and submit the http request
142         setRequestPathInfo("/a/saveReader");
143         addRequestParameter(Constants.ACTION, Constants.CREATE);
144         insertBeanAsParameters(newbie);
145         addRequestParameter("confirmPwd", newbie.getPwd());
146         actionPerform();
147         
148         // Forward back to input, with duplicate errors
149         verifyInputForward();
150         verifyActionErrors(new String[] {"error.reader.idTaken",
151         "error.reader.already.registered"});        
152     }
153     
154     public void testUpdateSuccessful() throws Exception {
155         
156         logger.info(" in testUpdateSuccessful"); 
157         
158         // Insert first
159         readerInstance.insert(newbie);
160         changed.setId(newbie.getId());
161         
162         // Set up and submit the http request
163         setRequestPathInfo("/a/saveReader");
164         addRequestParameter(Constants.ACTION, Constants.UPDATE);
165         // Changed has same uid, but different props
166         insertBeanAsParameters(changed);
167         actionPerform();
168         
169         // Check for no errors
170         verifyForward("success");
171         verifyNoActionErrors();
172         
173         // Verify that the update happened
174         Reader reader = libraryManagerInstance.retrieveByUid("newbie");
175         assertEquals(reader, changed);   
176     }
177     
178 }