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.FeatureKeyScope.ALL_USERS;
9 import static com.atlassian.sal.api.features.FeatureKeyScope.ALL_USERS_READ_ONLY;
10 import static com.atlassian.sal.api.features.FeatureKeyScope.CURRENT_USER_ONLY;
11 import static com.atlassian.sal.api.features.FeatureKeyScopePredicate.filterBy;
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 private static final String FEATURE_KEY = "feature";
18 private static final String ANOTHER_FEATURE_KEY = "another-feature";
19
20 @Test
21 public void unmodifiableFeaturesEnabledForAllUsers() {
22 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS_READ_ONLY);
23 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
24 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
25 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS_READ_ONLY)), containsInAnyOrder(FEATURE_KEY));
26 }
27
28 @Test
29 public void modifiableFeaturesEnabledForAllUser() {
30 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS);
31 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
32 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
33 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS)), containsInAnyOrder(FEATURE_KEY));
34 }
35
36 @Test
37 public void featuresEnabledForAllUsers() {
38 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(ALL_USERS);
39 assertThat(enabledDarkFeatures.getFeatureKeys(Predicates.<FeatureKeyScope>or(filterBy(ALL_USERS_READ_ONLY), filterBy(ALL_USERS))), containsInAnyOrder(FEATURE_KEY));
40 }
41
42 @Test
43 public void featuresEnabledForCurrentUser() {
44 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatures(CURRENT_USER_ONLY);
45 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
46 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
47 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(CURRENT_USER_ONLY)), containsInAnyOrder(FEATURE_KEY));
48 }
49
50 @Test
51 public void featureEnabledForAllAndPerUser() {
52 final EnabledDarkFeatures enabledDarkFeatures = createEnabledDarkFeatureInTwoScopes(ALL_USERS_READ_ONLY, CURRENT_USER_ONLY);
53 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
54 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY));
55 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(ALL_USERS_READ_ONLY)), containsInAnyOrder(FEATURE_KEY));
56 assertThat(enabledDarkFeatures.getFeatureKeys(filterBy(CURRENT_USER_ONLY)), containsInAnyOrder(FEATURE_KEY));
57 }
58
59 @Test
60 public void multipleFeaturesEnabledForAllUsers() {
61 final EnabledDarkFeatures enabledDarkFeatures = createTwoEnabledDarkFeatures(ALL_USERS_READ_ONLY);
62 assertTrue(enabledDarkFeatures.isFeatureEnabled(FEATURE_KEY));
63 assertTrue(enabledDarkFeatures.isFeatureEnabled(ANOTHER_FEATURE_KEY));
64 assertThat(enabledDarkFeatures.getFeatureKeys(), containsInAnyOrder(FEATURE_KEY, ANOTHER_FEATURE_KEY));
65 }
66
67 private EnabledDarkFeatures createEnabledDarkFeatures(final FeatureKeyScope featureKeyScope) {
68 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(featureKeyScope, ImmutableSet.of(FEATURE_KEY)));
69 }
70
71 private EnabledDarkFeatures createTwoEnabledDarkFeatures(final FeatureKeyScope featureKeyScope) {
72 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(featureKeyScope, ImmutableSet.of(FEATURE_KEY, ANOTHER_FEATURE_KEY)));
73 }
74
75 private EnabledDarkFeatures createEnabledDarkFeatureInTwoScopes(final FeatureKeyScope featureKeyScope1, final FeatureKeyScope featureKeyScope2) {
76 return new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of(
77 featureKeyScope1, ImmutableSet.of(FEATURE_KEY),
78 featureKeyScope2, ImmutableSet.of(FEATURE_KEY)
79 ));
80 }
81 }