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

package com.azure.resourcemanager.security.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.security.SecurityManager;
import com.azure.resourcemanager.security.models.GovernanceRule;
import com.azure.resourcemanager.security.models.GovernanceRuleOwnerSourceType;
import com.azure.resourcemanager.security.models.GovernanceRuleSourceResourceType;
import com.azure.resourcemanager.security.models.GovernanceRuleType;
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 GovernanceRulesListMockTests {
    @Test
    public void testList() 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\":{\"tenantId\":\"oaeoc\",\"displayName\":\"ajtfey\",\"description\":\"bdgddkrh\",\"remediationTimeframe\":\"cxbeuuqutkzwtjww\",\"isGracePeriod\":false,\"rulePriority\":574110550,\"isDisabled\":false,\"ruleType\":\"Integrated\",\"sourceResourceType\":\"Assessments\",\"excludedScopes\":[\"nondegjdyd\"],\"conditionSets\":[\"datakkkbjuckcatuqbh\",\"dataowcnxtpz\",\"datalyseidtoak\",\"datat\"],\"includeMemberScopes\":true,\"ownerSource\":{\"type\":\"ByTag\",\"value\":\"hzbqfdpfawrptvcs\"},\"governanceEmailNotification\":{\"disableManagerEmailNotification\":true,\"disableOwnerEmailNotification\":true},\"metadata\":{\"createdBy\":\"tbqgdirdamm\",\"createdOn\":\"2021-04-05T19:23:11Z\",\"updatedBy\":\"cfjfxtbwjj\",\"updatedOn\":\"2021-06-25T16:41:01Z\"}},\"id\":\"uydgfttmdofgeoa\",\"name\":\"fuof\",\"type\":\"nxodwxmdajwiyg\"}]}";

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

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

        PagedIterable<GovernanceRule> response =
            manager.governanceRules().list("yjfzi", com.azure.core.util.Context.NONE);

        Assertions.assertEquals("ajtfey", response.iterator().next().displayName());
        Assertions.assertEquals("bdgddkrh", response.iterator().next().description());
        Assertions.assertEquals("cxbeuuqutkzwtjww", response.iterator().next().remediationTimeframe());
        Assertions.assertEquals(false, response.iterator().next().isGracePeriod());
        Assertions.assertEquals(574110550, response.iterator().next().rulePriority());
        Assertions.assertEquals(false, response.iterator().next().isDisabled());
        Assertions.assertEquals(GovernanceRuleType.INTEGRATED, response.iterator().next().ruleType());
        Assertions
            .assertEquals(
                GovernanceRuleSourceResourceType.ASSESSMENTS, response.iterator().next().sourceResourceType());
        Assertions.assertEquals("nondegjdyd", response.iterator().next().excludedScopes().get(0));
        Assertions.assertEquals(true, response.iterator().next().includeMemberScopes());
        Assertions.assertEquals(GovernanceRuleOwnerSourceType.BY_TAG, response.iterator().next().ownerSource().type());
        Assertions.assertEquals("hzbqfdpfawrptvcs", response.iterator().next().ownerSource().value());
        Assertions
            .assertEquals(
                true, response.iterator().next().governanceEmailNotification().disableManagerEmailNotification());
        Assertions
            .assertEquals(
                true, response.iterator().next().governanceEmailNotification().disableOwnerEmailNotification());
    }
}
