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

import static com.sequenceiq.it.cloudbreak.assertion.distrox.DistroXExternalDatabaseAssertion.validateTemplateContainsExternalDatabaseHostname;
import static com.sequenceiq.it.cloudbreak.context.RunningParameter.key;

import javax.inject.Inject;

import org.testng.annotations.Test;

import com.sequenceiq.distrox.api.v1.distrox.model.database.DistroXDatabaseAvailabilityType;
import com.sequenceiq.it.TestParameter;
import com.sequenceiq.it.cloudbreak.actor.Actor;
import com.sequenceiq.it.cloudbreak.client.DistroXTestClient;
import com.sequenceiq.it.cloudbreak.client.SdxTestClient;
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.externaldatabase.DistroXExternalDatabaseTestDto;
import com.sequenceiq.it.cloudbreak.dto.distrox.image.DistroXImageTestDto;
import com.sequenceiq.it.cloudbreak.dto.sdx.SdxInternalTestDto;
import com.sequenceiq.sdx.api.model.SdxClusterStatusResponse;
import com.sequenceiq.sdx.api.model.SdxDatabaseAvailabilityType;
import com.sequenceiq.sdx.api.model.SdxDatabaseRequest;

public class InternalSdxDistroxTest extends ImageValidatorE2ETest {

    @Inject
    private SdxTestClient sdxTestClient;

    @Inject
    private DistroXTestClient distroXTestClient;

    @Inject
    private TestParameter testParameter;

    @Test(dataProvider = TEST_CONTEXT)
    @Description(
            given = "a SDX internal request and a DistroX request",
            when = "a SDX internal create request is sent",
            then = "the SDX cluster and the corresponding DistroX cluster is created")
    public void testCreateInternalSdxAndDistrox(TestContext testContext) {
        String externalDB = resourcePropertyProvider().getName();
        String distrox = resourcePropertyProvider().getName();
        SdxDatabaseRequest sdxDatabaseRequest = new SdxDatabaseRequest();
        sdxDatabaseRequest.setAvailabilityType(SdxDatabaseAvailabilityType.NONE);

        testContext.given(SdxInternalTestDto.class)
                .withDatabase(sdxDatabaseRequest)
                .withTemplate(sdxTemplateName)
                .withRuntimeVersion(runtimeVersion)
                .withImageCatalogNameOnly(sourceImageCatalogName)
                .when(sdxTestClient.createInternal())
                .awaitForFlow(key(resourcePropertyProvider().getName()))
                .await(SdxClusterStatusResponse.RUNNING)
                .when(sdxTestClient.describeInternal())
                .validate();
        testContext
                .given(externalDB, DistroXExternalDatabaseTestDto.class)
                .withAvailabilityType(DistroXDatabaseAvailabilityType.NON_HA)
                .given(distrox, DistroXTestDto.class)
                .withTemplate(distroxTemplateName)
                .withImageSettings(testContext
                        .given(DistroXImageTestDto.class)
                        .withImageCatalog(testContext.get(SdxInternalTestDto.class).getResponse().getStackV4Response().getImage().getCatalogName())
                        .withImageId(testContext.get(SdxInternalTestDto.class).getResponse().getStackV4Response().getImage().getId()))
                .withExternalDatabase(externalDB)
                .when(distroXTestClient.create(), key(distrox))
                .await(STACK_AVAILABLE, key(distrox))
                .then(validateTemplateContainsExternalDatabaseHostname(Actor.defaultUser(testParameter)))
                .when(distroXTestClient.get(), key(distrox))
                .then((tc, dto, client) -> {
                    dto.getResponse();
                    return dto;
                })
                .validate();
    }

    @Override
    protected String getImageId(TestContext testContext) {
        return testContext.get(SdxInternalTestDto.class).getResponse().getStackV4Response().getImage().getId();
    }
}

@@ -96,430 +96,104 @@
 eak.
-assertion.distrox.DistroXExternalDatabaseAssertion.validateTemplateContainsExternalDatabaseHostname;%0Aimport static com.sequenceiq.it.cloudbreak.context.RunningParameter.key;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.testng.annotations.Test;%0A%0Aimport com.sequenceiq.distrox.api.v1.distrox.model.database.DistroXDatabaseAvailabilityType;%0Aimport com.sequenceiq.it.TestParameter;%0Aimport com.sequenceiq.it.cloudbreak.actor.Actor;
+context.RunningParameter.key;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.testng.annotations.Test;%0A
 %0Aimp
@@ -491,105 +491,8 @@
 to;%0A
-import com.sequenceiq.it.cloudbreak.dto.distrox.externaldatabase.DistroXExternalDatabaseTestDto;%0A
 impo
@@ -999,62 +999,8 @@
 t;%0A%0A
-    @Inject%0A    private TestParameter testParameter;%0A%0A
     
@@ -1346,137 +1346,8 @@
 ) %7B%0A
-        String externalDB = resourcePropertyProvider().getName();%0A        String distrox = resourcePropertyProvider().getName();%0A
     
@@ -1498,16 +1498,60 @@
 .NONE);%0A
+        sdxDatabaseRequest.setCreate(false);
 %0A       
@@ -2102,192 +2102,15 @@
 text
-%0A                .given(externalDB, DistroXExternalDatabaseTestDto.class)%0A                .withAvailabilityType(DistroXDatabaseAvailabilityType.NON_HA)%0A                .given(distrox, 
+.given(
 Dist
@@ -2570,58 +2570,8 @@
 )))%0A
-                .withExternalDatabase(externalDB)%0A
     
@@ -2614,30 +2614,16 @@
 create()
-, key(distrox)
 )%0A      
@@ -2654,30 +2654,16 @@
 VAILABLE
-, key(distrox)
 )%0A      
@@ -2682,183 +2682,25 @@
 hen(
-validateTemplateContainsExternalDatabaseHostname(Actor.defaultUser(testParameter)))%0A                .when(distroXTestClient.get(), key(distrox))%0A                .then((tc, dto
+(context, distrox
 , cl
@@ -2731,18 +2731,22 @@
        d
-to
+istrox
 .getResp
@@ -2781,18 +2781,22 @@
 return d
-to
+istrox
 ;%0A      
@@ -2808,16 +2808,63 @@
      %7D)%0A
+                .when(distroXTestClient.get())%0A
         

Revert "CB-6370 Restore the validate ExternalDatabaseHostname test step to Distrox tests"