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.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);
    }
}


@@ -285,16 +285,118 @@
 ional;%0A%0A
+import static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A
 import s
@@ -1616,12 +1616,944 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldRenderAnalyticsCodeIfPresent() throws Exception %7B%0A        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);%0A        ViewFactory viewFactoryMock = mock(ViewFactory.class);%0A%0A        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -%3E Optional.of(%22codeForTest%22));%0A%0A        String s = homePageResource.analyticsTrackingId();%0A%0A        assertThat(s, equalTo(%22var gaTrackingId = %5C%22codeForTest%5C%22;%5Cn%22));%0A    %7D%0A%0A    @Test%0A    public void shouldRenderEmptyJsFileIfCodeIsAbsent() throws Exception %7B%0A        RegisterReadOnly registerMock = mock(RegisterReadOnly.class);%0A        ViewFactory viewFactoryMock = mock(ViewFactory.class);%0A%0A        HomePageResource homePageResource = new HomePageResource(registerMock, viewFactoryMock, () -%3E Optional.empty());%0A%0A        String s = homePageResource.analyticsTrackingId();%0A%0A        assertThat(s, equalTo(%22%22));%0A    %7D
 %0A%7D%0A%0A

add test for analytics code endpoint