package de.tblsoft.search.mock;

import com.google.common.base.Splitter;
import de.tblsoft.search.pipeline.filter.solr.MockSolrClient;
import org.mockito.Mockito;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by tbl on 25.11.17.
 */
public class Mockfactory {

    public static HttpServletRequest createHttpServletRequest(String url){
        try {
            URI uri = new URI(url);


            final Map<String, String> parametersMap = Splitter.on("&")
                    .omitEmptyStrings()
                    .trimResults()
                    .withKeyValueSeparator("=")
                    .split(uri.getQuery());


            HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
            Enumeration<String> parameterNames = Collections.enumeration(parametersMap.keySet());
            Mockito.when(httpServletRequest.getParameterNames()).thenReturn(parameterNames);
            for (String key : parametersMap.keySet()) {
                Mockito.when(httpServletRequest.getParameter(key)).thenReturn(parametersMap.get(key));
            }

            Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(parameterNames);
            Cookie[] cookies = {};
            Mockito.when(httpServletRequest.getCookies()).thenReturn(cookies);

            return httpServletRequest;

        } catch (Exception e ) {
            throw new RuntimeException(e);
        }
    }

    public static MockSolrClient createSolrClient(String url) {
        MockSolrClient mockSolrClient = new MockSolrClient(url);
        //mockSolrClient.setRecord(true);
        return mockSolrClient;
    }
}

@@ -253,15 +253,88 @@
 ava.
-net.URI
+io.UnsupportedEncodingException;%0Aimport java.net.URI;%0Aimport java.net.URLDecoder
 ;%0Aim
@@ -412,16 +412,52 @@
 til.Map;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -879,24 +879,358 @@
 tQuery());%0A%0A
+            final Map%3CString, String%5B%5D%3E decodedParametersMap = parametersMap.%0A                    entrySet().%0A                    stream().%0A                    collect(Collectors.toMap(%0A                            Map.Entry::getKey, entry -%3E decodeUrlParam(entry.getValue())));%0A%0A%0A            //map(e -%3E decodeUrlParam(e.getValue()))%0A%0A
 %0A           
@@ -1627,33 +1627,40 @@
 ey)).thenReturn(
-p
+decodedP
 arametersMap.get
@@ -1664,16 +1664,19 @@
 get(key)
+%5B0%5D
 );%0A     
@@ -1906,110 +1906,639 @@
     
-return httpServletRequest;%0A%0A        %7D catch (Exception e ) %7B%0A            throw new RuntimeException(e)
+Mockito.when(httpServletRequest.getRequestURI()).thenReturn(uri.getPath());%0A%0A            Mockito.when(httpServletRequest.getParameterMap()).thenReturn(decodedParametersMap);%0A%0A            return httpServletRequest;%0A%0A        %7D catch (Exception e ) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    public static String%5B%5D decodeUrlParam(String param) %7B%0A        try %7B%0A            String%5B%5D paramArray = new String%5B%5D %7BURLDecoder.decode(param, %22UTF-8%22)%7D;%0A            return paramArray;%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            String%5B%5D paramArray = new String%5B%5D %7Bparam%7D;%0A            return paramArray
 ;%0A  

improve the http servlet mock