UsernameActivationStrategy implements ActivationStrategy { @Override public boolean isActive(FeatureState state, FeatureUser user) { String usersAsString = state.getParameter(PARAM_USERS); if (Strings.isNotBlank(usersAsString)) { List<String> users = Strings.splitAndTrim(usersAsString, ","); if (user != null && Strings.isNotBlank(user.getName())) { for (String username : users) { if (username.equals(user.getName())) { return true; } } } } return false; }  @Override String getId(); @Override String getName(); @Override boolean isActive(FeatureState state, FeatureUser user); @Override Parameter[] getParameters(); static final String ID; static final String PARAM_USERS; }
@Test void shouldReturnFalseForEmptyUserlist() { FeatureUser user = new SimpleFeatureUser("ck", false); FeatureState state = new FeatureState(MyFeature.FEATURE) .enable() .setStrategyId(UsernameActivationStrategy.ID); boolean active = strategy.isActive(state, user); assertFalse(active); }
@Test void shouldReturnFalseForUnknownUser() { FeatureUser user = null; FeatureState state = new FeatureState(MyFeature.FEATURE) .enable() .setStrategyId(UsernameActivationStrategy.ID) .setParameter(UsernameActivationStrategy.PARAM_USERS, "person1,ck,person2"); boolean active = strategy.isActive(state, user); assertFalse(active); }
@Test void shouldReturnFalseForDifferentUser() { FeatureUser user = new SimpleFeatureUser("john", false); FeatureState state = new FeatureState(MyFeature.FEATURE) .enable() .setStrategyId(UsernameActivationStrategy.ID) .setParameter(UsernameActivationStrategy.PARAM_USERS, "person1,ck,person2"); boolean active = strategy.isActive(state, user); assertFalse(active); }
@Test void shouldReturnTrueForCorrectUser() { FeatureUser user = new SimpleFeatureUser("ck", false); FeatureState state = new FeatureState(MyFeature.FEATURE) .enable() .setStrategyId(UsernameActivationStrategy.ID) .setParameter(UsernameActivationStrategy.PARAM_USERS, "person1,ck,person2"); boolean active = strategy.isActive(state, user); assertTrue(active); }