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.junit.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.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
26
27 @RunWith(MockitoJUnitRunner.class)
28 public class TestDefaultSiteDarkFeaturesStorage {
29 private static final String FEATURE_KEY = "dark.feature";
30 private static final String ANOTHER_DARK_FEATURE = "another.dark.feature";
31
32 @Mock
33 private PluginSettingsFactory pluginSettingsFactory;
34 @Mock
35 private PluginSettings pluginSettings;
36
37 @Before
38 public void setUp() {
39 when(pluginSettingsFactory.createGlobalSettings()).thenReturn(pluginSettings);
40 }
41
42 @Test
43 public void enableDarkFeature() {
44 givenEnabledDarkFeatures();
45 createStorage().enable(FEATURE_KEY);
46 thenPluginSettingsUpdated();
47 }
48
49 @Test
50 public void ignoreEnableForAlreadyEnabledDarkFeature() {
51 givenEnabledDarkFeatures(FEATURE_KEY);
52 createStorage().enable(FEATURE_KEY);
53 thenPluginSettingsNotUpdated();
54 }
55
56 @Test
57 public void ignoreDisableForNotEnabledDarkFeature() {
58 givenEnabledDarkFeatures();
59 createStorage().disable(FEATURE_KEY);
60 thenPluginSettingsNotUpdated();
61 }
62
63 @Test
64 public void disableDarkFeature() {
65 givenEnabledDarkFeatures(FEATURE_KEY);
66 createStorage().disable(FEATURE_KEY);
67 thenPluginSettingsUpdated();
68 }
69
70 @Test
71 public void noStoredFeatures() {
72 when(pluginSettings.get(anyString())).thenReturn(null);
73 final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
74 assertTrue(enabledDarkFeatures.isEmpty());
75 }
76
77 @Test
78 public void getEmptySetOfEnabledDarkFeatures() {
79 givenEnabledDarkFeatures();
80 final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
81 assertTrue(enabledDarkFeatures.isEmpty());
82 }
83
84 @Test
85 public void getOneEnabledDarkFeature() {
86 givenEnabledDarkFeatures(FEATURE_KEY);
87 final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
88 assertThat(enabledDarkFeatures, hasItem(FEATURE_KEY));
89 }
90
91 @Test
92 public void getTwoEnabledDarkFeatures() {
93 givenEnabledDarkFeatures(FEATURE_KEY, ANOTHER_DARK_FEATURE);
94 final ImmutableSet<String> enabledDarkFeatures = createStorage().getEnabledDarkFeatures();
95 assertThat(enabledDarkFeatures, hasItem(FEATURE_KEY));
96 assertThat(enabledDarkFeatures, hasItem(ANOTHER_DARK_FEATURE));
97 }
98
99 private SiteDarkFeaturesStorage createStorage() {
100 return new DefaultSiteDarkFeaturesStorage(pluginSettingsFactory);
101 }
102
103 private void givenEnabledDarkFeatures(final String... featureKeys) {
104 when(pluginSettings.get(anyString())).thenReturn(Arrays.asList(featureKeys));
105 }
106
107 private void thenPluginSettingsUpdated() {
108 verify(pluginSettings).put(anyString(), argThat(instanceOf(List.class)));
109 }
110
111 private void thenPluginSettingsNotUpdated() {
112 verify(pluginSettings, never()).put(anyString(), any(Object.class));
113 }
114 }