1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
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
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
117 Preference preferences = getService().findPreference("userB","KME_PUSH");
118 assertTrue("FindPreferencesWithWrongUsernameAndShortname should have returned null and didn't.",null==preferences);
119
120
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
132
133 }
134
135 @Test
136 public void testFindPreferenceWithWrongUsernameAndSender(){
137 Sender sender = new Sender();
138 sender.setShortName("KME_PUSH");
139 sender.setId(1L);
140
141 Preference preferences = getService().findPreference("userB",sender);
142 assertTrue("FindPreferenceWithWrongUsernameAndSender should have returned null and didn't.",null==preferences);
143
144
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
195
196 public EntityManager getEntityManager() {
197 return entityManager;
198 }
199
200
201
202
203 public void setEntityManager(EntityManager entityManager) {
204 this.entityManager = entityManager;
205 }
206
207
208
209
210 public PreferenceService getService() {
211 return service;
212 }
213
214
215
216
217 public void setService(PreferenceService service) {
218 this.service = service;
219 }
220
221
222
223
224 public PreferenceDao getDao() {
225 return dao;
226 }
227
228
229
230
231 public void setDao(PreferenceDao dao) {
232 this.dao = dao;
233 }
234
235
236
237 }