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

package com.azure.resourcemanager.apimanagement.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.apimanagement.ApiManagementManager;
import com.azure.resourcemanager.apimanagement.models.AlwaysLog;
import com.azure.resourcemanager.apimanagement.models.DiagnosticContract;
import com.azure.resourcemanager.apimanagement.models.HttpCorrelationProtocol;
import com.azure.resourcemanager.apimanagement.models.OperationNameFormat;
import com.azure.resourcemanager.apimanagement.models.SamplingType;
import com.azure.resourcemanager.apimanagement.models.Verbosity;
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 DiagnosticsGetWithResponseMockTests {
    @Test
    public void testGetWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"alwaysLog\":\"allErrors\",\"loggerId\":\"ytlplslfcve\",\"sampling\":{\"samplingType\":\"fixed\",\"percentage\":88.87549570219586},\"frontend\":{\"request\":{\"headers\":[\"qfnjo\",\"rhnxzmfvmw\",\"nrtc\"],\"body\":{\"bytes\":1043024037},\"dataMasking\":{\"queryParams\":[{},{}],\"headers\":[{},{},{},{}]}},\"response\":{\"headers\":[\"bydmvwnbun\"],\"body\":{\"bytes\":966139027},\"dataMasking\":{\"queryParams\":[{},{},{},{}],\"headers\":[{},{},{}]}}},\"backend\":{\"request\":{\"headers\":[\"bmpshaezcld\"],\"body\":{\"bytes\":2146258576},\"dataMasking\":{\"queryParams\":[{},{},{},{}],\"headers\":[{}]}},\"response\":{\"headers\":[\"ejst\"],\"body\":{\"bytes\":104765725},\"dataMasking\":{\"queryParams\":[{},{},{}],\"headers\":[{}]}}},\"logClientIp\":true,\"httpCorrelationProtocol\":\"Legacy\",\"verbosity\":\"information\",\"operationNameFormat\":\"Name\",\"metrics\":false},\"id\":\"hxwdjoxwkbdh\",\"name\":\"obvcnsbioez\",\"type\":\"lfhn\"}";

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

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

        DiagnosticContract response =
            manager
                .diagnostics()
                .getWithResponse("jnzpqhqqsa", "kyu", "fameaasjqenhekza", com.azure.core.util.Context.NONE)
                .getValue();

        Assertions.assertEquals(AlwaysLog.ALL_ERRORS, response.alwaysLog());
        Assertions.assertEquals("ytlplslfcve", response.loggerId());
        Assertions.assertEquals(SamplingType.FIXED, response.sampling().samplingType());
        Assertions.assertEquals(88.87549570219586D, response.sampling().percentage());
        Assertions.assertEquals("qfnjo", response.frontend().request().headers().get(0));
        Assertions.assertEquals(1043024037, response.frontend().request().body().bytes());
        Assertions.assertEquals("bydmvwnbun", response.frontend().response().headers().get(0));
        Assertions.assertEquals(966139027, response.frontend().response().body().bytes());
        Assertions.assertEquals("bmpshaezcld", response.backend().request().headers().get(0));
        Assertions.assertEquals(2146258576, response.backend().request().body().bytes());
        Assertions.assertEquals("ejst", response.backend().response().headers().get(0));
        Assertions.assertEquals(104765725, response.backend().response().body().bytes());
        Assertions.assertEquals(true, response.logClientIp());
        Assertions.assertEquals(HttpCorrelationProtocol.LEGACY, response.httpCorrelationProtocol());
        Assertions.assertEquals(Verbosity.INFORMATION, response.verbosity());
        Assertions.assertEquals(OperationNameFormat.NAME, response.operationNameFormat());
        Assertions.assertEquals(false, response.metrics());
    }
}
