/**
 * 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;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSTestUtil;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.junit.Assert;
import org.junit.Test;
import org.smartdata.admin.SmartAdmin;
import org.smartdata.metastore.MetaStore;
import org.smartdata.model.ActionInfo;
import org.smartdata.model.FileState;
import org.smartdata.model.RuleState;
import org.smartdata.model.S3FileState;
import org.smartdata.server.engine.CmdletManager;

import java.util.List;

public class TestCopy2S3Scheduler extends MiniSmartClusterHarness {

  @Test(timeout = 45000)
  public void testDir() throws Exception {
    waitTillSSMExitSafeMode();
    MetaStore metaStore = ssm.getMetaStore();
    SmartAdmin admin = new SmartAdmin(smartContext.getConf());
    CmdletManager cmdletManager = ssm.getCmdletManager();
    DistributedFileSystem dfs = cluster.getFileSystem();
    final String srcPath = "/src/";
    dfs.mkdirs(new Path(srcPath));
    // Write to src
    for (int i = 0; i < 3; i++) {
      // Create test files
      DFSTestUtil.createFile(dfs, new Path(srcPath + i),
          1024, (short) 1, 1);
    }
    long ruleId = admin.submitRule(
        "file: path matches \"/src/*\"| copy2s3 -dest s3a://xxxctest/dest/",
        RuleState.ACTIVE);
    List<ActionInfo> actions;
    do {
      actions = metaStore.getActions(ruleId, 0);
      Thread.sleep(1000);
    } while (actions.size() < 3);
    Assert.assertTrue(actions.size() == 3);
  }

  @Test(timeout = 45000)
  public void testZeroLength() throws Exception {
    waitTillSSMExitSafeMode();
    MetaStore metaStore = ssm.getMetaStore();
    SmartAdmin admin = new SmartAdmin(smartContext.getConf());
    CmdletManager cmdletManager = ssm.getCmdletManager();
    DistributedFileSystem dfs = cluster.getFileSystem();
    final String srcPath = "/src/";
    dfs.mkdirs(new Path(srcPath));
    // Write to src
    for (int i = 0; i < 3; i++) {
      // Create test files
      DFSTestUtil.createFile(dfs, new Path(srcPath + i),
          0, (short) 1, 1);
    }
    long ruleId = admin.submitRule(
        "file: path matches \"/src/*\"| copy2s3 -dest s3a://xxxctest/dest/",
        RuleState.ACTIVE);
    Thread.sleep(2500);
    List<ActionInfo> actions = metaStore.getActions(ruleId, 0);
    Assert.assertTrue(actions.size() == 0);
  }

  @Test(timeout = 45000)
  public void testOnS3() throws Exception {
    waitTillSSMExitSafeMode();
    MetaStore metaStore = ssm.getMetaStore();
    SmartAdmin admin = new SmartAdmin(smartContext.getConf());
    CmdletManager cmdletManager = ssm.getCmdletManager();
    DistributedFileSystem dfs = cluster.getFileSystem();
    final String srcPath = "/src/";
    dfs.mkdirs(new Path(srcPath));
    // Write to src
    for (int i = 0; i < 3; i++) {
      // Create test files
      // Not 0 because this file may be not be truncated yet
      DFSTestUtil.createFile(dfs, new Path(srcPath + i),
          10, (short) 1, 1);
      FileState fileState = new S3FileState(srcPath + i);
      metaStore.insertUpdateFileState(fileState);
    }
    Thread.sleep(500);
    long ruleId = admin.submitRule(
        "file: path matches \"/src/*\"| copy2s3 -dest s3a://xxxctest/dest/",
        RuleState.ACTIVE);
    Thread.sleep(2500);
    List<ActionInfo> actions = metaStore.getActions(ruleId, 0);
    Assert.assertTrue(actions.size() == 0);
  }
}

@@ -1248,58 +1248,8 @@
 ate;
-%0Aimport org.smartdata.server.engine.CmdletManager;
 %0A%0Aim
@@ -1551,66 +1551,8 @@
 ));%0A
-    CmdletManager cmdletManager = ssm.getCmdletManager();%0A
     
@@ -2142,52 +2142,8 @@
 3);%0A
-    Assert.assertTrue(actions.size() == 3);%0A
   %7D%0A
@@ -2362,66 +2362,8 @@
 ));%0A
-    CmdletManager cmdletManager = ssm.getCmdletManager();%0A
     
@@ -2895,36 +2895,38 @@
    Assert.assert
-True
+Equals
 (actions.size() 
@@ -2916,35 +2916,33 @@
 s(actions.size()
- ==
+,
  0);%0A  %7D%0A%0A  @Tes
@@ -3148,66 +3148,8 @@
 ));%0A
-    CmdletManager cmdletManager = ssm.getCmdletManager();%0A
     
@@ -3496,24 +3496,49 @@
 ort) 1, 1);%0A
+      // Add S3 Statuses%0A
       FileSt
@@ -3911,12 +3911,14 @@
 sert
-True
+Equals
 (act
@@ -3932,11 +3932,9 @@
 ze()
- ==
+,
  0);

Solve #1691, fix TestCopy2S3Scheduler.testDir (#1705)