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

package com.azure.resourcemanager.batch.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.batch.BatchManager;
import com.azure.resourcemanager.batch.models.BatchPoolIdentity;
import com.azure.resourcemanager.batch.models.ComputeNodeFillType;
import com.azure.resourcemanager.batch.models.DeploymentConfiguration;
import com.azure.resourcemanager.batch.models.DynamicVNetAssignmentScope;
import com.azure.resourcemanager.batch.models.InterNodeCommunicationState;
import com.azure.resourcemanager.batch.models.NetworkConfiguration;
import com.azure.resourcemanager.batch.models.NodeCommunicationMode;
import com.azure.resourcemanager.batch.models.Pool;
import com.azure.resourcemanager.batch.models.PoolIdentityType;
import com.azure.resourcemanager.batch.models.ScaleSettings;
import com.azure.resourcemanager.batch.models.StartTask;
import com.azure.resourcemanager.batch.models.TaskSchedulingPolicy;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
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 PoolsCreateWithResponseMockTests {
    @Test
    public void testCreateWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"displayName\":\"spuunnoxyhkxgq\",\"lastModified\":\"2021-07-24T05:54:49Z\",\"creationTime\":\"2020-12-28T12:46:28Z\",\"provisioningState\":\"Succeeded\",\"provisioningStateTransitionTime\":\"2021-05-06T08:51:47Z\",\"allocationState\":\"Steady\",\"allocationStateTransitionTime\":\"2021-10-10T08:04:34Z\",\"vmSize\":\"wdaomdjvlpjxxkzb\",\"deploymentConfiguration\":{},\"currentDedicatedNodes\":1225406633,\"currentLowPriorityNodes\":1985792970,\"scaleSettings\":{},\"autoScaleRun\":{\"evaluationTime\":\"2021-02-04T06:42:43Z\",\"results\":\"ncj\"},\"interNodeCommunication\":\"Enabled\",\"networkConfiguration\":{\"subnetId\":\"oggculapz\",\"dynamicVnetAssignmentScope\":\"job\"},\"taskSlotsPerNode\":1569571564,\"taskSchedulingPolicy\":{\"nodeFillType\":\"Pack\"},\"userAccounts\":[],\"metadata\":[],\"startTask\":{\"commandLine\":\"b\",\"resourceFiles\":[],\"environmentSettings\":[],\"maxTaskRetryCount\":781075850,\"waitForSuccess\":false},\"certificates\":[],\"applicationPackages\":[],\"applicationLicenses\":[\"vfcibyfmowux\",\"kjpvdwxf\",\"wiivwzjbhyzsx\"],\"resizeOperationStatus\":{\"targetDedicatedNodes\":300385433,\"targetLowPriorityNodes\":2109306549,\"resizeTimeout\":\"PT11H6M1S\",\"nodeDeallocationOption\":\"Requeue\",\"startTime\":\"2021-03-06T04:34:10Z\",\"errors\":[]},\"mountConfiguration\":[],\"targetNodeCommunicationMode\":\"Classic\",\"currentNodeCommunicationMode\":\"Classic\"},\"identity\":{\"type\":\"UserAssigned\",\"userAssignedIdentities\":{}},\"etag\":\"astjbkkdmflvestm\",\"id\":\"lx\",\"name\":\"ril\",\"type\":\"zapeewchpx\"}";

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

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

        Pool response =
            manager
                .pools()
                .define("loazuruocbgoo")
                .withExistingBatchAccount("xkdmligo", "ibrxkp")
                .withIdentity(
                    new BatchPoolIdentity()
                        .withType(PoolIdentityType.USER_ASSIGNED)
                        .withUserAssignedIdentities(mapOf()))
                .withDisplayName("eoybfhjxakvvjgs")
                .withVmSize("xnyed")
                .withDeploymentConfiguration(new DeploymentConfiguration())
                .withScaleSettings(new ScaleSettings())
                .withInterNodeCommunication(InterNodeCommunicationState.ENABLED)
                .withNetworkConfiguration(
                    new NetworkConfiguration()
                        .withSubnetId("ybvpay")
                        .withDynamicVnetAssignmentScope(DynamicVNetAssignmentScope.JOB))
                .withTaskSlotsPerNode(417091375)
                .withTaskSchedulingPolicy(new TaskSchedulingPolicy().withNodeFillType(ComputeNodeFillType.SPREAD))
                .withUserAccounts(Arrays.asList())
                .withMetadata(Arrays.asList())
                .withStartTask(
                    new StartTask()
                        .withCommandLine("gstcyohpf")
                        .withResourceFiles(Arrays.asList())
                        .withEnvironmentSettings(Arrays.asList())
                        .withMaxTaskRetryCount(172770191)
                        .withWaitForSuccess(true))
                .withCertificates(Arrays.asList())
                .withApplicationPackages(Arrays.asList())
                .withApplicationLicenses(Arrays.asList("nwqjnoba", "yhddvia"))
                .withMountConfiguration(Arrays.asList())
                .withTargetNodeCommunicationMode(NodeCommunicationMode.SIMPLIFIED)
                .withIfMatch("fcvlerch")
                .withIfNoneMatch("qbmfpjbabwidf")
                .create();

        Assertions.assertEquals(PoolIdentityType.USER_ASSIGNED, response.identity().type());
        Assertions.assertEquals("spuunnoxyhkxgq", response.displayName());
        Assertions.assertEquals("wdaomdjvlpjxxkzb", response.vmSize());
        Assertions.assertEquals(InterNodeCommunicationState.ENABLED, response.interNodeCommunication());
        Assertions.assertEquals("oggculapz", response.networkConfiguration().subnetId());
        Assertions
            .assertEquals(DynamicVNetAssignmentScope.JOB, response.networkConfiguration().dynamicVnetAssignmentScope());
        Assertions.assertEquals(1569571564, response.taskSlotsPerNode());
        Assertions.assertEquals(ComputeNodeFillType.PACK, response.taskSchedulingPolicy().nodeFillType());
        Assertions.assertEquals("b", response.startTask().commandLine());
        Assertions.assertEquals(781075850, response.startTask().maxTaskRetryCount());
        Assertions.assertEquals(false, response.startTask().waitForSuccess());
        Assertions.assertEquals("vfcibyfmowux", response.applicationLicenses().get(0));
        Assertions.assertEquals(NodeCommunicationMode.CLASSIC, response.targetNodeCommunicationMode());
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
