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

package com.azure.resourcemanager.cognitiveservices.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.cognitiveservices.CognitiveServicesManager;
import com.azure.resourcemanager.cognitiveservices.models.Deployment;
import com.azure.resourcemanager.cognitiveservices.models.DeploymentModel;
import com.azure.resourcemanager.cognitiveservices.models.DeploymentProperties;
import com.azure.resourcemanager.cognitiveservices.models.DeploymentScaleSettings;
import com.azure.resourcemanager.cognitiveservices.models.DeploymentScaleType;
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 DeploymentsCreateOrUpdateMockTests {
    @Test
    public void testCreateOrUpdate() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"etag\":\"trnegvmnvuqeqvld\",\"properties\":{\"provisioningState\":\"Succeeded\",\"model\":{\"format\":\"bkkd\",\"name\":\"lves\",\"version\":\"jlxr\"},\"scaleSettings\":{\"scaleType\":\"Manual\",\"capacity\":646254417,\"activeCapacity\":848547164},\"capabilities\":{\"evufuztck\":\"hpxlktwkuziycs\",\"wmmrq\":\"yhjtqedcgzu\",\"zgkrvqe\":\"zrrjvpgly\"},\"raiPolicyName\":\"toepryu\",\"callRateLimit\":{\"count\":13.608098,\"renewalPeriod\":23.045176,\"rules\":[]}},\"id\":\"dmovzvfva\",\"name\":\"wzqa\",\"type\":\"f\"}";

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

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

        Deployment response =
            manager
                .deployments()
                .define("widf")
                .withExistingAccount("uqypfcvle", "chpqbmfpjba")
                .withProperties(
                    new DeploymentProperties()
                        .withModel(
                            new DeploymentModel()
                                .withFormat("fhoqca")
                                .withName("wdaomdjvlpjxxkzb")
                                .withVersion("sgeivsiy"))
                        .withScaleSettings(
                            new DeploymentScaleSettings()
                                .withScaleType(DeploymentScaleType.STANDARD)
                                .withCapacity(1333047825))
                        .withRaiPolicyName("lvofqzhvfcibyfmo"))
                .create();

        Assertions.assertEquals("bkkd", response.properties().model().format());
        Assertions.assertEquals("lves", response.properties().model().name());
        Assertions.assertEquals("jlxr", response.properties().model().version());
        Assertions.assertEquals(DeploymentScaleType.MANUAL, response.properties().scaleSettings().scaleType());
        Assertions.assertEquals(646254417, response.properties().scaleSettings().capacity());
        Assertions.assertEquals("toepryu", response.properties().raiPolicyName());
    }
}
