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

package com.azure.resourcemanager.resourcehealth.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.resourcehealth.ResourceHealthManager;
import com.azure.resourcemanager.resourcehealth.models.AvailabilityStateValues;
import com.azure.resourcemanager.resourcehealth.models.AvailabilityStatus;
import com.azure.resourcemanager.resourcehealth.models.ReasonChronicityTypes;
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 AvailabilityStatusesListMockTests {
    @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\":[{\"id\":\"hojvpajqgxysmocm\",\"name\":\"fqvm\",\"type\":\"xozap\",\"location\":\"elxprglyatddck\",\"properties\":{\"availabilityState\":\"Unavailable\",\"title\":\"jrjxgciqibrhosx\",\"summary\":\"qrhzoymibmrqyib\",\"detailedStatus\":\"wfluszdt\",\"reasonType\":\"rkwofyyvoqa\",\"context\":\"iexpbtgiwbwo\",\"category\":\"washr\",\"articleId\":\"tkcnqxwb\",\"rootCauseAttributionTime\":\"2021-11-17T19:43:56Z\",\"healthEventType\":\"lpiujwaa\",\"healthEventCause\":\"pqiiobyuqe\",\"healthEventCategory\":\"qlpqwcciuq\",\"healthEventId\":\"dbutauvfbtkuwhh\",\"resolutionETA\":\"2021-03-31T16:47:11Z\",\"occuredTime\":\"2021-08-07T17:01:26Z\",\"reasonChronicity\":\"Transient\",\"reportedTime\":\"2021-01-30T06:06:55Z\",\"recentlyResolved\":{\"unavailableOccuredTime\":\"2021-09-02T16:03:41Z\",\"resolvedTime\":\"2021-08-20T12:23:25Z\",\"unavailableSummary\":\"ichkoymkcdyhb\"},\"recommendedActions\":[],\"serviceImpactingEvents\":[]}}]}";

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

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

        PagedIterable<AvailabilityStatus> response =
            manager.availabilityStatuses().list("nubexk", "zksmondj", "quxvypomgkop", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("hojvpajqgxysmocm", response.iterator().next().id());
        Assertions.assertEquals("fqvm", response.iterator().next().name());
        Assertions.assertEquals("xozap", response.iterator().next().type());
        Assertions.assertEquals("elxprglyatddck", response.iterator().next().location());
        Assertions
            .assertEquals(
                AvailabilityStateValues.UNAVAILABLE, response.iterator().next().properties().availabilityState());
        Assertions.assertEquals("jrjxgciqibrhosx", response.iterator().next().properties().title());
        Assertions.assertEquals("qrhzoymibmrqyib", response.iterator().next().properties().summary());
        Assertions.assertEquals("wfluszdt", response.iterator().next().properties().detailedStatus());
        Assertions.assertEquals("rkwofyyvoqa", response.iterator().next().properties().reasonType());
        Assertions.assertEquals("iexpbtgiwbwo", response.iterator().next().properties().context());
        Assertions.assertEquals("washr", response.iterator().next().properties().category());
        Assertions.assertEquals("tkcnqxwb", response.iterator().next().properties().articleId());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-11-17T19:43:56Z"),
                response.iterator().next().properties().rootCauseAttributionTime());
        Assertions.assertEquals("lpiujwaa", response.iterator().next().properties().healthEventType());
        Assertions.assertEquals("pqiiobyuqe", response.iterator().next().properties().healthEventCause());
        Assertions.assertEquals("qlpqwcciuq", response.iterator().next().properties().healthEventCategory());
        Assertions.assertEquals("dbutauvfbtkuwhh", response.iterator().next().properties().healthEventId());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-03-31T16:47:11Z"), response.iterator().next().properties().resolutionEta());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-08-07T17:01:26Z"), response.iterator().next().properties().occuredTime());
        Assertions
            .assertEquals(ReasonChronicityTypes.TRANSIENT, response.iterator().next().properties().reasonChronicity());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-01-30T06:06:55Z"), response.iterator().next().properties().reportedTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-09-02T16:03:41Z"),
                response.iterator().next().properties().recentlyResolved().unavailableOccuredTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-08-20T12:23:25Z"),
                response.iterator().next().properties().recentlyResolved().resolvedTime());
        Assertions
            .assertEquals(
                "ichkoymkcdyhb", response.iterator().next().properties().recentlyResolved().unavailableSummary());
    }
}
