// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.mariadb.generated;

import com.azure.core.credential.AccessToken;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.mariadb.MariaDBManager;
import com.azure.resourcemanager.mariadb.models.GeoRedundantBackup;
import com.azure.resourcemanager.mariadb.models.MinimalTlsVersionEnum;
import com.azure.resourcemanager.mariadb.models.PublicNetworkAccessEnum;
import com.azure.resourcemanager.mariadb.models.Server;
import com.azure.resourcemanager.mariadb.models.ServerPropertiesForCreate;
import com.azure.resourcemanager.mariadb.models.ServerState;
import com.azure.resourcemanager.mariadb.models.ServerVersion;
import com.azure.resourcemanager.mariadb.models.Sku;
import com.azure.resourcemanager.mariadb.models.SkuTier;
import com.azure.resourcemanager.mariadb.models.SslEnforcementEnum;
import com.azure.resourcemanager.mariadb.models.StorageAutogrow;
import com.azure.resourcemanager.mariadb.models.StorageProfile;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public final class ServersCreateMockTests {
    @Test
    public void testCreate() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"sku\":{\"name\":\"fw\",\"tier\":\"GeneralPurpose\",\"capacity\":1139569570,\"size\":\"acjvefkd\",\"family\":\"oakggkfpag\"},\"properties\":{\"administratorLogin\":\"pulpqblylsyxk\",\"version\":\"10.2\",\"sslEnforcement\":\"Enabled\",\"minimalTlsVersion\":\"TLS1_0\",\"userVisibleState\":\"Disabled\",\"fullyQualifiedDomainName\":\"agxsdszuemps\",\"earliestRestoreDate\":\"2021-10-12T12:33:37Z\",\"storageProfile\":{\"backupRetentionDays\":2080244634,\"geoRedundantBackup\":\"Disabled\",\"storageMB\":946127719,\"storageAutogrow\":\"Enabled\"},\"replicationRole\":\"cvinvkjjxdxrbuuk\",\"masterServerId\":\"lewyhmlwpaz\",\"replicaCapacity\":1765730251,\"publicNetworkAccess\":\"Enabled\",\"privateEndpointConnections\":[]},\"location\":\"ckw\",\"tags\":{\"ppriol\":\"qwhxxbuyqaxzfeqz\",\"altol\":\"or\",\"wcsdbnwdcfhucq\":\"ncwsob\",\"vxb\":\"pfuvglsbjjca\"},\"id\":\"t\",\"name\":\"udutnco\",\"type\":\"mr\"}";

        Mockito.when(httpResponse.getStatusCode()).thenReturn(200);
        Mockito.when(httpResponse.getHeaders()).thenReturn(new HttpHeaders());
        Mockito
            .when(httpResponse.getBody())
            .thenReturn(Flux.just(ByteBuffer.wrap(responseStr.getBytes(StandardCharsets.UTF_8))));
        Mockito
            .when(httpResponse.getBodyAsByteArray())
            .thenReturn(Mono.just(responseStr.getBytes(StandardCharsets.UTF_8)));
        Mockito
            .when(httpClient.send(httpRequest.capture(), Mockito.any()))
            .thenReturn(
                Mono
                    .defer(
                        () -> {
                            Mockito.when(httpResponse.getRequest()).thenReturn(httpRequest.getValue());
                            return Mono.just(httpResponse);
                        }));

        MariaDBManager manager =
            MariaDBManager
                .configure()
                .withHttpClient(httpClient)
                .authenticate(
                    tokenRequestContext -> Mono.just(new AccessToken("this_is_a_token", OffsetDateTime.MAX)),
                    new AzureProfile("", "", AzureEnvironment.AZURE));

        Server response =
            manager
                .servers()
                .define("hzv")
                .withRegion("xxle")
                .withExistingResourceGroup("zsoibjudpfrxtr")
                .withProperties(
                    new ServerPropertiesForCreate()
                        .withVersion(ServerVersion.ONE_ZERO_THREE)
                        .withSslEnforcement(SslEnforcementEnum.DISABLED)
                        .withMinimalTlsVersion(MinimalTlsVersionEnum.TLS1_0)
                        .withPublicNetworkAccess(PublicNetworkAccessEnum.ENABLED)
                        .withStorageProfile(
                            new StorageProfile()
                                .withBackupRetentionDays(1061692620)
                                .withGeoRedundantBackup(GeoRedundantBackup.DISABLED)
                                .withStorageMB(1682572993)
                                .withStorageAutogrow(StorageAutogrow.ENABLED)))
                .withTags(mapOf("wxuqlcvydypatdoo", "amxjezwlw", "kooebwnu", "ojknio", "vdkcrodtj", "hemms"))
                .withSku(
                    new Sku()
                        .withName("tdwkqbrq")
                        .withTier(SkuTier.BASIC)
                        .withCapacity(1150856748)
                        .withSize("exiili")
                        .withFamily("dtiirqt"))
                .create();

        Assertions.assertEquals("ckw", response.location());
        Assertions.assertEquals("qwhxxbuyqaxzfeqz", response.tags().get("ppriol"));
        Assertions.assertEquals("fw", response.sku().name());
        Assertions.assertEquals(SkuTier.GENERAL_PURPOSE, response.sku().tier());
        Assertions.assertEquals(1139569570, response.sku().capacity());
        Assertions.assertEquals("acjvefkd", response.sku().size());
        Assertions.assertEquals("oakggkfpag", response.sku().family());
        Assertions.assertEquals("pulpqblylsyxk", response.administratorLogin());
        Assertions.assertEquals(ServerVersion.ONE_ZERO_TWO, response.version());
        Assertions.assertEquals(SslEnforcementEnum.ENABLED, response.sslEnforcement());
        Assertions.assertEquals(MinimalTlsVersionEnum.TLS1_0, response.minimalTlsVersion());
        Assertions.assertEquals(ServerState.DISABLED, response.userVisibleState());
        Assertions.assertEquals("agxsdszuemps", response.fullyQualifiedDomainName());
        Assertions.assertEquals(OffsetDateTime.parse("2021-10-12T12:33:37Z"), response.earliestRestoreDate());
        Assertions.assertEquals(2080244634, response.storageProfile().backupRetentionDays());
        Assertions.assertEquals(GeoRedundantBackup.DISABLED, response.storageProfile().geoRedundantBackup());
        Assertions.assertEquals(946127719, response.storageProfile().storageMB());
        Assertions.assertEquals(StorageAutogrow.ENABLED, response.storageProfile().storageAutogrow());
        Assertions.assertEquals("cvinvkjjxdxrbuuk", response.replicationRole());
        Assertions.assertEquals("lewyhmlwpaz", response.masterServerId());
        Assertions.assertEquals(1765730251, response.replicaCapacity());
        Assertions.assertEquals(PublicNetworkAccessEnum.ENABLED, response.publicNetworkAccess());
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
