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

package com.azure.resourcemanager.managednetworkfabric.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.http.rest.PagedIterable;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager;
import com.azure.resourcemanager.managednetworkfabric.models.CommunityActionTypes;
import com.azure.resourcemanager.managednetworkfabric.models.Condition;
import com.azure.resourcemanager.managednetworkfabric.models.IpPrefix;
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 IpPrefixesListByResourceGroupMockTests {
    @Test
    public void testListByResourceGroup() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"properties\":{\"configurationState\":\"Failed\",\"provisioningState\":\"Deleting\",\"administrativeState\":\"MAT\",\"ipPrefixRules\":[{\"action\":\"Permit\",\"sequenceNumber\":5376347157026383231,\"networkPrefix\":\"urxrjwyz\",\"condition\":\"GreaterThanOrEqualTo\",\"subnetMaskLength\":\"tqmlzuwtbdzqa\"}],\"annotation\":\"kmpebfhlgeehb\"},\"location\":\"gplnl\",\"tags\":{\"afm\":\"eszunb\",\"vequzytapgzdhz\":\"ubukqmierzrnob\",\"v\":\"jecdsysxnku\"},\"id\":\"nlsevzcrrwnkk\",\"name\":\"dwqymxsfqe\",\"type\":\"xdqeluvmsaq\"}]}";

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

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

        PagedIterable<IpPrefix> response =
            manager.ipPrefixes().listByResourceGroup("jzrfx", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("gplnl", response.iterator().next().location());
        Assertions.assertEquals("eszunb", response.iterator().next().tags().get("afm"));
        Assertions
            .assertEquals(CommunityActionTypes.PERMIT, response.iterator().next().ipPrefixRules().get(0).action());
        Assertions
            .assertEquals(5376347157026383231L, response.iterator().next().ipPrefixRules().get(0).sequenceNumber());
        Assertions.assertEquals("urxrjwyz", response.iterator().next().ipPrefixRules().get(0).networkPrefix());
        Assertions
            .assertEquals(
                Condition.GREATER_THAN_OR_EQUAL_TO, response.iterator().next().ipPrefixRules().get(0).condition());
        Assertions.assertEquals("tqmlzuwtbdzqa", response.iterator().next().ipPrefixRules().get(0).subnetMaskLength());
        Assertions.assertEquals("kmpebfhlgeehb", response.iterator().next().annotation());
    }
}
