// 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.QueryStatistic;
import com.azure.resourcemanager.mariadb.models.TopQueryStatisticsInput;
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 TopQueryStatisticsListByServerMockTests {
    @Test
    public void testListByServer() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"properties\":{\"queryId\":\"qkdlw\",\"startTime\":\"2021-06-02T22:55:20Z\",\"endTime\":\"2021-04-24T09:11:55Z\",\"aggregationFunction\":\"lkxt\",\"databaseNames\":[\"fsmlmbtxhwgfw\",\"rtawcoezb\",\"hubskhudygooo\",\"kqfqjbvl\"],\"queryExecutionCount\":648948306510074398,\"metricName\":\"luiqtqzfavyvnqq\",\"metricDisplayName\":\"aryeu\",\"metricValue\":21.689444940847412,\"metricValueUnit\":\"abqgzslesjcbh\"},\"id\":\"rnntiewdjcv\",\"name\":\"quwrbehwag\",\"type\":\"hbuffkmrq\"}]}";

        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<QueryStatistic> response =
            manager
                .topQueryStatistics()
                .listByServer(
                    "uodpv",
                    "uudl",
                    new TopQueryStatisticsInput()
                        .withNumberOfTopQueries(1016035136)
                        .withAggregationFunction("ibthostgktstvd")
                        .withObservedMetric("eclze")
                        .withObservationStartTime(OffsetDateTime.parse("2021-03-03T03:15:27Z"))
                        .withObservationEndTime(OffsetDateTime.parse("2021-03-24T08:02:06Z"))
                        .withAggregationWindow("cvhzlhp"),
                    com.azure.core.util.Context.NONE);

        Assertions.assertEquals("qkdlw", response.iterator().next().queryId());
        Assertions.assertEquals(OffsetDateTime.parse("2021-06-02T22:55:20Z"), response.iterator().next().startTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-04-24T09:11:55Z"), response.iterator().next().endTime());
        Assertions.assertEquals("lkxt", response.iterator().next().aggregationFunction());
        Assertions.assertEquals("fsmlmbtxhwgfw", response.iterator().next().databaseNames().get(0));
        Assertions.assertEquals(648948306510074398L, response.iterator().next().queryExecutionCount());
        Assertions.assertEquals("luiqtqzfavyvnqq", response.iterator().next().metricName());
        Assertions.assertEquals("aryeu", response.iterator().next().metricDisplayName());
        Assertions.assertEquals(21.689444940847412D, response.iterator().next().metricValue());
        Assertions.assertEquals("abqgzslesjcbh", response.iterator().next().metricValueUnit());
    }
}
