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

package com.azure.resourcemanager.commerce.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.commerce.UsageManager;
import com.azure.resourcemanager.commerce.models.ResourceRateCardInfo;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.UUID;
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 RateCardsGetWithResponseMockTests {
    @Test
    public void testGetWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"Currency\":\"ypl\",\"Locale\":\"kbasyypn\",\"IsTaxIncluded\":true,\"OfferTerms\":[{\"Name\":\"OfferTermInfoAutoGenerated\",\"EffectiveDate\":\"2021-11-24T04:31Z\"},{\"Name\":\"OfferTermInfoAutoGenerated\",\"EffectiveDate\":\"2021-03-24T00:19:08Z\"},{\"Name\":\"OfferTermInfoAutoGenerated\",\"EffectiveDate\":\"2021-12-07T05:26:51Z\"}],\"Meters\":[{\"MeterId\":\"b3250b1c-d2dd-4892-a595-82de8cc6e56e\",\"MeterName\":\"tynqgoul\",\"MeterCategory\":\"dlikwyqkgfgibma\",\"MeterSubCategory\":\"akeqs\",\"Unit\":\"yb\",\"MeterTags\":[\"e\",\"qytbciq\",\"ouf\",\"mmnkzsmodmgl\"],\"MeterRegion\":\"gpbkwtmut\",\"MeterRates\":{\"ta\":85.38377,\"pwgcuertu\":94.51547,\"dosvqwhbmdgbbjf\":38.05988,\"gmbmbexppbh\":21.297592},\"EffectiveDate\":\"2021-09-17T16:41:16Z\",\"IncludedQuantity\":8.5479145}]}";

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

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

        ResourceRateCardInfo response =
            manager.rateCards().getWithResponse("fkgiawxk", com.azure.core.util.Context.NONE).getValue();

        Assertions.assertEquals("ypl", response.currency());
        Assertions.assertEquals("kbasyypn", response.locale());
        Assertions.assertEquals(true, response.isTaxIncluded());
        Assertions
            .assertEquals(OffsetDateTime.parse("2021-11-24T04:31Z"), response.offerTerms().get(0).effectiveDate());
        Assertions
            .assertEquals(UUID.fromString("b3250b1c-d2dd-4892-a595-82de8cc6e56e"), response.meters().get(0).meterId());
        Assertions.assertEquals("tynqgoul", response.meters().get(0).meterName());
        Assertions.assertEquals("dlikwyqkgfgibma", response.meters().get(0).meterCategory());
        Assertions.assertEquals("akeqs", response.meters().get(0).meterSubCategory());
        Assertions.assertEquals("yb", response.meters().get(0).unit());
        Assertions.assertEquals("e", response.meters().get(0).meterTags().get(0));
        Assertions.assertEquals("gpbkwtmut", response.meters().get(0).meterRegion());
        Assertions.assertEquals(85.38377f, response.meters().get(0).meterRates().get("ta"));
        Assertions.assertEquals(OffsetDateTime.parse("2021-09-17T16:41:16Z"), response.meters().get(0).effectiveDate());
        Assertions.assertEquals(8.5479145F, response.meters().get(0).includedQuantity());
    }
}
