// 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.http.rest.PagedIterable;
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.InfrastructureEncryptionState;
import com.azure.resourcemanager.recoveryservices.models.PublicNetworkAccess;
import com.azure.resourcemanager.recoveryservices.models.ResourceIdentityType;
import com.azure.resourcemanager.recoveryservices.models.SkuName;
import com.azure.resourcemanager.recoveryservices.models.Vault;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
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 VaultsListByResourceGroupMockTests {
    @Test
    public void testListByResourceGroup() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"identity\":{\"principalId\":\"d\",\"tenantId\":\"wyhzdx\",\"type\":\"None\",\"userAssignedIdentities\":{}},\"properties\":{\"provisioningState\":\"mnvdfzn\",\"upgradeDetails\":{\"operationId\":\"od\",\"startTimeUtc\":\"2021-09-09T21:04:39Z\",\"lastUpdatedTimeUtc\":\"2021-01-24T14:26:11Z\",\"endTimeUtc\":\"2021-10-12T01:30:58Z\",\"status\":\"Failed\",\"message\":\"pstdbhhxsrzd\",\"triggerType\":\"UserTriggered\",\"upgradedResourceId\":\"rsc\",\"previousResourceId\":\"t\"},\"privateEndpointConnections\":[],\"privateEndpointStateForBackup\":\"Enabled\",\"privateEndpointStateForSiteRecovery\":\"Enabled\",\"encryption\":{\"infrastructureEncryption\":\"Enabled\"},\"moveDetails\":{\"operationId\":\"lswtmweriofzpyqs\",\"startTimeUtc\":\"2021-06-06T05:20:48Z\",\"completionTimeUtc\":\"2021-07-25T08:24:12Z\",\"sourceResourceId\":\"ets\",\"targetResourceId\":\"szhedplvw\"},\"moveState\":\"MoveSucceeded\",\"backupStorageVersion\":\"V1\",\"publicNetworkAccess\":\"Enabled\",\"monitoringSettings\":{},\"restoreSettings\":{},\"redundancySettings\":{\"standardTierStorageRedundancy\":\"ZoneRedundant\",\"crossRegionRestore\":\"Enabled\"},\"securitySettings\":{}},\"sku\":{\"name\":\"Standard\",\"tier\":\"cxogaokonzm\",\"family\":\"ikvmkqzeqqk\",\"size\":\"tfz\",\"capacity\":\"hhvh\"},\"etag\":\"eodkwobda\",\"location\":\"xtibqdxbxwakbog\",\"tags\":{\"lbpodxunk\":\"dlkzgxhuri\",\"lrb\":\"ebxmubyynt\",\"l\":\"tkoievseotgq\"},\"id\":\"tmuwlauwzi\",\"name\":\"xbmp\",\"type\":\"cjefuzmu\"}]}";

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

        PagedIterable<Vault> response =
            manager.vaults().listByResourceGroup("dnbbglzps", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("xtibqdxbxwakbog", response.iterator().next().location());
        Assertions.assertEquals("dlkzgxhuri", response.iterator().next().tags().get("lbpodxunk"));
        Assertions.assertEquals(ResourceIdentityType.NONE, response.iterator().next().identity().type());
        Assertions
            .assertEquals(
                InfrastructureEncryptionState.ENABLED,
                response.iterator().next().properties().encryption().infrastructureEncryption());
        Assertions
            .assertEquals(PublicNetworkAccess.ENABLED, response.iterator().next().properties().publicNetworkAccess());
        Assertions.assertEquals(SkuName.STANDARD, response.iterator().next().sku().name());
        Assertions.assertEquals("cxogaokonzm", response.iterator().next().sku().tier());
        Assertions.assertEquals("ikvmkqzeqqk", response.iterator().next().sku().family());
        Assertions.assertEquals("tfz", response.iterator().next().sku().size());
        Assertions.assertEquals("hhvh", response.iterator().next().sku().capacity());
        Assertions.assertEquals("eodkwobda", response.iterator().next().etag());
    }
}
