View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  //	@Test
70  //	public void testGetAllActiveNewsFeeds() {
71  //		NewsService service = (NewsService)getApplicationContext().getBean("newsService");
72  //		List<NewsFeed> feeds = service.getAllActiveNewsFeeds();
73  //		assertTrue( "Failed to find news feeds.", feeds != null && feeds.size() > 0 );
74  //	}
75  //
76  //	@Test
77  //	public void testGetNewsFeeds() {
78  //		NewsService service = (NewsService)getApplicationContext().getBean("newsService");
79  //		List<NewsFeed> feeds = service.getNewsFeeds( new Long(1), new Boolean( true ) );
80  //		assertTrue( "Failed to find child news feed for source 1.", feeds != null && feeds.size() > 0 );
81  //	}
82  //
83  //	@Test
84  //	public void testGetNewsFeed() {
85  //		NewsService service = (NewsService)getApplicationContext().getBean("newsService");
86  //		NewsFeed feed = service.getNewsFeed( new Long(2) );
87  //		assertTrue( "Failed to find news feed for source 2.", feed != null && feed.getArticles() != null && feed.getArticles().size() > 0 );
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 }