package com.losd.reqbot.controller;

import com.losd.reqbot.model.Request;
import com.losd.reqbot.repository.RequestRepo;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMethod;
import uk.co.it.modular.hamcrest.date.Moments;

import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static uk.co.it.modular.hamcrest.date.IsWithin.within;

/**
 * The MIT License (MIT)
 * <p>
 * Copyright (c) 2015 Andrew Braithwaite
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
public class IncomingRequestControllerTest {
    private MockMvc mockMvc;

    @Mock
    private RequestRepo requestRepo;

    @InjectMocks
    private IncomingRequestController incomingRequestController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(incomingRequestController).build();
    }

    @Test
    public void handleGetTestWithoutQueryParameters() throws Exception {
        mockMvc.perform(get("/bucket/x")).andExpect(status().isOk()).andExpect(content().string("OK"));

        ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
        verify(requestRepo, times(1)).save(requestCaptor.capture());

        Request request = requestCaptor.getValue();

        assertThat(request.getBody(), is(nullValue()));
        assertThat(request.getQueryParameters(), is(equalTo(Collections.EMPTY_MAP)));
        assertThat(request.getBucket(), is(equalTo("x")));
        assertThat(request.getMethod(), is(equalTo(RequestMethod.GET.name())));
        assertThat(request.getUuid(), is(not(nullValue())));

        Date timestamp = Date.from(Instant.parse(request.getTimestamp()));
        assertThat(timestamp, within(2, TimeUnit.SECONDS, Moments.now()));
    }
}

@@ -603,16 +603,64 @@
 l.Date;%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import j
@@ -952,16 +952,104 @@
 rs.get;%0A
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;%0A
 import s
@@ -3013,24 +3013,1215 @@
 ng(%22OK%22));%0A%0A
+        validate(%22x%22, Collections.EMPTY_MAP, RequestMethod.GET, null);%0A    %7D%0A%0A    @Test%0A    public void handlePostTestWithoutQueryParameters() throws Exception %7B%0A        mockMvc.perform(post(%22/bucket/x%22).content(%22hello%22)).andExpect(status().isOk()).andExpect(content().string(%22OK%22));%0A%0A        validate(%22x%22, Collections.EMPTY_MAP, RequestMethod.POST, %22hello%22);%0A    %7D%0A%0A    @Test%0A    public void handleGetTestWithQueryParameters() throws Exception %7B%0A        mockMvc.perform(get(%22/bucket/x?a=1%22)).andExpect(status().isOk()).andExpect(content().string(%22OK%22));%0A        Map%3CString, String%3E queryParams = new HashMap%3C%3E();%0A        queryParams.put(%22a%22, %221%22);%0A%0A        validate(%22x%22, queryParams, RequestMethod.GET, null);%0A    %7D%0A%0A    @Test%0A    public void handlePostTestWithQueryParameters() throws Exception %7B%0A        mockMvc.perform(post(%22/bucket/x?a=1%22).content(%22hello%22)).andExpect(status().isOk()).andExpect(content().string(%22OK%22));%0A        Map%3CString, String%3E queryParams = new HashMap%3C%3E();%0A        queryParams.put(%22a%22, %221%22);%0A%0A        validate(%22x%22, queryParams, RequestMethod.POST, %22hello%22);%0A    %7D%0A%0A    private void validate(String bucket, Map queryParameters, RequestMethod method, String body) %7B%0A
         Argu
@@ -4418,17 +4418,16 @@
 alue();%0A
-%0A
         
@@ -4459,26 +4459,28 @@
 y(), is(
-nullValue(
+equalTo(body
 )));%0A   
@@ -4536,37 +4536,31 @@
 equalTo(
-Collections.EMPTY_MAP
+queryParameters
 )));%0A   
@@ -4607,19 +4607,22 @@
 equalTo(
-%22x%22
+bucket
 )));%0A   
@@ -4673,26 +4673,15 @@
 lTo(
-RequestM
+m
 ethod.
-GET.
 name

refactor tests