package com.atlassian.bitbucket.internal.mirroring.mirror.rest.cloud;

import com.atlassian.bitbucket.internal.mirroring.mirror.ExternalRepository;
import com.atlassian.bitbucket.internal.mirroring.mirror.rest.MirrorJsonEntity;
import org.junit.Rule;
import org.junit.Test;

import java.util.Optional;

import static com.atlassian.bitbucket.internal.mirroring.mirror.rest.cloud.RestCloudTestUtils.assertAccount;
import static com.atlassian.bitbucket.internal.mirroring.mirror.rest.cloud.RestCloudTestUtils.assertProject;
import static java.util.Optional.of;
import static org.junit.Assert.*;

public class RestCloudExternalRepositoryTest {


    @Rule
    public MirrorJsonEntity withoutOriginJson = new MirrorJsonEntity("cloud/json/repository-without-origin.json");
    @Rule
    public MirrorJsonEntity withoutProjectJson = new MirrorJsonEntity("cloud/json/repository-without-project.json");
    @Rule
    public MirrorJsonEntity withOriginJson = new MirrorJsonEntity("cloud/json/repository-with-origin.json");

    @Test
    public void testBasicDetails() throws Exception {
        RestCloudExternalRepository repo =
                new RestCloudExternalRepository(withOriginJson.getValue());

        assertEquals("{48e473f6-929f-4b39-b803-54e6d05d4f0e}", repo.getId());
        assertEquals("atlassian-pocketknife-dynamic-modules", repo.getName());
        assertEquals("atlassian-pocketknife-dynamic-modules", repo.getSlug());
        assertEquals(of("2015-06-26T00:08:14.990059+00:00"), repo.getContentHash());
        assertEquals(of("https://bitbucket.org/atlassian/atlassian-pocketknife-dynamic-modules.git"), repo.getCloneUrl());
        assertEquals("git", repo.getScmId());
        assertAccount(repo.getOwner(), "{02b941e3-cfaa-40f9-9a58-cec53e20bdc3}", "atlassian");
        assertProject(repo.getProject(), "{899b158f-cf7f-44f1-8ade-ea7c3bdc3db3}", "SOMETHING", "Some Project");
    }

    @Test
    public void testWithOriginDetails() throws Exception {
        RestCloudExternalRepository repo =
                new RestCloudExternalRepository(withOriginJson.getValue());

        Optional<ExternalRepository> optionalOrigin = repo.getOrigin();
        assertTrue(optionalOrigin.isPresent());

        assertProject(repo.getProject(), "{899b158f-cf7f-44f1-8ade-ea7c3bdc3db3}", "SOMETHING", "Some Project");

        ExternalRepository origin = optionalOrigin.get();

        assertEquals("{55b39384-5e6e-4bbc-9d25-c7a3d34d8746}", origin.getId());
        assertEquals("atlassian-pocketknife", origin.getName());
        assertEquals("atlassian-pocketknife", origin.getSlug());
        assertEquals("git", origin.getScmId());
        assertEquals(of("2016-02-26T06:14:24.500891+00:00"), origin.getContentHash());
        assertEquals(of("https://bitbucket.org/atlassian/atlassian-pocketknife.git"), origin.getCloneUrl());
        assertAccount(origin.getOwner(), "{02b941e3-cfaa-40f9-9a58-cec53e20bdc3}", "atlassian");
        assertFalse(origin.getOrigin().isPresent());
    }

    @Test
    public void testWithoutOrigin() throws Exception {
        RestCloudExternalRepository repo = new RestCloudExternalRepository(withoutOriginJson.getValue());

        assertFalse(repo.getOrigin().isPresent());
    }

    /**
     * TODO: remove once project entities are included in the production API's JSON
     */
    @Test
    public void testWithoutProject() throws Exception {
        RestCloudExternalRepository repo = new RestCloudExternalRepository(withoutProjectJson.getValue());

        assertProject(repo.getProject(), "{bitbucket-org}", "BITBUCKET", "bitbucket.org");
    }
}