1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.alerts.service;
16
17 import static org.junit.Assert.*;
18
19 import java.io.IOException;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.log4j.Logger;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.kuali.mobility.alerts.entity.Alert;
31 import org.kuali.mobility.alerts.entity.Alerts;
32 import org.kuali.mobility.campus.entity.Campus;
33 import org.kuali.mobility.campus.service.CampusService;
34 import org.kuali.mobility.util.mapper.DataMapper;
35 import org.mockito.Mock;
36 import org.springframework.context.ApplicationContext;
37 import static org.mockito.Mockito.when;
38 import static org.mockito.Matchers.*;
39
40 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
41 public class AlertsServiceImplTest {
42 private static final Logger LOG = Logger.getLogger(AlertsServiceImplTest.class);
43 private static ApplicationContext applicationContext;
44
45 private AlertsServiceImpl service;
46
47 @Mock
48 DataMapper dataMapper;
49
50 @Mock
51 CampusService campusService;
52
53 public static ApplicationContext getApplicationContext() {
54 return applicationContext;
55 }
56 public static void setApplicationContext(ApplicationContext applicationContext) {
57 AlertsServiceImplTest.applicationContext = applicationContext;
58 }
59
60 @Before
61 public void setUpClass() throws Exception {
62 setService(new AlertsServiceImpl());
63 getService().setCacheMinutes("0");
64 service.setCampusService(campusService);
65 service.setDataMapper(dataMapper);
66
67 Map<String, List<String>> alertUrls = new HashMap<String, List<String>>();
68 List<String> urls = new ArrayList<String>();
69 urls.add("http://indianauniversity.info");
70 alertUrls.put("BL", urls);
71 ArrayList<Alert> alerts = new ArrayList<Alert>();
72 Alert alert = new Alert();
73 alert.setCampus("IU");
74 alert.setType("Emergency");
75 alert.setKey(1);
76 alert.setTitle("Earthquake Emergency");
77 alert.setPriority("1");
78 alert.setMobileText("Earthquake test");
79 alert.setUrl("http://indianauniversity.info");
80 alerts.add(alert);
81 Alert alert2 = new Alert();
82 alert2.setCampus("IU");
83 alert2.setType("Emergency");
84 alert2.setKey(2);
85 alert2.setTitle("Sunshine Emergency");
86 alert2.setPriority("1");
87 alert2.setMobileText("Sunshine test");
88 alert2.setUrl("http://indianauniversity.info");
89 alerts.add(alert2);
90
91 try {
92 when(dataMapper.mapData(anyObject(), any(URL.class), any(URL.class))).thenReturn(alerts);
93 } catch (ClassNotFoundException e) {
94 e.printStackTrace();
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 service.setAlertUrls(alertUrls);
99 service.setDataMappingUrl("https://www.indiana.edu/~iumobile/unit-testData/alertMapping.xml");
100 }
101
102 @Test
103 public void testFindAlertsByCampus() {
104 List<Campus> campuses = new ArrayList<Campus>();
105 Campus campus = new Campus();
106 campus.setCode("BL");
107 campuses.add(campus);
108 when(campusService.findCampusesByTool(any(String.class))).thenReturn(campuses);
109
110 List<Alert> BLAlerts = service.findAlertsByCampus("BL");
111 assertTrue("Two alerts were not found", BLAlerts.get(0).getCampus().equalsIgnoreCase("IU"));
112 List<Alert> ZZAlerts = service.findAlertsByCampus("ZZ");
113 assertTrue("Found campus ZZ instead of ALL", ZZAlerts.get(0).getCampus().equalsIgnoreCase("ALL"));
114 List<Alert> ALLAlerts = service.findAlertsByCampus("");
115 assertTrue("Did not find Both test Alerts", ALLAlerts.size() == 2);
116 }
117
118 @Test
119 public void testFindCountByCampus() {
120 List<Campus> campuses = new ArrayList<Campus>();
121 Campus campus = new Campus();
122 campus.setCode("BL");
123 campuses.add(campus);
124 when(campusService.findCampusesByTool(any(String.class))).thenReturn(campuses);
125
126 int BLCount = service.findAlertCountByCampus("BL");
127 assertTrue("Did not find two BL alerts", BLCount == 2);
128 int ZZCount = service.findAlertCountByCampus("ZZ");
129 assertTrue("Found a campus ZZ", ZZCount == 0);
130 int ALLCount = service.findAlertCountByCampus("");
131 assertTrue("Did not find the two test Alerts", ALLCount == 2);
132 }
133
134 public AlertsServiceImpl getService() {
135 return service;
136 }
137
138 public void setService(AlertsServiceImpl service) {
139 this.service = service;
140 }
141 }