1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.push.service;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.kuali.mobility.push.dao.PushDao;
21 import org.kuali.mobility.push.entity.Device;
22 import org.kuali.mobility.push.entity.Push;
23 import org.kuali.mobility.push.entity.PushDeviceTuple;
24 import org.kuali.mobility.push.service.send.SendServiceDelegator;
25 import org.mockito.Mock;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.Matchers.any;
32 import static org.mockito.Mockito.when;
33
34
35
36
37 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
38 public class PushServiceImplTest {
39
40 @Mock
41 private PushDao dao;
42 @Mock
43 private DeviceService deviceService;
44 @Mock
45 private SendServiceDelegator delegator;
46 @Mock
47 private PushDeviceTupleService tupleService;
48 private PushServiceImpl service;
49
50 @Before
51 public void preTest() {
52 setService(new PushServiceImpl());
53 getService().setPushDao(getDao());
54 getService().setDeviceService(getDeviceService());
55 getService().setPdtService(getTupleService());
56 getService().setSendService(getDelegator());
57 }
58
59 @Test
60 public void testSavePush() {
61 getService().savePush(new Push());
62 }
63
64 @Test
65 public void testRemovePush() {
66 Push push = getService().findPushById(new Long(1));
67
68 }
69
70
71 @Test
72 public void testSavePushWithDevices() {
73 getService().savePush(new Push(),new ArrayList<Device>());
74 }
75
76 @Test
77 public void testFindPushById() {
78 Push p = new Push();
79 p.setPushId(new Long(53));
80 when(getDao().findPushById(p.getPushId())).thenReturn(p);
81 Push p2 = getService().findPushById(p.getPushId());
82 getService().getPushDao();
83 assertTrue("Failed to find expected push object by ID.", p.equals(p2));
84 }
85
86 @Test
87 public void testFindDevicesForPush() {
88 Push p = new Push();
89 p.setPushId(new Long(53));
90 List<Device> devices = new ArrayList<Device>();
91 devices.add(new Device());
92 devices.add(new Device());
93 when(getDao().findDevicesForPush(p)).thenReturn(devices);
94 List<Device> d2 = getService().findDevicesForPush(p);
95 assertTrue("Failed to find devices for push.",d2!=null && !d2.isEmpty());
96 assertTrue("Failed to return the proper number of devices for push.",d2.size()==2);
97 }
98
99 @Test
100 public void testFindUnsentPushTuples() {
101 List<PushDeviceTuple> tuples = new ArrayList<PushDeviceTuple>();
102 tuples.add(new PushDeviceTuple());
103 tuples.add(new PushDeviceTuple());
104 tuples.add(new PushDeviceTuple());
105 when(getDao().findUnsentPushTuples()).thenReturn(tuples);
106 List<PushDeviceTuple> t2 = getService().findUnsentPushTuples();
107 assertTrue("Failed to find unsent tuples.",t2!=null && !t2.isEmpty());
108 assertTrue("Failed to return the proper number of unsent tuples.",t2.size()==tuples.size() && t2.size()==3);
109 }
110
111 @Test
112 public void testFindAllPush() {
113 List<Push> pushes = new ArrayList<Push>();
114 pushes.add(new Push());
115 pushes.add(new Push());
116 pushes.add(new Push());
117 when(getDao().findAllPush()).thenReturn(pushes);
118 List<Push> p2 = getService().findAllPush();
119 assertTrue("Failed to find all push notifications.",p2!=null && p2.size()==pushes.size());
120 }
121
122 @Test
123 public void testCountPushes() {
124 List<Push> pushes = new ArrayList<Push>();
125 pushes.add(new Push());
126 pushes.add(new Push());
127 pushes.add(new Push());
128 when(getDao().countPushes()).thenReturn(pushes.size());
129 int count = getService().countPushes();
130 assertTrue("Found incorrect number of push notifications.",count==pushes.size());
131 }
132
133 @Test
134 public void testSendPush() {
135 getService().sendPush(new Push(), new Device());
136 }
137
138 @Test
139 public void testSendPushToMultipleDevices() {
140 getService().sendPush(new Push(),new ArrayList<Device>());
141 }
142
143 @Test
144 public void testSendPushWithTuples() {
145 List<PushDeviceTuple> tuples = new ArrayList<PushDeviceTuple>();
146 tuples.add(new PushDeviceTuple());
147 tuples.get(0).setPushId(new Long(1));
148 tuples.get(0).setDeviceId(new Long(9));
149 tuples.add(new PushDeviceTuple());
150 tuples.get(1).setPushId(new Long(2));
151 tuples.get(1).setDeviceId(new Long(8));
152 tuples.add(new PushDeviceTuple());
153 tuples.get(2).setPushId(new Long(3));
154 tuples.get(2).setDeviceId(new Long(7));
155 Push p = new Push();
156 p.setTitle("Faux Title");
157 Device d = new Device();
158 d.setDeviceName("Faux Device");
159 when(getDao().findPushById(any(Long.class))).thenReturn(p);
160 when(getDeviceService().findDeviceById(any(Long.class))).thenReturn(d);
161 getService().sendPush(tuples);
162 }
163
164 public PushDao getDao() {
165 return dao;
166 }
167
168 public void setDao(PushDao dao) {
169 this.dao = dao;
170 }
171
172 public PushServiceImpl getService() {
173 return service;
174 }
175
176 public void setService(PushServiceImpl service) {
177 this.service = service;
178 }
179
180 public DeviceService getDeviceService() {
181 return deviceService;
182 }
183
184 public void setDeviceService(DeviceService deviceService) {
185 this.deviceService = deviceService;
186 }
187
188 public SendServiceDelegator getDelegator() {
189 return delegator;
190 }
191
192 public void setDelegator(SendServiceDelegator delegator) {
193 this.delegator = delegator;
194 }
195
196 public PushDeviceTupleService getTupleService() {
197 return tupleService;
198 }
199
200 public void setTupleService(PushDeviceTupleService tupleService) {
201 this.tupleService = tupleService;
202 }
203 }