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

package com.azure.resourcemanager.recoveryservicesbackup.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.recoveryservicesbackup.RecoveryServicesBackupManager;
import com.azure.resourcemanager.recoveryservicesbackup.models.CreateMode;
import com.azure.resourcemanager.recoveryservicesbackup.models.ProtectedItemResource;
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 ProtectedItemOperationResultsGetWithResponseMockTests {
    @Test
    public void testGetWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"protectedItemType\":\"ProtectedItem\",\"backupManagementType\":\"DefaultBackup\",\"workloadType\":\"SQLDataBase\",\"containerName\":\"cbgydlqi\",\"sourceResourceId\":\"wmhmptyrilkfbnr\",\"policyId\":\"xvztpbnfnqtxj\",\"lastRecoveryPoint\":\"2021-07-24T21:24:44Z\",\"backupSetName\":\"lswbnf\",\"createMode\":\"Default\",\"deferredDeleteTimeInUTC\":\"2021-04-15T08:17:41Z\",\"isScheduledForDeferredDelete\":true,\"deferredDeleteTimeRemaining\":\"jnsfzygleexahvmy\",\"isDeferredDeleteScheduleUpcoming\":false,\"isRehydrate\":true,\"resourceGuardOperationRequests\":[\"ryc\",\"jjzyvoaqajuve\",\"zptdmkrrbh\",\"pfulube\"],\"isArchiveEnabled\":false,\"policyName\":\"pmfbfununmpzkr\",\"softDeleteRetentionPeriod\":988456755},\"eTag\":\"fkd\",\"location\":\"chlzvfi\",\"tags\":{\"hbkgfyrt\":\"kjjwgcwn\",\"mjpjscdfpdqwty\":\"gm\",\"wmseharxifvqn\":\"ev\",\"mbpjptnvwjh\":\"x\"},\"id\":\"sidqpxlbtpakft\",\"name\":\"gatwmykyu\",\"type\":\"rymdwmf\"}";

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

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

        ProtectedItemResource response =
            manager
                .protectedItemOperationResults()
                .getWithResponse("zvkiwrsiwdy", "quryk", "rra", "eek", "s", "eh", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals("chlzvfi", response.location());
        Assertions.assertEquals("kjjwgcwn", response.tags().get("hbkgfyrt"));
        Assertions.assertEquals("cbgydlqi", response.properties().containerName());
        Assertions.assertEquals("wmhmptyrilkfbnr", response.properties().sourceResourceId());
        Assertions.assertEquals("xvztpbnfnqtxj", response.properties().policyId());
        Assertions
            .assertEquals(OffsetDateTime.parse("2021-07-24T21:24:44Z"), response.properties().lastRecoveryPoint());
        Assertions.assertEquals("lswbnf", response.properties().backupSetName());
        Assertions.assertEquals(CreateMode.DEFAULT, response.properties().createMode());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-04-15T08:17:41Z"), response.properties().deferredDeleteTimeInUtc());
        Assertions.assertEquals(true, response.properties().isScheduledForDeferredDelete());
        Assertions.assertEquals("jnsfzygleexahvmy", response.properties().deferredDeleteTimeRemaining());
        Assertions.assertEquals(false, response.properties().isDeferredDeleteScheduleUpcoming());
        Assertions.assertEquals(true, response.properties().isRehydrate());
        Assertions.assertEquals("ryc", response.properties().resourceGuardOperationRequests().get(0));
        Assertions.assertEquals(false, response.properties().isArchiveEnabled());
        Assertions.assertEquals("pmfbfununmpzkr", response.properties().policyName());
        Assertions.assertEquals(988456755, response.properties().softDeleteRetentionPeriod());
        Assertions.assertEquals("fkd", response.etag());
    }
}
