package uk.gov.register.resources;

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 {

    @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);

        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);
        ViewFactory viewFactoryMock = mock(ViewFactory.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"));
        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 {
        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);
        ViewFactory viewFactoryMock = mock(ViewFactory.class);

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

        String s = homePageResource.analyticsTrackingId();

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

    @Test
    public void shouldRenderEmptyJsFileIfCodeIsAbsent() throws Exception {
        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);
        ViewFactory viewFactoryMock = mock(ViewFactory.class);

        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -> Optional.empty());

        String s = homePageResource.analyticsTrackingId();

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


@@ -29,16 +29,74 @@
 urces;%0A%0A
+import org.flywaydb.core.Flyway;%0Aimport org.junit.Before;%0A
 import o
@@ -520,16 +520,326 @@
 Test %7B%0A%0A
+    private RegisterReadOnly registerMock;%0A    private ViewFactory viewFactoryMock;%0A    private Flyway flywayMock;%0A%0A    @Before%0A    public void beforeEach()%7B%0A        registerMock = mock(RegisterReadOnly.class);%0A        viewFactoryMock = mock(ViewFactory.class);%0A        flywayMock = mock(Flyway.class);%0A    %7D%0A%0A
     @Tes
@@ -1146,142 +1146,8 @@
 );%0A%0A
-        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);%0A        ViewFactory viewFactoryMock = mock(ViewFactory.class);%0A%0A
     
@@ -1595,16 +1595,28 @@
 kingId%22)
+, flywayMock
 );%0A     
@@ -1950,142 +1950,8 @@
 n %7B%0A
-        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);%0A        ViewFactory viewFactoryMock = mock(ViewFactory.class);%0A%0A
     
@@ -2074,19 +2074,39 @@
 orTest%22)
-);%0A
+, flywayMock);%0A        
 %0A       
@@ -2327,142 +2327,8 @@
 n %7B%0A
-        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);%0A        ViewFactory viewFactoryMock = mock(ViewFactory.class);%0A%0A
     
@@ -2441,16 +2441,28 @@
 .empty()
+, flywayMock
 );%0A%0A    

Fix broken tests