package uk.gov.register.presentation.resource;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.jackson.Jackson;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.gov.register.presentation.DbContent;
import uk.gov.register.presentation.DbEntry;
import uk.gov.register.presentation.config.RegistersConfiguration;
import uk.gov.register.presentation.dao.RecentEntryIndexQueryDAO;
import uk.gov.register.presentation.view.ListVersionView;
import uk.gov.register.presentation.view.ViewFactory;

import javax.ws.rs.NotFoundException;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HistoryResourceTest {

    private HistoryResource resource;
    @Mock
    private RecentEntryIndexQueryDAO queryDAO;
    @Mock
    private ViewFactory viewFactory;
    private ObjectMapper objectMapper;

    @Before
    public void setUp() throws Exception {
        resource = new HistoryResource(new RequestContext(new RegistersConfiguration()) {
            @Override
            public String getRegisterPrimaryKey() {
                return "school";
            }
        }, queryDAO, viewFactory);
        objectMapper = Jackson.newObjectMapper();
    }

    @Test
    public void history_throwsNotFoundExceptionForNonPrimaryKeyRequests() throws Exception {
        try {
            resource.history("someOtherKey", "value");
            fail("Must fail");
        } catch (NotFoundException e) {
            //success
        }
    }

    @Test
    public void history_returnsVersionsFromDatabase() throws Exception {
        when(queryDAO.findAllByKeyValue("school", "1234")).thenReturn(Arrays.asList(
                new DbEntry(3, new DbContent("hash1", objectMapper.readTree("{\"school\":\"1234\",\"head-teacher\":\"John Smith\"}"))),
                new DbEntry(17, new DbContent("hash2", objectMapper.readTree("{\"school\":\"1234\",\"head-teacher\":\"Caroline Atkins\"}")))
        ));

        ListVersionView history = resource.history("school", "1234");

        assertThat(history.getVersions().get(0).hash, equalTo("hash1"));
        assertThat(history.getVersions().get(0).serialNumber, equalTo(3));
        assertThat(history.getVersions().get(1).hash, equalTo("hash2"));
        assertThat(history.getVersions().get(1).serialNumber, equalTo(17));
    }
}

@@ -212,16 +212,51 @@
 unWith;%0A
+import org.mockito.ArgumentCaptor;%0A
 import o
@@ -411,16 +411,61 @@
 bEntry;%0A
+import uk.gov.register.presentation.Version;%0A
 import u
@@ -593,66 +593,8 @@
 AO;%0A
-import uk.gov.register.presentation.view.ListVersionView;%0A
 impo
@@ -706,16 +706,39 @@
 .Arrays;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -870,16 +870,58 @@
 t.fail;%0A
+import static org.mockito.Mockito.verify;%0A
 import s
@@ -1848,32 +1848,112 @@
    %7D%0A%0A    @Test%0A
+    @SuppressWarnings(%22unchecked, limitations of ArgumentCaptor with generics%22)%0A
     public void 
@@ -2383,25 +2383,24 @@
     ));%0A
-%0A
         
 ListVers
@@ -2395,111 +2395,289 @@
     
+ArgumentCaptor%3C
 List
-V
+%3E v
 ersion
-View his
+sCap
 tor
-y
  = 
-resource.history(%22school%22, %221234%22);%0A%0A        assertThat(history.getV
+ArgumentCaptor.forClass(List.class);%0A%0A        resource.history(%22school%22, %221234%22);%0A%0A        verify(viewFactory).listVersionView(versionsCaptor.capture());%0A%0A        List%3CVersion%3E versions = versionsCaptor.getValue();%0A%0A        assertThat(v
 ersions
-()
 .get
@@ -2724,37 +2724,24 @@
 ertThat(
-history.getV
+v
 ersions
-()
 .get(0).
@@ -2786,37 +2786,24 @@
 ertThat(
-history.getV
+v
 ersions
-()
 .get(1).
@@ -2850,29 +2850,16 @@
 hat(
-history.getV
+v
 ersions
-()
 .get

fix broken test