View Javadoc

1   package com.atlassian.sal.core.features;
2   
3   import com.atlassian.sal.api.features.SiteDarkFeaturesStorage;
4   import com.atlassian.sal.api.pluginsettings.PluginSettings;
5   import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
6   import com.google.common.collect.ImmutableSet;
7   import org.junit.Before;
8   import org.junit.Test;
9   import org.junit.runner.RunWith;
10  import org.mockito.Mock;
11  import org.mockito.runners.MockitoJUnitRunner;
12  
13  import java.util.Arrays;
14  import java.util.List;
15  
16  import static org.hamcrest.Matchers.hasItem;
17  import static org.hamcrest.Matchers.instanceOf;
18  import static org.junit.Assert.assertThat;
19  import static org.junit.Assert.assertTrue;
20  import static org.mockito.Matchers.any;
21  import static org.mockito.Matchers.anyString;
22  import static org.mockito.Matchers.argThat;
23  import static org.mockito.Mockito.never;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.when;
26  
27  @RunWith(MockitoJUnitRunner.class)
28  public class TestDefaultSiteDarkFeaturesStorage
29  {
30      private static final String FEATURE_KEY = "dark.feature";
31      private static final String ANOTHER_DARK_FEATURE = "another.dark.feature";
32  
33      @Mock private PluginSettingsFactory pluginSettingsFactory;
34      @Mock private PluginSettings pluginSettings;
35  
36      @Before
37      public void setUp()
38      {
39          when(pluginSettingsFactory.createGlobalSettings()).thenReturn(pluginSettings);
40      }
41  
42      @Test
43      public void enableDarkFeature()
44      {
45          givenEnabledDarkFeatures();
46          createStorage().enable(FEATURE_KEY);
47          thenPluginSettingsUpdated();
48      }
49  
50      @Test
51      public void ignoreEnableForAlreadyEnabledDarkFeature()
52      {
53          givenEnabledDarkFeatures(FEATURE_KEY);
54          createStorage().enable(FEATURE_KEY);
55          thenPluginSettingsNotUpdated();
56      }
57  
58      @Test
59      public void ignoreDisableForNotEnabledDarkFeature()
60      {
61          givenEnabledDarkFeatures();
62          createStorage().disable(FEATURE_KEY);
63          thenPluginSettingsNotUpdated();
64      }
65  
66      @Test
67      public void disableDarkFeature()
68      {
69          givenEnabledDarkFeatures(FEATURE_KEY);
70          createStorage().disable(FEATURE_KEY);
71          thenPluginSettingsUpdated();
72      }
73  
74      @Test
75      public void noStoredFeatures()
76      {
77          when(pluginSettings.get(anyString())).thenReturn(null);
78          final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
79          assertTrue(enabledDarkFeatures.isEmpty());
80      }
81  
82      @Test
83      public void getEmptySetOfEnabledDarkFeatures()
84      {
85          givenEnabledDarkFeatures();
86          final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
87          assertTrue(enabledDarkFeatures.isEmpty());
88      }
89  
90      @Test
91      public void getOneEnabledDarkFeature()
92      {
93          givenEnabledDarkFeatures(FEATURE_KEY);
94          final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
95          assertThat(enabledDarkFeatures, hasItem(FEATURE_KEY));
96      }
97  
98      @Test
99      public void getTwoEnabledDarkFeatures()
100     {
101         givenEnabledDarkFeatures(FEATURE_KEY, ANOTHER_DARK_FEATURE);
102         final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
103         assertThat(enabledDarkFeatures, hasItem(FEATURE_KEY));
104         assertThat(enabledDarkFeatures, hasItem(ANOTHER_DARK_FEATURE));
105     }
106 
107     private SiteDarkFeaturesStorage createStorage()
108     {
109         return new DefaultSiteDarkFeaturesStorage(pluginSettingsFactory);
110     }
111 
112     private void givenEnabledDarkFeatures(final String...featureKeys)
113     {
114         when(pluginSettings.get(anyString())).thenReturn(Arrays.asList(featureKeys));
115     }
116 
117     private void thenPluginSettingsUpdated()
118     {
119         verify(pluginSettings).put(anyString(), argThat(instanceOf(List.class)));
120     }
121 
122     private void thenPluginSettingsNotUpdated()
123     {
124         verify(pluginSettings, never()).put(anyString(), any(Object.class));
125     }
126 }