// 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.RefundRequest;
import com.azure.resourcemanager.reservations.models.RefundRequestProperties;
import com.azure.resourcemanager.reservations.models.ReservationBillingPlan;
import com.azure.resourcemanager.reservations.models.ReservationOrderResponse;
import com.azure.resourcemanager.reservations.models.ReservationTerm;
import com.azure.resourcemanager.reservations.models.ReservationToReturn;
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 ReturnsPostMockTests {
    @Test
    public void testPost() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"etag\":1313812453,\"properties\":{\"displayName\":\"yhddvia\",\"requestDateTime\":\"2021-08-22T18:38:50Z\",\"createdDateTime\":\"2021-09-06T17:32:09Z\",\"expiryDateTime\":\"2021-10-12T00:07:33Z\",\"benefitStartTime\":\"2021-02-23T02:35:37Z\",\"originalQuantity\":328676113,\"term\":\"P1Y\",\"provisioningState\":\"Succeeded\",\"billingPlan\":\"Monthly\",\"planInformation\":{\"transactions\":[]},\"reservations\":[],\"reviewDateTime\":\"2021-07-06T10:06:19Z\"},\"id\":\"l\",\"name\":\"chp\",\"type\":\"db\"}";

        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
                .returns()
                .post(
                    "hxuuwhcjyxccybvp",
                    new RefundRequest()
                        .withProperties(
                            new RefundRequestProperties()
                                .withSessionId("kkudzp")
                                .withScope("wjplma")
                                .withReservationToReturn(
                                    new ReservationToReturn()
                                        .withReservationId("cyohpfkyrkdbd")
                                        .withQuantity(863803765))
                                .withReturnReason("sjkmnwqj")),
                    com.azure.core.util.Context.NONE);

        Assertions.assertEquals(1313812453, response.etag());
        Assertions.assertEquals("yhddvia", response.displayName());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-22T18:38:50Z"), response.requestDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-09-06T17:32:09Z"), response.createdDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-10-12T00:07:33Z"), response.expiryDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-02-23T02:35:37Z"), response.benefitStartTime());
        Assertions.assertEquals(328676113, response.originalQuantity());
        Assertions.assertEquals(ReservationTerm.P1Y, response.term());
        Assertions.assertEquals(ProvisioningState.SUCCEEDED, response.provisioningState());
        Assertions.assertEquals(ReservationBillingPlan.MONTHLY, response.billingPlan());
        Assertions.assertEquals(OffsetDateTime.parse("2021-07-06T10:06:19Z"), response.reviewDateTime());
    }
}
