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

package com.azure.resourcemanager.newrelicobservability.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.newrelicobservability.NewRelicObservabilityManager;
import com.azure.resourcemanager.newrelicobservability.models.AccountCreationSource;
import com.azure.resourcemanager.newrelicobservability.models.BillingCycle;
import com.azure.resourcemanager.newrelicobservability.models.ManagedServiceIdentityType;
import com.azure.resourcemanager.newrelicobservability.models.NewRelicMonitorResource;
import com.azure.resourcemanager.newrelicobservability.models.OrgCreationSource;
import com.azure.resourcemanager.newrelicobservability.models.PlanData;
import com.azure.resourcemanager.newrelicobservability.models.SwitchBillingRequest;
import com.azure.resourcemanager.newrelicobservability.models.UsageType;
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 MonitorsSwitchBillingWithResponseMockTests {
    @Test
    public void testSwitchBillingWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"provisioningState\":\"Updating\",\"monitoringStatus\":\"Disabled\",\"marketplaceSubscriptionStatus\":\"Active\",\"marketplaceSubscriptionId\":\"txmedj\",\"newRelicAccountProperties\":{\"userId\":\"lynqwwncwzzh\"},\"userInfo\":{\"firstName\":\"gucnapkte\",\"lastName\":\"llwptfdy\",\"emailAddress\":\"fqbuaceopzf\",\"phoneNumber\":\"hhuao\",\"country\":\"pcqeqx\"},\"planData\":{\"usageType\":\"COMMITTED\",\"billingCycle\":\"MONTHLY\",\"planDetails\":\"xcto\",\"effectiveDate\":\"2021-01-13T03:12:52Z\"},\"liftrResourceCategory\":\"MonitorLogs\",\"liftrResourcePreference\":1740681281,\"orgCreationSource\":\"NEWRELIC\",\"accountCreationSource\":\"LIFTR\"},\"identity\":{\"principalId\":\"1151cee7-9e16-4851-9d31-70346fb26ae6\",\"tenantId\":\"7bd4edf3-d1c7-4626-a15b-a59b946f8cc5\",\"type\":\"SystemAssigned\",\"userAssignedIdentities\":{}},\"location\":\"bunrmfqjhhk\",\"tags\":{\"vkr\":\"vjymjhxxjyngud\",\"szjfauvjfdxxivet\":\"swbxqz\",\"qaqtdoqmcbxvwvxy\":\"t\",\"obl\":\"lqbhsf\"},\"id\":\"tkblmpewww\",\"name\":\"bkrvrnsvshqj\",\"type\":\"hxcr\"}";

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

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

        NewRelicMonitorResource response =
            manager
                .monitors()
                .switchBillingWithResponse(
                    "xyawj",
                    "yaqcslyjpkiidz",
                    new SwitchBillingRequest()
                        .withAzureResourceId("xznelixhnrztf")
                        .withOrganizationId("hb")
                        .withPlanData(
                            new PlanData()
                                .withUsageType(UsageType.COMMITTED)
                                .withBillingCycle(BillingCycle.MONTHLY)
                                .withPlanDetails("ulppggdtpnapnyir")
                                .withEffectiveDate(OffsetDateTime.parse("2021-02-09T03:01:47Z")))
                        .withUserEmail("hpigv"),
                    com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals("bunrmfqjhhk", response.location());
        Assertions.assertEquals("vjymjhxxjyngud", response.tags().get("vkr"));
        Assertions.assertEquals(ManagedServiceIdentityType.SYSTEM_ASSIGNED, response.identity().type());
        Assertions.assertEquals("lynqwwncwzzh", response.newRelicAccountProperties().userId());
        Assertions.assertEquals("gucnapkte", response.userInfo().firstName());
        Assertions.assertEquals("llwptfdy", response.userInfo().lastName());
        Assertions.assertEquals("fqbuaceopzf", response.userInfo().emailAddress());
        Assertions.assertEquals("hhuao", response.userInfo().phoneNumber());
        Assertions.assertEquals("pcqeqx", response.userInfo().country());
        Assertions.assertEquals(UsageType.COMMITTED, response.planData().usageType());
        Assertions.assertEquals(BillingCycle.MONTHLY, response.planData().billingCycle());
        Assertions.assertEquals("xcto", response.planData().planDetails());
        Assertions.assertEquals(OffsetDateTime.parse("2021-01-13T03:12:52Z"), response.planData().effectiveDate());
        Assertions.assertEquals(OrgCreationSource.NEWRELIC, response.orgCreationSource());
        Assertions.assertEquals(AccountCreationSource.LIFTR, response.accountCreationSource());
    }
}
