PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/user/flag/TestFlagDismissalServiceImpl.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 234 lines | 199 code | 32 blank | 3 comment | 1 complexity | 2c684f68bbe5d999cdfa4fd95cb7aa98 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.user.flag;
  2. import com.atlassian.core.util.Clock;
  3. import com.atlassian.jira.propertyset.JiraPropertySetFactory;
  4. import com.atlassian.jira.user.ApplicationUser;
  5. import com.atlassian.jira.user.preferences.ExtendedPreferences;
  6. import com.atlassian.jira.user.preferences.UserPreferencesManager;
  7. import com.atlassian.jira.util.json.JSONObject;
  8. import com.atlassian.mock.propertyset.MockPropertySet;
  9. import com.opensymphony.module.propertyset.PropertySet;
  10. import org.hamcrest.Matcher;
  11. import org.hamcrest.collection.IsEmptyCollection;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.mockito.ArgumentCaptor;
  15. import org.mockito.Mock;
  16. import org.mockito.MockitoAnnotations;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import static com.google.common.collect.Lists.newArrayList;
  22. import static com.google.common.collect.Maps.newHashMap;
  23. import static org.hamcrest.MatcherAssert.assertThat;
  24. import static org.hamcrest.Matchers.nullValue;
  25. import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
  26. import static org.mockito.Matchers.anyString;
  27. import static org.mockito.Matchers.argThat;
  28. import static org.mockito.Matchers.eq;
  29. import static org.mockito.Mockito.doNothing;
  30. import static org.mockito.Mockito.verify;
  31. import static org.mockito.Mockito.verifyNoMoreInteractions;
  32. import static org.mockito.Mockito.when;
  33. public class TestFlagDismissalServiceImpl {
  34. private static final String FLAG_KEY = "flag.key";
  35. private static final String DISMISSALS_KEY = "com.atlassian.jira.flag.dismissals";
  36. private static final String RESETS_KEY = "com.atlassian.jira.flag.resets";
  37. private static final long DISMISSAL_TIME = 1234L;
  38. @Mock
  39. private ExtendedPreferences extendedPreferences;
  40. @Mock
  41. private UserPreferencesManager userPreferencesManager;
  42. @Mock
  43. private JiraPropertySetFactory jiraPropertySetFactory;
  44. @Mock
  45. private PropertySet propertySet = new MockPropertySet();
  46. @Mock
  47. private Clock clock;
  48. @Mock
  49. private Date date;
  50. @Mock
  51. private ApplicationUser user;
  52. private FlagDismissalServiceImpl flagDismissalService;
  53. @Before
  54. public void setUp() throws Exception {
  55. MockitoAnnotations.initMocks(this);
  56. flagDismissalService = new FlagDismissalServiceImpl(
  57. userPreferencesManager,
  58. jiraPropertySetFactory,
  59. clock);
  60. when(userPreferencesManager
  61. .getExtendedPreferences(user))
  62. .thenReturn(extendedPreferences);
  63. when(extendedPreferences.getText(anyString())).thenReturn("");
  64. when(clock.getCurrentDate()).thenReturn(date);
  65. when(date.getTime()).thenReturn(DISMISSAL_TIME);
  66. when(jiraPropertySetFactory
  67. .buildCachingDefaultPropertySet(anyString()))
  68. .thenReturn(propertySet);
  69. }
  70. @Test
  71. public void removeDismissFlagForUserWhenUserIsNullIsNoop() {
  72. flagDismissalService.removeDismissFlagForUser("something", null);
  73. verifyNoMoreInteractions(userPreferencesManager);
  74. verifyNoMoreInteractions(propertySet);
  75. verifyNoMoreInteractions(extendedPreferences);
  76. }
  77. @Test
  78. public void removeDismissFlagForUserWhenFlagIsBlankIsNoop() {
  79. flagDismissalService.removeDismissFlagForUser("", user);
  80. flagDismissalService.removeDismissFlagForUser(null, user);
  81. verifyNoMoreInteractions(userPreferencesManager);
  82. verifyNoMoreInteractions(propertySet);
  83. verifyNoMoreInteractions(extendedPreferences);
  84. }
  85. @Test
  86. public void removeDismissFlagForUserRemovesFlagFromUser() throws Exception {
  87. //given
  88. JSONObject jsonObject = new JSONObject();
  89. jsonObject.put(FLAG_KEY, DISMISSAL_TIME);
  90. final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
  91. when(extendedPreferences.getText(DISMISSALS_KEY)).thenReturn(jsonObject.toString());
  92. doNothing().when(extendedPreferences).setText(eq(DISMISSALS_KEY), captor.capture());
  93. //when
  94. flagDismissalService.removeDismissFlagForUser(FLAG_KEY, user);
  95. //then
  96. JSONObject object = new JSONObject(captor.getValue());
  97. assertThat(object.opt(FLAG_KEY), nullValue());
  98. }
  99. @Test
  100. public void dismissFlagForUserIsNoOpWhenUserIsNull() throws Exception {
  101. flagDismissalService.dismissFlagForUser(FLAG_KEY, null);
  102. verifyNoMoreInteractions(userPreferencesManager);
  103. verifyNoMoreInteractions(jiraPropertySetFactory);
  104. }
  105. @Test
  106. public void dismissFlagForUserIsNoOpWhenFlagKeyIsBlank() throws Exception {
  107. flagDismissalService.dismissFlagForUser("", user);
  108. verifyNoMoreInteractions(userPreferencesManager);
  109. verifyNoMoreInteractions(jiraPropertySetFactory);
  110. }
  111. @Test
  112. public void dismissFlagForUserIsNoOpWhenFlagKeyIsNull() throws Exception {
  113. flagDismissalService.dismissFlagForUser(null, user);
  114. verifyNoMoreInteractions(userPreferencesManager);
  115. verifyNoMoreInteractions(jiraPropertySetFactory);
  116. }
  117. @Test
  118. public void dismissFlagForUserSavesDismissalsAgainstProvidedUser()
  119. throws Exception {
  120. flagDismissalService.dismissFlagForUser(FLAG_KEY, user);
  121. verify(extendedPreferences).getText(DISMISSALS_KEY);
  122. verify(extendedPreferences).setText(
  123. eq(DISMISSALS_KEY),
  124. anyString());
  125. }
  126. @Test
  127. public void dismissFlagForUserCreatesDismissalEntryWhenNoneExistsForUser()
  128. throws Exception {
  129. when(extendedPreferences.getText(anyString())).thenReturn(null);
  130. flagDismissalService.dismissFlagForUser(FLAG_KEY, user);
  131. verify(extendedPreferences).setText(eq(DISMISSALS_KEY), argThat(isJsonWithLongEntry(FLAG_KEY, DISMISSAL_TIME)));
  132. }
  133. @Test
  134. public void dismissFlagForUserUpdatesExistingDismissalEntryWhenOneExists()
  135. throws Exception {
  136. when(extendedPreferences.getText(anyString())).thenReturn(asJsonString(FLAG_KEY, 123L));
  137. flagDismissalService.dismissFlagForUser(FLAG_KEY, user);
  138. verify(extendedPreferences).setText(eq(DISMISSALS_KEY), argThat(isJsonWithLongEntry(FLAG_KEY, DISMISSAL_TIME)));
  139. }
  140. @Test
  141. public void dismissFlagForUserPreservesOtherDismissalsWhenTheyArePresent()
  142. throws Exception {
  143. when(extendedPreferences.getText(anyString())).thenReturn(asJsonString("other.flag.key", 10L));
  144. flagDismissalService.dismissFlagForUser(FLAG_KEY, user);
  145. verify(extendedPreferences).setText(eq(DISMISSALS_KEY), argThat(isJsonWithLongEntry(FLAG_KEY, DISMISSAL_TIME)));
  146. verify(extendedPreferences).setText(eq(DISMISSALS_KEY), argThat(isJsonWithLongEntry("other.flag.key", 10L)));
  147. }
  148. @Test
  149. public void testGetDismissedFlagsForUserReturnsDismissedFlagsForUser() throws Exception {
  150. when(extendedPreferences
  151. .getText(DISMISSALS_KEY))
  152. .thenReturn(asJsonString(FLAG_KEY, 1L, "other.flag.key", 2L));
  153. Collection<String> flags = flagDismissalService.getDismissedFlagsForUser(user);
  154. assertThat(flags, containsInAnyOrder(FLAG_KEY, "other.flag.key"));
  155. }
  156. @Test
  157. public void testGetDismissedFlagsForUserReturnsEmptyListWhenUserHasNoDismissals()
  158. throws Exception {
  159. when(extendedPreferences.getText(DISMISSALS_KEY)).thenReturn(null);
  160. Collection<String> flags = flagDismissalService.getDismissedFlagsForUser(user);
  161. assertThat(flags, IsEmptyCollection.<String>empty());
  162. }
  163. @Test
  164. public void testGetDismissedFlagsForUserExcludesResetFlags()
  165. throws Exception {
  166. when(extendedPreferences
  167. .getText(DISMISSALS_KEY))
  168. .thenReturn(asJsonString(FLAG_KEY, 999L));
  169. when(propertySet.getText(RESETS_KEY)).thenReturn(asJsonString(FLAG_KEY, 1000L));
  170. Collection<String> flags = flagDismissalService.getDismissedFlagsForUser(user);
  171. assertThat(flags, IsEmptyCollection.<String>empty());
  172. }
  173. @Test
  174. public void testGetDismissedFlagsForUserIncludesFlagsDismissedSinceReset() throws Exception {
  175. when(extendedPreferences
  176. .getText(DISMISSALS_KEY))
  177. .thenReturn(asJsonString(FLAG_KEY, 1000L));
  178. when(propertySet.getText(RESETS_KEY)).thenReturn(asJsonString(FLAG_KEY, 999L));
  179. Collection<String> flags = flagDismissalService.getDismissedFlagsForUser(user);
  180. assertThat(flags, containsInAnyOrder(FLAG_KEY));
  181. }
  182. @Test
  183. public void testResetFlagDismissalsStoresTimestampAgainstFlag()
  184. throws Exception {
  185. flagDismissalService.resetFlagDismissals(FLAG_KEY);
  186. verify(propertySet).setText(
  187. eq(RESETS_KEY), argThat(isJsonWithLongEntry(FLAG_KEY, DISMISSAL_TIME)));
  188. }
  189. private String asJsonString(Object... keysAndValues) {
  190. Iterator<Object> entries = newArrayList(keysAndValues).iterator();
  191. Map<String, Object> jsonEntries = newHashMap();
  192. while (entries.hasNext()) {
  193. jsonEntries.put(
  194. String.valueOf(entries.next()),
  195. entries.hasNext() ? entries.next() : null);
  196. }
  197. return new JSONObject(jsonEntries).toString();
  198. }
  199. private static Matcher<String> isJsonWithLongEntry(final String key, final long value) {
  200. return new JsonLongMatcher(key, value);
  201. }
  202. }