// 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.AppliedScopeProperties;
import com.azure.resourcemanager.reservations.models.AppliedScopeType;
import com.azure.resourcemanager.reservations.models.InstanceFlexibility;
import com.azure.resourcemanager.reservations.models.ProvisioningState;
import com.azure.resourcemanager.reservations.models.PurchaseRequest;
import com.azure.resourcemanager.reservations.models.PurchaseRequestPropertiesReservedResourceProperties;
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.ReservedResourceType;
import com.azure.resourcemanager.reservations.models.SkuName;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
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 ReservationOrdersPurchaseMockTests {
    @Test
    public void testPurchase() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"etag\":930110171,\"properties\":{\"displayName\":\"mduvw\",\"requestDateTime\":\"2021-07-13T14:15:30Z\",\"createdDateTime\":\"2021-02-12T11:01:27Z\",\"expiryDateTime\":\"2021-10-06T10:39:24Z\",\"benefitStartTime\":\"2021-01-18T09:11:28Z\",\"originalQuantity\":1521698279,\"term\":\"P3Y\",\"provisioningState\":\"Succeeded\",\"billingPlan\":\"Upfront\",\"planInformation\":{\"transactions\":[]},\"reservations\":[],\"reviewDateTime\":\"2021-01-06T23:48:23Z\"},\"id\":\"xgibbda\",\"name\":\"confozauors\",\"type\":\"kokwbqplhlvnu\"}";

        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()
                .purchase(
                    "qgzsles",
                    new PurchaseRequest()
                        .withSku(new SkuName().withName("hernntiewdjc"))
                        .withLocation("quwrbehwag")
                        .withReservedResourceType(ReservedResourceType.SQL_DATABASES)
                        .withBillingScopeId("fkmr")
                        .withTerm(ReservationTerm.P1Y)
                        .withBillingPlan(ReservationBillingPlan.MONTHLY)
                        .withQuantity(901267810)
                        .withDisplayName("tdrjfutacoebj")
                        .withAppliedScopeType(AppliedScopeType.SHARED)
                        .withAppliedScopes(Arrays.asList("jznmwcpmguaa", "raufactkahzova", "j"))
                        .withAppliedScopeProperties(
                            new AppliedScopeProperties()
                                .withTenantId("xxpshneeku")
                                .withManagementGroupId("gs")
                                .withSubscriptionId("ubkwdle")
                                .withResourceGroupId("d")
                                .withDisplayName("tujbazpju"))
                        .withRenew(true)
                        .withReservedResourceProperties(
                            new PurchaseRequestPropertiesReservedResourceProperties()
                                .withInstanceFlexibility(InstanceFlexibility.ON))
                        .withReviewDateTime(OffsetDateTime.parse("2021-09-28T06:02:31Z")),
                    com.azure.core.util.Context.NONE);

        Assertions.assertEquals(930110171, response.etag());
        Assertions.assertEquals("mduvw", response.displayName());
        Assertions.assertEquals(OffsetDateTime.parse("2021-07-13T14:15:30Z"), response.requestDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-02-12T11:01:27Z"), response.createdDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-10-06T10:39:24Z"), response.expiryDateTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-01-18T09:11:28Z"), response.benefitStartTime());
        Assertions.assertEquals(1521698279, response.originalQuantity());
        Assertions.assertEquals(ReservationTerm.P3Y, response.term());
        Assertions.assertEquals(ProvisioningState.SUCCEEDED, response.provisioningState());
        Assertions.assertEquals(ReservationBillingPlan.UPFRONT, response.billingPlan());
        Assertions.assertEquals(OffsetDateTime.parse("2021-01-06T23:48:23Z"), response.reviewDateTime());
    }
}
