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

package com.azure.resourcemanager.mediaservices.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.mediaservices.MediaServicesManager;
import com.azure.resourcemanager.mediaservices.models.ArmStreamingEndpointCurrentSku;
import com.azure.resourcemanager.mediaservices.models.CrossSiteAccessPolicies;
import com.azure.resourcemanager.mediaservices.models.StreamingEndpoint;
import com.azure.resourcemanager.mediaservices.models.StreamingEndpointAccessControl;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
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 StreamingEndpointsCreateMockTests {
    @Test
    public void testCreate() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"description\":\"uygdhgaqipirp\",\"scaleUnits\":779964189,\"availabilitySetName\":\"qofu\",\"accessControl\":{},\"maxCacheAge\":5746310488025921747,\"customHostNames\":[\"whc\"],\"hostName\":\"pibkephuu\",\"cdnEnabled\":false,\"cdnProvider\":\"t\",\"cdnProfile\":\"oyin\",\"provisioningState\":\"Succeeded\",\"resourceState\":\"Starting\",\"crossSiteAccessPolicies\":{\"clientAccessPolicy\":\"cvcrrp\",\"crossDomainPolicy\":\"ttbst\"},\"freeTrialEndTime\":\"2021-10-12T17:16:27Z\",\"created\":\"2021-03-11T12:48:52Z\",\"lastModified\":\"2021-09-27T05:41:57Z\"},\"sku\":{\"name\":\"fkoxmlghktuidvr\",\"capacity\":1891751992},\"location\":\"lpdwwexymzvlazi\",\"tags\":{\"ziven\":\"pwvqsgnyyu\",\"vpkpatlb\":\"rpmey\",\"gsksrfhf\":\"jp\"},\"id\":\"olmk\",\"name\":\"bnxwc\",\"type\":\"ommpvfqaw\"}";

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

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

        StreamingEndpoint response =
            manager
                .streamingEndpoints()
                .define("rzdcgdzbenribcaw")
                .withRegion("yvk")
                .withExistingMediaservice("q", "jxcx")
                .withTags(mapOf("tod", "rocxnehvs"))
                .withSku(new ArmStreamingEndpointCurrentSku().withCapacity(2000870713))
                .withDescription("qd")
                .withScaleUnits(1373265486)
                .withAvailabilitySetName("wflj")
                .withAccessControl(new StreamingEndpointAccessControl())
                .withMaxCacheAge(1040471271589317610L)
                .withCustomHostNames(Arrays.asList("mzwcjjncqt", "z"))
                .withCdnEnabled(false)
                .withCdnProvider("bxn")
                .withCdnProfile("ebwgga")
                .withCrossSiteAccessPolicies(
                    new CrossSiteAccessPolicies().withClientAccessPolicy("x").withCrossDomainPolicy("qzasunwqrjzfrgqh"))
                .withAutoStart(true)
                .create();

        Assertions.assertEquals("lpdwwexymzvlazi", response.location());
        Assertions.assertEquals("pwvqsgnyyu", response.tags().get("ziven"));
        Assertions.assertEquals(1891751992, response.sku().capacity());
        Assertions.assertEquals("uygdhgaqipirp", response.description());
        Assertions.assertEquals(779964189, response.scaleUnits());
        Assertions.assertEquals("qofu", response.availabilitySetName());
        Assertions.assertEquals(5746310488025921747L, response.maxCacheAge());
        Assertions.assertEquals("whc", response.customHostNames().get(0));
        Assertions.assertEquals(false, response.cdnEnabled());
        Assertions.assertEquals("t", response.cdnProvider());
        Assertions.assertEquals("oyin", response.cdnProfile());
        Assertions.assertEquals("cvcrrp", response.crossSiteAccessPolicies().clientAccessPolicy());
        Assertions.assertEquals("ttbst", response.crossSiteAccessPolicies().crossDomainPolicy());
    }

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