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

        String responseStr =
            "{\"id\":\"fvhqc\",\"name\":\"ilvpnppfuflrwd\",\"type\":\"dlxyjrxs\",\"location\":\"afcnih\",\"properties\":{\"availabilityState\":\"Degraded\",\"title\":\"nedgfbc\",\"summary\":\"cvqvpkeqdcv\",\"detailedStatus\":\"hvoodsotbobzd\",\"reasonType\":\"cjwvn\",\"context\":\"ld\",\"category\":\"gx\",\"articleId\":\"rslpmutwuoeg\",\"rootCauseAttributionTime\":\"2021-08-11T07:57:19Z\",\"healthEventType\":\"jw\",\"healthEventCause\":\"yqsluic\",\"healthEventCategory\":\"ggkzzlvmbmpa\",\"healthEventId\":\"odfvuefywsbp\",\"resolutionETA\":\"2021-09-23T19:46:07Z\",\"occuredTime\":\"2021-11-18T13:44:32Z\",\"reasonChronicity\":\"Persistent\",\"reportedTime\":\"2021-03-30T01:45:45Z\",\"recentlyResolved\":{\"unavailableOccuredTime\":\"2021-03-28T00:46:50Z\",\"resolvedTime\":\"2021-06-19T21:54:55Z\",\"unavailableSummary\":\"kcpwiy\"},\"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));

        AvailabilityStatus response =
            manager
                .availabilityStatuses()
                .getByResourceWithResponse(
                    "nosggbhcoh", "wdsjnkalju", "iiswacffgdkzze", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals("fvhqc", response.id());
        Assertions.assertEquals("ilvpnppfuflrwd", response.name());
        Assertions.assertEquals("dlxyjrxs", response.type());
        Assertions.assertEquals("afcnih", response.location());
        Assertions.assertEquals(AvailabilityStateValues.DEGRADED, response.properties().availabilityState());
        Assertions.assertEquals("nedgfbc", response.properties().title());
        Assertions.assertEquals("cvqvpkeqdcv", response.properties().summary());
        Assertions.assertEquals("hvoodsotbobzd", response.properties().detailedStatus());
        Assertions.assertEquals("cjwvn", response.properties().reasonType());
        Assertions.assertEquals("ld", response.properties().context());
        Assertions.assertEquals("gx", response.properties().category());
        Assertions.assertEquals("rslpmutwuoeg", response.properties().articleId());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-08-11T07:57:19Z"), response.properties().rootCauseAttributionTime());
        Assertions.assertEquals("jw", response.properties().healthEventType());
        Assertions.assertEquals("yqsluic", response.properties().healthEventCause());
        Assertions.assertEquals("ggkzzlvmbmpa", response.properties().healthEventCategory());
        Assertions.assertEquals("odfvuefywsbp", response.properties().healthEventId());
        Assertions.assertEquals(OffsetDateTime.parse("2021-09-23T19:46:07Z"), response.properties().resolutionEta());
        Assertions.assertEquals(OffsetDateTime.parse("2021-11-18T13:44:32Z"), response.properties().occuredTime());
        Assertions.assertEquals(ReasonChronicityTypes.PERSISTENT, response.properties().reasonChronicity());
        Assertions.assertEquals(OffsetDateTime.parse("2021-03-30T01:45:45Z"), response.properties().reportedTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-03-28T00:46:50Z"),
                response.properties().recentlyResolved().unavailableOccuredTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-06-19T21:54:55Z"), response.properties().recentlyResolved().resolvedTime());
        Assertions.assertEquals("kcpwiy", response.properties().recentlyResolved().unavailableSummary());
    }
}
