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

package com.azure.resourcemanager.deploymentmanager.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.deploymentmanager.DeploymentManager;
import com.azure.resourcemanager.deploymentmanager.models.Rollout;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.List;
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 RolloutsListWithResponseMockTests {
    @Test
    public void testListWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "[{\"identity\":{\"type\":\"jhwuaanozjos\",\"identityIds\":[\"youlp\",\"rvxaglrvimjwosy\"]},\"properties\":{\"status\":\"tcs\",\"totalRetryAttempts\":1365275067,\"services\":[],\"buildVersion\":\"umiekkezzi\",\"artifactSourceId\":\"ly\",\"targetServiceTopologyId\":\"jhdgqggebdunyga\",\"stepGroups\":[]},\"location\":\"idb\",\"tags\":{\"xllrxcyjm\":\"t\",\"su\":\"a\"},\"id\":\"arm\",\"name\":\"wdmjsjqbjhhyx\",\"type\":\"rw\"},{\"identity\":{\"type\":\"co\",\"identityIds\":[\"hp\",\"xkgymareqnajxqu\",\"jhkycub\"]},\"properties\":{\"status\":\"gssofwq\",\"totalRetryAttempts\":761384571,\"services\":[],\"buildVersion\":\"rmnjijpx\",\"artifactSourceId\":\"q\",\"targetServiceTopologyId\":\"udfnbyxba\",\"stepGroups\":[]},\"location\":\"bjyvay\",\"tags\":{\"zrtuzq\":\"m\",\"fdnw\":\"gsexne\",\"euzsoi\":\"wmewzsyy\"},\"id\":\"judpfrxt\",\"name\":\"thzvaytdwkqbrqu\",\"type\":\"paxh\"},{\"identity\":{\"type\":\"iilivpdtiirqtd\",\"identityIds\":[\"axoruzfgsquy\",\"xrxxlep\",\"ramxjezwlwnw\"]},\"properties\":{\"status\":\"lcvydy\",\"totalRetryAttempts\":1772856853,\"services\":[],\"buildVersion\":\"aojkniodk\",\"artifactSourceId\":\"ebwnujhe\",\"targetServiceTopologyId\":\"msbvdkcrodtjinf\",\"stepGroups\":[]},\"location\":\"lfltka\",\"tags\":{\"akggkfpag\":\"efkdlf\",\"lyls\":\"owpulpq\",\"agxsdszuemps\":\"xkqjnsjervt\",\"nqicvinvkjjxdxrb\":\"zkfzbeyv\"},\"id\":\"ukzclewyhmlwpaz\",\"name\":\"zpof\",\"type\":\"cckwyfzqwhxxbu\"},{\"identity\":{\"type\":\"a\",\"identityIds\":[\"feqztppriol\",\"or\",\"altol\"]},\"properties\":{\"status\":\"wsobqwcsdbn\",\"totalRetryAttempts\":297328779,\"services\":[],\"buildVersion\":\"cqdpfuv\",\"artifactSourceId\":\"sbjjc\",\"targetServiceTopologyId\":\"nvxbvt\",\"stepGroups\":[]},\"location\":\"dut\",\"tags\":{\"dflvkg\":\"rmrlxqtvcof\",\"gdknnqv\":\"u\",\"sgsahmkycgr\":\"aznqntoru\",\"s\":\"uwjuetaeburuvdmo\"},\"id\":\"zlxwabmqoefkifr\",\"name\":\"tpuqujmq\",\"type\":\"gkfbtndoaong\"}]";

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

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

        List<Rollout> response =
            manager.rollouts().listWithResponse("fatkld", com.azure.core.util.Context.NONE).getValue();

        Assertions.assertEquals("idb", response.get(0).location());
        Assertions.assertEquals("t", response.get(0).tags().get("xllrxcyjm"));
        Assertions.assertEquals("jhwuaanozjos", response.get(0).identity().type());
        Assertions.assertEquals("youlp", response.get(0).identity().identityIds().get(0));
        Assertions.assertEquals("umiekkezzi", response.get(0).buildVersion());
        Assertions.assertEquals("ly", response.get(0).artifactSourceId());
        Assertions.assertEquals("jhdgqggebdunyga", response.get(0).targetServiceTopologyId());
    }
}
