1 package com.atlassian.sal.api.features;
2
3 import com.google.common.base.Predicates;
4 import com.google.common.collect.ImmutableMap;
5 import com.google.common.collect.ImmutableSet;
6 import org.junit.Test;
7
8 import static com.atlassian.sal.api.features.FeatureKeyScopePredicate.filterBy;
9 import static com.atlassian.sal.api.features.FeatureKeyScope.ALL_USERS;
10 import static com.atlassian.sal.api.features.FeatureKeyScope.ALL_USERS_READ_ONLY;
11 import static com.atlassian.sal.api.features.FeatureKeyScope.CURRENT_USER_ONLY;
12 import static org.hamcrest.Matchers.containsInAnyOrder;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15
16 public class TestEnabledDarkFeatures
17 {
18 private static final String FEATURE_KEY = "feature";
19 private static final String ANOTHER_FEATURE_KEY = "another-feature";
20
21 @Test
22 public void unmodifiableFeaturesEnabledForAllUsers()
23 {
24 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS_READ_ONLY);
25 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
26 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
27 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS_READ_ONLY)), containsInAnyOrder(FEATURE_KEY));
28 }
29
30 @Test
31 public void modifiableFeaturesEnabledForAllUser()
32 {
33 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS);
34 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
35 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
36 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS)), containsInAnyOrder(FEATURE_KEY));
37 }
38
39 @Test
40 public void featuresEnabledForAllUsers()
41 {
42 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS);
43 assertThat(enabledDarkFeatures.getFeatureKeys(Predicates.<FeatureKeyScope>or(filterBy(ALL_USERS_READ_ONLY), filterBy(ALL_USERS))), containsInAnyOrder(FEATURE_KEY));
44 }
45
46 @Test
47 public void featuresEnabledForCurrentUser() {
48 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(CURRENT_USER_ONLY);
49 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
50 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
51 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(CURRENT_USER_ONLY)), containsInAnyOrder(FEATURE_KEY));
52 }
53
54 @Test
55 public void featureEnabledForAllAndPerUser()
56 {
57 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatureInTwoScopes(ALL_USERS_READ_ONLY, CURRENT_USER_ONLY);
58 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
59 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
60 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS_READ_ONLY)), containsInAnyOrder(FEATURE_KEY));
61 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(CURRENT_USER_ONLY)), containsInAnyOrder(FEATURE_KEY));
62 }
63
64 @Test
65 public void multipleFeaturesEnabledForAllUsers()
66 {
67 final EnabledDarkFeatures enabledDarkFeatures = createTwoEnabledDarkFeatures(ALL_USERS_READ_ONLY);
68 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
69 assertTrue(enabledDarkFeatures.isFeatureEnabled(ANOTHER_FEATURE_KEY));
70 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY, ANOTHER_FEATURE_KEY));
71 }
72
73 private EnabledDarkFeatures createEnabledDarkFeatures(final FeatureKeyScope featureKeyScope)
74 {
75 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(featureKeyScope, ImmutableSet.of(FEATURE_KEY)));
76 }
77
78 private EnabledDarkFeatures createTwoEnabledDarkFeatures(final FeatureKeyScope featureKeyScope)
79 {
80 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(featureKeyScope, ImmutableSet.of(FEATURE_KEY, ANOTHER_FEATURE_KEY)));
81 }
82
83 private EnabledDarkFeatures createEnabledDarkFeatureInTwoScopes(final FeatureKeyScope featureKeyScope1, final FeatureKeyScope featureKeyScope2)
84 {
85 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(
86 featureKeyScope1, ImmutableSet.of(FEATURE_KEY),
87 featureKeyScope2, ImmutableSet.of(FEATURE_KEY)
88 ));
89 }
90 }