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

package com.azure.resourcemanager.dataprotection.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.dataprotection.DataProtectionManager;
import com.azure.resourcemanager.dataprotection.models.AzureBackupJobResource;
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 JobsListMockTests {
    @Test
    public void testList() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"properties\":{\"activityID\":\"esvecuijpx\",\"backupInstanceFriendlyName\":\"xs\",\"backupInstanceId\":\"prtujwsawdd\",\"dataSourceId\":\"ibabxvititvtzeex\",\"dataSourceLocation\":\"vo\",\"dataSourceName\":\"tfgle\",\"dataSourceSetName\":\"m\",\"dataSourceType\":\"qbw\",\"duration\":\"pqtgsfjac\",\"endTime\":\"2021-12-08T10:07:16Z\",\"errorDetails\":[],\"extendedInfo\":{\"additionalDetails\":{},\"backupInstanceState\":\"bxv\",\"dataTransferredInBytes\":31.139800233304584,\"recoveryDestination\":\"sirudhzmmesckdlp\",\"subTasks\":[]},\"isUserTriggered\":true,\"operation\":\"x\",\"operationCategory\":\"a\",\"policyId\":\"cfxwmdbox\",\"policyName\":\"gsftufqobrjlnacg\",\"progressEnabled\":true,\"progressUrl\":\"knh\",\"restoreType\":\"izvy\",\"sourceResourceGroup\":\"nrzvuljraaer\",\"sourceSubscriptionID\":\"nok\",\"startTime\":\"2021-08-09T04:53:10Z\",\"status\":\"ukkjqnvbroyla\",\"subscriptionId\":\"xulcdisdos\",\"supportedActions\":[\"b\",\"svgjrwhryvy\",\"ytdc\"],\"vaultName\":\"xgccknfnw\",\"etag\":\"tmvpdvjdhtt\",\"sourceDataStoreName\":\"efedxihchrphkm\",\"destinationDataStoreName\":\"jdqnsdfzpbgt\"},\"id\":\"ylkdghrje\",\"name\":\"utlwxezwzhok\",\"type\":\"bwnhhtql\"}]}";

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

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

        PagedIterable<AzureBackupJobResource> response =
            manager.jobs().list("ekkkzd", "rtkgdojbmxvavref", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("esvecuijpx", response.iterator().next().properties().activityId());
        Assertions.assertEquals("xs", response.iterator().next().properties().backupInstanceFriendlyName());
        Assertions.assertEquals("ibabxvititvtzeex", response.iterator().next().properties().dataSourceId());
        Assertions.assertEquals("vo", response.iterator().next().properties().dataSourceLocation());
        Assertions.assertEquals("tfgle", response.iterator().next().properties().dataSourceName());
        Assertions.assertEquals("m", response.iterator().next().properties().dataSourceSetName());
        Assertions.assertEquals("qbw", response.iterator().next().properties().dataSourceType());
        Assertions.assertEquals("pqtgsfjac", response.iterator().next().properties().duration());
        Assertions.assertEquals(true, response.iterator().next().properties().isUserTriggered());
        Assertions.assertEquals("x", response.iterator().next().properties().operation());
        Assertions.assertEquals("a", response.iterator().next().properties().operationCategory());
        Assertions.assertEquals(true, response.iterator().next().properties().progressEnabled());
        Assertions.assertEquals("nrzvuljraaer", response.iterator().next().properties().sourceResourceGroup());
        Assertions.assertEquals("nok", response.iterator().next().properties().sourceSubscriptionId());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-08-09T04:53:10Z"), response.iterator().next().properties().startTime());
        Assertions.assertEquals("ukkjqnvbroyla", response.iterator().next().properties().status());
        Assertions.assertEquals("xulcdisdos", response.iterator().next().properties().subscriptionId());
        Assertions.assertEquals("b", response.iterator().next().properties().supportedActions().get(0));
        Assertions.assertEquals("xgccknfnw", response.iterator().next().properties().vaultName());
        Assertions.assertEquals("tmvpdvjdhtt", response.iterator().next().properties().etag());
        Assertions.assertEquals("efedxihchrphkm", response.iterator().next().properties().sourceDataStoreName());
        Assertions.assertEquals("jdqnsdfzpbgt", response.iterator().next().properties().destinationDataStoreName());
    }
}
