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.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.kuali.mobility.news.entity.NewsSource;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.context.ApplicationContextAware;
23 import org.springframework.test.context.ContextConfiguration;
24 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25
26 import javax.annotation.Resource;
27 import java.util.List;
28
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32
33
34
35 @RunWith(SpringJUnit4ClassRunner.class)
36 @ContextConfiguration(value = "classpath:SpringBeans.xml")
37 public class NewsDaoImplTest implements ApplicationContextAware {
38
39 private static final Logger LOG = Logger.getLogger(NewsDaoImplTest.class);
40 private ApplicationContext applicationContext;
41 @Resource(name="newsDao")
42 private NewsDaoImpl dao;
43 @Resource(name="newsInitBean")
44 private NewsInitBean initBean;
45
46 @Before
47 public void setUpTests() {
48 getInitBean().loadData();
49 }
50
51 @Test
52 public void testFindAllActiveNewsSources() {
53 List<NewsSource> sources = (List<NewsSource>)getDao().findAllActiveNewsSources();
54 assertTrue("Failed to find news sources.", sources != null && sources.size() > 0);
55 }
56
57 @Test
58 public void testFindAllNewsSources() {
59 List<NewsSource> sources = (List<NewsSource>)getDao().findAllNewsSources();
60 assertTrue("Failed to find news sources.", sources != null && sources.size() > 0);
61 }
62
63 @Test
64 public void testFindNewsSources() {
65 List<NewsSource> sources = (List<NewsSource>)getDao().findNewsSources(Long.valueOf(0), new Boolean(true));
66 assertTrue("Failed to find news sources.", sources != null && sources.size() > 0);
67 }
68
69 @Test
70 public void testLookup() {
71 NewsSource source = getDao().lookup(new Long(2));
72 assertTrue("Failed to find news source.", source != null && "BBC - News".equalsIgnoreCase(source.getName()));
73 }
74
75 @Test
76 public void testUpdateCache() {
77 Long id = new Long(50);
78 NewsSource source = getDao().lookup(id);
79 assertTrue("Failed to find news source for id.", source!=null );
80
81 getDao().getCache().updateCache(source);
82 NewsSource source2 = getDao().lookup(id);
83 assertTrue("Failed to find news source for id after update.", source != null );
84 }
85
86 @Test
87 public void testUpdateCacheWithInactiveSource() {
88 NewsSource source = (NewsSource)getApplicationContext().getBean("newsSource");
89 source.setId( new Long(1337) );
90 source.setActive(false);
91
92 getDao().getCache().updateCache(source);
93 assertTrue("Source added to cache and should not have been.",null==getDao().lookup(source.getId()));
94 }
95
96 @Test
97 public void testUpdateCacheWithNewSource() {
98 NewsSource source = (NewsSource)getApplicationContext().getBean("newsSource");
99 source.setId( new Long(1337) );
100 source.setActive(true);
101 source.setTitle("CNN.com - Travel");
102 source.setUrl("http://rss.cnn.com/rss/cnn_travel.rss");
103
104 getDao().getCache().updateCache(source);
105
106 NewsSource source2 = getDao().lookup(source.getId());
107 assertTrue("Source failed to add to cache.",source2!=null);
108
109 assertTrue("Source added did not have articles.",null!=source2.getArticles() && !source2.getArticles().isEmpty());
110 }
111
112 @Test
113 public void testUpdateSourceWithNull() {
114 try {
115 getDao().getCache().updateSource(null);
116 } catch( Exception e ) {
117 fail("Exception thrown when updating sources in cache with null objects.");
118 }
119 }
120
121 public ApplicationContext getApplicationContext() {
122 return applicationContext;
123 }
124
125 public void setApplicationContext(ApplicationContext applicationContext) {
126 this.applicationContext = applicationContext;
127 }
128
129 public NewsDaoImpl getDao() {
130 return dao;
131 }
132
133 public void setDao(NewsDaoImpl dao) {
134 this.dao = dao;
135 }
136
137 public NewsInitBean getInitBean() {
138 return initBean;
139 }
140
141 public void setInitBean(NewsInitBean initBean) {
142 this.initBean = initBean;
143 }
144 }