public void uninstallService(String serviceName, boolean wait)
     throws IOException, InterruptedException {
   String command =
       "connect localhost:8100;uninstall-service --verbose -timeout 25 " + serviceName;
   if (wait) {
     LogUtils.log("Waiting for uninstall-service to finish...");
     CommandTestUtils.runCommandAndWait(command);
   } else {
     LogUtils.log("Not waiting for uninstall-service to finish, assuming it will succeed");
     CommandTestUtils.runCommand(command);
   }
 }
 @Test(timeOut = AbstractTestSupport.DEFAULT_TEST_TIMEOUT * 2, enabled = false)
 public void installSimpleApplicationTest() throws IOException, InterruptedException {
   installApplicationAndWait(APPLICATION_FOLDER_PATH, APPLICATION_NAME);
   String output =
       CommandTestUtils.runCommandAndWait("connect " + getRestUrl() + ";list-applications");
   Assert.assertTrue(output.contains(APPLICATION_NAME));
 }
  @BeforeTest(alwaysRun = true)
  public void init() throws Exception {
    service = new HpGrizzlyCloudService();
    super.bootstrap(service);
    cloud = service.getCloud();
    initClients();

    // install service with network
    String simpleServicePath = CommandTestUtils.getPath(USM_SERVICES_PATH + SERVICE_NAME);
    super.installServiceAndWait(simpleServicePath, SERVICE_NAME);
  }
  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void tamperWithSecurityFileTest() throws Exception {

    String fakeCloudAdminUserAndPassword = "******";

    String originalFilePath = SecurityConstants.BUILD_SECURITY_FILE_PATH;
    String backupFilePath = originalFilePath + ".tempBackup";
    String fakeFilePath =
        CommandTestUtils.getPath("src/main/config/security/custom-spring-security.xml");
    File originalFile = new File(originalFilePath);
    File backupFile = new File(backupFilePath);
    File fakeFile = new File(fakeFilePath);
    String output = "no output";

    LogUtils.log("moving " + originalFilePath + " to " + backupFilePath);
    FileUtils.moveFile(originalFile, backupFile);

    try {
      LogUtils.log("copying " + fakeFilePath + " to " + originalFilePath);
      FileUtils.copyFile(fakeFile, originalFile);

      output =
          installApplicationAndWait(
              SIMPLE_APP_PATH,
              SIMPLE_APP_NAME,
              TIMEOUT_IN_MINUTES,
              fakeCloudAdminUserAndPassword,
              fakeCloudAdminUserAndPassword,
              true,
              null);

    } finally {
      LogUtils.log("deleting " + originalFilePath);
      try {
        FileUtils.deleteQuietly(originalFile);
      } catch (Exception e) {
        LogUtils.log("deletion of " + originalFilePath + " failed", e);
      }

      LogUtils.log("moving " + backupFilePath + " to " + originalFilePath);
      try {
        FileUtils.moveFile(backupFile, originalFile);
      } catch (Exception e) {
        LogUtils.log("moving of " + backupFilePath + " failed", e);
      }
    }

    assertTrue(
        "install access granted to viewer " + fakeCloudAdminUserAndPassword,
        output.contains(ACCESS_DENIED_MESSAGE));
  }
public class DynamicByonInstallTest extends AbstractByonCloudTest {

  protected final String APPLICATION_FOLDER_PATH =
      CommandTestUtils.getPath("src/main/resources/apps/USM/usm/applications/verySimple");
  protected final String APPLICATION_NAME = "verySimple";

  @Override
  protected String getCloudName() {
    return "dynamic-byon";
  }

  @BeforeClass(alwaysRun = true)
  @Override
  protected void bootstrap() throws Exception {
    super.bootstrap();
  }

  @AfterClass(alwaysRun = true)
  @Override
  protected void teardown() throws Exception {
    super.teardown();
  }

  @Override
  protected void customizeCloud() throws Exception {
    super.customizeCloud();
    getService().getProperties().put("password", "tgrid");
    getService().getProperties().put("password", "tgrid");
    String[] machines = getService().getIpList().split(",");
    getService().getProperties().put("startMachineIP", machines[0]);
    getService().getProperties().put("managementMachines", machines[1]);
  }

  @Test(timeOut = AbstractTestSupport.DEFAULT_TEST_TIMEOUT * 2, enabled = false)
  public void installSimpleApplicationTest() throws IOException, InterruptedException {
    installApplicationAndWait(APPLICATION_FOLDER_PATH, APPLICATION_NAME);
    String output =
        CommandTestUtils.runCommandAndWait("connect " + getRestUrl() + ";list-applications");
    Assert.assertTrue(output.contains(APPLICATION_NAME));
  }

  @AfterMethod
  public void cleanup() throws IOException, InterruptedException {
    uninstallApplicationIfFound(APPLICATION_NAME);
  }
}
/** User: yohana Date: 12/8/13 Time: 1:44 PM */
public class Ec2XAP9xApplicationTest extends NewAbstractCloudTest {
  private static final String LOCAL_GIT_REPO_PATH =
      ScriptUtils.getBuildPath() + "/git-recipes-" + Ec2XAP9xApplicationTest.class.getSimpleName();
  private static final String XAP9X_MANAGEMENT = "xap-management";
  private static final String SG_VALIDATOR = "sgvalidator";
  private static final String XAP9X_PATH = LOCAL_GIT_REPO_PATH + "/apps/xap9x-tiny/";
  private static final String SG_VALIDATOR_PATH =
      CommandTestUtils.getPath("src/main/resources/apps/cloudify/recipes/sgvalidator");
  private static final String APP_NAME = "xap9x-tiny";
  private static final String BRANCH_NAME = "master";
  private static final String PUURL =
      "https://s3-eu-west-1.amazonaws.com/gigaspaces-maven-repository-eu/com/gigaspaces/quality/sgtest/apps/listeners/Space/3.0.0-SNAPSHOT/Space-3.0.0-SNAPSHOT.jar";
  private static final String DEPLOYPU_COMMAND =
      "deploy-pu pu-using-deploy-pu " + PUURL + " partitioned-sync2backup 1 1 0 0";
  private static final String UNDEPLOYGRID_DEPLOYPU_COMMAND = "undeploy-grid pu-using-deploy-pu";
  private static final String DEPLOYPUBASIC_COMMAND = "deploy-pu-basic " + PUURL;
  private static final String UNDEPLOYGRID_DEPLOYPUBASIC_COMMAND =
      "undeploy-grid Space-3.0.0-SNAPSHOT.jar";
  private static final String DEPLOYGRID_COMMAND =
      "deploy-grid pu-using-deploy-grid partitioned-sync2backup 1 1 0 0";
  private static final String UNDEPLOYGRID_DEPLOYGRID_COMMAND =
      "undeploy-grid pu-using-deploy-grid";
  private static final String DEPLOYGRIDBASIC_COMMAND = "deploy-grid-basic puusing-deploygridbasic";
  private static final String UNDEPLOYGRID_DEPLOYGRIDBASIC_COMMAND =
      "undeploy-grid puusing-deploygridbasic";

  // This grid is already deployed by the recipe but we need these lines for validateInstance and
  // undeploy command
  private static final String DEPLOYGRID_MYDATAGRID_COMMAND =
      "deploy-grid-basic myDataGrid"; // partitioned-sync2backup 1 0 1 1
  private static final String UNDEPLOYGRID_MYDATAGRID_COMMAND = "undeploy-grid myDataGrid";

  @Override
  protected String getCloudName() {
    return "ec2";
  }

  @Override
  protected boolean isReusableCloud() {
    return false;
  }

  @BeforeClass(alwaysRun = true)
  protected void prepareTest() throws Exception {
    // Checkout Recipes
    if (!(new File(LOCAL_GIT_REPO_PATH).exists())) {
      String remotePath = "https://github.com/CloudifySource/cloudify-recipes.git";
      JGitUtils.clone(LOCAL_GIT_REPO_PATH, remotePath, BRANCH_NAME);
    }

    super.bootstrap();
  }

  @AfterClass(alwaysRun = true)
  protected void teardown() throws Exception {
    super.teardown();
  }

  @Test(timeOut = AbstractTestSupport.DEFAULT_TEST_TIMEOUT * 4, enabled = true)
  public void testXAP9xRecipe() throws Exception {
    InvocationResult result;
    InvokeServiceCommandResponse response;

    // Install and verify xap-management
    String installationLog =
        installApplicationAndWait(
            XAP9X_PATH, APP_NAME); // installAndVerify(XAP9X_MANAGEMENT_PATH,XAP9X_MANAGEMENT);
    verifyPUInstallation(APP_NAME);
    Pattern pattern =
        Pattern.compile(
            "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}] - xap-management-1 START invoked"); // Regex for ip to match in the log of xap-management installation
    Matcher matcher = pattern.matcher(installationLog);
    if (!matcher.find()) {
      throw new Exception("Unable to find lookuplocator's ip in the log: " + installationLog);
    }
    String managementPrivateIP = matcher.group(0);
    String lookuplocators =
        managementPrivateIP.substring(
                managementPrivateIP.indexOf("/") + 1, managementPrivateIP.indexOf("]"))
            + ":4242";
    log("Using lookuplocators: " + lookuplocators);
    // Install and verify sgvalidator
    installAndVerify(SG_VALIDATOR_PATH, SG_VALIDATOR);

    // First validate and undeploy myDataGrid that the container-service deploy
    validateInstance(lookuplocators, DEPLOYGRID_MYDATAGRID_COMMAND);
    // undeploy-grid
    response = customCommand(UNDEPLOYGRID_MYDATAGRID_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);

    // deploy-pu
    response = customCommand(DEPLOYPU_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    validateInstance(lookuplocators, DEPLOYPU_COMMAND);
    // undeploy-grid
    response = customCommand(UNDEPLOYGRID_DEPLOYPU_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);

    // deploy-pu-basic
    response = customCommand(DEPLOYPUBASIC_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    validateInstance(lookuplocators, DEPLOYPUBASIC_COMMAND);
    // undeploy-grid
    response = customCommand(UNDEPLOYGRID_DEPLOYPUBASIC_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);

    // deploy-grid
    response = customCommand(DEPLOYGRID_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    validateInstance(lookuplocators, DEPLOYGRID_COMMAND);
    // undeploy-grid
    response = customCommand(UNDEPLOYGRID_DEPLOYGRID_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);

    // deploy-grid
    response = customCommand(DEPLOYGRIDBASIC_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    validateInstance(lookuplocators, DEPLOYGRIDBASIC_COMMAND);
    // undeploy-grid
    response = customCommand(UNDEPLOYGRID_DEPLOYGRIDBASIC_COMMAND, XAP9X_MANAGEMENT, APP_NAME);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);

    String uninstallOutput = uninstallAndVerify();
  }

  private void validateInstance(String lookuplocators, String deploypuCommand) throws Exception {
    String gridname;
    String partitions, backups, maxPerVM, maxPerMachine;
    String[] deployCmdArr = deploypuCommand.split(" ");
    if (deployCmdArr[0].equals("deploy-pu")) {
      gridname = deployCmdArr[1];
      partitions = deployCmdArr[4];
      backups = deployCmdArr[5];
      maxPerVM = deployCmdArr[6];
      maxPerMachine = deployCmdArr[7];
    } else if (deployCmdArr[0].equals("deploy-grid")) {
      gridname = deployCmdArr[1];
      partitions = deployCmdArr[3];
      backups = deployCmdArr[4];
      maxPerVM = deployCmdArr[5];
      maxPerMachine = deployCmdArr[6];
    } else if (deployCmdArr[0].equals("deploy-pu-basic")) {
      File file = new File(PUURL);
      gridname = file.getName();
      partitions = "1";
      backups = "0";
      maxPerVM = "1";
      maxPerMachine = "1";
    } else if (deployCmdArr[0].equals("deploy-grid-basic")) {
      gridname = deployCmdArr[1];
      partitions = "1";
      backups = "1";
      maxPerVM = "0";
      maxPerMachine = "0";
    } else {
      throw new Exception(
          "Unknown command. Please recheck the command or add it to validateInstance function.");
    }
    InvokeServiceCommandResponse response;
    InvocationResult result;
    String appName = "default";
    String numOfInstances =
        String.valueOf(Integer.valueOf(partitions) * (1 + Integer.valueOf(backups)));

    response =
        customCommand(
            "get-datagrid-instances " + gridname + " " + lookuplocators + " " + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), numOfInstances);

    response =
        customCommand(
            "get-datagrid-deploymentstatus "
                + gridname
                + " "
                + lookuplocators
                + " "
                + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), String.valueOf(DeploymentStatus.INTACT));

    response =
        customCommand(
            "get-datagrid-partitions " + gridname + " " + lookuplocators + " " + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), partitions);

    response =
        customCommand(
            "get-datagrid-backups " + gridname + " " + lookuplocators + " " + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), backups);

    response =
        customCommand(
            "get-datagrid-maxinstancespermachine "
                + gridname
                + " "
                + lookuplocators
                + " "
                + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), maxPerMachine);

    response =
        customCommand(
            "get-datagrid-maxinstancespervm "
                + gridname
                + " "
                + lookuplocators
                + " "
                + numOfInstances,
            SG_VALIDATOR,
            appName);
    result = getCustomCommandResult(response);
    Assert.assertEquals(result.getInvocationStatus(), CloudifyConstants.InvocationStatus.SUCCESS);
    Assert.assertEquals(result.getResult(), maxPerVM);
  }

  private String uninstallAndVerify() throws Exception {
    uninstallServiceAndWait(SG_VALIDATOR);
    String log = uninstallApplicationAndWait(APP_NAME, false, 25);
    super.scanForLeakedAgentNodes();
    return log;
  }

  private InvokeServiceCommandResponse customCommand(
      String commandStr, String serviceName, String appName)
      throws IOException, InterruptedException, RestClientException {
    String[] commandArr = commandStr.split(" ");
    String command = commandStr;
    List<String> params = new ArrayList<String>();
    log("Invoking command: " + command);
    if (commandArr.length > 1) {
      command = commandArr[0];
      for (int i = 1; i < commandArr.length; i++) {
        params.add(commandArr[i]);
      }
    }
    CommandInvoker commandInvoker = new CommandInvoker(getRestUrl());
    return commandInvoker.restInvokeServiceCommand(appName, serviceName, command, params);
  }

  private InvocationResult getCustomCommandResult(InvokeServiceCommandResponse response)
      throws Exception {
    Map<String, InvocationResult> results =
        parseInvocationResults(response.getInvocationResultPerInstance());
    if (results.values().size() != 1) {
      throw new Exception("Custom command returns more than 1 result");
    }
    return ((InvocationResult) results.values().toArray()[0]);
  }

  private String installAndVerify(String servicePath, String serviceName) throws Exception {
    String result = installServiceAndWait(servicePath, serviceName, 25);

    verifyPUInstallation(serviceName);
    return result;
  }

  private void verifyPUInstallation(String appName) throws Exception {
    LogUtils.log("verifying successful installation");
    String restUrl = getRestUrl();

    final URL appURL = new URL(restUrl + "/admin/ProcessingUnits/Names/" + appName + "");
    AbstractTestSupport.assertTrue(WebUtils.isURLAvailable(appURL));
  }

  private Map<String, InvocationResult> parseInvocationResults(
      final Map<String, Map<String, String>> restInvocationResultsPerInstance) {

    final Map<String, InvocationResult> invocationResultsMap =
        new LinkedHashMap<String, InvocationResult>();

    for (final Map.Entry<String, Map<String, String>> entry :
        restInvocationResultsPerInstance.entrySet()) {
      final String instanceName = entry.getKey();
      final Map<String, String> restInvocationResult = entry.getValue();
      invocationResultsMap.put(instanceName, parseInvocationResult(restInvocationResult));
    }

    return invocationResultsMap;
  }

  private InvocationResult parseInvocationResult(final Map<String, String> restInvocationResult) {

    InvocationResult invocationResult = null;
    if (restInvocationResult != null) {
      invocationResult = InvocationResult.createInvocationResult(restInvocationResult);
    }

    return invocationResult;
  }

  protected void log(String msg) {
    LogUtils.log(msg);
  }
}
 public void setPathToServiceRelativeToSGTestRootDir(String recipe, String relativePath) {
   this.pathToService = CommandTestUtils.getPath(relativePath);
 }
public class Ec2WinSecurityTest extends NewAbstractSecurityCloudTest {

  private static final String SIMPLE_APP_NAME = "simple";
  private static final String SIMPLE_APP_PATH =
      CommandTestUtils.getPath("src/main/resources/apps/USM/usm/applications/" + SIMPLE_APP_NAME);
  private static final String SIMPLE_SERVICE_NAME = "simple";
  private static final String SIMPLE_SERVICE_PATH =
      CommandTestUtils.getPath("/src/main/resources/apps/USM/usm/" + SIMPLE_SERVICE_NAME);

  private static final String GROOVY_APP_NAME = "groovyApp";
  private static final String GROOVY_APP_PATH =
      CommandTestUtils.getPath("/src/main/resources/apps/USM/usm/applications/" + GROOVY_APP_NAME);
  private static final String GROOVY_SERVICE_NAME = "groovy";
  private static final String GROOVY2_SERVICE_NAME = "groovy2";

  private static final String INSTANCE_VERIFICATION_STRING = "instance #1";
  private static final String ACCESS_DENIED_MESSAGE = "no_permission_access_is_denied";
  private static final String BAD_CREDENTIALS_MESSAGE = "Bad credentials";

  private static final int TIMEOUT_IN_MINUTES = 60;

  @BeforeClass(alwaysRun = true)
  protected void bootstrap() throws Exception {
    super.bootstrap();
  }

  @AfterClass(alwaysRun = true)
  protected void teardown() throws Exception {
    super.teardown();
  }

  @AfterMethod(alwaysRun = true)
  protected void uninstall() throws Exception {

    uninstallApplicationIfFound(
        SIMPLE_APP_NAME,
        SecurityConstants.USER_PWD_ALL_ROLES,
        SecurityConstants.USER_PWD_ALL_ROLES);
    uninstallApplicationIfFound(
        GROOVY_APP_NAME,
        SecurityConstants.USER_PWD_ALL_ROLES,
        SecurityConstants.USER_PWD_ALL_ROLES);

    uninstallServiceIfFound(
        SIMPLE_SERVICE_NAME,
        SecurityConstants.USER_PWD_ALL_ROLES,
        SecurityConstants.USER_PWD_ALL_ROLES);
    uninstallServiceIfFound(
        GROOVY_SERVICE_NAME,
        SecurityConstants.USER_PWD_ALL_ROLES,
        SecurityConstants.USER_PWD_ALL_ROLES);
    uninstallServiceIfFound(
        GROOVY2_SERVICE_NAME,
        SecurityConstants.USER_PWD_ALL_ROLES,
        SecurityConstants.USER_PWD_ALL_ROLES);
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 6, enabled = true)
  public void installAndUninstallTest() throws IOException, InterruptedException {

    installAndUninstall(
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        false);
    installAndUninstall(
        SecurityConstants.USER_PWD_APP_MANAGER, SecurityConstants.USER_PWD_APP_MANAGER, false);
    installAndUninstall(
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        false);
    installAndUninstall(
        SecurityConstants.USER_PWD_CLOUD_ADMIN, SecurityConstants.USER_PWD_CLOUD_ADMIN, false);
    installAndUninstall(SecurityConstants.USER_PWD_VIEWER, SecurityConstants.USER_PWD_VIEWER, true);
    installAndUninstall(
        SecurityConstants.USER_PWD_NO_ROLE, SecurityConstants.USER_PWD_NO_ROLE, true);
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void installAndUninstallWithDifferentUsersTest() throws IOException, InterruptedException {

    String output = "no output";

    installApplicationAndWait(
        SIMPLE_APP_PATH,
        SIMPLE_APP_NAME,
        TIMEOUT_IN_MINUTES,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        false,
        null);

    output =
        uninstallApplicationAndWait(
            SIMPLE_APP_PATH,
            SIMPLE_APP_NAME,
            TIMEOUT_IN_MINUTES,
            SecurityConstants.USER_PWD_VIEWER,
            SecurityConstants.USER_PWD_VIEWER,
            true,
            null);
    assertTrue(
        "uninstall access granted to " + SecurityConstants.VIEWER_DESCRIPTIN,
        output.contains(ACCESS_DENIED_MESSAGE));

    uninstallApplicationAndWait(
        SIMPLE_APP_PATH,
        SIMPLE_APP_NAME,
        TIMEOUT_IN_MINUTES,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.USER_PWD_APP_MANAGER,
        false,
        null);
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void installWithoutCredentialsTest() throws IOException, InterruptedException {

    String output = "no output";
    output =
        installApplicationAndWait(
            SIMPLE_APP_PATH, SIMPLE_APP_NAME, TIMEOUT_IN_MINUTES, null, null, true, null);

    assertTrue(
        "install access granted to an Anonymous user", output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 4, enabled = true)
  public void installingAndViewingTest() throws IOException, InterruptedException {

    installApplicationAndWait(
        SIMPLE_APP_PATH,
        SIMPLE_APP_NAME,
        TIMEOUT_IN_MINUTES,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.USER_PWD_APP_MANAGER,
        false,
        null);
    installApplicationAndWait(
        GROOVY_APP_PATH,
        GROOVY_APP_NAME,
        TIMEOUT_IN_MINUTES,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        false,
        null);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN,
        SecurityConstants.CLOUD_ADMIN_DESCRIPTIN,
        SIMPLE_APP_NAME,
        false);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN,
        SecurityConstants.CLOUD_ADMIN_DESCRIPTIN,
        GROOVY_APP_NAME,
        false);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.CLOUD_ADMIN_AND_APP_MANAGER_DESCRIPTION,
        SIMPLE_APP_NAME,
        true);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.CLOUD_ADMIN_AND_APP_MANAGER_DESCRIPTION,
        GROOVY_APP_NAME,
        true);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SIMPLE_APP_NAME,
        true);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.USER_PWD_APP_MANAGER,
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        GROOVY_APP_NAME,
        true);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SIMPLE_APP_NAME,
        true);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        GROOVY_APP_NAME,
        true);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_VIEWER,
        SecurityConstants.USER_PWD_VIEWER,
        SecurityConstants.VIEWER_DESCRIPTIN,
        SIMPLE_APP_NAME,
        false);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_VIEWER,
        SecurityConstants.USER_PWD_VIEWER,
        SecurityConstants.VIEWER_DESCRIPTIN,
        GROOVY_APP_NAME,
        true);

    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_DESCRIPTIN,
        SecurityConstants.USER_PWD_NO_ROLE,
        SecurityConstants.USER_PWD_NO_ROLE,
        SecurityConstants.NO_ROLE_DESCRIPTIN,
        SIMPLE_APP_NAME,
        false);
    verifyVisibleLists(
        SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        SecurityConstants.USER_PWD_NO_ROLE,
        SecurityConstants.USER_PWD_NO_ROLE,
        SecurityConstants.NO_ROLE_DESCRIPTIN,
        GROOVY_APP_NAME,
        false);
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void loginTest() throws IOException, InterruptedException {

    String output = "no output";

    output = login(SecurityConstants.USER_PWD_VIEWER, SecurityConstants.USER_PWD_VIEWER, false);
    assertTrue(
        "login failed for: " + SecurityConstants.VIEWER_DESCRIPTIN,
        output.contains("Logged in successfully"));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void connectWithNonexistentUserTest() throws IOException, InterruptedException {

    String output =
        connect(
            SecurityConstants.USER_PWD_CLOUD_ADMIN + "bad",
            SecurityConstants.USER_PWD_CLOUD_ADMIN,
            true);
    assertTrue(
        "connect succeeded for user: "******"bad",
        output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void connectWithNoPasswordTest() throws IOException, InterruptedException {

    String output = connect(SecurityConstants.USER_PWD_CLOUD_ADMIN, null, true);
    assertTrue(
        "connect succeeded for: "
            + SecurityConstants.CLOUD_ADMIN_DESCRIPTIN
            + " without providing a password",
        output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void loginWithNonexistentUserTest() throws IOException, InterruptedException {

    String output = "no output";

    output =
        login(
            SecurityConstants.USER_PWD_CLOUD_ADMIN + "bad",
            SecurityConstants.USER_PWD_CLOUD_ADMIN,
            true);

    assertTrue(
        "login succeeded for user: "******"bad",
        output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void connectWithWrongPassword() throws IOException, InterruptedException {

    String output =
        connect(
            SecurityConstants.USER_PWD_CLOUD_ADMIN,
            SecurityConstants.USER_PWD_CLOUD_ADMIN + "bad",
            true);
    assertTrue(
        "connect succeeded for password: "******"bad",
        output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT, enabled = true)
  public void loginWithWrongPassword() throws IOException, InterruptedException {

    String output = "no output";

    output =
        login(
            SecurityConstants.USER_PWD_CLOUD_ADMIN,
            SecurityConstants.USER_PWD_CLOUD_ADMIN + "bad",
            true);

    assertTrue(
        "login succeeded for password: "******"bad",
        output.contains(BAD_CREDENTIALS_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void installWithWrongGroup() throws IOException, InterruptedException {

    String output = "no output";

    output =
        installApplicationAndWait(
            SIMPLE_APP_PATH,
            SIMPLE_APP_NAME,
            TIMEOUT_IN_MINUTES,
            SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
            SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
            true,
            "ROLE_CLOUDADMINS");

    assertTrue(
        "install succeeded with authGroup ROLE_CLOUDADMINS for: "
            + SecurityConstants.APP_MANAGER_AND_VIEWER_DESCRIPTIN,
        output.contains(ACCESS_DENIED_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void installAndUninstallWithDifferentGroup() throws IOException, InterruptedException {

    String output = "no output";

    installApplicationAndWait(
        SIMPLE_APP_PATH,
        SIMPLE_APP_NAME,
        TIMEOUT_IN_MINUTES,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        SecurityConstants.USER_PWD_CLOUD_ADMIN_AND_APP_MANAGER,
        false,
        "ROLE_CLOUDADMINS");
    output =
        uninstallApplicationAndWait(
            SIMPLE_APP_PATH,
            SIMPLE_APP_NAME,
            TIMEOUT_IN_MINUTES,
            SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
            SecurityConstants.USER_PWD_APP_MANAGER_AND_VIEWER,
            true,
            null);

    assertTrue("unseen application uninstall succeeded", output.contains(ACCESS_DENIED_MESSAGE));
  }

  @Test(timeOut = DEFAULT_TEST_TIMEOUT * 2, enabled = true)
  public void tamperWithSecurityFileTest() throws Exception {

    String fakeCloudAdminUserAndPassword = "******";

    String originalFilePath = SecurityConstants.BUILD_SECURITY_FILE_PATH;
    String backupFilePath = originalFilePath + ".tempBackup";
    String fakeFilePath =
        CommandTestUtils.getPath("src/main/config/security/custom-spring-security.xml");
    File originalFile = new File(originalFilePath);
    File backupFile = new File(backupFilePath);
    File fakeFile = new File(fakeFilePath);
    String output = "no output";

    LogUtils.log("moving " + originalFilePath + " to " + backupFilePath);
    FileUtils.moveFile(originalFile, backupFile);

    try {
      LogUtils.log("copying " + fakeFilePath + " to " + originalFilePath);
      FileUtils.copyFile(fakeFile, originalFile);

      output =
          installApplicationAndWait(
              SIMPLE_APP_PATH,
              SIMPLE_APP_NAME,
              TIMEOUT_IN_MINUTES,
              fakeCloudAdminUserAndPassword,
              fakeCloudAdminUserAndPassword,
              true,
              null);

    } finally {
      LogUtils.log("deleting " + originalFilePath);
      try {
        FileUtils.deleteQuietly(originalFile);
      } catch (Exception e) {
        LogUtils.log("deletion of " + originalFilePath + " failed", e);
      }

      LogUtils.log("moving " + backupFilePath + " to " + originalFilePath);
      try {
        FileUtils.moveFile(backupFile, originalFile);
      } catch (Exception e) {
        LogUtils.log("moving of " + backupFilePath + " failed", e);
      }
    }

    assertTrue(
        "install access granted to viewer " + fakeCloudAdminUserAndPassword,
        output.contains(ACCESS_DENIED_MESSAGE));
  }

  protected void verifyVisibleLists(
      String installer,
      String viewerName,
      String viewerPassword,
      String viewerDescription,
      String appName,
      boolean isVisible)
      throws IOException, InterruptedException {

    String output = "no output";

    if (isVisible) {
      output = listApplications(viewerName, viewerPassword, false);
      assertTrue(
          viewerDescription + " doesn't see the application of " + installer,
          output.contains(appName));
    } else {
      output = listApplications(viewerName, viewerPassword, true);
      assertTrue(
          viewerDescription + " sees the application of " + installer, !output.contains(appName));
    }

    if (isVisible) {
      if (appName.equalsIgnoreCase(SIMPLE_APP_NAME)) {

        output = listServices(viewerName, viewerPassword, SIMPLE_APP_NAME, false);
        assertTrue(
            viewerDescription + " doesn't see the services of " + installer,
            output.contains(SIMPLE_APP_NAME + "." + SIMPLE_SERVICE_NAME));
      } else {

        output = listServices(viewerName, viewerPassword, GROOVY_APP_NAME, true);
        assertTrue(
            viewerDescription + " doesn't see the services of " + installer,
            output.contains(GROOVY_APP_NAME + "." + GROOVY_SERVICE_NAME)
                && output.contains(GROOVY_APP_NAME + "." + GROOVY2_SERVICE_NAME));
      }

    } else {
      if (appName.equalsIgnoreCase(SIMPLE_APP_NAME)) {
        output = listServices(viewerName, viewerPassword, SIMPLE_APP_NAME, true);
        assertTrue(
            viewerDescription + " sees the services of " + installer,
            !output.contains(SIMPLE_APP_NAME + "." + SIMPLE_SERVICE_NAME));
      } else {

        output = listServices(viewerName, viewerPassword, GROOVY_APP_NAME, true);
        assertTrue(
            viewerDescription + " sees the services of " + installer,
            !(output.contains(GROOVY_APP_NAME + "." + GROOVY_SERVICE_NAME)
                || output.contains(GROOVY_APP_NAME + "." + GROOVY2_SERVICE_NAME)));
      }
    }

    if (appName.equalsIgnoreCase(SIMPLE_APP_NAME)) {

      if (isVisible) {
        output =
            listInstances(viewerName, viewerPassword, SIMPLE_APP_NAME, SIMPLE_SERVICE_NAME, false);
        assertTrue(
            viewerDescription + " doesn't see the instances of " + installer,
            output.contains(INSTANCE_VERIFICATION_STRING));
      } else {
        output =
            listInstances(viewerName, viewerPassword, SIMPLE_APP_NAME, SIMPLE_SERVICE_NAME, true);
        assertTrue(
            viewerDescription + " sees the instances of " + installer,
            !output.contains(INSTANCE_VERIFICATION_STRING));
      }

    } else {
      if (isVisible) {
        output =
            listInstances(viewerName, viewerPassword, GROOVY_APP_NAME, GROOVY_SERVICE_NAME, false);
        assertTrue(
            viewerDescription + " doesn't see the instances of " + installer,
            output.contains(INSTANCE_VERIFICATION_STRING));
      } else {
        output =
            listInstances(viewerName, viewerPassword, GROOVY_APP_NAME, GROOVY_SERVICE_NAME, true);
        assertTrue(
            viewerDescription + " sees the instances of " + installer,
            !output.contains(INSTANCE_VERIFICATION_STRING));
      }
    }
  }

  public void installAndUninstall(String user, String password, boolean isInstallExpectedToFail)
      throws IOException, InterruptedException {

    String output =
        installApplicationAndWait(
            SIMPLE_APP_PATH,
            SIMPLE_APP_NAME,
            TIMEOUT_IN_MINUTES,
            user,
            password,
            isInstallExpectedToFail,
            null);

    if (isInstallExpectedToFail) {
      assertTrue(
          "application installation access granted to " + user,
          output.contains(ACCESS_DENIED_MESSAGE));
    }

    if (output.contains("Application " + SIMPLE_APP_NAME + " installed successfully")) {
      uninstallApplicationAndWait(
          SIMPLE_APP_PATH,
          SIMPLE_APP_NAME,
          TIMEOUT_IN_MINUTES,
          user,
          password,
          isInstallExpectedToFail,
          null);
    }

    output =
        installServiceAndWait(
            SIMPLE_SERVICE_PATH,
            SIMPLE_SERVICE_NAME,
            TIMEOUT_IN_MINUTES,
            user,
            password,
            isInstallExpectedToFail,
            null);

    if (isInstallExpectedToFail) {
      assertTrue(
          "service installation access granted to " + user, output.contains(ACCESS_DENIED_MESSAGE));
    }

    if (output.contains("Service \"" + SIMPLE_SERVICE_NAME + "\" successfully installed")) {
      uninstallServiceAndWait(
          SIMPLE_APP_PATH,
          SIMPLE_APP_NAME,
          TIMEOUT_IN_MINUTES,
          user,
          password,
          isInstallExpectedToFail,
          null);
    }
  }

  @Override
  protected String getCloudName() {
    return "ec2-win";
  }

  @Override
  protected boolean isReusableCloud() {
    return false;
  }
}