// 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 SapDatabaseInstancesStartInstanceMockTests {
    @Test
    public void testStartInstance() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"id\":\"lpmjerb\",\"name\":\"elvidizozsdbccx\",\"status\":\"monfdgnwncypuuww\",\"percentComplete\":0.8582592,\"startTime\":\"2021-11-20T18:00Z\",\"endTime\":\"2021-02-10T14:17:27Z\",\"operations\":[{\"id\":\"nkeifz\",\"name\":\"mkdasv\",\"status\":\"lyhb\",\"percentComplete\":95.53096,\"startTime\":\"2021-01-15T10:18:43Z\",\"endTime\":\"2021-12-10T05:26:35Z\",\"operations\":[]},{\"id\":\"oldforobw\",\"name\":\"vizbfhfo\",\"status\":\"vacqpbtuodxesz\",\"percentComplete\":87.85364,\"startTime\":\"2021-07-01T15:33:02Z\",\"endTime\":\"2021-08-31T22:37:44Z\",\"operations\":[]},{\"id\":\"slzkwrrwoycqu\",\"name\":\"yhahnomdrkyw\",\"status\":\"hpsvfu\",\"percentComplete\":58.89424,\"startTime\":\"2021-08-15T09:05:35Z\",\"endTime\":\"2021-11-04T07:23:46Z\",\"operations\":[]},{\"id\":\"alniexzsrzp\",\"name\":\"pqtybb\",\"status\":\"wpgdak\",\"percentComplete\":27.999931,\"startTime\":\"2021-03-31T06:49:58Z\",\"endTime\":\"2021-04-07T03:58:53Z\",\"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()
                .startInstance("k", "reljeamur", "zmlovuanash", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("lpmjerb", response.id());
        Assertions.assertEquals("elvidizozsdbccx", response.name());
        Assertions.assertEquals("monfdgnwncypuuww", response.status());
        Assertions.assertEquals(0.8582592F, response.percentComplete());
        Assertions.assertEquals(OffsetDateTime.parse("2021-11-20T18:00Z"), response.startTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-02-10T14:17:27Z"), response.endTime());
        Assertions.assertEquals("nkeifz", response.operations().get(0).id());
        Assertions.assertEquals("mkdasv", response.operations().get(0).name());
        Assertions.assertEquals("lyhb", response.operations().get(0).status());
        Assertions.assertEquals(95.53096F, response.operations().get(0).percentComplete());
        Assertions.assertEquals(OffsetDateTime.parse("2021-01-15T10:18:43Z"), response.operations().get(0).startTime());
        Assertions.assertEquals(OffsetDateTime.parse("2021-12-10T05:26:35Z"), response.operations().get(0).endTime());
    }
}
