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

package com.azure.resourcemanager.workloads.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.workloads.WorkloadsManager;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
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 SapDatabaseInstancesDeleteMockTests {
    @Test
    public void testDelete() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"id\":\"lllibph\",\"name\":\"zmizakakan\",\"status\":\"jpdn\",\"percentComplete\":9.865725,\"startTime\":\"2021-03-02T00:11:10Z\",\"endTime\":\"2021-05-28T06:55:36Z\",\"operations\":[{\"id\":\"lmuoyxprimrsopte\",\"name\":\"jme\",\"status\":\"slstvasy\",\"percentComplete\":20.932108,\"startTime\":\"2021-03-08T01:37:13Z\",\"endTime\":\"2021-01-12T02:29:32Z\",\"operations\":[]},{\"id\":\"hguuf\",\"name\":\"boyjathwt\",\"status\":\"olbaemwmdx\",\"percentComplete\":92.43111,\"startTime\":\"2021-09-30T09:00:56Z\",\"endTime\":\"2021-10-19T05:13Z\",\"operations\":[]}]}";

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

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

        OperationStatusResult response =
            manager.sapDatabaseInstances().delete("qtgdqohmcwsl", "riz", "tpwb", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("lllibph", response.id());
        Assertions.assertEquals("zmizakakan", response.name());
        Assertions.assertEquals("jpdn", response.status());
        Assertions.assertEquals(9.865725F, response.percentComplete());
        Assertions.assertEquals(OffsetDateTime.parse("2021-03-02T00:11:10Z"), response.startTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-05-28T06:55:36Z"), response.endTime());
        Assertions.assertEquals("lmuoyxprimrsopte", response.operations().get(0).id());
        Assertions.assertEquals("jme", response.operations().get(0).name());
        Assertions.assertEquals("slstvasy", response.operations().get(0).status());
        Assertions.assertEquals(20.932108F, response.operations().get(0).percentComplete());
        Assertions.assertEquals(OffsetDateTime.parse("2021-03-08T01:37:13Z"), response.operations().get(0).startTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-01-12T02:29:32Z"), response.operations().get(0).endTime());
    }
}
