package uk.gov.register.core;

import org.flywaydb.core.Flyway;
import org.junit.Before;
import org.junit.Test;
import org.skife.jdbi.v2.DBI;
import uk.gov.register.auth.RegisterAuthenticator;
import uk.gov.register.configuration.ConfigManager;
import uk.gov.register.exceptions.NoSuchConfigException;
import uk.gov.register.service.RegisterLinkService;

import java.io.IOException;
import java.util.Optional;

import static java.util.Collections.emptyList;
import static org.mockito.Mockito.*;

public class RegisterContextTest {
    private RegisterName registerName;
    private ConfigManager configManager;
    private RegisterLinkService registerLinkService;
    private DBI dbi;
    private Flyway flyway;

    @Before
    public void setup() {
        registerName = new RegisterName("register");
        configManager = mock(ConfigManager.class, RETURNS_DEEP_STUBS);
        registerLinkService = mock(RegisterLinkService.class);
        dbi = mock(DBI.class);
        flyway = mock(Flyway.class);
    }

    @Test
    public void resetRegister_shouldNotResetRegister_whenEnableRegisterDataDeleteIsDisabled() throws IOException, NoSuchConfigException {
        RegisterContext context = new RegisterContext(registerName, configManager, registerLinkService, dbi, flyway, Optional.empty(), false, false, Optional.empty(), Optional.empty(), emptyList(), new RegisterAuthenticator("", ""));
        context.resetRegister();

        verify(flyway, never()).clean();
        verify(configManager, never()).refreshConfig();
        verify(flyway, never()).migrate();
    }

    @Test
    public void resetRegister_shouldResetRegister_whenEnableRegisterDataDeleteIsEnabled() throws IOException, NoSuchConfigException {
        RegisterContext context = new RegisterContext(registerName, configManager, registerLinkService, dbi, flyway, Optional.empty(), true, false, Optional.empty(), Optional.empty(), emptyList(), new RegisterAuthenticator("", ""));
        context.resetRegister();

        verify(flyway, times(1)).clean();
        verify(configManager, times(1)).refreshConfig();
        verify(flyway, times(1)).migrate();
    }
}

@@ -238,16 +238,77 @@
 anager;%0A
+import uk.gov.register.configuration.RegistersConfiguration;%0A
 import u
@@ -352,24 +352,24 @@
 gException;%0A
-
 import uk.go
@@ -433,24 +433,54 @@
 OException;%0A
+import java.util.Collections;%0A
 import java.
@@ -538,24 +538,126 @@
 .emptyList;%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.core.IsEqual.equalTo;%0A
 import stati
@@ -2264,32 +2264,32 @@
 efreshConfig();%0A
-
         verify(f
@@ -2321,11 +2321,1620 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void getRegisterMetadata_returnsUpToDateConfigProvidedByConfigManager() %7B%0A        RegisterMetadata initialMetadata = new RegisterMetadata(%0A                new RegisterName(%22test-register-1%22),%0A                Collections.emptyList(),%0A                %22copyright-1%22,%0A                %22registry-1%22,%0A                %22text-1%22,%0A                %22phase-1%22);%0A%0A        RegisterMetadata expectedUpdatedMetadata = new RegisterMetadata(%0A                new RegisterName(%22test-register-2%22),%0A                Collections.emptyList(),%0A                %22copyright-2%22,%0A                %22registry-2%22,%0A                %22text-2%22,%0A                %22phase-2%22);%0A%0A%0A        RegistersConfiguration rcMock = mock(RegistersConfiguration.class);%0A%0A        when(rcMock.getRegisterMetadata(registerName)).thenReturn(initialMetadata);%0A        when(configManager.getRegistersConfiguration()).thenReturn(rcMock);%0A%0A        RegisterContext context = new RegisterContext(registerName, configManager, registerLinkService, dbi, flyway, Optional.empty(), true, false, Optional.empty(), Optional.empty(), emptyList(), new RegisterAuthenticator(%22%22, %22%22));%0A%0A        RegisterMetadata actualInitialMetadata = context.getRegisterMetadata();%0A        assertThat(actualInitialMetadata, equalTo(initialMetadata));%0A%0A        when(rcMock.getRegisterMetadata(registerName)).thenReturn(expectedUpdatedMetadata);%0A        when(configManager.getRegistersConfiguration()).thenReturn(rcMock);%0A%0A        RegisterMetadata actualUpdatedMetadata = context.getRegisterMetadata();%0A        assertThat(actualUpdatedMetadata, equalTo(expectedUpdatedMetadata));%0A    %7D
 %0A%7D%0A

Add test - prove register metadata is always updated