// 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 ChildAvailabilityStatusesGetByResourceWithResponseMockTests {
    @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\":\"tawfsdjpvkvp\",\"name\":\"xbkzbzkdvncj\",\"type\":\"udurgkakmokz\",\"location\":\"jk\",\"properties\":{\"availabilityState\":\"Unknown\",\"title\":\"ouw\",\"summary\":\"gzrf\",\"detailedStatus\":\"eyebizikayuhql\",\"reasonType\":\"bs\",\"context\":\"bqwrvtldgm\",\"category\":\"gvmpipaslthaqfx\",\"articleId\":\"mwutwbdsre\",\"rootCauseAttributionTime\":\"2021-03-19T15:07:21Z\",\"healthEventType\":\"hneuyowqkd\",\"healthEventCause\":\"t\",\"healthEventCategory\":\"ib\",\"healthEventId\":\"cgpik\",\"resolutionETA\":\"2021-11-09T05:31:13Z\",\"occuredTime\":\"2021-04-11T09:56:20Z\",\"reasonChronicity\":\"Transient\",\"reportedTime\":\"2021-11-14T04:35:06Z\",\"recentlyResolved\":{\"unavailableOccuredTime\":\"2020-12-28T15:25:21Z\",\"resolvedTime\":\"2021-05-28T04:48:51Z\",\"unavailableSummary\":\"vrmbzono\"},\"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
                .childAvailabilityStatuses()
                .getByResourceWithResponse(
                    "mkrefajpjorwkq", "yhgbijtjivfx", "sjabibs", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals("tawfsdjpvkvp", response.id());
        Assertions.assertEquals("xbkzbzkdvncj", response.name());
        Assertions.assertEquals("udurgkakmokz", response.type());
        Assertions.assertEquals("jk", response.location());
        Assertions.assertEquals(AvailabilityStateValues.UNKNOWN, response.properties().availabilityState());
        Assertions.assertEquals("ouw", response.properties().title());
        Assertions.assertEquals("gzrf", response.properties().summary());
        Assertions.assertEquals("eyebizikayuhql", response.properties().detailedStatus());
        Assertions.assertEquals("bs", response.properties().reasonType());
        Assertions.assertEquals("bqwrvtldgm", response.properties().context());
        Assertions.assertEquals("gvmpipaslthaqfx", response.properties().category());
        Assertions.assertEquals("mwutwbdsre", response.properties().articleId());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-03-19T15:07:21Z"), response.properties().rootCauseAttributionTime());
        Assertions.assertEquals("hneuyowqkd", response.properties().healthEventType());
        Assertions.assertEquals("t", response.properties().healthEventCause());
        Assertions.assertEquals("ib", response.properties().healthEventCategory());
        Assertions.assertEquals("cgpik", response.properties().healthEventId());
        Assertions.assertEquals(OffsetDateTime.parse("2021-11-09T05:31:13Z"), response.properties().resolutionEta());
        Assertions.assertEquals(OffsetDateTime.parse("2021-04-11T09:56:20Z"), response.properties().occuredTime());
        Assertions.assertEquals(ReasonChronicityTypes.TRANSIENT, response.properties().reasonChronicity());
        Assertions.assertEquals(OffsetDateTime.parse("2021-11-14T04:35:06Z"), response.properties().reportedTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2020-12-28T15:25:21Z"),
                response.properties().recentlyResolved().unavailableOccuredTime());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-05-28T04:48:51Z"), response.properties().recentlyResolved().resolvedTime());
        Assertions.assertEquals("vrmbzono", response.properties().recentlyResolved().unavailableSummary());
    }
}
