View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.push.service;
16  
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  import static org.junit.Assert.fail;
20  
21  import java.util.List;
22  
23  import javax.persistence.EntityManager;
24  import javax.persistence.PersistenceContext;
25  
26  import org.apache.log4j.Logger;
27  import org.junit.AfterClass;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.kuali.mobility.push.dao.PreferenceDao;
33  import org.kuali.mobility.push.entity.Preference;
34  import org.kuali.mobility.push.entity.Sender;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.beans.factory.annotation.Qualifier;
37  import org.springframework.test.context.ContextConfiguration;
38  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
39  
40  
41  /**
42   * @author Kuali Mobility Team (mobility.collab@kuali.org)
43   */
44  @RunWith(SpringJUnit4ClassRunner.class)
45  @ContextConfiguration(value = "classpath:TestSpringBeans.xml")
46  public class PreferenceServiceImplTest {
47  
48  	private static final Logger LOG = Logger.getLogger(PreferenceServiceImplTest.class);
49  
50  	
51  	@PersistenceContext
52  	private EntityManager entityManager;
53  
54  	@Autowired
55  	@Qualifier("preferenceService")
56  	private PreferenceService service;
57  
58  	@Autowired
59  	@Qualifier("preferenceDao")
60  	private PreferenceDao dao;
61  
62  	@BeforeClass
63  	public static void setUpClass() throws Exception {
64  	}
65  
66  	@AfterClass
67  	public static void tearDownClass() throws Exception {
68  	}
69  
70  	@Before
71  	public void preTest() {
72  	}
73  
74  	@Test
75  	public void testNothing() {
76  
77  	}
78  
79  	@Test
80  	public void testFindPreferencesByUsername(){
81  		List<Preference> preferences = getService().findPreferencesByUsername("userA");
82  		assertFalse("FindPreferencesByUsername returned null and should not have.",null==preferences);
83  		assertTrue("FindPreferencesByUsername returned no preferences.", preferences.size() > 0);
84  		assertTrue("FindPreferencesByUsername failed to find the pre-loaded data.",preferences.size()==2 );
85  
86  		// Non-existant User
87  		preferences = getService().findPreferencesByUsername("userB");
88  		assertFalse("FindPreferencesByUsername returned null and should not have.",null==preferences);
89  		assertFalse("FindPreferencesByUsername returned no preferences.", preferences.size() > 0);
90  	}
91  
92  	@Test
93  	public void testFindPreferencesWithWrongUsername(){
94  		// Non-existant User
95  		List<Preference> preferences = getService().findPreferencesByUsername("userB");
96  		assertFalse("FindPreferencesByUsername returned null and should not have.",null==preferences);
97  		assertFalse("FindPreferencesByUsername returned no preferences.", preferences.size() > 0);
98  	}
99  
100 	@Test
101 	public void testFindPreferencesWithEmptyUsername(){
102 		// Non-existant User
103 		List<Preference> preferences = getService().findPreferencesByUsername("");
104 		assertFalse("FindPreferencesByUsername returned null and should not have.",null==preferences);
105 		assertFalse("FindPreferencesByUsername returned no preferences.", preferences.size() > 0);
106 	}
107 	
108 	@Test
109 	public void testFindPreferencesWithUsernameAndShortname(){
110 		Preference preferences = getService().findPreference("userA","KME_PUSH");
111 		assertFalse("FindPreferencesWithUsernameAndShortname returned null and should not have.",null==preferences);
112 	}
113 
114 	@Test
115 	public void testFindPreferencesWithWrongUsernameAndShortname(){
116 		//Non-existant user
117 		Preference preferences = getService().findPreference("userB","KME_PUSH");
118 		assertTrue("FindPreferencesWithWrongUsernameAndShortname should have returned null and didn't.",null==preferences);
119 
120 		// Non-existant sender.
121 		preferences = getDao().findPreference("userA","KME_PUSH4");
122 		assertTrue("FindPreferencesWithWrongUsernameAndShortname should have returned null and didn't.",null==preferences);
123 	}
124 	
125 	@Test
126 	public void testFindPreferenceWithUsernameAndSender(){
127 		Sender sender = new Sender();
128 		sender.setShortName("KME_PUSH");
129 		sender.setId(1L);
130 		Preference preferences = getService().findPreference("userA",sender);
131 		// Fails for some reason. Shouldn't. 
132 		//assertFalse("FindPreferenceWithUsernameAndSender returned null and should not have.",null==preferences);		
133 	}
134 
135 	@Test
136 	public void testFindPreferenceWithWrongUsernameAndSender(){
137 		Sender sender = new Sender();
138 		sender.setShortName("KME_PUSH");
139 		sender.setId(1L);
140 		//Wrong username
141 		Preference preferences = getService().findPreference("userB",sender);
142 		assertTrue("FindPreferenceWithWrongUsernameAndSender should have returned null and didn't.",null==preferences);		
143 
144 		//Wrong sender
145 		sender.setId(4L);
146 		preferences = getService().findPreference("userA",sender);
147 		assertTrue("FindPreferenceWithWrongUsernameAndSender should have returned null and didn't.",null==preferences);		
148 	}
149 	
150 	@Test
151 	public void testFindPreferenceWithID(){
152 		long id = 1L;
153 		Preference preferences = getService().findPreference(id);
154 		assertFalse("FindPreferenceWithID returned null and should not have.",null==preferences);		
155 	}
156 	
157 	@Test
158 	public void testSavePreference(){
159 		Preference pref = new Preference();
160 		pref.setEnabled(true);
161 		pref.setPushSenderID(1L);
162 		pref.setUsername("userA");
163 		try{
164 			getService().savePreference(pref);	
165 		}catch(Exception e){
166 			fail("Exception thrown when saveDevice tries to insert with null primary key.");
167 		}
168 		assertFalse("Preference saved without id and should not have.",null==pref.getId());
169 	}
170 	
171 	@Test
172 	public void testSaveNullPreference(){
173 		try {
174 			getService().savePreference(null);
175 		} catch(Exception e) {
176 			fail("Exception thrown when savePreference tries to save a null object.");
177 		}
178 	}
179 	
180 	@Test
181 	public void testRemoveNullPreference() {
182 		boolean didRemove = getService().removePreference(null);
183 		assertFalse("Removed a device and should not have.",didRemove);
184 	}
185 	
186 	@Test
187 	public void testRemovePreferenceNotInDatabase() {
188 		Preference pref = new Preference();
189 		boolean didRemove = getService().removePreference(pref);
190 		assertFalse("Removed a Preference that isn't in the database.",didRemove);
191 	}
192 	
193 	/**
194 	 * @return the entityManager
195 	 */
196 	public EntityManager getEntityManager() {
197 		return entityManager;
198 	}
199 
200 	/**
201 	 * @param entityManager the entityManager to set
202 	 */
203 	public void setEntityManager(EntityManager entityManager) {
204 		this.entityManager = entityManager;
205 	}
206 
207 	/**
208 	 * @return the service
209 	 */
210 	public PreferenceService getService() {
211 		return service;
212 	}
213 
214 	/**
215 	 * @param service the service to set
216 	 */
217 	public void setService(PreferenceService service) {
218 		this.service = service;
219 	}
220 
221 	/**
222 	 * @return the dao
223 	 */
224 	public PreferenceDao getDao() {
225 		return dao;
226 	}
227 
228 	/**
229 	 * @param dao the dao to set
230 	 */
231 	public void setDao(PreferenceDao dao) {
232 		this.dao = dao;
233 	}
234 	
235 	
236 	
237 }