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

package com.azure.resourcemanager.reservations.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.reservations.ReservationsManager;
import com.azure.resourcemanager.reservations.models.ProvisioningState;
import com.azure.resourcemanager.reservations.models.ReservationBillingPlan;
import com.azure.resourcemanager.reservations.models.ReservationOrderResponse;
import com.azure.resourcemanager.reservations.models.ReservationTerm;
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 ReservationOrdersGetWithResponseMockTests {
    @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 =
            "{\"etag\":1394452791,\"properties\":{\"displayName\":\"rwrbi\",\"requestDateTime\":\"2021-02-28T14:54:18Z\",\"createdDateTime\":\"2021-07-22T12:33Z\",\"expiryDateTime\":\"2021-12-04T14:45:36Z\",\"benefitStartTime\":\"2021-09-24T23:29:19Z\",\"originalQuantity\":1788432373,\"term\":\"P5Y\",\"provisioningState\":\"Failed\",\"billingPlan\":\"Monthly\",\"planInformation\":{\"transactions\":[]},\"reservations\":[],\"reviewDateTime\":\"2021-10-29T21:13:13Z\"},\"id\":\"dufiq\",\"name\":\"dieuzaofj\",\"type\":\"hvcyyysfg\"}";

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

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

        ReservationOrderResponse response =
            manager
                .reservationOrders()
                .getWithResponse("epzl", "phwzsoldweyuqdu", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals(1394452791, response.etag());
        Assertions.assertEquals("rwrbi", response.displayName());
        Assertions.assertEquals(OffsetDateTime.parse("2021-02-28T14:54:18Z"), response.requestDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-07-22T12:33Z"), response.createdDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-12-04T14:45:36Z"), response.expiryDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-09-24T23:29:19Z"), response.benefitStartTime());
        Assertions.assertEquals(1788432373, response.originalQuantity());
        Assertions.assertEquals(ReservationTerm.P5Y, response.term());
        Assertions.assertEquals(ProvisioningState.FAILED, response.provisioningState());
        Assertions.assertEquals(ReservationBillingPlan.MONTHLY, response.billingPlan());
        Assertions.assertEquals(OffsetDateTime.parse("2021-10-29T21:13:13Z"), response.reviewDateTime());
    }
}
