package com.sequenceiq.it.cloudbreak.testcase.e2e.distrox;

import static com.sequenceiq.it.cloudbreak.context.RunningParameter.key;

import javax.inject.Inject;

import org.testng.annotations.Test;

import com.sequenceiq.it.cloudbreak.assertion.distrox.AwsAvailabilityZoneAssertion;
import com.sequenceiq.it.cloudbreak.client.DistroXTestClient;
import com.sequenceiq.it.cloudbreak.client.SdxTestClient;
import com.sequenceiq.it.cloudbreak.cloud.v4.CommonClusterManagerProperties;
import com.sequenceiq.it.cloudbreak.context.Description;
import com.sequenceiq.it.cloudbreak.context.TestContext;
import com.sequenceiq.it.cloudbreak.dto.distrox.DistroXTestDto;
import com.sequenceiq.it.cloudbreak.dto.distrox.cluster.DistroXUpgradeTestDto;
import com.sequenceiq.it.cloudbreak.dto.sdx.SdxTestDto;
import com.sequenceiq.it.cloudbreak.dto.sdx.SdxUpgradeTestDto;
import com.sequenceiq.it.cloudbreak.testcase.e2e.AbstractE2ETest;
import com.sequenceiq.it.cloudbreak.util.spot.UseSpotInstances;
import com.sequenceiq.sdx.api.model.SdxClusterStatusResponse;
import com.sequenceiq.sdx.api.model.SdxUpgradeReplaceVms;

public class DistroXUpgradeTests extends AbstractE2ETest {

    @Inject
    private SdxTestClient sdxTestClient;

    @Inject
    private DistroXTestClient distroXTestClient;

    @Inject
    private CommonClusterManagerProperties commonClusterManagerProperties;

    @Override
    protected void setupTest(TestContext testContext) {
        createDefaultUser(testContext);
        createDefaultCredential(testContext);
        initializeDefaultBlueprints(testContext);
        createEnvironmentWithFreeIpa(testContext);
    }

    @Test(dataProvider = TEST_CONTEXT)
    @UseSpotInstances
    @Description(given = "there is a running Cloudbreak, and an environment with SDX and DistroX cluster in available state",
            when = "upgrade called on the DistroX cluster", then = "DistroX upgrade should be successful, the cluster should be up and running")
    public void testDistroXUpgrade(TestContext testContext) {

        String sdxName = resourcePropertyProvider().getName();
        String distroXName = resourcePropertyProvider().getName();
        String currentRuntimeVersion = commonClusterManagerProperties.getUpgrade().getDistroXUpgradeCurrentVersion();
        String targetRuntimeVersion = commonClusterManagerProperties.getUpgrade().getDistroXUpgradeTargetVersion();
        testContext
                .given(sdxName, SdxTestDto.class)
                .withCloudStorage()
                .withRuntimeVersion(currentRuntimeVersion)
                .when(sdxTestClient.create(), key(sdxName))
                .await(SdxClusterStatusResponse.RUNNING, key(sdxName))
                .awaitForHealthyInstances()
                .validate();
        testContext
                .given(distroXName, DistroXTestDto.class)
                .withTemplate(String.format(commonClusterManagerProperties.getInternalDistroXBlueprintType(), currentRuntimeVersion))
                .withPreferredSubnetsForInstanceNetworkIfMultiAzEnabledOrJustFirst()
                .when(distroXTestClient.create(), key(distroXName))
                .await(STACK_AVAILABLE)
                .awaitForHealthyInstances()
                .then(new AwsAvailabilityZoneAssertion())
                .validate();
        testContext
                .given(distroXName, DistroXTestDto.class)
                .when(distroXTestClient.stop(), key(distroXName))
                .await(STACK_STOPPED)
                .validate();
        testContext
                .given(SdxUpgradeTestDto.class)
                .withReplaceVms(SdxUpgradeReplaceVms.DISABLED)
                .withRuntime(targetRuntimeVersion)
                .given(sdxName, SdxTestDto.class)
                .when(sdxTestClient.upgrade(), key(sdxName))
                .await(SdxClusterStatusResponse.DATALAKE_UPGRADE_IN_PROGRESS, key(sdxName).withWaitForFlow(Boolean.FALSE))
                .await(SdxClusterStatusResponse.RUNNING, key(sdxName))
                .awaitForHealthyInstances()
                .validate();
        testContext
                .given(distroXName, DistroXTestDto.class)
                .when(distroXTestClient.start(), key(distroXName))
                .await(STACK_AVAILABLE)
                .validate();
        testContext
                .given(DistroXUpgradeTestDto.class)
                .withRuntime(targetRuntimeVersion)
                .given(distroXName, DistroXTestDto.class)
                .when(distroXTestClient.upgrade(), key(distroXName))
                .await(STACK_AVAILABLE, key(distroXName))
                .awaitForHealthyInstances()
                .then(new AwsAvailabilityZoneAssertion())
                .validate();
    }
}

@@ -791,71 +791,8 @@
 to;%0A
-import com.sequenceiq.it.cloudbreak.dto.sdx.SdxUpgradeTestDto;%0A
 impo
@@ -853,16 +853,16 @@
 2ETest;%0A
+
 import c
@@ -1018,33 +1018,96 @@
 odel.Sdx
-UpgradeReplaceVms
+DatabaseAvailabilityType;%0Aimport com.sequenceiq.sdx.api.model.SdxDatabaseRequest
 ;%0A%0Apubli
@@ -1707,16 +1707,29 @@
 ription(
+%0A            
 given = 
@@ -1888,16 +1888,28 @@
 luster%22,
+%0A           
  then = 
@@ -2405,32 +2405,189 @@
 argetVersion();%0A
+        SdxDatabaseRequest sdxDatabaseRequest = new SdxDatabaseRequest();%0A        sdxDatabaseRequest.setAvailabilityType(SdxDatabaseAvailabilityType.NONE);%0A%0A
         testCont
@@ -2727,32 +2727,90 @@
 RuntimeVersion)%0A
+                .withExternalDatabase(sdxDatabaseRequest)%0A
                 
@@ -3438,32 +3438,32 @@
 lthyInstances()%0A
+
                 
@@ -3557,993 +3557,8 @@
 ext%0A
-                .given(distroXName, DistroXTestDto.class)%0A                .when(distroXTestClient.stop(), key(distroXName))%0A                .await(STACK_STOPPED)%0A                .validate();%0A        testContext%0A                .given(SdxUpgradeTestDto.class)%0A                .withReplaceVms(SdxUpgradeReplaceVms.DISABLED)%0A                .withRuntime(targetRuntimeVersion)%0A                .given(sdxName, SdxTestDto.class)%0A                .when(sdxTestClient.upgrade(), key(sdxName))%0A                .await(SdxClusterStatusResponse.DATALAKE_UPGRADE_IN_PROGRESS, key(sdxName).withWaitForFlow(Boolean.FALSE))%0A                .await(SdxClusterStatusResponse.RUNNING, key(sdxName))%0A                .awaitForHealthyInstances()%0A                .validate();%0A        testContext%0A                .given(distroXName, DistroXTestDto.class)%0A                .when(distroXTestClient.start(), key(distroXName))%0A                .await(STACK_AVAILABLE)%0A                .validate();%0A        testContext%0A
     

Optimise DistroXUpgradeTests.testDistroXUpgrade E2E test to reduce execution time