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

package com.azure.resourcemanager.signalr.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.signalr.SignalRManager;
import com.azure.resourcemanager.signalr.models.AclAction;
import com.azure.resourcemanager.signalr.models.LiveTraceConfiguration;
import com.azure.resourcemanager.signalr.models.ManagedIdentity;
import com.azure.resourcemanager.signalr.models.ManagedIdentityType;
import com.azure.resourcemanager.signalr.models.ResourceLogConfiguration;
import com.azure.resourcemanager.signalr.models.ResourceSku;
import com.azure.resourcemanager.signalr.models.ServerlessSettings;
import com.azure.resourcemanager.signalr.models.ServerlessUpstreamSettings;
import com.azure.resourcemanager.signalr.models.ServiceKind;
import com.azure.resourcemanager.signalr.models.SignalRCorsSettings;
import com.azure.resourcemanager.signalr.models.SignalRNetworkACLs;
import com.azure.resourcemanager.signalr.models.SignalRResource;
import com.azure.resourcemanager.signalr.models.SignalRSkuTier;
import com.azure.resourcemanager.signalr.models.SignalRTlsSettings;
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 SignalRsCreateOrUpdateMockTests {
    @Test
    public void testCreateOrUpdate() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"sku\":{\"name\":\"mzqhoftrmaequi\",\"tier\":\"Basic\",\"size\":\"cslfaoqzpiyylha\",\"family\":\"swhccsphk\",\"capacity\":185919180},\"properties\":{\"provisioningState\":\"Succeeded\",\"externalIP\":\"scywuggwoluhc\",\"hostName\":\"wem\",\"publicPort\":19957652,\"serverPort\":1127329459,\"version\":\"rgzdwmsweyp\",\"privateEndpointConnections\":[],\"sharedPrivateLinkResources\":[],\"tls\":{\"clientCertEnabled\":true},\"hostNamePrefix\":\"cnxqhuexmkttlst\",\"features\":[],\"liveTraceConfiguration\":{\"enabled\":\"emhzrncsdtc\",\"categories\":[]},\"resourceLogConfiguration\":{\"categories\":[]},\"cors\":{\"allowedOrigins\":[]},\"serverless\":{\"connectionTimeoutInSeconds\":1674447280},\"upstream\":{\"templates\":[]},\"networkACLs\":{\"defaultAction\":\"Allow\",\"privateEndpoints\":[]},\"publicNetworkAccess\":\"eadcygqukyhejhz\",\"disableLocalAuth\":true,\"disableAadAuth\":false},\"kind\":\"RawWebSockets\",\"identity\":{\"type\":\"None\",\"userAssignedIdentities\":{},\"principalId\":\"ksrpqv\",\"tenantId\":\"zraehtwd\"},\"location\":\"ftswibyrcdlbhsh\",\"tags\":{\"hevxcced\":\"racstwity\"},\"id\":\"pnmdyodnwzxltjcv\",\"name\":\"hlt\",\"type\":\"ugcxnavvwxq\"}";

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

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

        SignalRResource response =
            manager
                .signalRs()
                .define("zkoj")
                .withRegion("ogvbbejdcngq")
                .withExistingResourceGroup("wwa")
                .withTags(mapOf("wr", "akufgmjz", "u", "grtwae"))
                .withSku(new ResourceSku().withName("c").withTier(SignalRSkuTier.STANDARD).withCapacity(1660457179))
                .withKind(ServiceKind.RAW_WEB_SOCKETS)
                .withIdentity(
                    new ManagedIdentity()
                        .withType(ManagedIdentityType.SYSTEM_ASSIGNED)
                        .withUserAssignedIdentities(mapOf()))
                .withTls(new SignalRTlsSettings().withClientCertEnabled(false))
                .withFeatures(Arrays.asList())
                .withLiveTraceConfiguration(
                    new LiveTraceConfiguration().withEnabled("uximerqfobw").withCategories(Arrays.asList()))
                .withResourceLogConfiguration(new ResourceLogConfiguration().withCategories(Arrays.asList()))
                .withCors(new SignalRCorsSettings().withAllowedOrigins(Arrays.asList()))
                .withServerless(new ServerlessSettings().withConnectionTimeoutInSeconds(189482477))
                .withUpstream(new ServerlessUpstreamSettings().withTemplates(Arrays.asList()))
                .withNetworkACLs(
                    new SignalRNetworkACLs().withDefaultAction(AclAction.DENY).withPrivateEndpoints(Arrays.asList()))
                .withPublicNetworkAccess("hrskdsnfd")
                .withDisableLocalAuth(true)
                .withDisableAadAuth(true)
                .create();

        Assertions.assertEquals("ftswibyrcdlbhsh", response.location());
        Assertions.assertEquals("racstwity", response.tags().get("hevxcced"));
        Assertions.assertEquals("mzqhoftrmaequi", response.sku().name());
        Assertions.assertEquals(SignalRSkuTier.BASIC, response.sku().tier());
        Assertions.assertEquals(185919180, response.sku().capacity());
        Assertions.assertEquals(ServiceKind.RAW_WEB_SOCKETS, response.kind());
        Assertions.assertEquals(ManagedIdentityType.NONE, response.identity().type());
        Assertions.assertEquals(true, response.tls().clientCertEnabled());
        Assertions.assertEquals("emhzrncsdtc", response.liveTraceConfiguration().enabled());
        Assertions.assertEquals(1674447280, response.serverless().connectionTimeoutInSeconds());
        Assertions.assertEquals(AclAction.ALLOW, response.networkACLs().defaultAction());
        Assertions.assertEquals("eadcygqukyhejhz", response.publicNetworkAccess());
        Assertions.assertEquals(true, response.disableLocalAuth());
        Assertions.assertEquals(false, response.disableAadAuth());
    }

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