// 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 MonitorsListByResourceGroupMockTests {
    @Test
    public void testListByResourceGroup() 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\":\"Accepted\",\"monitoringStatus\":\"Disabled\",\"marketplaceSubscriptionStatus\":\"Suspended\",\"marketplaceSubscriptionId\":\"pqsxvnmicy\",\"newRelicAccountProperties\":{\"userId\":\"eoveilovnotyf\"},\"userInfo\":{\"firstName\":\"k\",\"lastName\":\"x\",\"emailAddress\":\"bttk\",\"phoneNumber\":\"ywpnvjt\",\"country\":\"nermcl\"},\"planData\":{\"usageType\":\"PAYG\",\"billingCycle\":\"YEARLY\",\"planDetails\":\"uscrpabgyepsb\",\"effectiveDate\":\"2021-01-26T13:24:27Z\"},\"liftrResourceCategory\":\"MonitorLogs\",\"liftrResourcePreference\":2075352957,\"orgCreationSource\":\"NEWRELIC\",\"accountCreationSource\":\"NEWRELIC\"},\"identity\":{\"principalId\":\"43342a83-3a17-432a-9476-d5df04a78549\",\"tenantId\":\"1d876899-4898-434d-bb81-20fd15cc7d98\",\"type\":\"None\",\"userAssignedIdentities\":{}},\"location\":\"wfqkquj\",\"tags\":{\"xtccmg\":\"uyonobglaoc\",\"wfudwpzntxhdzhl\":\"udxytlmoyrx\",\"hckfrlhrx\":\"qj\",\"ca\":\"bkyvp\"},\"id\":\"uzbpzkafku\",\"name\":\"b\",\"type\":\"rnwb\"}]}";

        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().listByResourceGroup("cwscwsvlx", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("wfqkquj", response.iterator().next().location());
        Assertions.assertEquals("uyonobglaoc", response.iterator().next().tags().get("xtccmg"));
        Assertions.assertEquals(ManagedServiceIdentityType.NONE, response.iterator().next().identity().type());
        Assertions.assertEquals("eoveilovnotyf", response.iterator().next().newRelicAccountProperties().userId());
        Assertions.assertEquals("k", response.iterator().next().userInfo().firstName());
        Assertions.assertEquals("x", response.iterator().next().userInfo().lastName());
        Assertions.assertEquals("bttk", response.iterator().next().userInfo().emailAddress());
        Assertions.assertEquals("ywpnvjt", response.iterator().next().userInfo().phoneNumber());
        Assertions.assertEquals("nermcl", response.iterator().next().userInfo().country());
        Assertions.assertEquals(UsageType.PAYG, response.iterator().next().planData().usageType());
        Assertions.assertEquals(BillingCycle.YEARLY, response.iterator().next().planData().billingCycle());
        Assertions.assertEquals("uscrpabgyepsb", response.iterator().next().planData().planDetails());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-01-26T13:24:27Z"), response.iterator().next().planData().effectiveDate());
        Assertions.assertEquals(OrgCreationSource.NEWRELIC, response.iterator().next().orgCreationSource());
        Assertions.assertEquals(AccountCreationSource.NEWRELIC, response.iterator().next().accountCreationSource());
    }
}
