// 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.PerformanceTierProperties;
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 ServerBasedPerformanceTiersListMockTests {
    @Test
    public void testList() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"id\":\"uu\",\"maxBackupRetentionDays\":1575839239,\"minBackupRetentionDays\":1077343695,\"maxStorageMB\":335571328,\"minLargeStorageMB\":950264717,\"maxLargeStorageMB\":1399716201,\"minStorageMB\":676587800,\"serviceLevelObjectives\":[{\"id\":\"tgseinqfiufxqkn\",\"edition\":\"rgnepttwqmsniffc\",\"vCore\":2032190732,\"hardwareGeneration\":\"r\",\"maxBackupRetentionDays\":678890206,\"minBackupRetentionDays\":285792006,\"maxStorageMB\":735498603,\"minStorageMB\":2062903356}]}]}";

        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<PerformanceTierProperties> response =
            manager.serverBasedPerformanceTiers().list("fdntwjchrdgoih", "umwctondz", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("uu", response.iterator().next().id());
        Assertions.assertEquals(1575839239, response.iterator().next().maxBackupRetentionDays());
        Assertions.assertEquals(1077343695, response.iterator().next().minBackupRetentionDays());
        Assertions.assertEquals(335571328, response.iterator().next().maxStorageMB());
        Assertions.assertEquals(950264717, response.iterator().next().minLargeStorageMB());
        Assertions.assertEquals(1399716201, response.iterator().next().maxLargeStorageMB());
        Assertions.assertEquals(676587800, response.iterator().next().minStorageMB());
        Assertions.assertEquals("tgseinqfiufxqkn", response.iterator().next().serviceLevelObjectives().get(0).id());
        Assertions
            .assertEquals("rgnepttwqmsniffc", response.iterator().next().serviceLevelObjectives().get(0).edition());
        Assertions.assertEquals(2032190732, response.iterator().next().serviceLevelObjectives().get(0).vCore());
        Assertions.assertEquals("r", response.iterator().next().serviceLevelObjectives().get(0).hardwareGeneration());
        Assertions
            .assertEquals(
                678890206, response.iterator().next().serviceLevelObjectives().get(0).maxBackupRetentionDays());
        Assertions
            .assertEquals(
                285792006, response.iterator().next().serviceLevelObjectives().get(0).minBackupRetentionDays());
        Assertions.assertEquals(735498603, response.iterator().next().serviceLevelObjectives().get(0).maxStorageMB());
        Assertions.assertEquals(2062903356, response.iterator().next().serviceLevelObjectives().get(0).minStorageMB());
    }
}
