1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.news.service;
17
18 import static org.junit.Assert.assertTrue;
19
20 import java.util.List;
21
22 import org.apache.log4j.Logger;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.kuali.mobility.news.entity.NewsArticle;
26 import org.kuali.mobility.news.entity.NewsSource;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.support.FileSystemXmlApplicationContext;
29
30 public class NewsServiceImplTest {
31
32 private static final Logger LOG = Logger.getLogger( NewsServiceImplTest.class );
33
34 private static ApplicationContext applicationContext;
35
36 @BeforeClass
37 public static void createApplicationContext() {
38 NewsServiceImplTest.setApplicationContext(new FileSystemXmlApplicationContext(getConfigLocations()));
39 }
40
41 private static String[] getConfigLocations() {
42 return new String[] { "classpath:/SpringBeans.xml" };
43 }
44
45 @Test
46 public void testGetAllNewsSources() {
47 NewsService service = (NewsService)getApplicationContext().getBean("newsService");
48 assertTrue( "Service did not instantiate properly.", service != null );
49 assertTrue( "Service does not have a dao reference.", service.getDao() != null );
50 assertTrue( "Service does not have a cache reference.", service.getCache() != null );
51 List<NewsSource> sources = (List<NewsSource>)(List<?>)service.getAllNewsSources();
52 assertTrue( "Failed to find news sources.", sources != null && sources.size() > 0 );
53 }
54
55 @Test
56 public void testGetAllActiveNewsSources() {
57 NewsService service = (NewsService)getApplicationContext().getBean("newsService");
58 List<NewsSource> sources = (List<NewsSource>)(List<?>)service.getAllNewsSources();
59 assertTrue( "Failed to find news sources.", sources != null && sources.size() > 0 );
60 }
61
62 @Test
63 public void testGetNewsSourceById() {
64 NewsService service = (NewsService)getApplicationContext().getBean("newsService");
65 NewsSource source = service.getNewsSourceById(new Long(2) );
66 assertTrue( "Failed to find news source.", source != null && "BBC - News".equalsIgnoreCase( source.getName() ) );
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 @Test
91 public void testGetNewsArticle() {
92 NewsService service = (NewsService)getApplicationContext().getBean("newsService");
93 NewsSource source = service.getNewsSourceById( new Long(2) );
94 assertTrue( "Failed to find news feed for source 2.", source != null && source.getArticles() != null && source.getArticles().size() > 0 );
95 NewsArticle articleFirst = source.getArticles().get(0);
96 assertTrue( "No articles found for feed in source 2.", articleFirst != null );
97 LOG.debug( "Article ID: "+articleFirst.getArticleId() );
98 LOG.debug( "Article Title: "+articleFirst.getTitle() );
99 LOG.debug( "Article's Source ID: "+articleFirst.getSourceId() );
100 NewsArticle articleSecond = service.getNewsArticle(articleFirst.getArticleId(), articleFirst.getSourceId() );
101 assertTrue( "Failed to lookup article by source ID, article ID.", articleSecond != null);
102 articleSecond = null;
103 articleSecond = service.getNewsArticle(articleFirst.getArticleId());
104 assertTrue( "Failed to lookup article by article ID.", articleSecond != null);
105 }
106
107 public static ApplicationContext getApplicationContext() {
108 return applicationContext;
109 }
110
111 public static void setApplicationContext(ApplicationContext applicationContext) {
112 NewsServiceImplTest.applicationContext = applicationContext;
113 }
114
115 }