// 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.http.rest.PagedIterable;
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.ServerState;
import com.azure.resourcemanager.mariadb.models.ServerVersion;
import com.azure.resourcemanager.mariadb.models.SkuTier;
import com.azure.resourcemanager.mariadb.models.SslEnforcementEnum;
import com.azure.resourcemanager.mariadb.models.StorageAutogrow;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
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 ServersListByResourceGroupMockTests {
    @Test
    public void testListByResourceGroup() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"sku\":{\"name\":\"biknsorgjhxbld\",\"tier\":\"Basic\",\"capacity\":1505312764,\"size\":\"kdmtncvokotll\",\"family\":\"yhgsy\"},\"properties\":{\"administratorLogin\":\"gjltdtbnnhado\",\"version\":\"10.3\",\"sslEnforcement\":\"Disabled\",\"minimalTlsVersion\":\"TLSEnforcementDisabled\",\"userVisibleState\":\"Dropping\",\"fullyQualifiedDomainName\":\"vpa\",\"earliestRestoreDate\":\"2021-01-08T00:37:46Z\",\"storageProfile\":{\"backupRetentionDays\":1810691305,\"geoRedundantBackup\":\"Disabled\",\"storageMB\":443622503,\"storageAutogrow\":\"Enabled\"},\"replicationRole\":\"ggxkallatmelwuip\",\"masterServerId\":\"cjzkzivgvvcna\",\"replicaCapacity\":2075344266,\"publicNetworkAccess\":\"Disabled\",\"privateEndpointConnections\":[]},\"location\":\"xmueed\",\"tags\":{\"qtc\":\"dvstkw\",\"piohgwxrtfu\":\"ealmfmtdaaygdvwv\",\"k\":\"xepxgyqagvrvmn\"},\"id\":\"kghimdblxgwimfnj\",\"name\":\"fjxwmsz\",\"type\":\"kfoqreyfkzikfj\"}]}";

        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));

        PagedIterable<Server> response =
            manager.servers().listByResourceGroup("mwzn", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("xmueed", response.iterator().next().location());
        Assertions.assertEquals("dvstkw", response.iterator().next().tags().get("qtc"));
        Assertions.assertEquals("biknsorgjhxbld", response.iterator().next().sku().name());
        Assertions.assertEquals(SkuTier.BASIC, response.iterator().next().sku().tier());
        Assertions.assertEquals(1505312764, response.iterator().next().sku().capacity());
        Assertions.assertEquals("kdmtncvokotll", response.iterator().next().sku().size());
        Assertions.assertEquals("yhgsy", response.iterator().next().sku().family());
        Assertions.assertEquals("gjltdtbnnhado", response.iterator().next().administratorLogin());
        Assertions.assertEquals(ServerVersion.ONE_ZERO_THREE, response.iterator().next().version());
        Assertions.assertEquals(SslEnforcementEnum.DISABLED, response.iterator().next().sslEnforcement());
        Assertions
            .assertEquals(
                MinimalTlsVersionEnum.TLSENFORCEMENT_DISABLED, response.iterator().next().minimalTlsVersion());
        Assertions.assertEquals(ServerState.DROPPING, response.iterator().next().userVisibleState());
        Assertions.assertEquals("vpa", response.iterator().next().fullyQualifiedDomainName());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-01-08T00:37:46Z"), response.iterator().next().earliestRestoreDate());
        Assertions.assertEquals(1810691305, response.iterator().next().storageProfile().backupRetentionDays());
        Assertions
            .assertEquals(
                GeoRedundantBackup.DISABLED, response.iterator().next().storageProfile().geoRedundantBackup());
        Assertions.assertEquals(443622503, response.iterator().next().storageProfile().storageMB());
        Assertions.assertEquals(StorageAutogrow.ENABLED, response.iterator().next().storageProfile().storageAutogrow());
        Assertions.assertEquals("ggxkallatmelwuip", response.iterator().next().replicationRole());
        Assertions.assertEquals("cjzkzivgvvcna", response.iterator().next().masterServerId());
        Assertions.assertEquals(2075344266, response.iterator().next().replicaCapacity());
        Assertions.assertEquals(PublicNetworkAccessEnum.DISABLED, response.iterator().next().publicNetworkAccess());
    }
}
