package org.rmatil.sync.event.aggregator.test.mocks;

import org.rmatil.sync.commons.hashing.Hash;
import org.rmatil.sync.event.aggregator.config.Config;
import org.rmatil.sync.persistence.api.IPathElement;
import org.rmatil.sync.persistence.api.IStorageAdapter;
import org.rmatil.sync.persistence.exceptions.InputOutputException;
import org.rmatil.sync.version.api.IObjectManager;
import org.rmatil.sync.version.core.model.Index;
import org.rmatil.sync.version.core.model.PathObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Mocks an object manager
 *
 * @see IObjectManager
 */
public class ObjectManagerMock implements IObjectManager {

    Index index;

    Map<String, PathObject> pathObjects;

    public ObjectManagerMock() {
        this.index = new Index(new HashMap<>());
        this.pathObjects = new HashMap<>();
    }

    @Override
    public void clear()
            throws InputOutputException {
        this.pathObjects = new HashMap<>();
        this.index = new Index(new HashMap<>());
    }

    @Override
    public void writeObject(PathObject path)
            throws InputOutputException {
        this.pathObjects.put(Hash.hash(Config.DEFAULT.getHashingAlgorithm(), path.getAbsolutePath()), path);
    }

    @Override
    public PathObject getObject(String fileNameHash)
            throws InputOutputException {
        return this.pathObjects.get(fileNameHash);
    }

    @Override
    public PathObject getObjectForPath(String s)
            throws InputOutputException {
        throw new RuntimeException("Not implemented");
    }

    @Override
    public String getHashForPath(String s) {
        return "testHash";
    }

    @Override
    public void removeObject(String fileNameHash)
            throws InputOutputException {
        this.pathObjects.remove(fileNameHash);
    }

    @Override
    public List<PathObject> getChildren(String s)
            throws InputOutputException {
        List<PathObject> pathObjects = new ArrayList<>();
        for (Map.Entry<String, PathObject> entry : this.pathObjects.entrySet()) {
            if (entry.getKey().startsWith(s + "/")) {
                pathObjects.add(entry.getValue());
            }
        }

        return pathObjects;
    }

    @Override
    public IPathElement getObjectDir() {
        throw new RuntimeException("Not implemented");
    }

    @Override
    public IStorageAdapter getStorageAdapater() {
        throw new RuntimeException("Not implemented");
    }

    @Override
    public Index getIndex() {
        return this.index;
    }

    @Override
    public String getIndexFileName() {
        return "index.json";
    }
}

@@ -1186,29 +1186,22 @@
     
-this.pathObjects.put(
+String hash = 
 Hash
@@ -1267,16 +1267,116 @@
 ePath())
+;%0A        this.index.getPaths().put(path.getAbsolutePath(), hash);%0A        this.pathObjects.put(hash
 , path);
@@ -1667,53 +1667,437 @@
     
-throw new RuntimeException(%22Not implemented%22)
+String hash = this.index.getPaths().get(s);%0A        if (null == hash) %7B%0A            throw new InputOutputException(%22Failed to get hash for path %22 + s + %22. No such file or directory%22);%0A        %7D%0A%0A        PathObject pathObject = this.pathObjects.get(hash);%0A        if (null == pathObject) %7B%0A            throw new InputOutputException(%22Failed to get object for path %22 + s + %22. No object found%22);%0A        %7D%0A%0A        return pathObject
 ;%0A  

Use index too