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

package com.azure.resourcemanager.cognitiveservices.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.cognitiveservices.CognitiveServicesManager;
import com.azure.resourcemanager.cognitiveservices.models.QuotaUsageStatus;
import com.azure.resourcemanager.cognitiveservices.models.UnitType;
import com.azure.resourcemanager.cognitiveservices.models.UsageListResult;
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 AccountsListUsagesWithResponseMockTests {
    @Test
    public void testListUsagesWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"nextLink\":\"pzlrphw\",\"value\":[{\"unit\":\"Percent\",\"name\":{\"value\":\"yuqdu\",\"localizedValue\":\"mnnrwr\"},\"quotaPeriod\":\"ork\",\"limit\":33.80598367891427,\"currentValue\":65.86009286032747,\"nextResetTime\":\"gdnhxmsiv\",\"status\":\"Unknown\"},{\"unit\":\"Milliseconds\",\"name\":{\"value\":\"ggdufiqndieu\",\"localizedValue\":\"ofjchvcyyysf\"},\"quotaPeriod\":\"otcubi\",\"limit\":77.02580661377183,\"currentValue\":2.414000523273596,\"nextResetTime\":\"onmacjekniz\",\"status\":\"Included\"},{\"unit\":\"Seconds\",\"name\":{\"value\":\"pevf\",\"localizedValue\":\"b\"},\"quotaPeriod\":\"rilbywdx\",\"limit\":15.215514950847652,\"currentValue\":71.46991928606101,\"nextResetTime\":\"fscjfnynszquji\",\"status\":\"Blocked\"}]}";

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

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

        UsageListResult response =
            manager
                .accounts()
                .listUsagesWithResponse("xgibbda", "confozauors", "kokwbqplhlvnu", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals("pzlrphw", response.nextLink());
        Assertions.assertEquals(UnitType.PERCENT, response.value().get(0).unit());
        Assertions.assertEquals("yuqdu", response.value().get(0).name().value());
        Assertions.assertEquals("mnnrwr", response.value().get(0).name().localizedValue());
        Assertions.assertEquals("ork", response.value().get(0).quotaPeriod());
        Assertions.assertEquals(33.80598367891427D, response.value().get(0).limit());
        Assertions.assertEquals(65.86009286032747D, response.value().get(0).currentValue());
        Assertions.assertEquals("gdnhxmsiv", response.value().get(0).nextResetTime());
        Assertions.assertEquals(QuotaUsageStatus.UNKNOWN, response.value().get(0).status());
    }
}
