package com.longcoding.moon.interceptors.impl;

import com.longcoding.moon.helpers.Constant;
import com.longcoding.moon.models.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.MimeTypeUtils;

@Slf4j
public class InitializeInterceptorTest {

    private MockHttpServletRequest request = new MockHttpServletRequest("POST", "/stackoverflow/2.2/question/coding");
    private MockHttpServletResponse response = new MockHttpServletResponse();
    private InitializeInterceptor interceptor = new InitializeInterceptor();

    @Before
    public void setup() throws Exception {
        request.addHeader("Content-Type", MimeTypeUtils.APPLICATION_JSON_VALUE);
        request.addHeader("apiKey", "5000-5000-5000-5000");
        request.setQueryString("version=2.2");

        interceptor.preHandler(request, response, null);
    }

    @Test
    public void initializeRequestWithApikey() {
        log.info("Initializes the request. And Check apiKey");

        RequestInfo requestInfo = (RequestInfo) request.getAttribute(Constant.REQUEST_INFO_DATA);
        Assert.assertEquals("5000-5000-5000-5000", requestInfo.getHeaders().get("apikey"));
    }

    @Test
    public void initializeRequestWithHeaders() {
        log.info("Initializes the request. And Check headers");

        RequestInfo requestInfo = (RequestInfo) request.getAttribute(Constant.REQUEST_INFO_DATA);
        Assert.assertEquals(2, requestInfo.getHeaders().size());
    }

    @Test
    public void initializeRequestWithHttpMethod() {
        log.info("Initializes the request. And Check headers");

        RequestInfo requestInfo = (RequestInfo) request.getAttribute(Constant.REQUEST_INFO_DATA);
        Assert.assertEquals("POST", requestInfo.getRequestMethod());
    }

    @Test
    public void initializeRequestWithQueryParam() {
        log.info("Initializes the request. And Check query parameters");

        RequestInfo requestInfo = (RequestInfo) request.getAttribute(Constant.REQUEST_INFO_DATA);
        Assert.assertTrue(requestInfo.getQueryStringMap().containsKey("version"));
    }

}

@@ -171,33 +171,8 @@
 4j;%0A
-import org.junit.Assert;%0A
 impo
@@ -192,16 +192,16 @@
 Before;%0A
+
 import o
@@ -384,16 +384,155 @@
 Utils;%0A%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.equalTo;%0A%0A
 @Slf4j%0Ap
@@ -1387,51 +1387,19 @@
     
-Assert.assertEquals(%225000-5000-5000-5000%22, 
+assertThat(
 requ
@@ -1432,16 +1432,48 @@
 apikey%22)
+, equalTo(%225000-5000-5000-5000%22)
 );%0A    %7D
@@ -1708,31 +1708,19 @@
     
-Assert.assertEquals(2, 
+assertThat(
 requ
@@ -1746,16 +1746,28 @@
 ).size()
+, equalTo(2)
 );%0A    %7D
@@ -1876,38 +1876,42 @@
 est. And Check h
-eaders
+ttp Method
 %22);%0A%0A        Req
@@ -2009,36 +2009,19 @@
     
-A
+a
 ssert
-.assertEquals(%22POST%22, 
+That(
 requ
@@ -2046,16 +2046,33 @@
 Method()
+, equalTo(%22POST%22)
 );%0A    %7D
@@ -2319,25 +2319,18 @@
     
-A
+a
 ssert
-.assertTrue
+That
 (req
@@ -2380,16 +2380,26 @@
 ersion%22)
+, is(true)
 );%0A    %7D

Apply hamcrest library