package org.ow2.proactive.resourcemanager.db;

import java.util.List;

import org.ow2.proactive.resourcemanager.common.NodeState;
import org.ow2.proactive.resourcemanager.core.history.NodeHistory;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;


public class NodeHistoryTest {

    protected static RMDBManager dbManager;

    @BeforeClass
    public static void initDB() throws Exception {
        PAResourceManagerProperties.RM_ALIVE_EVENT_FREQUENCY.updateProperty("100");
        Configuration config = new Configuration().configure("/functionaltests/config/hibernate.cfg.xml");
        dbManager = new RMDBManager(config, true, true);
    }

    @AfterClass
    public static void cleanup() {
        if (dbManager != null) {
            dbManager.close();
        }
    }

    @Test
    public void testNodeHistory() throws Exception {
        testSaveHistory();
        testUpdateHistory();
    }

    public void testSaveHistory() throws Exception {

        Configuration config = new Configuration().configure("/functionaltests/config/hibernate.cfg.xml");
        dbManager = new RMDBManager(config, true, true);

        NodeHistory expected = createNodeHistory(1);

        dbManager.saveNodeHistory(expected);

        List<?> rows = dbManager.executeSqlQuery("from NodeHistory");
        Assert.assertEquals(1, rows.size());

        NodeHistory actual = (NodeHistory) rows.get(0);
        assertEquals(expected, actual);
    }

    public void testUpdateHistory() throws Exception {

        NodeHistory expected1 = createNodeHistory(1);
        expected1.setEndTime(2);
        NodeHistory expected2 = createNodeHistory(2);

        dbManager.saveNodeHistory(expected2);

        List<?> rows = dbManager.executeSqlQuery("from NodeHistory");
        Assert.assertEquals(2, rows.size());

        assertEquals(expected1, (NodeHistory) rows.get(0));
        assertEquals(expected2, (NodeHistory) rows.get(1));

        dbManager.close();
    }

    private void assertEquals(NodeHistory expected, NodeHistory actual) {
        Assert.assertEquals(expected.getHost(), actual.getHost());
        Assert.assertEquals(expected.getNodeSource(), actual.getNodeSource());
        Assert.assertEquals(expected.getNodeState(), actual.getNodeState());
        Assert.assertEquals(expected.getNodeUrl(), actual.getNodeUrl());
        Assert.assertEquals(expected.getProviderName(), actual.getProviderName());
        Assert.assertEquals(expected.getStartTime(), actual.getStartTime());
        Assert.assertEquals(expected.getEndTime(), actual.getEndTime());
    }

    private NodeHistory createNodeHistory(long startTime) {

        NodeHistory nodeHistory = new NodeHistory();

        nodeHistory.setHost("host");
        nodeHistory.setNodeSource("ns");
        nodeHistory.setNodeState(NodeState.FREE);
        nodeHistory.setNodeUrl("url");
        nodeHistory.setProviderName("provider");
        nodeHistory.setStartTime(startTime);
        nodeHistory.setEndTime(0);
        nodeHistory.setStoreInDataBase(true);
        nodeHistory.setLocked(true);

        return nodeHistory;
    }
}

@@ -40,16 +40,73 @@
 er.db;%0A%0A
+import static com.google.common.truth.Truth.assertThat;%0A%0A
 import j
@@ -481,16 +481,55 @@
 .Test;%0A%0A
+import com.google.common.truth.Truth;%0A%0A
 %0Apublic 
@@ -2225,32 +2225,372 @@
 close();%0A    %7D%0A%0A
+    @Test%0A    public void testSetLocked() %7B%0A        NodeHistory nodeHistory = new NodeHistory();%0A        assertThat(nodeHistory.isLocked()).isFalse();%0A        nodeHistory.setLocked(true);%0A        assertThat(nodeHistory.isLocked()).isTrue();%0A        nodeHistory.setLocked(false);%0A        assertThat(nodeHistory.isLocked()).isFalse();%0A    %7D%0A%0A
     private void

Add unit test to NodeHistoryTest