package api;

import com.perimeterx.api.ip.IPByHeaderProvider;
import com.perimeterx.api.ip.RemoteAddressIPProvider;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;

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

/**
 * Testing {@link com.perimeterx.api.ip.IPProvider}
 * <p>
 * Created by shikloshi on 12/07/2016.
 */
@Test
public class IPProviderTest {

    public static final String IP_HEADER = "this-is-where-my-ip-is";
    private HttpServletRequest request;

    public void setUp() throws Exception {
        Map<String, String> headers = new HashMap<>();
        headers.put(IP_HEADER, "127.0.0.1");
        this.request = Mockito.mock(HttpServletRequest.class);
        Iterator<String> iterator = headers.keySet().iterator();
        Enumeration headerNames = new Enumeration() {
            @Override
            public boolean hasMoreElements() {
                return iterator.hasNext();
            }

            @Override
            public Object nextElement() {
                return iterator.next();
            }
        };
        Mockito.when(request.getHeaderNames()).thenReturn(headerNames);
        Mockito.doAnswer(invocationOnMock -> {
            Object[] args = invocationOnMock.getArguments();
            return headers.get(args[0]);
        }).when(request).getHeader(IP_HEADER);
        Mockito.when(request.getRemoteAddr()).thenReturn("81.82.81.82");
    }

    @Test
    public void testGetRequestIPByHeader() throws Exception {
        IPByHeaderProvider ipProvider = new IPByHeaderProvider(IP_HEADER);
        String ipHeaderValue = request.getHeader(IP_HEADER);
        Assert.assertEquals(ipHeaderValue, ipProvider.getRequestIP(request));
    }

    @Test
    public void testGetRequestIPByRemoteAddress() {
        RemoteAddressIPProvider ipProvider = new RemoteAddressIPProvider();
        Assert.assertEquals(ipProvider.getRequestIP(request), "81.82.81.82");
    }
}

@@ -138,16 +138,100 @@
 ockito;%0A
+import org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;%0A
 import o
@@ -352,94 +352,9 @@
 til.
-Enumeration;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map
+*
 ;%0A%0A/
@@ -500,21 +500,22 @@
 %7B%0A%0A    p
-ublic
+rivate
  static 
@@ -657,16 +657,22 @@
         
+final 
 Map%3CStri
@@ -830,74 +830,71 @@
     
-Iterator%3CString%3E iterator = headers.keySet().iterator();%0A        E
+Mockito.when(request.getHeaderNames()).thenReturn(Collections.e
 nume
@@ -903,519 +903,357 @@
 tion
- 
+(
 header
-Names = new Enumeration() %7B%0A            @Override%0A            public boolean hasMoreElements() %7B%0A                return iterator.hasNext();%0A            %7D%0A%0A            @Override%0A            public Object nextElement() %7B%0A                return iterator.next();%0A            %7D%0A        %7D;%0A        Mockito.when(request.getHeaderNames()).thenReturn(headerNames);%0A        Mockito.doAnswer(invocationOnMock -%3E %7B%0A            Object%5B%5D args = invocationOnMock.getArguments();%0A            return headers.get(args%5B0%5D);
+s.keySet()));%0A        Mockito.doAnswer(new Answer() %7B%0A            @Override%0A            public String answer(final InvocationOnMock invocationOnMock) throws Throwable %7B%0A                Object%5B%5D args = invocationOnMock.getArguments();%0A                final String name = (String)args%5B0%5D;%0A                return headers.get(name);%0A            %7D
 %0A   

test pass as java 1.7 code