// 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.AppliedScopeType;
import com.azure.resourcemanager.reservations.models.InstanceFlexibility;
import com.azure.resourcemanager.reservations.models.Kind;
import com.azure.resourcemanager.reservations.models.MergeRequest;
import com.azure.resourcemanager.reservations.models.ProvisioningState;
import com.azure.resourcemanager.reservations.models.ReservationBillingPlan;
import com.azure.resourcemanager.reservations.models.ReservationResponse;
import com.azure.resourcemanager.reservations.models.ReservationTerm;
import com.azure.resourcemanager.reservations.models.ReservedResourceType;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.List;
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 ReservationsMergeMockTests {
    @Test
    public void testMerge() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "[{\"location\":\"hxvrhmzkwpjg\",\"etag\":856431804,\"sku\":{\"name\":\"ghftqsxhqxujxuk\"},\"properties\":{\"reservedResourceType\":\"AzureFiles\",\"instanceFlexibility\":\"Off\",\"displayName\":\"jguufzdm\",\"appliedScopes\":[\"tfih\",\"hbotzingamvppho\",\"zqzudph\",\"amvdkfwynwcvtbv\"],\"appliedScopeType\":\"Single\",\"archived\":false,\"capabilities\":\"nvyq\",\"quantity\":686807657,\"provisioningState\":\"Failed\",\"effectiveDateTime\":\"2021-05-19T06:58:08Z\",\"benefitStartTime\":\"2021-08-16T19:44:14Z\",\"lastUpdatedDateTime\":\"2021-09-23T11:43:17Z\",\"expiryDateTime\":\"2021-02-07T21:37:46Z\",\"reviewDateTime\":\"2021-05-22T23:16:36Z\",\"skuDescription\":\"gvvs\",\"billingPlan\":\"Upfront\",\"displayProvisioningState\":\"g\",\"provisioningSubState\":\"fhwygzlvdnkfxus\",\"purchaseDateTime\":\"2021-02-14T09:42:03Z\",\"billingScopeId\":\"apfcqdpsq\",\"renew\":true,\"renewSource\":\"svuo\",\"renewDestination\":\"gc\",\"term\":\"P5Y\",\"userFriendlyAppliedScopeType\":\"zrypqlmfeokerqw\",\"userFriendlyRenewState\":\"hkobopgxedk\"},\"kind\":\"Microsoft.Compute\",\"id\":\"qpcrf\",\"name\":\"bwccsnjvcdwxlpqe\",\"type\":\"ftnkhtj\"},{\"location\":\"i\",\"etag\":2104302004,\"sku\":{\"name\":\"atmtdhtmdvy\"},\"properties\":{\"reservedResourceType\":\"Databricks\",\"instanceFlexibility\":\"On\",\"displayName\":\"zywkb\",\"appliedScopes\":[\"yuzhlhkjoqrvq\",\"aatjinrvgoupmfi\"],\"appliedScopeType\":\"Shared\",\"archived\":false,\"capabilities\":\"ioolvrwxkvtkkgll\",\"quantity\":342609300,\"provisioningState\":\"Split\",\"effectiveDateTime\":\"2021-07-24T13:52:35Z\",\"benefitStartTime\":\"2021-03-15T01:10:56Z\",\"lastUpdatedDateTime\":\"2021-05-06T19:16:01Z\",\"expiryDateTime\":\"2021-04-03T14:41:24Z\",\"reviewDateTime\":\"2021-03-17T12:51:03Z\",\"skuDescription\":\"uhbxvvy\",\"billingPlan\":\"Monthly\",\"displayProvisioningState\":\"byrqufeg\",\"provisioningSubState\":\"vwz\",\"purchaseDateTime\":\"2021-03-16T15:12:59Z\",\"billingScopeId\":\"p\",\"renew\":false,\"renewSource\":\"tvgbmhrixkwmy\",\"renewDestination\":\"ejvegrhbpnaixex\",\"term\":\"P5Y\",\"userFriendlyAppliedScopeType\":\"eaxhcexdrrvqahqk\",\"userFriendlyRenewState\":\"tpwijnh\"},\"kind\":\"Microsoft.Compute\",\"id\":\"ycxzbfvoo\",\"name\":\"vrvmtgjqppyost\",\"type\":\"on\"}]";

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

        List<ReservationResponse> response =
            manager
                .reservations()
                .merge(
                    "ldvriiiojnalghfk",
                    new MergeRequest().withSources(Arrays.asList("ex", "owueluqh")),
                    com.azure.core.util.Context.NONE);

        Assertions.assertEquals("hxvrhmzkwpjg", response.get(0).location());
        Assertions.assertEquals(856431804, response.get(0).etag());
        Assertions.assertEquals("ghftqsxhqxujxuk", response.get(0).sku().name());
        Assertions.assertEquals(ReservedResourceType.AZURE_FILES, response.get(0).properties().reservedResourceType());
        Assertions.assertEquals(InstanceFlexibility.OFF, response.get(0).properties().instanceFlexibility());
        Assertions.assertEquals("jguufzdm", response.get(0).properties().displayName());
        Assertions.assertEquals("tfih", response.get(0).properties().appliedScopes().get(0));
        Assertions.assertEquals(AppliedScopeType.SINGLE, response.get(0).properties().appliedScopeType());
        Assertions.assertEquals(false, response.get(0).properties().archived());
        Assertions.assertEquals("nvyq", response.get(0).properties().capabilities());
        Assertions.assertEquals(686807657, response.get(0).properties().quantity());
        Assertions.assertEquals(ProvisioningState.FAILED, response.get(0).properties().provisioningState());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-05-19T06:58:08Z"), response.get(0).properties().effectiveDateTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-08-16T19:44:14Z"), response.get(0).properties().benefitStartTime());
        Assertions
            .assertEquals(OffsetDateTime.parse("2021-02-07T21:37:46Z"), response.get(0).properties().expiryDateTime());
        Assertions
            .assertEquals(OffsetDateTime.parse("2021-05-22T23:16:36Z"), response.get(0).properties().reviewDateTime());
        Assertions.assertEquals("gvvs", response.get(0).properties().skuDescription());
        Assertions.assertEquals(ReservationBillingPlan.UPFRONT, response.get(0).properties().billingPlan());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-02-14T09:42:03Z"), response.get(0).properties().purchaseDateTime());
        Assertions.assertEquals("apfcqdpsq", response.get(0).properties().billingScopeId());
        Assertions.assertEquals(true, response.get(0).properties().renew());
        Assertions.assertEquals("svuo", response.get(0).properties().renewSource());
        Assertions.assertEquals("gc", response.get(0).properties().renewDestination());
        Assertions.assertEquals(ReservationTerm.P5Y, response.get(0).properties().term());
        Assertions.assertEquals(Kind.MICROSOFT_COMPUTE, response.get(0).kind());
    }
}
