package org.carlspring.strongbox.controllers.layout.npm;

import org.carlspring.strongbox.artifact.coordinates.NpmArtifactCoordinates;
import org.carlspring.strongbox.config.IntegrationTest;
import org.carlspring.strongbox.domain.ArtifactEntry;
import org.carlspring.strongbox.domain.RemoteArtifactEntry;
import org.carlspring.strongbox.rest.common.NpmRestAssuredBaseTest;
import org.carlspring.strongbox.services.ArtifactEntryService;
import org.carlspring.strongbox.storage.repository.Repository;
import org.carlspring.strongbox.testing.repository.NpmRepository;
import org.carlspring.strongbox.testing.storage.repository.RepositoryManagementTestExecutionListener;
import org.carlspring.strongbox.testing.storage.repository.TestRepository.Group;
import org.carlspring.strongbox.testing.storage.repository.TestRepository.Remote;

import javax.inject.Inject;
import java.nio.file.Paths;

import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.http.HttpStatus;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Pablo Tirado
 */
@IntegrationTest
public class NpmArtifactControllerTestIT
        extends NpmRestAssuredBaseTest
{

    private static final String REPOSITORY_PROXY = "nactit-npm-proxy";

    private static final String REPOSITORY_GROUP = "nactit-npm-group";

    private static final String REMOTE_URL = "https://registry.npmjs.org/";

    @Inject
    private ArtifactEntryService artifactEntryService;

    @Override
    @BeforeEach
    public void init()
        throws Exception
    {
        super.init();
    }

    /**
     * Note: This test requires an Internet connection.
     *
     * @throws Exception
     */
    @ExtendWith(RepositoryManagementTestExecutionListener.class)
    @Test
    public void testResolveArtifactViaProxy(@Remote(url = REMOTE_URL)
                                            @NpmRepository(storageId = STORAGE0,
                                                           repositoryId = REPOSITORY_PROXY)
                                            Repository proxyRepository)
            throws Exception
    {
        // https://registry.npmjs.org/compression/-/compression-1.7.2.tgz
        String artifactPath = Paths.get("storages",
                                        proxyRepository.getStorage().getId(),
                                        proxyRepository.getId(),
                                        "compression",
                                        "-",
                                        "compression-1.7.2.tgz").toString();

        resolveArtifact(artifactPath);
    }

    /**
     * Note: This test requires an Internet connection.
     *
     * @throws Exception
     */
    @ExtendWith(RepositoryManagementTestExecutionListener.class)
    @Test
    public void testResolveArtifactViaGroupWithProxy(@Remote(url = REMOTE_URL)
                                                     @NpmRepository(storageId = STORAGE0,
                                                                    repositoryId = REPOSITORY_PROXY)
                                                     Repository proxyRepository,
                                                     @Group(repositories = REPOSITORY_PROXY)
                                                     @NpmRepository(storageId = STORAGE0,
                                                                    repositoryId = REPOSITORY_GROUP)
                                                     Repository groupRepository)
            throws Exception
    {
        // https://registry.npmjs.org/compression/-/compression-1.7.2.tgz
        String artifactPath = Paths.get("storages",
                                        groupRepository.getStorage().getId(),
                                        groupRepository.getId(),
                                        "compression",
                                        "-",
                                        "compression-1.7.2.tgz").toString();

        resolveArtifact(artifactPath);
    }

    @ExtendWith(RepositoryManagementTestExecutionListener.class)
    @Test
    public void testViewArtifactViaProxy(@Remote(url = REMOTE_URL)
                                         @NpmRepository(storageId = STORAGE0,
                                                        repositoryId = REPOSITORY_PROXY)
                                         Repository proxyRepository)
    {
        final String storageId = proxyRepository.getStorage().getId();
        final String repositoryId = proxyRepository.getId();

        NpmArtifactCoordinates coordinates = NpmArtifactCoordinates.of("react", "16.5.0");

        String url = getContextBaseUrl() + "/storages/{storageId}/{repositoryId}/{artifactId}";
        given().when()
               .get(url, storageId, repositoryId, coordinates.getId())
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .and()
               .body("name", CoreMatchers.equalTo("react"))
               .body("versions.size()", Matchers.greaterThan(0));

        ArtifactEntry artifactEntry = artifactEntryService.findOneArtifact(storageId,
                                                                           repositoryId,
                                                                           coordinates.toPath());
        assertNotNull(artifactEntry);
        assertTrue(artifactEntry instanceof RemoteArtifactEntry);
        assertFalse(((RemoteArtifactEntry)artifactEntry).getIsCached());
    }

    @ExtendWith(RepositoryManagementTestExecutionListener.class)
    @Test
    public void testSearchArtifactViaProxy(@Remote(url = REMOTE_URL)
                                           @NpmRepository(storageId = STORAGE0,
                                                          repositoryId = REPOSITORY_PROXY)
                                           Repository proxyRepository)
    {
        final String storageId = proxyRepository.getStorage().getId();
        final String repositoryId = proxyRepository.getId();

        String url = getContextBaseUrl() +
                     "/storages/{storageId}/{repositoryId}/-/v1/search?text=reston&size=10";
        given().when()
               .get(url, storageId, repositoryId)
               .peek()
               .then()
               .statusCode(HttpStatus.OK.value())
               .and()
               .body("objects.package.name",
                     CoreMatchers.hasItem("Reston"));
        
        ArtifactEntry artifactEntry = artifactEntryService.findOneArtifact(storageId,
                                                                           repositoryId,
                                                                           "Reston/Reston/0.2.0/Reston-0.2.0.tgz");
        assertNotNull(artifactEntry);
        assertTrue(artifactEntry instanceof RemoteArtifactEntry);
        assertFalse(((RemoteArtifactEntry)artifactEntry).getIsCached());
    }
}

@@ -855,36 +855,8 @@
 ect;
-%0Aimport java.nio.file.Paths;
 %0A%0Aim
@@ -2379,148 +2379,84 @@
 th =
- Paths.get(%22storages%22,%0A                                        proxyRepository.getStorage().getId(),%0A                                       
+%0A                %22/storages/%22 + proxyRepository.getStorage().getId() + %22/%22 +
  pro
@@ -2471,37 +2471,33 @@
 tory.getId()
-,%0A           
+ + %22/%22 +%0A
             
@@ -2504,122 +2504,23 @@
     
-             %22compression%22,%0A                                        %22-%22,%0A                                        %22
+%22compression/-/
 comp
@@ -2533,36 +2533,24 @@
 n-1.7.2.tgz%22
-).toString()
 ;%0A%0A        r
@@ -3623,148 +3623,84 @@
 th =
- Paths.get(%22storages%22,%0A                                        groupRepository.getStorage().getId(),%0A                                       
+%0A                %22/storages/%22 + groupRepository.getStorage().getId() + %22/%22 +
  gro
@@ -3719,29 +3719,25 @@
 .getId()
-,%0A           
+ + %22/%22 +%0A
         
@@ -3748,122 +3748,23 @@
     
-             %22compression%22,%0A                                        %22-%22,%0A                                        %22
+%22compression/-/
 comp
@@ -3785,20 +3785,8 @@
 tgz%22
-).toString()
 ;%0A%0A 

Fix artifact paths.