package no.finn.unleash.repository;

import org.junit.Test;

import java.net.URI;

import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.*;


public class FeatureToggleRepositoryTest {
    private ToggleRepository toggleRepository;

    @Test
    public void noBackupFileAndNoRepositoryAvailable() {
        ToggleFetcher toggleFetcher = new HttpToggleFetcher(URI.create("http://localhost:4242/features"));
        ToggleBackupHandler toggleBackupHandler = new ToggleBackupHandlerFile();
        toggleRepository = new FeatureToggleRepository(toggleFetcher, toggleBackupHandler);
        assertNull("should be null", toggleRepository.getToggle("unknownFeature"));
    }

    @Test
    public void backup_toggles_should_be_loaded_at_startup() {
        ToggleBackupHandler toggleBackupHandler = mock(ToggleBackupHandler.class);
        ToggleFetcher toggleFetcher = mock(ToggleFetcher.class);
        new FeatureToggleRepository(toggleFetcher, toggleBackupHandler, Long.MAX_VALUE);

        verify(toggleBackupHandler, times(1)).read();
    }

}

@@ -41,88 +41,300 @@
 ort 
-org.junit.Test;%0A%0Aimport java.net.URI;%0A%0Aimport static org.junit.Assert.assertNull
+no.finn.unleash.FeatureToggle;%0Aimport org.junit.Test;%0A%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue
 ;%0Aim
@@ -416,55 +416,8 @@
 st %7B
-%0A    private ToggleRepository toggleRepository;
 %0A%0A  
@@ -677,16 +677,33 @@
         
+ToggleRepository 
 toggleRe
@@ -1241,10 +1241,1420 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void featureTogglesShouldBeUpdated() throws URISyntaxException, InterruptedException %7B%0A        ToggleFetcher toggleFetcher = mock(ToggleFetcher.class);%0A        ToggleBackupHandler toggleBackupHandler = mock(ToggleBackupHandler.class);%0A%0A        ToggleCollection toggleCollection = populatedToggleCollection(new FeatureToggle(%22toggleFetcherCalled%22, false, %22custom%22, null));%0A        when(toggleBackupHandler.read()).thenReturn(toggleCollection);%0A%0A        toggleCollection = populatedToggleCollection(new FeatureToggle(%22toggleFetcherCalled%22, true, %22custom%22, null));%0A        Response response = new Response(Response.Status.CHANGED, toggleCollection);%0A        when(toggleFetcher.fetchToggles()).thenReturn(response);%0A%0A        ToggleRepository toggleRepository = new FeatureToggleRepository(toggleFetcher, toggleBackupHandler, 1L);%0A        assertFalse(toggleRepository.getToggle(%22toggleFetcherCalled%22).isEnabled());%0A        Thread.sleep(1); //wait for background fetching%0A%0A        assertTrue(toggleRepository.getToggle(%22toggleFetcherCalled%22).isEnabled());%0A        verify(toggleFetcher, times(1)).fetchToggles();%0A        verify(toggleBackupHandler, times(1)).read();%0A    %7D%0A%0A    private ToggleCollection populatedToggleCollection(FeatureToggle featureToggle) %7B%0A        List%3CFeatureToggle%3E list = new ArrayList();%0A        list.add(featureToggle);%0A        return new ToggleCollection(list);%0A%0A    %7D%0A%0A
 %7D%0A

Test service > file in FeatureToggleRepository