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

package com.azure.resourcemanager.datalakestore.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.datalakestore.DataLakeStoreManager;
import com.azure.resourcemanager.datalakestore.models.DataLakeStoreAccount;
import com.azure.resourcemanager.datalakestore.models.EncryptionConfig;
import com.azure.resourcemanager.datalakestore.models.EncryptionConfigType;
import com.azure.resourcemanager.datalakestore.models.EncryptionIdentity;
import com.azure.resourcemanager.datalakestore.models.EncryptionState;
import com.azure.resourcemanager.datalakestore.models.FirewallAllowAzureIpsState;
import com.azure.resourcemanager.datalakestore.models.FirewallState;
import com.azure.resourcemanager.datalakestore.models.TierType;
import com.azure.resourcemanager.datalakestore.models.TrustedIdProviderState;
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 AccountsCreateMockTests {
    @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 =
            "{\"identity\":{\"principalId\":\"a17186ff-d35b-4bd3-9614-5969b7152240\",\"tenantId\":\"91d96eca-fb1d-40ca-aa78-3b7373123724\"},\"properties\":{\"defaultGroup\":\"pgn\",\"encryptionConfig\":{\"type\":\"UserManaged\"},\"encryptionState\":\"Enabled\",\"encryptionProvisioningState\":\"Creating\",\"firewallRules\":[],\"virtualNetworkRules\":[],\"firewallState\":\"Enabled\",\"firewallAllowAzureIps\":\"Disabled\",\"trustedIdProviders\":[],\"trustedIdProviderState\":\"Enabled\",\"newTier\":\"Commitment_1PB\",\"currentTier\":\"Commitment_10TB\",\"accountId\":\"b39e2aaf-53a6-4ad3-a232-5a274c532e1b\",\"provisioningState\":\"Succeeded\",\"state\":\"Active\",\"creationTime\":\"2021-06-27T19:00:45Z\",\"lastModifiedTime\":\"2021-12-07T02:31:05Z\",\"endpoint\":\"plwzbhvgyugu\"},\"location\":\"svmkfssxquk\",\"tags\":{\"mg\":\"l\",\"vlopwiyighx\":\"xnkjzkdesl\",\"upedeojnabckhs\":\"kdwzbaiuebbaumny\"},\"id\":\"txp\",\"name\":\"ie\",\"type\":\"tfhvpesapskrdqmh\"}";

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

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

        DataLakeStoreAccount response =
            manager
                .accounts()
                .define("nrs")
                .withRegion("nlqidybyxczf")
                .withExistingResourceGroup("enbtkcxywny")
                .withTags(
                    mapOf("fkts", "aaxdbabphlwrq", "nyyazttbtwwrqpue", "hsucoc", "xibxujwbhqwalm", "ckzywbiexzfeyue"))
                .withIdentity(new EncryptionIdentity())
                .withDefaultGroup("epdkzja")
                .withEncryptionConfig(new EncryptionConfig().withType(EncryptionConfigType.USER_MANAGED))
                .withEncryptionState(EncryptionState.DISABLED)
                .withFirewallRules(Arrays.asList())
                .withVirtualNetworkRules(Arrays.asList())
                .withFirewallState(FirewallState.ENABLED)
                .withFirewallAllowAzureIps(FirewallAllowAzureIpsState.ENABLED)
                .withTrustedIdProviders(Arrays.asList())
                .withTrustedIdProviderState(TrustedIdProviderState.DISABLED)
                .withNewTier(TierType.COMMITMENT_10TB)
                .create();

        Assertions.assertEquals("svmkfssxquk", response.location());
        Assertions.assertEquals("l", response.tags().get("mg"));
    }

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