QueryParameterActivationStrategy implements ActivationStrategy { @Override public boolean isActive(FeatureState featureState, FeatureUser user) { HttpServletRequest request = HttpServletRequestHolder.get(); if (request == null) { return false; } String triggerParamsString = featureState.getParameter(PARAM_URL_PARAMS); if (Strings.isBlank(triggerParamsString)) { return false; } String[] triggerParams = SPLIT_ON_COMMA.split(triggerParamsString.trim()); Map<String, String[]> actualParams = new HashMap<>(); actualParams.putAll(request.getParameterMap()); String referer = request.getHeader("referer"); if (referer != null) { for (Map.Entry<String, List<String>> entry : getRefererParameters(actualParams, referer).entrySet()) { actualParams.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()])); } } return isTriggerParamPresent(actualParams, triggerParams); }  @Override String getId(); @Override String getName(); @Override boolean isActive(FeatureState featureState, FeatureUser user); @Override Parameter[] getParameters(); static final String PARAM_URL_PARAMS; }
@Test public void shouldNotBeActiveWithANullRequestObject() { HttpServletRequestHolder.release(); boolean isActive = strategy.isActive(state, user); assertFalse(isActive); }
@Test public void shouldNotBeActiveWithNoPermittedParameters() { when(request.getParameterMap()).thenReturn(Collections.EMPTY_MAP); boolean isActive = strategy.isActive(state, user); assertFalse(isActive); }
@Test public void shouldNotBeActiveWhenOnlyNonMatchingParametersArePresent() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("somethingThatDoesNotMatch", new String[]{"true"}); parameters.put("somethingElse", new String[]{"aValue"}); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertFalse(isActive); }
@Test public void shouldNotBeActiveWhenMatchingParameterHasANonMatchingValue() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("toggleFeatureX", new String[]{"false"}); parameters.put("toggleAll", new String[]{"nope"}); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertFalse(isActive); }
@Test public void shouldNotBeActiveWhenMatchingParameterRequiringValueDoesNotHaveValue() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("toggleFeatureX", null); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertFalse(isActive); }
@Test public void shouldBeActiveWhenAnAcceptedParameterAndValueArePresent() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("toggleFeatureX", new String[]{"true"}); parameters.put("toggleAll", new String[]{"nope"}); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void shouldBeActiveWhenAnAcceptedParameterWithoutRequiredValueIsPresent() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("parameterWithoutValue", null); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void shouldBeActiveWhenAnAcceptedParameterWithoutRequiredValueIsPresentAndItHasAValue() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("parameterWithoutValue", new String[]{"anyVal"}); when(request.getParameterMap()).thenReturn(parameters); boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void shouldBeActiveRegardlessOfWhitespaceAroundActivationParams() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("parameterWithoutValue", null); parameters.put("anotherParam", null); when(request.getParameterMap()).thenReturn(parameters); state.setParameter(QueryParameterActivationStrategy.PARAM_URL_PARAMS, " toggleFeatureX = true, toggleAll= yes, parameterWithoutValue "); boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void shouldBeActiveIfParameterWithMultipleValuesHasAnyMatch() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("parameterWithoutValue", null); parameters.put("anotherParam", null); when(request.getParameterMap()).thenReturn(parameters); parameters.put("test", new String[]{"matches", "nomatch", "notanything"}); state.setParameter(QueryParameterActivationStrategy.PARAM_URL_PARAMS, "test=matches"); boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void strategyShouldCheckParametersOfReferrer() { Map<String, String[]> parameters = new HashMap<>(); when(request.getParameterMap()).thenReturn(parameters); when(request.getHeader("referer")).thenReturn("http: boolean isActive = strategy.isActive(state, user); assertTrue(isActive); when(request.getHeader("referer")).thenReturn("http: isActive = strategy.isActive(state, user); assertFalse(isActive); when(request.getHeader("referer")).thenReturn("http: isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void refererParamsShouldNotOverrideRequestParamsButWillQualifyAsActiveIfSeparate() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("toggleFeatureX", new String[]{"false"}); when(request.getParameterMap()).thenReturn(parameters); when(request.getHeader("referer")).thenReturn("http: boolean isActive = strategy.isActive(state, user); assertFalse(isActive); when(request.getHeader("referer")).thenReturn("http: isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void refererWithMultiValueParamsShouldBeChecked() { Map<String, String[]> parameters = new HashMap<>(); when(request.getParameterMap()).thenReturn(parameters); when(request.getHeader("referer")) .thenReturn("http: boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void refererWithEncodedParamsCanMatch() { Map<String, String[]> parameters = new HashMap<>(); when(request.getParameterMap()).thenReturn(parameters); state.setParameter(QueryParameterActivationStrategy.PARAM_URL_PARAMS, "toggleFeatureX=handles space,toggleAll=yes,parameterWithoutValue"); when(request.getHeader("referer")) .thenReturn("http: boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }
@Test public void refererWithoutQueryParam() { Map<String, String[]> parameters = new HashMap<>(); parameters.put("parameterWithoutValue", new String[0]); when(request.getParameterMap()).thenReturn(parameters); state.setParameter(QueryParameterActivationStrategy.PARAM_URL_PARAMS, "toggleFeatureX=handles space,toggleAll=yes,parameterWithoutValue"); when(request.getHeader("referer")) .thenReturn("http: boolean isActive = strategy.isActive(state, user); assertTrue(isActive); }