import static junit.framework.Assert.*;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import data.Taxonomy;
import org.rocksdb.RocksDB;

import util.db.RDB;

public class DBTest {

	static {
		RocksDB.loadLibrary();
	}

	// @Test
	public void dbConnectorTest() throws UnsupportedEncodingException {
		String key1 = "alfa";
		AtomicLong value1 = new AtomicLong(10);
		String key2 = "beta";
		AtomicLong value2 = new AtomicLong(20);
		String key3 = "alfa";
		AtomicLong value3 = new AtomicLong(50);
		String key4 = "theta";
		AtomicLong value4 = new AtomicLong(40);

		HashMap<String, AtomicLong> results = new HashMap<>();
		results.put(key1, value1);
		results.put(key2, value2);

		RDB db = new RDB();
		db.writeBatch(results);

		// let's check how that fared out
		Map<String, AtomicLong> dumpedResults = db.getDump();
		// should have 2 items
		assertEquals(2, dumpedResults.size());

		// entry comparison
		assertTrue(dumpedResults.containsKey(key1));
		assertTrue(value1.longValue() == dumpedResults.get(key1).longValue());
		assertTrue(dumpedResults.containsKey(key2));
		assertTrue(value2.longValue() == dumpedResults.get(key2).longValue());

		results = new HashMap<>();
		results.put(key3, value3);
		results.put(key4, value4);
		db.writeBatch(results);
		dumpedResults = db.getDump();

		// should have 3 items with alfa's value reflecting summation
		assertEquals(3, dumpedResults.size());

		// entry comparison
		assertTrue(dumpedResults.containsKey(key1));
		assertTrue(value1.longValue() + value3.longValue() == dumpedResults.get(key1).longValue());
		assertTrue(dumpedResults.containsKey(key2));
		assertTrue(value2.longValue() == dumpedResults.get(key2).longValue());
		assertTrue(dumpedResults.containsKey(key4));
		assertTrue(value4.longValue() == dumpedResults.get(key4).longValue());

		db.delete();
	}
}
