package uk.gov.register.resources;

import org.flywaydb.core.Flyway;
import org.junit.Before;
import org.junit.Test;
import uk.gov.register.core.RegisterReadOnly;
import uk.gov.register.views.HomePageView;
import uk.gov.register.views.ViewFactory;

import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;

public class HomePageResourceTest {

    private RegisterReadOnly registerMock;
    private ViewFactory viewFactoryMock;
    private Flyway flywayMock;

    @Before
    public void beforeEach(){
        registerMock = mock(RegisterReadOnly.class);
        viewFactoryMock = mock(ViewFactory.class);
        flywayMock = mock(Flyway.class);
    }

    @Test
    public void shouldReturnPageViewWithValidValues() throws NoSuchAlgorithmException {
        int totalRecords = 5;
        int totalEntries = 6;
        Optional<Instant> lastUpdated = Optional.of(Instant.ofEpochMilli(1459241964336L));
        HomePageView homePageView = mock(HomePageView.class);

        when(registerMock.getTotalRecords()).thenReturn(totalRecords);
        when(registerMock.getTotalEntries()).thenReturn(totalEntries);
        when(registerMock.getLastUpdatedTime()).thenReturn(lastUpdated);
        when(viewFactoryMock.homePageView(totalRecords, totalEntries, lastUpdated)).thenReturn(homePageView);

        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -> Optional.of("trackingId"), flywayMock);
        homePageResource.home();

        verify(registerMock, times(1)).getTotalRecords();
        verify(registerMock, times(1)).getTotalEntries();
        verify(viewFactoryMock, times(1)).homePageView(totalRecords, totalEntries, lastUpdated);
    }

    @Test
    public void shouldRenderAnalyticsCodeIfPresent() throws Exception {
        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -> Optional.of("codeForTest"), flywayMock);
        
        String s = homePageResource.analyticsTrackingId();

        assertThat(s, equalTo("var gaTrackingId = \"codeForTest\";\n"));
    }

    @Test
    public void shouldRenderEmptyJsFileIfCodeIsAbsent() throws Exception {
        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -> Optional.empty(), flywayMock);

        String s = homePageResource.analyticsTrackingId();

        assertThat(s, equalTo(""));
    }
}


@@ -242,16 +242,50 @@
 ctory;%0A%0A
+import javax.ws.rs.core.Response;%0A
 import j
@@ -710,16 +710,17 @@
 reEach()
+ 
 %7B%0A      
@@ -2124,24 +2124,16 @@
 yMock);%0A
-        
 %0A       
@@ -2585,12 +2585,446 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldUseFlywayToDeleteData() throws Exception %7B%0A        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -%3E Optional.empty(), flywayMock);%0A%0A        Response response = homePageResource.deleteRegisterData();%0A%0A        assertThat(response.getStatus(), equalTo(200));%0A        verify(flywayMock, times(1)).clean();%0A        verify(flywayMock, times(1)).migrate();%0A    %7D
 %0A%7D%0A%0A

Add data delete HomePageResource test