/*
 * 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.geode.management.internal.cli.commands;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;
import org.apache.geode.test.junit.rules.GfshCommandRule;



public class ExportStackTraceCommandDUnitTest {

  @ClassRule
  public static ClusterStartupRule lsRule = new ClusterStartupRule();

  @Rule
  public GfshCommandRule gfsh = new GfshCommandRule();

  private static MemberVM locator;

  @BeforeClass
  public static void beforeClass() throws Exception {
    locator = lsRule.startLocatorVM(0);
    lsRule.startServerVM(1, locator.getPort());
  }

  @Before
  public void before() throws Exception {
    gfsh.connectAndVerify(locator);
  }

  @Test
  public void exportStackTrace_no_file() {
    gfsh.executeAndAssertThat("export stack-traces").statusIsSuccess()
        .containsOutput("stack-trace(s) exported to file").containsOutput("On host : ");
    File[] files = locator.getWorkingDir().listFiles(x -> x.getName().startsWith("stacktrace_"));
    assertThat(files.length).isEqualTo(1);
    // delete this file afterwards so that we won't pollute the other tests in this class
    files[0].delete();
  }

  @Test
  public void exportStackTrace_on_one_member() {
    gfsh.executeAndAssertThat("export stack-traces --member=server-1").statusIsSuccess()
        .containsOutput("stack-trace(s) exported to file").containsOutput("On host : ");
    File[] files = locator.getWorkingDir().listFiles(x -> x.getName().startsWith("stacktrace_"));
    assertThat(files.length).isEqualTo(1);
    // delete this file afterwards so that we won't pollute the other tests in this class
    files[0].delete();
  }

  @Test
  public void exportStackTrace_with_file() {
    File stackTraceFile = new File(locator.getWorkingDir(), "my_file");
    gfsh.executeAndAssertThat("export stack-traces --file=" + stackTraceFile.getAbsolutePath())
        .statusIsSuccess().containsOutput("stack-trace(s) exported to file");

    // make sure file exists afterwards
    File[] files = locator.getWorkingDir().listFiles(x -> x.getName().startsWith("my_file"));
    assertThat(files.length).isEqualTo(1);

    // execute the command again with the abort flag
    gfsh.executeAndAssertThat(
        "export stack-traces --abort-if-file-exists --file=" + stackTraceFile.getAbsolutePath())
        .statusIsError().containsOutput("already present");

    // execute the command again without the abort flag
    gfsh.executeAndAssertThat("export stack-traces --file=" + stackTraceFile.getAbsolutePath())
        .statusIsSuccess().containsOutput("stack-trace(s) exported to file");
    // make sure the file is overwritten
    files = locator.getWorkingDir().listFiles(x -> x.getName().startsWith("my_file"));
    assertThat(files.length).isEqualTo(1);

    // delete this file afterwards so that we won't pollute the other tests in this class
    files[0].delete();
  }
}

@@ -918,20 +918,212 @@
 java.io.
-File
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Arrays;%0Aimport java.util.Date;%0Aimport java.util.List
 ;%0A%0Aimpor
@@ -3990,24 +3990,1457 @@
 qualTo(1);%0A%0A
+    // delete this file afterwards so that we won't pollute the other tests in this class%0A    files%5B0%5D.delete();%0A  %7D%0A%0A  @Test%0A  public void exportStackTrace_check_file_content() throws IOException %7B%0A    File stackTraceFile = new File(locator.getWorkingDir(), %22my_file%22);%0A    String stackTraceMsg = %22*** Stack-trace for member server-1 at %22;%0A    boolean containProperlyMsg = false;%0A%0A    gfsh.executeAndAssertThat(%22export stack-traces --file=%22 + stackTraceFile.getAbsolutePath())%0A        .statusIsSuccess().containsOutput(%22stack-trace(s) exported to file%22);%0A%0A    Date date = new Date();%0A    String possibleMsg = stackTraceMsg + new SimpleDateFormat(%22yyyy/MM/dd HH:mm:ss%22).format(date);%0A    date.setSeconds(date.getSeconds() - 1);%0A    String anotherPossibleMsg =%0A        stackTraceMsg + new SimpleDateFormat(%22yyyy/MM/dd HH:mm:ss%22).format(date);%0A%0A    List%3CString%3E expectedStrings = Arrays.asList(possibleMsg, anotherPossibleMsg);%0A%0A    // make sure file exists afterwards%0A    File%5B%5D files = locator.getWorkingDir().listFiles(x -%3E x.getName().startsWith(%22my_file%22));%0A    assertThat(files.length).isEqualTo(1);%0A%0A    BufferedReader bufferedReader = new BufferedReader(new FileReader(stackTraceFile));%0A    String firstLine = bufferedReader.readLine();%0A%0A    for (String message : expectedStrings) %7B%0A      if (firstLine.contains(message)) %7B%0A        containProperlyMsg = true;%0A      %7D%0A    %7D%0A%0A    assertThat(containProperlyMsg).isEqualTo(true);%0A%0A
     // delet

add test to verify changes