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

package com.azure.resourcemanager.policyinsights.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.core.util.Context;
import com.azure.resourcemanager.policyinsights.PolicyInsightsManager;
import com.azure.resourcemanager.policyinsights.models.PolicyState;
import com.azure.resourcemanager.policyinsights.models.PolicyStatesResource;
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 PolicyStatesListQueryResultsForSubscriptionLevelPolicyAssignmentMockTests {
    @Test
    public void testListQueryResultsForSubscriptionLevelPolicyAssignment() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"value\":[{\"@odata.id\":\"rsrrmoucsofldp\",\"@odata.context\":\"iyfc\",\"timestamp\":\"2021-07-23T22:31:54Z\",\"resourceId\":\"olhbhlvb\",\"policyAssignmentId\":\"uqibsxtkcu\",\"policyDefinitionId\":\"b\",\"effectiveParameters\":\"arfsi\",\"isCompliant\":true,\"subscriptionId\":\"jxnqp\",\"resourceType\":\"gf\",\"resourceLocation\":\"mhqykizmdksa\",\"resourceGroup\":\"fcluqvo\",\"resourceTags\":\"ycjimryvwgcwwpbm\",\"policyAssignmentName\":\"w\",\"policyAssignmentOwner\":\"ydsx\",\"policyAssignmentParameters\":\"fo\",\"policyAssignmentScope\":\"cbvopwndyqleallk\",\"policyDefinitionName\":\"tkhlowkxxpvbr\",\"policyDefinitionAction\":\"jmzsyzfh\",\"policyDefinitionCategory\":\"lhikcyychunsj\",\"policySetDefinitionId\":\"jrtwsz\",\"policySetDefinitionName\":\"vuicp\",\"policySetDefinitionOwner\":\"trrmhwrb\",\"policySetDefinitionCategory\":\"pyf\",\"policySetDefinitionParameters\":\"bhvjglr\",\"managementGroupIds\":\"uyzlw\",\"policyDefinitionReferenceId\":\"memhooclutnpq\",\"complianceState\":\"mczjkm\",\"policyEvaluationDetails\":{\"evaluatedExpressions\":[],\"ifNotExistsDetails\":{\"resourceId\":\"xsglh\",\"totalResources\":1961385254}},\"policyDefinitionGroupNames\":[\"ejylmbkzu\",\"nigrfihotjewl\",\"xuzzjgnrefq\",\"hqo\"],\"components\":[{\"id\":\"iqakydiwfbr\",\"type\":\"pzdqtvhcspod\",\"name\":\"axsipietgb\",\"timestamp\":\"2021-10-18T18:39:59Z\",\"complianceState\":\"ulbmoichdlp\",\"\":{}},{\"id\":\"b\",\"type\":\"nbatzvi\",\"name\":\"owsaaelcattcjuh\",\"timestamp\":\"2021-11-04T20:37:23Z\",\"complianceState\":\"k\",\"\":{}},{\"id\":\"mjvlgfgg\",\"type\":\"kyylizr\",\"name\":\"jpsfxsfu\",\"timestamp\":\"2021-08-20T03:24:47Z\",\"complianceState\":\"tmvag\",\"\":{}}],\"policyDefinitionVersion\":\"qlvh\",\"policySetDefinitionVersion\":\"oveofizrvjfnmj\",\"policyAssignmentVersion\":\"lwyzgib\",\"\":{}}]}";

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

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

        PagedIterable<PolicyState> response =
            manager
                .policyStates()
                .listQueryResultsForSubscriptionLevelPolicyAssignment(
                    PolicyStatesResource.LATEST,
                    "mmoiroqboshbra",
                    "apyyrmfsvbpavbo",
                    2096228970,
                    "ppdbwnupgahxkum",
                    "sjcaacfdmmcpugm",
                    OffsetDateTime.parse("2021-09-14T23:55:36Z"),
                    OffsetDateTime.parse("2021-06-17T13:12:39Z"),
                    "epvufhbzehew",
                    "oqhnlb",
                    "nbldxeaclgschori",
                    Context.NONE);

        Assertions.assertEquals(OffsetDateTime.parse("2021-07-23T22:31:54Z"), response.iterator().next().timestamp());
        Assertions.assertEquals("olhbhlvb", response.iterator().next().resourceId());
        Assertions.assertEquals("uqibsxtkcu", response.iterator().next().policyAssignmentId());
        Assertions.assertEquals("b", response.iterator().next().policyDefinitionId());
        Assertions.assertEquals("arfsi", response.iterator().next().effectiveParameters());
        Assertions.assertEquals(true, response.iterator().next().isCompliant());
        Assertions.assertEquals("jxnqp", response.iterator().next().subscriptionId());
        Assertions.assertEquals("gf", response.iterator().next().resourceType());
        Assertions.assertEquals("mhqykizmdksa", response.iterator().next().resourceLocation());
        Assertions.assertEquals("fcluqvo", response.iterator().next().resourceGroup());
        Assertions.assertEquals("ycjimryvwgcwwpbm", response.iterator().next().resourceTags());
        Assertions.assertEquals("w", response.iterator().next().policyAssignmentName());
        Assertions.assertEquals("ydsx", response.iterator().next().policyAssignmentOwner());
        Assertions.assertEquals("fo", response.iterator().next().policyAssignmentParameters());
        Assertions.assertEquals("cbvopwndyqleallk", response.iterator().next().policyAssignmentScope());
        Assertions.assertEquals("tkhlowkxxpvbr", response.iterator().next().policyDefinitionName());
        Assertions.assertEquals("jmzsyzfh", response.iterator().next().policyDefinitionAction());
        Assertions.assertEquals("lhikcyychunsj", response.iterator().next().policyDefinitionCategory());
        Assertions.assertEquals("jrtwsz", response.iterator().next().policySetDefinitionId());
        Assertions.assertEquals("vuicp", response.iterator().next().policySetDefinitionName());
        Assertions.assertEquals("trrmhwrb", response.iterator().next().policySetDefinitionOwner());
        Assertions.assertEquals("pyf", response.iterator().next().policySetDefinitionCategory());
        Assertions.assertEquals("bhvjglr", response.iterator().next().policySetDefinitionParameters());
        Assertions.assertEquals("uyzlw", response.iterator().next().managementGroupIds());
        Assertions.assertEquals("memhooclutnpq", response.iterator().next().policyDefinitionReferenceId());
        Assertions.assertEquals("mczjkm", response.iterator().next().complianceState());
        Assertions
            .assertEquals(
                "xsglh", response.iterator().next().policyEvaluationDetails().ifNotExistsDetails().resourceId());
        Assertions
            .assertEquals(
                1961385254, response.iterator().next().policyEvaluationDetails().ifNotExistsDetails().totalResources());
        Assertions.assertEquals("ejylmbkzu", response.iterator().next().policyDefinitionGroupNames().get(0));
        Assertions.assertEquals("iqakydiwfbr", response.iterator().next().components().get(0).id());
        Assertions.assertEquals("pzdqtvhcspod", response.iterator().next().components().get(0).type());
        Assertions.assertEquals("axsipietgb", response.iterator().next().components().get(0).name());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-10-18T18:39:59Z"),
                response.iterator().next().components().get(0).timestamp());
        Assertions.assertEquals("ulbmoichdlp", response.iterator().next().components().get(0).complianceState());
    }
}
