package com.unidev.zerostorage;

import com.unidev.zerostorage.index.IndexStorage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
 * Storage index tests
 */
public class TestIndexStorage {

    @Rule
    public TemporaryFolder folder= new TemporaryFolder();

    @Test
    public void testBlankIndexStorageSave() {
        IndexStorage indexStorage = new IndexStorage();

        Storage blankIndex = new Storage();
        indexStorage.index(blankIndex);

        File root = folder.getRoot();

        indexStorage.storageRoot(root);
        indexStorage.save();

    }

    @Test
    public void testIndexStorageSave() {
        IndexStorage indexStorage = new IndexStorage();

        Storage index = new Storage();
        index.details().put("Potato", "Tomato");
        index.details().put("Tomato", null);
        indexStorage.index(index);

        File root = folder.getRoot();

        indexStorage.storageRoot(root);
        indexStorage.save();

    }

    @Test
    public void testLoadEmptyIndexStorage() throws IOException {
        InputStream inputStream = TestStorage.class.getResourceAsStream("/blankIndex.json");
        File root = folder.getRoot();

        File index = new File(root, IndexStorage.INDEX_FILE);
        Files.copy(inputStream, index.toPath());

        IndexStorage indexStorage = new IndexStorage();
        indexStorage.storageRoot(root);
        indexStorage.load();
    }

    @Test
    public void testSaveLoadIndexStorage() {

        IndexStorage indexStorage = new IndexStorage();

        Storage index = new Storage();
        index.details().put("Potato", "Tomato");
        index.details().put("Tomato", null);
        indexStorage.index(index);

        File root = folder.getRoot();

        indexStorage.storageRoot(root);
        indexStorage.save();

        IndexStorage storageToLoad = new IndexStorage();
        storageToLoad.storageRoot(root);
        storageToLoad.load();

        Storage loadedIndex = storageToLoad.index();
        assertThat(loadedIndex, is(not(nullValue())));
        assertThat(loadedIndex.details(), is(not(nullValue())));

        assertThat(loadedIndex.details().opt("Potato"), is("Tomato"));


    }

}

@@ -353,16 +353,66 @@
 ers.*;%0A%0A
+import static com.unidev.zerostorage.Metadata.*;%0A%0A
 /**%0A * S
@@ -2459,16 +2459,811 @@
 ato%22));%0A
+    %7D%0A%0A    @Test%0A    public void testIndexMetadataManagement() throws IOException %7B%0A        IndexStorage indexStorage = new IndexStorage();%0A//        File root = new File(%22/tmp/111%22);%0A//        root.mkdirs();%0A        File root = folder.getRoot();%0A        indexStorage.storageRoot(root);%0A        indexStorage.index().details().put(IndexStorage.META_PER_STORAGE_KEY, 10);%0A%0A        for(int i = 0;i%3C13;i++) %7B%0A            Metadata meta = newMetadata();%0A            meta._id(%22id_%22 + i);%0A            indexStorage.addMetadata(meta);%0A        %7D%0A%0A        IndexStorage loadStorage = new IndexStorage().storageRoot(root).load();%0A        assertThat(loadStorage.index(), is(not(nullValue())));%0A        assertThat(loadStorage.index().metadata().size(), is(2)); // 13 records should be stored in 2 files: 10 + 3%0A
 %0A%0A    %7D%0A

Test case for metadata distribution in different files