package edu.berkeley.ground.api.models.postgres;

import org.junit.Test;

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

import edu.berkeley.ground.api.PostgresTest;
import edu.berkeley.ground.api.models.NodeVersion;
import edu.berkeley.ground.api.models.Tag;
import edu.berkeley.ground.api.versions.GroundType;
import edu.berkeley.ground.exceptions.GroundException;
import static org.junit.Assert.*;

public class PostgresNodeVersionFactoryTest extends PostgresTest {

    public PostgresNodeVersionFactoryTest() throws GroundException {
        super();
    }

    @Test
    public void testNodeVersionCreation() throws GroundException {
        String nodeName = "testNode";
        String nodeId = super.factories.getNodeFactory().create(nodeName).getId();

        String structureName = "testStructure";
        String structureId =  super.factories.getStructureFactory().create(structureName).getId();

        Map<String, GroundType> structureVersionAttributes = new HashMap<>();
        structureVersionAttributes.put("intfield", GroundType.INTEGER);
        structureVersionAttributes.put("boolfield", GroundType.BOOLEAN);
        structureVersionAttributes.put("strfield", GroundType.STRING);

        String structureVersionId = super.factories.getStructureVersionFactory().create(
                structureId, structureVersionAttributes, new ArrayList<>()).getId();

        Map<String, Tag> tags = new HashMap<>();
        tags.put("intfield", new Tag(null, "intfield", 1, GroundType.INTEGER));
        tags.put("strfield", new Tag(null, "strfield", "1", GroundType.STRING));
        tags.put("boolfield", new Tag(null, "boolfield", true, GroundType.BOOLEAN));

        String testReference = "http://www.google.com";
        Map<String, String> parameters = new HashMap<>();
        parameters.put("http", "GET");

        String nodeVersionId = super.factories.getNodeVersionFactory().create(tags,
                structureVersionId, testReference, parameters, nodeId, new ArrayList<>()).getId();

        NodeVersion retrieved = super.factories.getNodeVersionFactory().retrieveFromDatabase(nodeVersionId);

        assertEquals(nodeId, retrieved.getNodeId());
        assertEquals(structureVersionId, retrieved.getStructureVersionId());
        assertEquals(testReference, retrieved.getReference());

        assertEquals(parameters.size(), retrieved.getParameters().size());
        assertEquals(tags.size(), retrieved.getTags().size());

        Map<String, String> retrievedParameters = retrieved.getParameters();
        Map<String, Tag> retrievedTags = retrieved.getTags();

        for (String key : parameters.keySet()) {
            assert(retrievedParameters).containsKey(key);
            assertEquals(parameters.get(key), retrievedParameters.get(key));
        }

        for (String key : tags.keySet()) {
            assert(retrievedTags).containsKey(key);
            assertEquals(tags.get(key), retrievedTags.get(key));
        }

        List<String> leaves = super.factories.getNodeFactory().getLeaves(nodeName);

        assertTrue(leaves.contains(nodeVersionId));
        assertTrue(1 == leaves.size());
    }
}

@@ -3001,32 +3001,402 @@
 y));%0A        %7D%0A%0A
+        String nodeVersionId0 = super.factories.getNodeVersionFactory().create(tags,%0A                structureVersionId, testReference, parameters, nodeId, new ArrayList%3C%3E()).getId();%0A%0A        String nodeVersionId1 = super.factories.getNodeVersionFactory().create(tags,%0A                structureVersionId, testReference, parameters, nodeId, new ArrayList%3C%3E()).getId();%0A%0A
         List%3CStr
@@ -3512,16 +3512,17 @@
 ersionId
+0
 ));%0A    
@@ -3540,26 +3540,384 @@
 rue(
-1 == leaves.size()
+leaves.contains(nodeVersionId1));%0A        assertTrue(leaves.contains(nodeVersionId));%0A        for (String leaf: leaves) %7B%0A            System.out.println(%22leaf: %22 + leaf);%0A        %7D%0A        assertTrue(3 == leaves.size());%0A        assertEquals(leaves.get(0), nodeVersionId0);%0A        assertEquals(leaves.get(1), nodeVersionId1);%0A        assertEquals(leaves.get(2), nodeVersionId
 );%0A 

add tests for NodeVersionFactory.getLeaves()