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

package com.azure.resourcemanager.logic.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.logic.LogicManager;
import com.azure.resourcemanager.logic.models.FlowAccessControlConfiguration;
import com.azure.resourcemanager.logic.models.FlowEndpointsConfiguration;
import com.azure.resourcemanager.logic.models.ManagedServiceIdentity;
import com.azure.resourcemanager.logic.models.ManagedServiceIdentityType;
import com.azure.resourcemanager.logic.models.ResourceReference;
import com.azure.resourcemanager.logic.models.Workflow;
import com.azure.resourcemanager.logic.models.WorkflowState;
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 WorkflowsCreateOrUpdateWithResponseMockTests {
    @Test
    public void testCreateOrUpdateWithResponse() 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\":\"Updating\",\"createdTime\":\"2021-07-25T12:16:40Z\",\"changedTime\":\"2021-07-23T09:34:44Z\",\"state\":\"Suspended\",\"version\":\"artr\",\"accessEndpoint\":\"nlp\",\"endpointsConfiguration\":{},\"accessControl\":{},\"sku\":{\"name\":\"Premium\"},\"integrationAccount\":{\"id\":\"reuykbbmnwagl\",\"name\":\"xoe\",\"type\":\"nqlnfwmyymvqd\"},\"integrationServiceEnvironment\":{\"id\":\"hfckdvezcrcssbz\",\"name\":\"dubbnqfblhkale\",\"type\":\"avawugiqj\"},\"definition\":\"dataogqgdminict\",\"parameters\":{}},\"identity\":{\"type\":\"SystemAssigned\",\"tenantId\":\"9082117c-3d51-49bb-b759-115e86657f15\",\"principalId\":\"df5680be-b357-412d-b941-5aaaadd8ff17\",\"userAssignedIdentities\":{}},\"location\":\"spnbonhpcz\",\"tags\":{\"xqcsehch\":\"ktpv\"},\"id\":\"hufmpq\",\"name\":\"mqyjgy\",\"type\":\"zulo\"}";

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

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

        Workflow response =
            manager
                .workflows()
                .define("vcnrly")
                .withRegion("fgvaknokzwj")
                .withExistingResourceGroup("foanniyopetx")
                .withTags(mapOf("ixymmpujivyql", "ltixldzyyfytpq"))
                .withIdentity(
                    new ManagedServiceIdentity()
                        .withType(ManagedServiceIdentityType.USER_ASSIGNED)
                        .withUserAssignedIdentities(mapOf()))
                .withState(WorkflowState.COMPLETED)
                .withEndpointsConfiguration(new FlowEndpointsConfiguration())
                .withAccessControl(new FlowAccessControlConfiguration())
                .withIntegrationAccount(new ResourceReference().withId("vbrzcdb"))
                .withIntegrationServiceEnvironment(new ResourceReference().withId("mk"))
                .withDefinition("datacd")
                .withParameters(mapOf())
                .create();

        Assertions.assertEquals("spnbonhpcz", response.location());
        Assertions.assertEquals("ktpv", response.tags().get("xqcsehch"));
        Assertions.assertEquals(ManagedServiceIdentityType.SYSTEM_ASSIGNED, response.identity().type());
        Assertions.assertEquals(WorkflowState.SUSPENDED, response.state());
        Assertions.assertEquals("reuykbbmnwagl", response.integrationAccount().id());
        Assertions.assertEquals("hfckdvezcrcssbz", response.integrationServiceEnvironment().id());
    }

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