View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational Community
3    * License, Version 2.0 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    *
6    * http://www.osedu.org/licenses/ECL-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
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   * @author Kuali Mobility Team (mobility.collab@kuali.org)
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      * @return the applicationContext
109      */
110     public static ApplicationContext getApplicationContext() {
111         return applicationContext;
112     }
113 
114     /**
115      * @param aApplicationContext the applicationContext to set
116      */
117     public static void setApplicationContext(ApplicationContext aApplicationContext) {
118         applicationContext = aApplicationContext;
119     }
120 
121     /**
122      * @return the dao
123      */
124     public NewsCache getDao() {
125         return dao;
126     }
127 
128     /**
129      * @param dao the dao to set
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         //long sourceId = 1337;
141         Long sourceId = new Long(1337);
142         source.setId(new Long(1337));
143         newsSources.put(sourceId, source);
144         newsCache.setNewsSources(newsSources);
145     }
146 }