package com.intropro.prairie.unit.hbase;

import com.intropro.prairie.unit.common.PortProvider;
import com.intropro.prairie.unit.common.annotation.PrairieUnit;
import com.intropro.prairie.unit.common.exception.DestroyUnitException;
import com.intropro.prairie.unit.common.exception.InitUnitException;
import com.intropro.prairie.unit.hadoop.HadoopUnit;
import com.intropro.prairie.unit.zookeeper.ZookeeperUnit;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HConstants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

/**
 * Created by presidentio on 4/2/16.
 */
public class HBaseUnit extends HadoopUnit {

    private static final Logger LOGGER = LogManager.getLogger(HBaseUnit.class);

    @PrairieUnit
    private ZookeeperUnit zookeeperUnit;

    private HBaseTestingUtility hBaseTestingUtility;

    public HBaseUnit() {
        super("hbase");
    }

    @Override
    protected Configuration gatherConfigs() {
        Configuration conf = HBaseConfiguration.create(super.gatherConfigs());
        conf.addResource("hbase-site.prairie.xml");
        conf.setInt(HConstants.MASTER_PORT, PortProvider.nextPort());
        conf.setInt(HConstants.REGIONSERVER_PORT, PortProvider.nextPort());
        conf.set(HConstants.HBASE_DIR, getTmpDir().resolve("data").toString());
        conf.set(HConstants.ZOOKEEPER_QUORUM, zookeeperUnit.getHost() + ":" + zookeeperUnit.getPort());
        conf.set(HConstants.ZK_CFG_PROPERTY_PREFIX + HConstants.CLIENT_PORT_STR, "" + zookeeperUnit.getPort());
        return conf;
    }

    @Override
    public Configuration getConfig() {
        return hBaseTestingUtility.getConfiguration();
    }

    @Override
    protected void destroy() throws DestroyUnitException {
        try {
            hBaseTestingUtility.shutdownMiniHBaseCluster();
        } catch (IOException e) {
            throw new DestroyUnitException("Failed to shutdown hbase mini cluster", e);
        }
    }

    @Override
    protected void init() throws InitUnitException {
        hBaseTestingUtility = new HBaseTestingUtility(gatherConfigs());
        try {
            hBaseTestingUtility.startMiniHBaseCluster(1, 1);
        } catch (Exception e) {
            throw new InitUnitException("Failed to start hbase mini cluster", e);
        }
    }

}

@@ -441,32 +441,72 @@
 .Configuration;%0A
+import org.apache.hadoop.fs.FileSystem;%0A
 import org.apach
@@ -532,32 +532,32 @@
 eConfiguration;%0A
-
 import org.apach
@@ -1145,24 +1145,125 @@
 Configs() %7B%0A
+        System.setProperty(%22test.build.data.basedirectory%22, getTmpDir().resolve(%22data%22).toString());%0A
         Conf
@@ -1579,36 +1579,36 @@
 pDir().resolve(%22
-data
+root
 %22).toString());%0A
@@ -2109,24 +2109,170 @@
 eCluster();%0A
+            FileSystem.get(hBaseTestingUtility.getConfiguration()).delete(hBaseTestingUtility.getDefaultRootDirPath(),%0A                    true);%0A
         %7D ca
@@ -2457,32 +2457,32 @@
 UnitException %7B%0A
-
         hBaseTes
@@ -2543,32 +2543,265 @@
 ;%0A        try %7B%0A
+            FileSystem.get(hBaseTestingUtility.getConfiguration()).delete(hBaseTestingUtility.getDefaultRootDirPath(),%0A                    true);%0A        %7D catch (IOException e) %7B%0A            LOGGER.error(e);%0A        %7D%0A        try %7B%0A
             hBas

remove data directory after shutdown and before startup