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

package com.azure.resourcemanager.maintenance.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.maintenance.MaintenanceManager;
import com.azure.resourcemanager.maintenance.models.MaintenanceConfiguration;
import com.azure.resourcemanager.maintenance.models.MaintenanceScope;
import com.azure.resourcemanager.maintenance.models.Visibility;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
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 MaintenanceConfigurationsCreateOrUpdateWithResponseMockTests {
    @Test
    public void testCreateOrUpdateWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"location\":\"cugicjoox\",\"tags\":{\"euecivyhzceuoj\":\"bwpucwwfvovbv\",\"dytdxwitx\":\"jrwjueiotwm\",\"qwgxhniskx\":\"rjaw\"},\"properties\":{\"namespace\":\"py\",\"extensionProperties\":{\"nhjdauw\":\"lwn\",\"nbmpowuwprzq\":\"vylwzbtdhxuj\",\"pjmkhfxobbc\":\"veual\",\"tghfgblcgwxzvl\":\"wsrtjriplrbpbe\"},\"maintenanceScope\":\"SQLDB\",\"maintenanceWindow\":{\"startDateTime\":\"begibtnmxiebwwa\",\"expirationDateTime\":\"ayqcgw\",\"duration\":\"zjuzgwyz\",\"timeZone\":\"txon\",\"recurEvery\":\"ts\"},\"visibility\":\"Public\"},\"id\":\"bp\",\"name\":\"xqpsrknftguv\",\"type\":\"iuhprwmdyvxqta\"}";

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

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

        MaintenanceConfiguration response =
            manager
                .maintenanceConfigurations()
                .define("sbj")
                .withExistingResourceGroup("ye")
                .withRegion("zq")
                .withTags(
                    mapOf(
                        "jidsuyonobglaoc",
                        "ywpmueefjzwfqkq",
                        "udxytlmoyrx",
                        "xtccmg",
                        "qj",
                        "wfudwpzntxhdzhl",
                        "bkyvp",
                        "hckfrlhrx"))
                .withNamespace("n")
                .withExtensionProperties(mapOf("kafkuwbcrnwbm", "p", "us", "hhseyv", "ofmxagkvtmelmqkr", "tslhspkdeem"))
                .withMaintenanceScope(MaintenanceScope.HOST)
                .withVisibility(Visibility.PUBLIC)
                .withStartDateTime("juahaquhcdhmdual")
                .withExpirationDateTime("xqpvfadmw")
                .withDuration("crgvxpvgom")
                .withTimeZone("fmisg")
                .withRecurEvery("nbbelda")
                .create();

        Assertions.assertEquals("cugicjoox", response.location());
        Assertions.assertEquals("bwpucwwfvovbv", response.tags().get("euecivyhzceuoj"));
        Assertions.assertEquals("py", response.namespace());
        Assertions.assertEquals("lwn", response.extensionProperties().get("nhjdauw"));
        Assertions.assertEquals(MaintenanceScope.SQLDB, response.maintenanceScope());
        Assertions.assertEquals(Visibility.PUBLIC, response.visibility());
        Assertions.assertEquals("begibtnmxiebwwa", response.startDateTime());
        Assertions.assertEquals("ayqcgw", response.expirationDateTime());
        Assertions.assertEquals("zjuzgwyz", response.duration());
        Assertions.assertEquals("txon", response.timeZone());
        Assertions.assertEquals("ts", response.recurEvery());
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
