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

package com.azure.resourcemanager.recoveryservices.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.recoveryservices.RecoveryServicesManager;
import com.azure.resourcemanager.recoveryservices.models.IdentityData;
import com.azure.resourcemanager.recoveryservices.models.InfrastructureEncryptionState;
import com.azure.resourcemanager.recoveryservices.models.MonitoringSettings;
import com.azure.resourcemanager.recoveryservices.models.PublicNetworkAccess;
import com.azure.resourcemanager.recoveryservices.models.ResourceIdentityType;
import com.azure.resourcemanager.recoveryservices.models.RestoreSettings;
import com.azure.resourcemanager.recoveryservices.models.SecuritySettings;
import com.azure.resourcemanager.recoveryservices.models.Sku;
import com.azure.resourcemanager.recoveryservices.models.SkuName;
import com.azure.resourcemanager.recoveryservices.models.UpgradeDetails;
import com.azure.resourcemanager.recoveryservices.models.Vault;
import com.azure.resourcemanager.recoveryservices.models.VaultProperties;
import com.azure.resourcemanager.recoveryservices.models.VaultPropertiesEncryption;
import com.azure.resourcemanager.recoveryservices.models.VaultPropertiesMoveDetails;
import com.azure.resourcemanager.recoveryservices.models.VaultPropertiesRedundancySettings;
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 VaultsCreateOrUpdateMockTests {
    @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 =
            "{\"identity\":{\"principalId\":\"swsrms\",\"tenantId\":\"zrpzb\",\"type\":\"UserAssigned\",\"userAssignedIdentities\":{}},\"properties\":{\"provisioningState\":\"Succeeded\",\"upgradeDetails\":{\"operationId\":\"ked\",\"startTimeUtc\":\"2021-11-25T08:52:54Z\",\"lastUpdatedTimeUtc\":\"2021-11-10T14:09:39Z\",\"endTimeUtc\":\"2021-10-29T12:30:56Z\",\"status\":\"Failed\",\"message\":\"bzyh\",\"triggerType\":\"ForcedUpgrade\",\"upgradedResourceId\":\"mypyynpcdpu\",\"previousResourceId\":\"zgmwznmabikns\"},\"privateEndpointConnections\":[],\"privateEndpointStateForBackup\":\"None\",\"privateEndpointStateForSiteRecovery\":\"None\",\"encryption\":{\"infrastructureEncryption\":\"Disabled\"},\"moveDetails\":{\"operationId\":\"lkdmtncvokotllxd\",\"startTimeUtc\":\"2021-07-23T09:43:53Z\",\"completionTimeUtc\":\"2021-07-18T04:54:54Z\",\"sourceResourceId\":\"cogjltdtbn\",\"targetResourceId\":\"adoocrkvc\"},\"moveState\":\"CriticalFailure\",\"backupStorageVersion\":\"Unassigned\",\"publicNetworkAccess\":\"Disabled\",\"monitoringSettings\":{},\"restoreSettings\":{},\"redundancySettings\":{\"standardTierStorageRedundancy\":\"GeoRedundant\",\"crossRegionRestore\":\"Enabled\"},\"securitySettings\":{}},\"sku\":{\"name\":\"RS0\",\"tier\":\"xkalla\",\"family\":\"elwuipi\",\"size\":\"jzkzi\",\"capacity\":\"vvcnayr\"},\"etag\":\"nxxmueedndrdv\",\"location\":\"tkwqqtchealm\",\"tags\":{\"aygdvwvgpioh\":\"d\",\"udxepxgyqagv\":\"wxrt\",\"wi\":\"vmnpkukghimdblx\"},\"id\":\"fnjhfjxwmszkkfo\",\"name\":\"rey\",\"type\":\"kzikfjawneaivxwc\"}";

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

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

        Vault response =
            manager
                .vaults()
                .define("tddckcb")
                .withRegion("uijfqk")
                .withExistingResourceGroup("apvhelxprgly")
                .withTags(mapOf("ibwwiftohqkv", "wiipfpub"))
                .withIdentity(
                    new IdentityData().withType(ResourceIdentityType.NONE).withUserAssignedIdentities(mapOf()))
                .withProperties(
                    new VaultProperties()
                        .withUpgradeDetails(new UpgradeDetails())
                        .withEncryption(
                            new VaultPropertiesEncryption()
                                .withInfrastructureEncryption(InfrastructureEncryptionState.ENABLED))
                        .withMoveDetails(new VaultPropertiesMoveDetails())
                        .withPublicNetworkAccess(PublicNetworkAccess.DISABLED)
                        .withMonitoringSettings(new MonitoringSettings())
                        .withRestoreSettings(new RestoreSettings())
                        .withRedundancySettings(new VaultPropertiesRedundancySettings())
                        .withSecuritySettings(new SecuritySettings()))
                .withSku(
                    new Sku()
                        .withName(SkuName.RS0)
                        .withTier("vvqfovljxyws")
                        .withFamily("syrsndsytgadgvra")
                        .withSize("en")
                        .withCapacity("nzar"))
                .withEtag("q")
                .create();

        Assertions.assertEquals("tkwqqtchealm", response.location());
        Assertions.assertEquals("d", response.tags().get("aygdvwvgpioh"));
        Assertions.assertEquals(ResourceIdentityType.USER_ASSIGNED, response.identity().type());
        Assertions
            .assertEquals(
                InfrastructureEncryptionState.DISABLED, response.properties().encryption().infrastructureEncryption());
        Assertions.assertEquals(PublicNetworkAccess.DISABLED, response.properties().publicNetworkAccess());
        Assertions.assertEquals(SkuName.RS0, response.sku().name());
        Assertions.assertEquals("xkalla", response.sku().tier());
        Assertions.assertEquals("elwuipi", response.sku().family());
        Assertions.assertEquals("jzkzi", response.sku().size());
        Assertions.assertEquals("vvcnayr", response.sku().capacity());
        Assertions.assertEquals("nxxmueedndrdv", response.etag());
    }

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