/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.zookeeper;


import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.*;
import org.apache.zookeeper.KeeperException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(MediumTests.class)
public class TestZKTable {
  private static final Log LOG = LogFactory.getLog(TestZooKeeperNodeTracker.class);
  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    TEST_UTIL.startMiniZKCluster();
  }

  @AfterClass
  public static void tearDownAfterClass() throws Exception {
    TEST_UTIL.shutdownMiniZKCluster();
  }

  @Test
  public void testTableStates()
  throws ZooKeeperConnectionException, IOException, KeeperException {
    final TableName name =
        TableName.valueOf("testDisabled");
    Abortable abortable = new Abortable() {
      @Override
      public void abort(String why, Throwable e) {
        LOG.info(why, e);
      }
      
      @Override
      public boolean isAborted() {
        return false;
      }
      
    };
    ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
      name.getNameAsString(), abortable, true);
    ZKTable zkt = new ZKTable(zkw);
    assertFalse(zkt.isEnabledTable(name));
    assertFalse(zkt.isDisablingTable(name));
    assertFalse(zkt.isDisabledTable(name));
    assertFalse(zkt.isEnablingTable(name));
    assertFalse(zkt.isDisablingOrDisabledTable(name));
    assertFalse(zkt.isDisabledOrEnablingTable(name));
    assertFalse(zkt.isTablePresent(name));
    zkt.setDisablingTable(name);
    assertTrue(zkt.isDisablingTable(name));
    assertTrue(zkt.isDisablingOrDisabledTable(name));
    assertFalse(zkt.getDisabledTables().contains(name));
    assertTrue(zkt.isTablePresent(name));
    zkt.setDisabledTable(name);
    assertTrue(zkt.isDisabledTable(name));
    assertTrue(zkt.isDisablingOrDisabledTable(name));
    assertFalse(zkt.isDisablingTable(name));
    assertTrue(zkt.getDisabledTables().contains(name));
    assertTrue(zkt.isTablePresent(name));
    zkt.setEnablingTable(name);
    assertTrue(zkt.isEnablingTable(name));
    assertTrue(zkt.isDisabledOrEnablingTable(name));
    assertFalse(zkt.isDisabledTable(name));
    assertFalse(zkt.getDisabledTables().contains(name));
    assertTrue(zkt.isTablePresent(name));
    zkt.setEnabledTable(name);
    assertTrue(zkt.isEnabledTable(name));
    assertFalse(zkt.isEnablingTable(name));
    assertTrue(zkt.isTablePresent(name));
    zkt.setDeletedTable(name);
    assertFalse(zkt.isEnabledTable(name));
    assertFalse(zkt.isDisablingTable(name));
    assertFalse(zkt.isDisabledTable(name));
    assertFalse(zkt.isEnablingTable(name));
    assertFalse(zkt.isDisablingOrDisabledTable(name));
    assertFalse(zkt.isDisabledOrEnablingTable(name));
    assertFalse(zkt.isTablePresent(name));
  }

}

