// 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.http.rest.PagedIterable;
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.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 MonitorsListMockTests {
    @Test
    public void testList() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"properties\":{\"provisioningState\":\"Creating\",\"monitoringStatus\":\"Disabled\",\"marketplaceSubscriptionStatus\":\"Active\",\"marketplaceSubscriptionId\":\"niwdjsw\",\"newRelicAccountProperties\":{\"userId\":\"dbpgnxytxhp\"},\"userInfo\":{\"firstName\":\"zabglcuhxwt\",\"lastName\":\"yqiklbbovplwzb\",\"emailAddress\":\"gy\",\"phoneNumber\":\"uosvmkfssxqukk\",\"country\":\"l\"},\"planData\":{\"usageType\":\"COMMITTED\",\"billingCycle\":\"MONTHLY\",\"planDetails\":\"jzkdeslpvlopwi\",\"effectiveDate\":\"2021-09-12T13:52:28Z\"},\"liftrResourceCategory\":\"Unknown\",\"liftrResourcePreference\":1973154307,\"orgCreationSource\":\"LIFTR\",\"accountCreationSource\":\"NEWRELIC\"},\"identity\":{\"principalId\":\"75dd3c6a-354c-4390-a355-7aa10b3b870c\",\"tenantId\":\"f4f159ab-7620-4931-a69d-a7b80d4e817a\",\"type\":\"None\",\"userAssignedIdentities\":{}},\"location\":\"umnyqu\",\"tags\":{\"hsmtxpsiebtfhvp\":\"eojnabc\"},\"id\":\"sapskr\",\"name\":\"qmhjjdhtld\",\"type\":\"kyzxuutk\"}]}";

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

        PagedIterable<NewRelicMonitorResource> response = manager.monitors().list(com.azure.core.util.Context.NONE);

        Assertions.assertEquals("umnyqu", response.iterator().next().location());
        Assertions.assertEquals("eojnabc", response.iterator().next().tags().get("hsmtxpsiebtfhvp"));
        Assertions.assertEquals(ManagedServiceIdentityType.NONE, response.iterator().next().identity().type());
        Assertions.assertEquals("dbpgnxytxhp", response.iterator().next().newRelicAccountProperties().userId());
        Assertions.assertEquals("zabglcuhxwt", response.iterator().next().userInfo().firstName());
        Assertions.assertEquals("yqiklbbovplwzb", response.iterator().next().userInfo().lastName());
        Assertions.assertEquals("gy", response.iterator().next().userInfo().emailAddress());
        Assertions.assertEquals("uosvmkfssxqukk", response.iterator().next().userInfo().phoneNumber());
        Assertions.assertEquals("l", response.iterator().next().userInfo().country());
        Assertions.assertEquals(UsageType.COMMITTED, response.iterator().next().planData().usageType());
        Assertions.assertEquals(BillingCycle.MONTHLY, response.iterator().next().planData().billingCycle());
        Assertions.assertEquals("jzkdeslpvlopwi", response.iterator().next().planData().planDetails());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-09-12T13:52:28Z"), response.iterator().next().planData().effectiveDate());
        Assertions.assertEquals(OrgCreationSource.LIFTR, response.iterator().next().orgCreationSource());
        Assertions.assertEquals(AccountCreationSource.NEWRELIC, response.iterator().next().accountCreationSource());
    }
}
