diff --git a/tool/pom.xml b/tool/pom.xml index 1c59c5b..1dd00b0 100644 --- a/tool/pom.xml +++ b/tool/pom.xml @@ -130,8 +130,25 @@ 2.1 + + + junit + junit + 4.11 + test + + + org.mockito + mockito-core + 1.8.4 + test + + + org.sakaiproject + sakai-calendar-api + - + src/java @@ -148,5 +165,10 @@ + + + ${basedir}/src/test + + \ No newline at end of file diff --git a/tool/src/java/org/sakaiproject/signup/tool/jsf/SignupMeetingsBean.java b/tool/src/java/org/sakaiproject/signup/tool/jsf/SignupMeetingsBean.java index deb893c..1ea151d 100644 --- a/tool/src/java/org/sakaiproject/signup/tool/jsf/SignupMeetingsBean.java +++ b/tool/src/java/org/sakaiproject/signup/tool/jsf/SignupMeetingsBean.java @@ -258,15 +258,15 @@ public class SignupMeetingsBean implements SignupBeanConstants { //avoid multiple calls for one page loading : not refresh within one second if(allCategories == null || curr_time - lastUpdatedCatTime > 1000){ List categories = new ArrayList(); - List allCategories = null; + List allCats = null; try { - allCategories = signupMeetingService.getAllCategories(sakaiFacade.getCurrentLocationId()); + allCats = signupMeetingService.getAllCategories(sakaiFacade.getCurrentLocationId()); } catch (Exception e) { //do nothing - allCategories=null; + allCats=null; } - if(allCategories !=null){ - for(String c : allCategories) { + if(allCats !=null){ + for(String c : allCats) { if(StringUtils.isNotBlank(c)) { categories.add(new SelectItem(c)); } @@ -282,10 +282,10 @@ public class SignupMeetingsBean implements SignupBeanConstants { } lastUpdatedCatTime = curr_time; - allLocations = categories; + allCategories = categories; } - return allLocations; + return allCategories; } /** diff --git a/tool/src/test/org/sakaiproject/signup/tool/jsf/SignupMeetingsBeanTest.java b/tool/src/test/org/sakaiproject/signup/tool/jsf/SignupMeetingsBeanTest.java new file mode 100644 index 0000000..369a98c --- /dev/null +++ b/tool/src/test/org/sakaiproject/signup/tool/jsf/SignupMeetingsBeanTest.java @@ -0,0 +1,61 @@ +package org.sakaiproject.signup.tool.jsf; + +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; +import org.sakaiproject.signup.logic.SakaiFacade; +import org.sakaiproject.signup.logic.SignupMeetingService; +import java.util.ArrayList; + +import static org.mockito.Mockito.*; + +/** + * @author Ben Holmes + */ +public class SignupMeetingsBeanTest { + + private SignupMeetingsBean _signupMeetings; + + @Before + public void setUp() { + _signupMeetings = new SignupMeetingsBean(); + + SakaiFacade mockSakaiFacade = mock(SakaiFacade.class); + when(mockSakaiFacade.getCurrentLocationId()).thenReturn("siteId"); + _signupMeetings.setSakaiFacade(mockSakaiFacade); + } + + private void setMockedLocations(ArrayList locations) throws Exception { + SignupMeetingService mockMeetingsService = mock(SignupMeetingService.class); + when(mockMeetingsService.getAllLocations("siteId")).thenReturn(locations); + _signupMeetings.setSignupMeetingService(mockMeetingsService); + } + + @Test + public void testShouldCacheLocations() throws Exception { + ArrayList locations = new ArrayList(); + setMockedLocations(locations); + Assert.assertTrue(_signupMeetings.getAllLocations().isEmpty()); + + locations.add("Extra location"); + Assert.assertTrue(_signupMeetings.getAllLocations().isEmpty()); + } + + @Test + public void testShouldBehaveConsistentlyWithCategories() throws Exception { + + ArrayList locations = new ArrayList(); + setMockedLocations(locations); + locations.add("Extra location"); + Assert.assertFalse(_signupMeetings.getAllLocations().isEmpty()); + + // make a cheeky call to categories + SignupMeetingService mockMeetingsService = mock(SignupMeetingService.class); + when(mockMeetingsService.getAllCategories("siteId")).thenReturn(new ArrayList()); + _signupMeetings.setSignupMeetingService(mockMeetingsService); + _signupMeetings.getAllCategories(); + + Assert.assertFalse(_signupMeetings.getAllLocations().isEmpty()); + } + +}