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

package com.azure.resourcemanager.redisenterprise.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.redisenterprise.RedisEnterpriseManager;
import com.azure.resourcemanager.redisenterprise.models.AofFrequency;
import com.azure.resourcemanager.redisenterprise.models.ClusteringPolicy;
import com.azure.resourcemanager.redisenterprise.models.Database;
import com.azure.resourcemanager.redisenterprise.models.DatabasePropertiesGeoReplication;
import com.azure.resourcemanager.redisenterprise.models.EvictionPolicy;
import com.azure.resourcemanager.redisenterprise.models.Persistence;
import com.azure.resourcemanager.redisenterprise.models.Protocol;
import com.azure.resourcemanager.redisenterprise.models.RdbFrequency;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
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 DatabasesCreateMockTests {
    @Test
    public void testCreate() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"clientProtocol\":\"Encrypted\",\"port\":1688847935,\"provisioningState\":\"Succeeded\",\"resourceState\":\"CreateFailed\",\"clusteringPolicy\":\"OSSCluster\",\"evictionPolicy\":\"VolatileLFU\",\"persistence\":{\"aofEnabled\":true,\"rdbEnabled\":false,\"aofFrequency\":\"1s\",\"rdbFrequency\":\"12h\"},\"modules\":[],\"geoReplication\":{\"groupNickname\":\"qaqtdoqmcbxvwvxy\",\"linkedDatabases\":[]}},\"id\":\"bhsfxob\",\"name\":\"ytkblmpew\",\"type\":\"wfbkrvrns\"}";

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

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

        Database response =
            manager
                .databases()
                .define("pfqbuaceopzf")
                .withExistingRedisEnterprise("pkteo", "llwptfdy")
                .withClientProtocol(Protocol.ENCRYPTED)
                .withPort(350693240)
                .withClusteringPolicy(ClusteringPolicy.ENTERPRISE_CLUSTER)
                .withEvictionPolicy(EvictionPolicy.VOLATILE_LFU)
                .withPersistence(
                    new Persistence()
                        .withAofEnabled(true)
                        .withRdbEnabled(true)
                        .withAofFrequency(AofFrequency.ONES)
                        .withRdbFrequency(RdbFrequency.ONEH))
                .withModules(Arrays.asList())
                .withGeoReplication(
                    new DatabasePropertiesGeoReplication()
                        .withGroupNickname("gbkdmoizpos")
                        .withLinkedDatabases(Arrays.asList()))
                .create();

        Assertions.assertEquals(Protocol.ENCRYPTED, response.clientProtocol());
        Assertions.assertEquals(1688847935, response.port());
        Assertions.assertEquals(ClusteringPolicy.OSSCLUSTER, response.clusteringPolicy());
        Assertions.assertEquals(EvictionPolicy.VOLATILE_LFU, response.evictionPolicy());
        Assertions.assertEquals(true, response.persistence().aofEnabled());
        Assertions.assertEquals(false, response.persistence().rdbEnabled());
        Assertions.assertEquals(AofFrequency.ONES, response.persistence().aofFrequency());
        Assertions.assertEquals(RdbFrequency.ONE_TWOH, response.persistence().rdbFrequency());
        Assertions.assertEquals("qaqtdoqmcbxvwvxy", response.geoReplication().groupNickname());
    }
}
