1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.kuali.mobility.news.dao;
15
16 import org.apache.log4j.Logger;
17 import org.junit.*;
18 import org.kuali.mobility.news.entity.NewsSource;
19 import org.kuali.mobility.news.entity.NewsSourceImpl;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.support.FileSystemXmlApplicationContext;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29
30
31
32 public class NewsCacheImplTest {
33 private static final Logger LOG = Logger.getLogger( NewsCacheImplTest.class );
34
35 private static ApplicationContext applicationContext;
36 private NewsCache dao;
37
38 public NewsCacheImplTest() {
39 }
40
41 private static String[] getConfigLocations() {
42 return new String[] { "classpath:/SpringBeans.xml" };
43 }
44
45 @BeforeClass
46 public static void setUpClass() throws Exception {
47 NewsCacheImplTest.setApplicationContext( new FileSystemXmlApplicationContext( getConfigLocations() ));
48 }
49
50 @AfterClass
51 public static void tearDownClass() throws Exception {
52 }
53
54 @Before
55 public void setUp() {
56 }
57
58 @After
59 public void tearDown() {
60 }
61
62 @Test
63 public void testUpdateCacheForExistingSourceId() {
64 NewsCache newsCache = (NewsCache) getApplicationContext().getBean("newsCache");
65 NewsSource source = (NewsSource) getApplicationContext().getBean("newsSource");
66 long existingSourceId = 1337;
67 source.setId(existingSourceId);
68 populateMap();
69 Map<Long, NewsSource> newsSources = newsCache.getNewsSources();
70
71 assertTrue("Source id is not available", newsSources.containsKey(existingSourceId));
72 newsCache.updateCache(source);
73 assertTrue("Source id is not available", newsSources.containsKey(existingSourceId));
74
75 }
76
77 @Test
78 public void testUpdateCacheForNonExistingSourceId() {
79 NewsCache newsCache = (NewsCache) getApplicationContext().getBean("newsCache");
80 NewsSource source = (NewsSource) getApplicationContext().getBean("newsSource");
81 long nonExistingSourceId = 1336;
82 source.setId(nonExistingSourceId);
83 Map<Long, NewsSource> newsSources = newsCache.getNewsSources();
84 if(!newsSources.isEmpty()) {
85 assertTrue("Source id is not available", newsSources.containsKey(nonExistingSourceId));
86 newsCache.updateCache(source);
87 assertTrue("Source id is not available", newsSources.containsKey(nonExistingSourceId));
88 }
89 }
90
91 @Test
92 public void testGetNewsSources() {
93 NewsCache newsCache = (NewsCache) getApplicationContext().getBean("newsCache");
94 Map<Long, NewsSource> newsSources = newsCache.getNewsSources();
95 assertTrue("Failed to load News Sources", newsSources != null);
96 }
97
98 @Test
99 public void testUpdateSource() {
100 NewsCache newsCache = (NewsCache) getApplicationContext().getBean("newsCache");
101 NewsSource source = (NewsSource) getApplicationContext().getBean("newsSource");
102 assertTrue("Source is null, Can not update", source != null);
103 newsCache.updateSource(source);
104 assertTrue("Source is null, Can not update", source != null);
105 }
106
107
108
109
110 public static ApplicationContext getApplicationContext() {
111 return applicationContext;
112 }
113
114
115
116
117 public static void setApplicationContext(ApplicationContext aApplicationContext) {
118 applicationContext = aApplicationContext;
119 }
120
121
122
123
124 public NewsCache getDao() {
125 return dao;
126 }
127
128
129
130
131 public void setDao(NewsCache dao) {
132 this.dao = dao;
133 }
134
135 public void populateMap() {
136 NewsCache newsCache = (NewsCache) getApplicationContext().getBean("newsCache");
137 NewsSource source = (NewsSource) getApplicationContext().getBean("newsSource");
138
139 Map<Long, NewsSource> newsSources = new HashMap<Long, NewsSource>();
140
141 Long sourceId = new Long(1337);
142 source.setId(new Long(1337));
143 newsSources.put(sourceId, source);
144 newsCache.setNewsSources(newsSources);
145 }
146 }