package com.atlassian.bitbucket.internal.mirroring.mirror.dao;

import com.atlassian.bitbucket.internal.mirroring.mirror.CreateUpstreamServerRequestMatcher;
import com.atlassian.bitbucket.mirroring.mirror.IntegrationState;
import com.atlassian.bitbucket.mirroring.mirror.UpstreamServerType;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;

public class CreateUpstreamServerRequestTest {

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsNullBaseUrl() {
        new CreateUpstreamServerRequest.Builder().baseUrl(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsEmptyBaseUrl() {
        new CreateUpstreamServerRequest.Builder().baseUrl("    \t\n\r   ");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsNullId() {
        new CreateUpstreamServerRequest.Builder().id(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsEmptyId() {
        new CreateUpstreamServerRequest.Builder().id("    \t\n\r   ");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsNullNamespace() {
        new CreateUpstreamServerRequest.Builder().namespace(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsEmptyNamespace() {
        new CreateUpstreamServerRequest.Builder().namespace("    \t\n\r   ");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsNullUpstreamType() {
        new CreateUpstreamServerRequest.Builder().upstreamType(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testBuilderRejectsNullIntegrationState() {
        new CreateUpstreamServerRequest.Builder().state(null);
    }

    @Test(expected = IllegalStateException.class)
    public void testBuilderWontBuildWithNoUrl() {
        new CreateUpstreamServerRequest.Builder()
                .id("id")
                .namespace("namespace")
                .state(IntegrationState.INSTALLED)
                .upstreamType(UpstreamServerType.BITBUCKET_CLOUD)
                .build();
    }

    @Test(expected = IllegalStateException.class)
    public void testBuilderWontBuildWithNoId() {
        new CreateUpstreamServerRequest.Builder()
                .baseUrl("https://example.com")
                .namespace("namespace")
                .state(IntegrationState.INSTALLED)
                .upstreamType(UpstreamServerType.BITBUCKET_CLOUD)
                .build();

    }

    @Test(expected = IllegalStateException.class)
    public void testBuilderWontBuildWithNoNamespace() {
        new CreateUpstreamServerRequest.Builder()
                .baseUrl("https://example.com")
                .id("id")
                .state(IntegrationState.INSTALLED)
                .upstreamType(UpstreamServerType.BITBUCKET_CLOUD)
                .build();

    }

    @Test(expected = IllegalStateException.class)
    public void testBuilderWontBuildWithNoType() {
        new CreateUpstreamServerRequest.Builder()
                .baseUrl("https://example.com")
                .id("id")
                .namespace("namespace")
                .state(IntegrationState.INSTALLED)
                .build();

    }

    @Test(expected = IllegalStateException.class)
    public void testBuilderWontBuildWithNoIntegrationState() {
        new CreateUpstreamServerRequest.Builder()
                .baseUrl("https://example.com")
                .id("id")
                .namespace("namespace")
                .upstreamType(UpstreamServerType.BITBUCKET_CLOUD)
                .build();
    }

    @Test
    public void testBuilderCreatesRequest() {
        String baseUrl = "https://example.com";
        String id = "id";
        String namespace = "namespace";
        UpstreamServerType type = UpstreamServerType.BITBUCKET_CLOUD;
        IntegrationState state = IntegrationState.INSTALLED;

        CreateUpstreamServerRequest request = new CreateUpstreamServerRequest.Builder()
                .baseUrl(baseUrl)
                .id(id)
                .namespace(namespace)
                .upstreamType(type)
                .state(state)
                .build();

        assertThat(request, new CreateUpstreamServerRequestMatcher()
            .baseUrl(baseUrl)
            .id(id)
            .namespace(namespace)
            .upstreamType(type)
            .state(state));
    }
}