/**
   * This tests writing messages and removing them in a bulk delete.
   *
   * @throws HBaseException
   */
  @Test
  public void writeAndRemoveRows() throws HBaseException {
    MessagePersister mp = new MessagePersister();
    HBaseFacade hbs = new HBaseFacade();

    mp.writeMessage(messageKey1, data1);
    mp.writeMessage(messageKey2, data2);
    mp.writeMessage(messageKey3, data2);
    mp.writeMessage(messageKey4, data2);

    Set<Object> keys = new HashSet<Object>();
    keys.add(messageKey1);
    keys.add(messageKey2);
    keys.add(messageKey3);
    keys.add(messageKey4);

    hbs.removeRows(MessagePersister.TABLE_MESSAGE, keys);

    byte[] result = mp.readMessage(messageKey1);
    assertNull(result);
    result = mp.readMessage(messageKey2);
    assertNull(result);
    result = mp.readMessage(messageKey3);
    assertNull(result);
    result = mp.readMessage(messageKey4);
    assertNull(result);
  }
  /**
   * This tests writing messages, scanning the table, and removing the messages.
   *
   * @throws HBaseException
   */
  @Test
  public void writeAndScanMessageData() throws HBaseException {
    MessagePersister mp = new MessagePersister();
    HBaseFacade hbs = new HBaseFacade();

    mp.writeMessage(messageKey1, data1);
    mp.writeMessage(messageKey2, data2);
    mp.writeMessage(messageKey3, data2);
    mp.writeMessage(messageKey4, data2);
    mp.writeMessage(messageKey5, data2);
    mp.writeMessage(messageKey6, data2);
    mp.writeMessage(messageKey7, data2);
    mp.writeMessage(messageKey8, data2);
    mp.writeMessage(messageKey9, data2);
    mp.writeMessage(messageKey10, data2);

    Map<String, byte[]> dataMap =
        hbs.scan(
            MessagePersister.TABLE_MESSAGE,
            10,
            MessagePersister.COL_FAMILY_DATA,
            MessagePersister.QUANTIFIER_VALUE);

    assertTrue(Arrays.equals(dataMap.get(messageKey1), data1));
    assertTrue(Arrays.equals(dataMap.get(messageKey2), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey3), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey4), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey5), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey6), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey7), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey8), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey9), data2));
    assertTrue(Arrays.equals(dataMap.get(messageKey10), data2));

    mp.removeMessage(messageKey1);
    mp.removeMessage(messageKey2);
    mp.removeMessage(messageKey3);
    mp.removeMessage(messageKey4);
    mp.removeMessage(messageKey5);
    mp.removeMessage(messageKey6);
    mp.removeMessage(messageKey7);
    mp.removeMessage(messageKey8);
    mp.removeMessage(messageKey9);
    mp.removeMessage(messageKey10);
  }
  /**
   * This tests writing messages, scanning the table for the keys, and removing the messages.
   *
   * @throws HBaseException
   */
  @Test
  public void writeAndScanMessageKeys() throws HBaseException {
    MessagePersister mp = new MessagePersister();
    HBaseFacade hbs = new HBaseFacade();

    mp.writeMessage(messageKey1, data1);
    mp.writeMessage(messageKey2, data2);
    mp.writeMessage(messageKey3, data2);
    mp.writeMessage(messageKey4, data2);
    mp.writeMessage(messageKey5, data2);
    mp.writeMessage(messageKey6, data2);
    mp.writeMessage(messageKey7, data2);
    mp.writeMessage(messageKey8, data2);
    mp.writeMessage(messageKey9, data2);
    mp.writeMessage(messageKey10, data2);

    Set<Object> keys = hbs.scanForKeys(MessagePersister.TABLE_MESSAGE);
    assertTrue("Did not return a key", keys.contains(messageKey1));
    assertTrue("Did not return a key", keys.contains(messageKey2));
    assertTrue("Did not return a key", keys.contains(messageKey3));
    assertTrue("Did not return a key", keys.contains(messageKey4));
    assertTrue("Did not return a key", keys.contains(messageKey5));
    assertTrue("Did not return a key", keys.contains(messageKey6));
    assertTrue("Did not return a key", keys.contains(messageKey7));
    assertTrue("Did not return a key", keys.contains(messageKey8));
    assertTrue("Did not return a key", keys.contains(messageKey9));
    assertTrue("Did not return a key", keys.contains(messageKey10));

    mp.removeMessage(messageKey1);
    mp.removeMessage(messageKey2);
    mp.removeMessage(messageKey3);
    mp.removeMessage(messageKey4);
    mp.removeMessage(messageKey5);
    mp.removeMessage(messageKey6);
    mp.removeMessage(messageKey7);
    mp.removeMessage(messageKey8);
    mp.removeMessage(messageKey9);
    mp.removeMessage(messageKey10);
  }
  /**
   * IMPORTANT - cannot run this test two times in a row without disabling and dropping the test
   * table.
   *
   * @throws HBaseException
   */
  @Test
  public void createTable() throws HBaseException {
    String testCF = "testCF";
    String testDataVal = "This is some data.";
    String testTable = "testTable";
    String testField = "testField";
    String testKey = "testKey";

    List<String> colFamilies = new ArrayList<String>();
    colFamilies.add(testCF);

    HBaseFacade hbs = new HBaseFacade();
    try {
      hbs.createTable(testTable, colFamilies);
    } catch (HBaseException ex) {
      if (ex.getCause() instanceof TableExistsException) {
        System.err.println(
            "Cannot test createTable because the " + testTable + " table already exists.");
        return;
      } else {
        throw ex;
      }
    }

    Map<String, Map<String, byte[]>> testData = new HashMap<String, Map<String, byte[]>>();

    Map<String, byte[]> dataCells1 = new HashMap<String, byte[]>();
    dataCells1.put(testField, testDataVal.getBytes());
    testData.put(colFamilies.get(0), dataCells1);
    hbs.addRow(testTable, testKey, testData);

    testData = hbs.readRow(testTable, testKey, colFamilies);
    assertTrue(
        Arrays.equals(testData.get(colFamilies.get(0)).get(testField), testDataVal.getBytes()));

    hbs.removeRow(testTable, testKey);
  }