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

package com.azure.resourcemanager.logz.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.logz.LogzManager;
import com.azure.resourcemanager.logz.fluent.models.LogzMonitorResourceInner;
import com.azure.resourcemanager.logz.models.IdentityProperties;
import com.azure.resourcemanager.logz.models.LogzMonitorResource;
import com.azure.resourcemanager.logz.models.LogzOrganizationProperties;
import com.azure.resourcemanager.logz.models.ManagedIdentityTypes;
import com.azure.resourcemanager.logz.models.MarketplaceSubscriptionStatus;
import com.azure.resourcemanager.logz.models.MonitorProperties;
import com.azure.resourcemanager.logz.models.MonitoringStatus;
import com.azure.resourcemanager.logz.models.PlanData;
import com.azure.resourcemanager.logz.models.UserInfo;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
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 SubAccountsCreateMockTests {
    @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\":{\"provisioningState\":\"Succeeded\",\"monitoringStatus\":\"Enabled\",\"marketplaceSubscriptionStatus\":\"Active\",\"logzOrganizationProperties\":{\"companyName\":\"zdnds\",\"id\":\"nayqi\",\"enterpriseAppId\":\"nduhavhqlkthum\",\"singleSignOnUrl\":\"olbgycduiertgccy\"},\"userInfo\":{\"firstName\":\"olpsslqlf\",\"lastName\":\"dnbbglzps\",\"emailAddress\":\"ydmcwyhzdxssa\",\"phoneNumber\":\"zmnvdfznudaod\"},\"planData\":{\"usageType\":\"bncblylpstdbhhx\",\"billingCycle\":\"zdzucerscdntnevf\",\"planDetails\":\"jmygtdsslswtmwer\",\"effectiveDate\":\"2021-09-22T20:42:28Z\"},\"liftrResourceCategory\":\"Unknown\",\"liftrResourcePreference\":158092972},\"identity\":{\"principalId\":\"mwabnetshhszhedp\",\"tenantId\":\"wiwubm\",\"type\":\"UserAssigned\"},\"location\":\"esl\",\"tags\":{\"lcxog\":\"wwtppj\"},\"id\":\"okonzmnsikvmkqz\",\"name\":\"qqkdltfzxmhhvhgu\",\"type\":\"eodkwobda\"}";

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

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

        LogzMonitorResource response =
            manager
                .subAccounts()
                .create(
                    "ol",
                    "dahzxctobg",
                    "kdmoi",
                    new LogzMonitorResourceInner()
                        .withLocation("rfbjf")
                        .withTags(mapOf("zbexilzznfqqnvw", "ssotftpv"))
                        .withProperties(
                            new MonitorProperties()
                                .withMonitoringStatus(MonitoringStatus.ENABLED)
                                .withMarketplaceSubscriptionStatus(MarketplaceSubscriptionStatus.ACTIVE)
                                .withLogzOrganizationProperties(
                                    new LogzOrganizationProperties()
                                        .withCompanyName("unrmfqjhhkxb")
                                        .withEnterpriseAppId("hx")
                                        .withSingleSignOnUrl("yngudivk"))
                                .withUserInfo(
                                    new UserInfo()
                                        .withFirstName("wbxqzvszjfau")
                                        .withLastName("fdxxivetvtcqaqtd")
                                        .withEmailAddress("mcbxvwvxysl")
                                        .withPhoneNumber("hsfxoblytkb"))
                                .withPlanData(
                                    new PlanData()
                                        .withUsageType("ewwwfbkrvrnsv")
                                        .withBillingCycle("q")
                                        .withPlanDetails("hxcr")
                                        .withEffectiveDate(OffsetDateTime.parse("2020-12-28T12:25:09Z"))))
                        .withIdentity(new IdentityProperties().withType(ManagedIdentityTypes.USER_ASSIGNED)),
                    com.azure.core.util.Context.NONE);

        Assertions.assertEquals("esl", response.location());
        Assertions.assertEquals("wwtppj", response.tags().get("lcxog"));
        Assertions.assertEquals(MonitoringStatus.ENABLED, response.properties().monitoringStatus());
        Assertions
            .assertEquals(MarketplaceSubscriptionStatus.ACTIVE, response.properties().marketplaceSubscriptionStatus());
        Assertions.assertEquals("zdnds", response.properties().logzOrganizationProperties().companyName());
        Assertions.assertEquals("nduhavhqlkthum", response.properties().logzOrganizationProperties().enterpriseAppId());
        Assertions
            .assertEquals("olbgycduiertgccy", response.properties().logzOrganizationProperties().singleSignOnUrl());
        Assertions.assertEquals("olpsslqlf", response.properties().userInfo().firstName());
        Assertions.assertEquals("dnbbglzps", response.properties().userInfo().lastName());
        Assertions.assertEquals("ydmcwyhzdxssa", response.properties().userInfo().emailAddress());
        Assertions.assertEquals("zmnvdfznudaod", response.properties().userInfo().phoneNumber());
        Assertions.assertEquals("bncblylpstdbhhx", response.properties().planData().usageType());
        Assertions.assertEquals("zdzucerscdntnevf", response.properties().planData().billingCycle());
        Assertions.assertEquals("jmygtdsslswtmwer", response.properties().planData().planDetails());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-09-22T20:42:28Z"), response.properties().planData().effectiveDate());
        Assertions.assertEquals(ManagedIdentityTypes.USER_ASSIGNED, response.identity().type());
    }

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