/**
 * 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.fs.ftp;

import java.net.URI;
import junit.framework.TestCase;

import org.apache.ftpserver.DefaultFtpServerContext;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.mina.MinaListener;
import org.apache.ftpserver.usermanager.BaseUser;
import org.apache.ftpserver.usermanager.WritePermission;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSTestUtil;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.JobConf;

/**
 * Generates a bunch of random files and directories using class 'DFSTestUtil',
 * stores them on the FTP file system, copies them and check if all the files
 * were retrieved successfully without any data corruption
 */
public class TestFTPFileSystem extends TestCase {

  private Configuration defaultConf = new JobConf();
  private FtpServer server = null;
  private FileSystem localFs = null;
  private FileSystem ftpFs = null;

  private Path workDir = new Path(new Path(System.getProperty(
      "test.build.data", "."), "data"), "TestFTPFileSystem");

  Path ftpServerRoot = new Path(workDir, "FTPServer");
  Path ftpServerConfig = null;

  private void startServer() {
    try {
      DefaultFtpServerContext context = new DefaultFtpServerContext(false);
      MinaListener listener = new MinaListener();
      // Set port to 0 for OS to give a free port
      listener.setPort(0);
      context.setListener("default", listener);

      // Create a test user.
      UserManager userManager = context.getUserManager();
      BaseUser adminUser = new BaseUser();
      adminUser.setName("admin");
      adminUser.setPassword("admin");
      adminUser.setEnabled(true);
      adminUser.setAuthorities(new Authority[] { new WritePermission() });

      Path adminUserHome = new Path(ftpServerRoot, "user/admin");
      adminUser.setHomeDirectory(adminUserHome.toUri().getPath());
      adminUser.setMaxIdleTime(0);
      userManager.save(adminUser);

      // Initialize the server and start.
      server = new FtpServer(context);
      server.start();

    } catch (Exception e) {
      throw new RuntimeException("FTP server start-up failed", e);
    }
  }

  private void stopServer() {
    if (server != null) {
      server.stop();
    }
  }

  @Override
  public void setUp() throws Exception {
    startServer();
    defaultConf = new Configuration();
    localFs = FileSystem.getLocal(defaultConf);
    ftpServerConfig = new Path(localFs.getWorkingDirectory(), "res");
    MinaListener listener = (MinaListener) server.getServerContext()
        .getListener("default");
    int serverPort = listener.getPort();
    ftpFs = FileSystem.get(URI.create("ftp://admin:[email protected]:"
        + serverPort), defaultConf);
  }

  @Overr