/**
 * 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.smartdata.server.engine.rule;

import org.apache.hadoop.hdfs.DFSUtil;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.smartdata.admin.SmartAdmin;
import org.smartdata.common.rule.RuleState;
import org.smartdata.conf.SmartConf;
import org.smartdata.conf.SmartConfKeys;
import org.smartdata.server.SmartServer;
import org.smartdata.server.metastore.MetaUtil;
import org.smartdata.server.metastore.TestDBUtil;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY;

public class TestSubmitRule {
  private SmartConf conf;
  private MiniDFSCluster cluster;
  private SmartServer ssm;

  @Before
  public void setUp() throws Exception {
    conf = new SmartConf();
    cluster = new MiniDFSCluster.Builder(conf)
        .numDataNodes(3).build();

    Collection<URI> namenodes = DFSUtil.getInternalNsRpcUris(conf);
    List<URI> uriList = new ArrayList<>(namenodes);
    conf.set(DFS_NAMENODE_HTTP_ADDRESS_KEY, uriList.get(0).toString());
    conf.set(SmartConfKeys.DFS_SSM_NAMENODE_RPCSERVER_KEY,
        uriList.get(0).toString());

    // Set db used
    String dbFile = TestDBUtil.getUniqueEmptySqliteDBFile();
    String dbUrl = MetaUtil.SQLITE_URL_PREFIX + dbFile;
    conf.set(SmartConfKeys.DFS_SSM_DB_URL_KEY, dbUrl);

    // rpcServer start in SmartServer
    ssm = SmartServer.launchWith(conf);
  }

  @After
  public void cleanUp() throws Exception {
    if (ssm != null) {
      ssm.shutdown();
    }
    if (cluster != null) {
      cluster.shutdown();
    }
  }

  @Test
  public void testSubmitRule() throws Exception {
    String rule = "file: every 1s \n | length > 10 | cache";
    SmartAdmin client = new SmartAdmin(conf);

    long ruleId = 0l;
    boolean wait = true;
    while (wait) {
      try {
        ruleId = client.submitRule(rule, RuleState.ACTIVE);
        wait = false;
      } catch (IOException e) {
        if (!e.toString().contains("not ready")) {
          throw e;
        }
      }
    }

    for (int i = 0; i < 10; i++) {
      long id = client.submitRule(rule, RuleState.ACTIVE);
      Assert.assertTrue(ruleId + i + 1 == id);
    }

    String badRule = "something else";
    try {
      client.submitRule(badRule, RuleState.ACTIVE);
      Assert.fail("Should have an exception here");
    } catch (IOException e) {
    }

    try {
      client.checkRule(badRule);
      Assert.fail("Should have an exception here");
    } catch (IOException e) {
    }
  }
}

@@ -858,1700 +858,276 @@
 org.
-apache.hadoop.hdfs.DFSUtil;%0Aimport org.apache.hadoop.hdfs.MiniDFSCluster;%0Aimport org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.smartdata.admin.SmartAdmin;%0Aimport org.smartdata.common.rule.RuleState;%0Aimport org.smartdata.conf.SmartConf;%0Aimport org.smartdata.conf.SmartConfKeys;%0Aimport org.smartdata.server.SmartServer;%0Aimport org.smartdata.server.metastore.MetaUtil;%0Aimport org.smartdata.server.metastore.TestDBUtil;%0A%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0Aimport static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY;%0A%0Apublic class TestSubmitRule %7B%0A  private SmartConf conf;%0A  private MiniDFSCluster cluster;%0A  private SmartServer ssm;%0A%0A  @Before%0A  public void setUp() throws Exception %7B%0A    conf = new SmartConf();%0A    cluster = new MiniDFSCluster.Builder(conf)%0A        .numDataNodes(3).build();%0A%0A    Collection%3CURI%3E namenodes = DFSUtil.getInternalNsRpcUris(conf);%0A    List%3CURI%3E uriList = new ArrayList%3C%3E(namenodes);%0A    conf.set(DFS_NAMENODE_HTTP_ADDRESS_KEY, uriList.get(0).toString());%0A    conf.set(SmartConfKeys.DFS_SSM_NAMENODE_RPCSERVER_KEY,%0A        uriList.get(0).toString());%0A%0A    // Set db used%0A    String dbFile = TestDBUtil.getUniqueEmptySqliteDBFile();%0A    String dbUrl = MetaUtil.SQLITE_URL_PREFIX + dbFile;%0A    conf.set(SmartConfKeys.DFS_SSM_DB_URL_KEY, dbUrl);%0A%0A    // rpcServer start in SmartServer%0A    ssm = SmartServer.launchWith(conf);%0A  %7D%0A%0A  @After%0A  public void cleanUp() throws Exception %7B%0A    if (ssm != null) %7B%0A      ssm.shutdown();%0A    %7D%0A    if (cluster != null) %7B%0A      cluster.shutdown();%0A    %7D%0A  %7D
+junit.Assert;%0Aimport org.junit.Test;%0Aimport org.smartdata.admin.SmartAdmin;%0Aimport org.smartdata.common.rule.RuleState;%0Aimport org.smartdata.server.TestEmptyMiniSmartCluster;%0A%0Aimport java.io.IOException;%0A%0Apublic class TestSubmitRule extends TestEmptyMiniSmartCluster %7B
 %0A%0A  
@@ -1182,16 +1182,48 @@
 ption %7B%0A
+    waitTillSSMExitSafeMode();%0A%0A
     Stri
@@ -1352,267 +1352,59 @@
     
-boolean wait = true;%0A    while (wait) %7B%0A      try %7B%0A        ruleId = client.submitRule(rule, RuleState.ACTIVE);%0A        wait = false;%0A      %7D catch (IOException e) %7B%0A        if (!e.toString().contains(%22not ready%22)) %7B%0A          throw e;%0A        %7D%0A      %7D%0A    %7D
+ruleId = client.submitRule(rule, RuleState.ACTIVE);
 %0A%0A  

Rewrite TestSubmitRule