// 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.PolicyEvent;
import com.azure.resourcemanager.policyinsights.models.PolicyEventsResourceType;
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 PolicyEventsListQueryResultsForResourceMockTests {
    @Test
    public void testListQueryResultsForResource() 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\":\"xylfsb\",\"@odata.context\":\"adpysownbt\",\"timestamp\":\"2021-04-15T00:26:04Z\",\"resourceId\":\"grjqctojcmi\",\"policyAssignmentId\":\"fieypefojyqdhcup\",\"policyDefinitionId\":\"plcwkhi\",\"effectiveParameters\":\"hlhzdsqtzbsrgno\",\"isCompliant\":false,\"subscriptionId\":\"fgmvecactxmwo\",\"resourceType\":\"yowc\",\"resourceLocation\":\"qovekqvgqou\",\"resourceGroup\":\"fzmpjwyivq\",\"resourceTags\":\"f\",\"policyAssignmentName\":\"vhrfsphuagrt\",\"policyAssignmentOwner\":\"kteusqczk\",\"policyAssignmentParameters\":\"klxubyja\",\"policyAssignmentScope\":\"mmfblcqcuubgqib\",\"policyDefinitionName\":\"a\",\"policyDefinitionAction\":\"etttwgdslqxihhr\",\"policyDefinitionCategory\":\"oi\",\"policySetDefinitionId\":\"seypxiutcxapz\",\"policySetDefinitionName\":\"rpetogebjoxsl\",\"policySetDefinitionOwner\":\"nhl\",\"policySetDefinitionCategory\":\"rqnkkzjcjbtr\",\"policySetDefinitionParameters\":\"ehvvib\",\"managementGroupIds\":\"jj\",\"policyDefinitionReferenceId\":\"oqbeitpkxzt\",\"complianceState\":\"ob\",\"tenantId\":\"ft\",\"principalOid\":\"gfcwqmpimaqxzhem\",\"components\":[{\"id\":\"hujswtwkozzwcul\",\"type\":\"awpfajnjwltlwtjj\",\"name\":\"ktalhsnvkcdmxz\",\"timestamp\":\"2021-05-04T00:50:01Z\",\"tenantId\":\"imlnwiaaomylw\",\"principalOid\":\"z\",\"policyDefinitionAction\":\"cse\",\"\":{}}],\"\":{}}]}";

        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<PolicyEvent> response =
            manager
                .policyEvents()
                .listQueryResultsForResource(
                    PolicyEventsResourceType.DEFAULT,
                    "ycavodggxdbees",
                    1327302833,
                    "eknlra",
                    "iaa",
                    OffsetDateTime.parse("2021-09-14T07:00:52Z"),
                    OffsetDateTime.parse("2021-11-28T06:42:14Z"),
                    "agy",
                    "wqfbylyrfgiagt",
                    "ojocqwogf",
                    "zjvusfzldmo",
                    Context.NONE);

        Assertions.assertEquals(OffsetDateTime.parse("2021-04-15T00:26:04Z"), response.iterator().next().timestamp());
        Assertions.assertEquals("grjqctojcmi", response.iterator().next().resourceId());
        Assertions.assertEquals("fieypefojyqdhcup", response.iterator().next().policyAssignmentId());
        Assertions.assertEquals("plcwkhi", response.iterator().next().policyDefinitionId());
        Assertions.assertEquals("hlhzdsqtzbsrgno", response.iterator().next().effectiveParameters());
        Assertions.assertEquals(false, response.iterator().next().isCompliant());
        Assertions.assertEquals("fgmvecactxmwo", response.iterator().next().subscriptionId());
        Assertions.assertEquals("yowc", response.iterator().next().resourceType());
        Assertions.assertEquals("qovekqvgqou", response.iterator().next().resourceLocation());
        Assertions.assertEquals("fzmpjwyivq", response.iterator().next().resourceGroup());
        Assertions.assertEquals("f", response.iterator().next().resourceTags());
        Assertions.assertEquals("vhrfsphuagrt", response.iterator().next().policyAssignmentName());
        Assertions.assertEquals("kteusqczk", response.iterator().next().policyAssignmentOwner());
        Assertions.assertEquals("klxubyja", response.iterator().next().policyAssignmentParameters());
        Assertions.assertEquals("mmfblcqcuubgqib", response.iterator().next().policyAssignmentScope());
        Assertions.assertEquals("a", response.iterator().next().policyDefinitionName());
        Assertions.assertEquals("etttwgdslqxihhr", response.iterator().next().policyDefinitionAction());
        Assertions.assertEquals("oi", response.iterator().next().policyDefinitionCategory());
        Assertions.assertEquals("seypxiutcxapz", response.iterator().next().policySetDefinitionId());
        Assertions.assertEquals("rpetogebjoxsl", response.iterator().next().policySetDefinitionName());
        Assertions.assertEquals("nhl", response.iterator().next().policySetDefinitionOwner());
        Assertions.assertEquals("rqnkkzjcjbtr", response.iterator().next().policySetDefinitionCategory());
        Assertions.assertEquals("ehvvib", response.iterator().next().policySetDefinitionParameters());
        Assertions.assertEquals("jj", response.iterator().next().managementGroupIds());
        Assertions.assertEquals("oqbeitpkxzt", response.iterator().next().policyDefinitionReferenceId());
        Assertions.assertEquals("ob", response.iterator().next().complianceState());
        Assertions.assertEquals("ft", response.iterator().next().tenantId());
        Assertions.assertEquals("gfcwqmpimaqxzhem", response.iterator().next().principalOid());
        Assertions.assertEquals("hujswtwkozzwcul", response.iterator().next().components().get(0).id());
        Assertions.assertEquals("awpfajnjwltlwtjj", response.iterator().next().components().get(0).type());
        Assertions.assertEquals("ktalhsnvkcdmxz", response.iterator().next().components().get(0).name());
        Assertions
            .assertEquals(
                OffsetDateTime.parse("2021-05-04T00:50:01Z"),
                response.iterator().next().components().get(0).timestamp());
        Assertions.assertEquals("imlnwiaaomylw", response.iterator().next().components().get(0).tenantId());
        Assertions.assertEquals("z", response.iterator().next().components().get(0).principalOid());
        Assertions.assertEquals("cse", response.iterator().next().components().get(0).policyDefinitionAction());
    }
}
