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

package com.azure.containers.containerregistry;

import com.azure.core.http.HttpClient;
import com.azure.core.http.rest.Response;
import com.azure.core.test.TestMode;
import com.azure.core.test.http.AssertingHttpClientBuilder;
import com.azure.core.util.Context;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import reactor.test.StepVerifier;

import java.util.Collections;

import static com.azure.containers.containerregistry.TestUtils.HELLO_WORLD_REPOSITORY_NAME;
import static com.azure.containers.containerregistry.TestUtils.HELLO_WORLD_SEATTLE_REPOSITORY_NAME;
import static com.azure.containers.containerregistry.TestUtils.HTTP_STATUS_CODE_202;
import static com.azure.containers.containerregistry.TestUtils.SKIP_AUTH_TOKEN_REQUEST_FUNCTION;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@Execution(ExecutionMode.SAME_THREAD)
public class ContainerRegistryClientTest extends ContainerRegistryClientsTestBase {
    private ContainerRegistryAsyncClient registryAsyncClient;
    private ContainerRegistryClient registryClient;
    private ContainerRepositoryAsync asyncClient;
    private ContainerRepository client;
    private HttpClient httpClient;
    private final String repositoryName = HELLO_WORLD_SEATTLE_REPOSITORY_NAME;

    private HttpClient buildSyncAssertingClient(HttpClient httpClient) {
        return new AssertingHttpClientBuilder(httpClient)
            .skipRequest(SKIP_AUTH_TOKEN_REQUEST_FUNCTION)
            .assertSync()
            .build();
    }

    private HttpClient buildAsyncAssertingClient(HttpClient httpClient) {
        return new AssertingHttpClientBuilder(httpClient)
            .skipRequest(SKIP_AUTH_TOKEN_REQUEST_FUNCTION)
            .assertAsync()
            .build();
    }
    private ContainerRegistryClient getContainerRegistryClient(HttpClient client) {
        return getContainerRegistryBuilder(buildSyncAssertingClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : client)).buildClient();
    }

    private ContainerRegistryAsyncClient getContainerRegistryAsyncClient(HttpClient client) {
        return getContainerRegistryBuilder(buildAsyncAssertingClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : client)).buildAsyncClient();
    }

    @BeforeEach
    void beforeEach() {
        TestUtils.importImage(getTestMode(), repositoryName, Collections.singletonList("latest"));
        if (getTestMode() == TestMode.PLAYBACK) {
            httpClient = interceptorManager.getPlaybackClient();
        } else {
            httpClient = HttpClient.createDefault();
        }

        registryClient = getContainerRegistryClient(httpClient);
        registryAsyncClient = getContainerRegistryAsyncClient(httpClient);
        asyncClient = registryAsyncClient.getRepository(repositoryName);
        client = registryClient.getRepository(repositoryName);
    }


    @Test
    public void deleteRepositoryByRegistryWithResponseAsyncClient() {
        StepVerifier.create(registryAsyncClient.deleteRepositoryWithResponse(repositoryName))
            .assertNext(res -> assertEquals(res.getStatusCode(), HTTP_STATUS_CODE_202))
            .verifyComplete();

        StepVerifier.create(registryAsyncClient.deleteRepositoryWithResponse(repositoryName))
            .assertNext(res -> assertEquals(res.getStatusCode(), HTTP_STATUS_CODE_202))
            .verifyComplete();
    }

    @Test
    public void deleteRepositoryByRegistryAsyncClient() {
        StepVerifier.create(registryAsyncClient.deleteRepository(repositoryName))
            .verifyComplete();

        StepVerifier.create(registryAsyncClient.deleteRepository(repositoryName))
            .verifyComplete();
    }

    @Test
    public void deleteRepositoryWithResponseAsyncClient() {
        StepVerifier.create(asyncClient.deleteWithResponse())
            .assertNext(res -> assertEquals(res.getStatusCode(), HTTP_STATUS_CODE_202))
            .verifyComplete();

        StepVerifier.create(asyncClient.deleteWithResponse())
            .assertNext(res -> assertEquals(res.getStatusCode(), HTTP_STATUS_CODE_202))
            .verifyComplete();
    }

    @Test
    public void deleteRepositoryAsyncClient() {
        StepVerifier.create(asyncClient.delete())
            .verifyComplete();
        StepVerifier.create(asyncClient.delete())
            .verifyComplete();
    }

    @Test
    public void deleteRepositoryWithResponseByRegistryClient() {
        Response<Void> response = registryClient.deleteRepositoryWithResponse(HELLO_WORLD_SEATTLE_REPOSITORY_NAME, Context.NONE);
        assertEquals(HTTP_STATUS_CODE_202, response.getStatusCode());
    }

    @Test
    public void deleteRepositoryByRegistryClient() {
        // This should not throw.
        registryClient.deleteRepository(HELLO_WORLD_SEATTLE_REPOSITORY_NAME);
    }

    @Test
    public void deleteRepositoryWithResponseClient() {
        client.deleteWithResponse(Context.NONE);
    }

    @Test
    public void deleteRepositoryClient() {
        client.delete();
    }

    @Test
    public void getRepositoryTestThrows() {
        assertThrows(NullPointerException.class, () -> registryClient.getRepository(null));
        assertThrows(IllegalArgumentException.class, () -> registryClient.getRepository(""));
        assertThrows(NullPointerException.class, () -> registryAsyncClient.getRepository(null));
        assertThrows(IllegalArgumentException.class, () -> registryAsyncClient.getRepository(""));
    }

    @Test
    public void getArtifactTestThrows() {
        assertThrows(NullPointerException.class, () -> registryClient.getArtifact(HELLO_WORLD_REPOSITORY_NAME, null));
        assertThrows(IllegalArgumentException.class, () -> registryClient.getArtifact(HELLO_WORLD_REPOSITORY_NAME, ""));
        assertThrows(NullPointerException.class, () -> registryClient.getArtifact(null, "digest"));
        assertThrows(IllegalArgumentException.class, () -> registryClient.getArtifact("", "digest"));
        assertThrows(NullPointerException.class, () -> registryAsyncClient.getArtifact(HELLO_WORLD_REPOSITORY_NAME, null));
        assertThrows(IllegalArgumentException.class, () -> registryAsyncClient.getArtifact(HELLO_WORLD_REPOSITORY_NAME, ""));
        assertThrows(NullPointerException.class, () -> registryAsyncClient.getArtifact(null, "digest"));
        assertThrows(IllegalArgumentException.class, () -> registryAsyncClient.getArtifact("", "digest"));
    }
}
