package de.otto.edison.togglz.activation;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.togglz.core.Feature;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.servlet.util.HttpServletRequestHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

public class HeaderOptOutStrategyTest {

    private FeatureUser user;
    private FeatureState state;
    private HttpServletRequest request;
    private final HeaderOptOutStrategy strategy = new HeaderOptOutStrategy();

    @BeforeEach
    public void setUp() {
        user = new SimpleFeatureUser("ea", false);
        state = new FeatureState(MyFeature.FEATURE).enable();
        request = Mockito.mock(HttpServletRequest.class);
        HttpServletRequestHolder.bind(request);
    }

    @AfterEach
    public void cleanup() {
        HttpServletRequestHolder.release();
    }

    private enum MyFeature implements Feature {
        FEATURE
    }

    @Test
    public void shouldBeActiveWithNoHeaders() {
        when(request.getHeader(any())).thenReturn(null);

        boolean isActive = strategy.isActive(state, user);

        assertTrue(isActive);
    }

    @Test
    public void shouldBeActiveWhenOnlyNonMatchingParametersIsPresent() {
        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);

        assertTrue(isActive);
    }

    @Test
    public void shouldBeActiveWhenRequestHeaderDoesNotIncludeFeatureToggle() {
        when(request.getHeader("X-Features")).thenReturn("OTHERFEATURE,ANOTHERFEATURE");

        boolean isActive = strategy.isActive(state, user);

        assertTrue(isActive);
    }

    @Test
    public void shouldNotBeActiveWhenRequestHeaderIncludesFeatureToggle() {
        when(request.getHeader("X-Features")).thenReturn("FEATURE,ANOTHERFEATURE");

        boolean isActive = strategy.isActive(state, user);

        assertFalse(isActive);
    }
}
@@ -40,48 +40,8 @@
 n;%0A%0A
-import org.junit.jupiter.api.AfterEach;%0A
 impo
@@ -140,16 +140,158 @@
 ockito;%0A
+import org.springframework.web.context.request.RequestContextHolder;%0Aimport org.springframework.web.context.request.ServletRequestAttributes;%0A
 import o
@@ -450,33 +450,29 @@
 reUser;%0A
+%0A
 import 
-org.togglz
+javax
 .servlet
@@ -472,20 +472,20 @@
 servlet.
-util
+http
 .HttpSer
@@ -495,24 +495,17 @@
 tRequest
-Holder;%0A
+;
 %0Aimport 
@@ -528,37 +528,38 @@
 tp.HttpServletRe
-quest
+sponse
 ;%0Aimport java.ut
@@ -760,16 +760,56 @@
 rs.any;%0A
+import static org.mockito.Mockito.mock;%0A
 import s
@@ -1301,126 +1301,248 @@
 etRe
-questHolder.bind(request
+sponse response = mock(HttpServletResponse.class
 );%0A    
-%7D%0A%0A
     
-@AfterEach%0A    public void cleanup() %7B%0A        HttpServletRequestHolder.release(
+final ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(request, response);%0A        RequestContextHolder.setRequestAttributes(servletRequestAttributes
 );%0A 

fix header opt out strategy test