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

package com.azure.resourcemanager.costmanagement.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.costmanagement.CostManagementManager;
import com.azure.resourcemanager.costmanagement.models.ScheduleFrequency;
import com.azure.resourcemanager.costmanagement.models.ScheduledAction;
import com.azure.resourcemanager.costmanagement.models.ScheduledActionKind;
import com.azure.resourcemanager.costmanagement.models.ScheduledActionStatus;
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 ScheduledActionsListMockTests {
    @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\":{\"displayName\":\"j\",\"fileDestination\":{\"fileFormats\":[]},\"notification\":{\"to\":[],\"language\":\"fnmdxotn\",\"message\":\"dgug\",\"regionalFormat\":\"zihgrkyu\",\"subject\":\"zabs\"},\"notificationEmail\":\"fpphoj\",\"schedule\":{\"frequency\":\"Monthly\",\"hourOfDay\":782057398,\"daysOfWeek\":[],\"weeksOfMonth\":[],\"dayOfMonth\":1922509434,\"startDate\":\"2021-07-27T14:52:36Z\",\"endDate\":\"2020-12-27T00:09:12Z\"},\"scope\":\"czbgomfgbeg\",\"status\":\"Expired\",\"viewId\":\"gleohi\"},\"eTag\":\"tnluankrr\",\"kind\":\"InsightAlert\",\"id\":\"ebtijvacvbmqzbqq\",\"name\":\"lajrnwxacevehj\",\"type\":\"uyxoaf\"}]}";

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

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

        PagedIterable<ScheduledAction> response =
            manager.scheduledActions().list("mkyoqufdvruzsl", com.azure.core.util.Context.NONE);

        Assertions.assertEquals(ScheduledActionKind.INSIGHT_ALERT, response.iterator().next().kind());
        Assertions.assertEquals("j", response.iterator().next().displayName());
        Assertions.assertEquals("fnmdxotn", response.iterator().next().notification().language());
        Assertions.assertEquals("dgug", response.iterator().next().notification().message());
        Assertions.assertEquals("zihgrkyu", response.iterator().next().notification().regionalFormat());
        Assertions.assertEquals("zabs", response.iterator().next().notification().subject());
        Assertions.assertEquals("fpphoj", response.iterator().next().notificationEmail());
        Assertions.assertEquals(ScheduleFrequency.MONTHLY, response.iterator().next().schedule().frequency());
        Assertions.assertEquals(782057398, response.iterator().next().schedule().hourOfDay());
        Assertions.assertEquals(1922509434, response.iterator().next().schedule().dayOfMonth());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-07-27T14:52:36Z"), response.iterator().next().schedule().startDate());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2020-12-27T00:09:12Z"), response.iterator().next().schedule().endDate());
        Assertions.assertEquals("czbgomfgbeg", response.iterator().next().scope());
        Assertions.assertEquals(ScheduledActionStatus.EXPIRED, response.iterator().next().status());
        Assertions.assertEquals("gleohi", response.iterator().next().viewId());
    }
}
