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

import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;

import org.apache.geode.distributed.ServerLauncherCacheProvider;
import org.apache.geode.launchers.startuptasks.WaitForFileToExist;
import org.apache.geode.rules.ServiceJarRule;
import org.apache.geode.test.junit.rules.ExecutorServiceRule;
import org.apache.geode.test.junit.rules.gfsh.GfshRule;

public class ServerStartupOnlineTest {

  @Rule
  public GfshRule gfshRule = new GfshRule();

  @Rule
  public TemporaryFolder temporaryFolder = new TemporaryFolder();

  @Rule
  public TestName testName = new TestName();

  @Rule
  public ServiceJarRule serviceJarRule = new ServiceJarRule();

  @Rule
  public ExecutorServiceRule executorServiceRule = new ExecutorServiceRule();

  private Path serverFolder;
  private String serverName;
  private String startServerCommand;

  @Before
  public void setup() {
    String serviceJarPath = serviceJarRule.createJarFor("ServerLauncherCacheProvider.jar",
        ServerLauncherCacheProvider.class, WaitForFileToExist.class);

    serverFolder = temporaryFolder.getRoot().toPath().toAbsolutePath();
    serverName = testName.getMethodName();

    startServerCommand = String.join(" ",
        "start server",
        "--name=" + serverName,
        "--dir=" + serverFolder,
        "--classpath=" + serviceJarPath,
        "--disable-default-server");
  }

  @After
  public void stopServer() {
    String stopServerCommand = "stop server --dir=" + serverFolder;
    gfshRule.execute(stopServerCommand);
  }

  @Test
  public void startServerReturnsAfterStartupTaskCompletes() throws IOException,
      InterruptedException {
    CompletableFuture<Void> startServerTask =
        executorServiceRule.runAsync(() -> gfshRule.execute(startServerCommand));

    waitForStartServerCommandToHang();

    assertThat(startServerTask).isNotDone();

    completeRemoteStartupTask();

    await().untilAsserted(() -> assertThat(startServerTask).isDone());
  }

  @Test
  public void statusServerReportsStartingUntilStartupTaskCompletes() throws IOException,
      InterruptedException {
    CompletableFuture<Void> startServerTask =
        executorServiceRule.runAsync(() -> gfshRule.execute(startServerCommand));

    waitForStartServerCommandToHang();

    await().untilAsserted(() -> {
      String startingStatus = getServerStatus();
      assertThat(startingStatus)
          .as("Status server command output")
          .contains("Starting Server");
    });

    completeRemoteStartupTask();

    await().untilAsserted(() -> {
      assertThat(startServerTask).isDone();
      String onlineStatus = getServerStatus();
      assertThat(onlineStatus)
          .as("Status server command output")
          .contains("is currently online");
    });
  }

  // TODO: Aaron: test state of MemberMXBean in server JVM

  private String getServerStatus() {
    String statusServerCommand = "status server --dir=" + serverFolder;
    return gfshRule.execute(statusServerCommand).getOutputText();
  }

  private void waitForStartServerCommandToHang()
      throws InterruptedException {
    await().untilAsserted(() -> assertThat(serverFolder.resolve(serverName + ".log")).exists());
    // Without sleeping, this test can pass when it shouldn't.
    Thread.sleep(10_000);
  }

  private void completeRemoteStartupTask() throws IOException {
    Files.createFile(serverFolder.resolve(WaitForFileToExist.WAITING_FILE_NAME));
  }
}

@@ -1311,24 +1311,78 @@
 heProvider;%0A
+import org.apache.geode.internal.AvailablePortHelper;%0A
 import org.a
@@ -1432,24 +1432,24 @@
 ileToExist;%0A
-
 import org.a
@@ -2387,24 +2387,158 @@
 hodName();%0A%0A
+    int%5B%5D ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);%0A%0A    int jmxHttpPort = ports%5B0%5D;%0A    int jmxRmiPort = ports%5B1%5D;%0A%0A
     startSer
@@ -2727,24 +2727,232 @@
 ault-server%22
+,%0A        %22--J=-Dgemfire.jmx-manager=true%22,%0A        %22--J=-Dgemfire.jmx-manager-start=true%22,%0A        %22--J=-Dgemfire.jmx-manager-http-port=%22 + jmxHttpPort,%0A        %22--J=-Dgemfire.jmx-manager-port=%22 + jmxRmiPort
 );%0A  %7D%0A%0A  @A
@@ -4346,64 +4346,676 @@
 %0A%0A  
-// TODO: Aaron: test state of MemberMXBean in server JVM
+@Test%0A  public void memberMXBeanStatusReportsStartingUntilStartupTaskCompletes() throws IOException,%0A      InterruptedException %7B%0A    CompletableFuture%3CVoid%3E startServerTask =%0A        executorServiceRule.runAsync(() -%3E gfshRule.execute(startServerCommand));%0A%0A    waitForStartServerCommandToHang();%0A%0A    await().untilAsserted(() -%3E %7B%0A      // Get memberMXBean status%0A%0A    %7D);%0A%0A    completeRemoteStartupTask();%0A%0A    await().untilAsserted(() -%3E %7B%0A      assertThat(startServerTask).isDone();%0A      String onlineStatus = getServerStatus();%0A      assertThat(onlineStatus)%0A          .as(%22Status server command output%22)%0A          .contains(%22is currently online%22);%0A    %7D);%0A  %7D%0A
 %0A%0A  

Add acceptance test for MemberMXBean status