package org.carlspring.strongbox.artifact.coordinates;

import org.carlspring.strongbox.config.NugetLayoutProviderTestConfig;
import org.carlspring.strongbox.providers.io.RootRepositoryPath;
import org.carlspring.strongbox.storage.repository.Repository;
import org.carlspring.strongbox.testing.TestCaseWithNugetArtifactGeneration;
import org.carlspring.strongbox.testing.artifact.ArtifactManagementTestExecutionListener;
import org.carlspring.strongbox.testing.artifact.NugetTestArtifact;
import org.carlspring.strongbox.testing.repository.NugetRepository;
import org.carlspring.strongbox.testing.storage.repository.RepositoryManagementTestExecutionListener;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

/**
 * @author Pablo Tirado
 */
@ContextConfiguration(classes = NugetLayoutProviderTestConfig.class)
@SpringBootTest
@ActiveProfiles(profiles = "test")
@Execution(CONCURRENT)
public class NugetArtifactConverterTest
        extends TestCaseWithNugetArtifactGeneration
{

    private static final String REPOSITORY_RELEASES = "nact-releases";

    private static final String ARTIFACT_ID = "Org.Carlspring.Strongbox.Examples.Nuget.Mono";

    private static final String ARTIFACT_VERSION = "1.0";

    @ExtendWith({ RepositoryManagementTestExecutionListener.class,
                  ArtifactManagementTestExecutionListener.class })
    @Test
    public void testArtifactPathToCoordinatesConversion(@NugetRepository(repositoryId = REPOSITORY_RELEASES)
                                                        Repository repository,
                                                        @NugetTestArtifact(repositoryId = REPOSITORY_RELEASES,
                                                                           id = ARTIFACT_ID,
                                                                           versions = ARTIFACT_VERSION)
                                                        Path artifactNupkgPath)
    {
        RootRepositoryPath repositoryPath = repositoryPathResolver.resolve(repository);

        Path relArtifactNupkgPath = repositoryPath.relativize(artifactNupkgPath);
        String nupkgPath = FilenameUtils.separatorsToUnix(relArtifactNupkgPath.toString());
        NugetArtifactCoordinates nac = NugetArtifactCoordinates.parse(nupkgPath);
        assertEquals(ARTIFACT_ID, nac.getId(),
                     "Failed to convert path to artifact coordinates!");
        assertEquals(ARTIFACT_VERSION, nac.getVersion(), "Failed to convert path to artifact coordinates!");

        String nupkgSha512Path = Paths.get(nupkgPath + ".sha512").toString();
        nac = NugetArtifactCoordinates.parse(nupkgSha512Path);
        assertEquals(ARTIFACT_ID, nac.getId(),
                     "Failed to convert path to artifact coordinates!");
        assertEquals(ARTIFACT_VERSION, nac.getVersion(), "Failed to convert path to artifact coordinates!");

        String nuspecPath = Paths.get(nupkgPath.replace("nupkg", "nuspec")).toString();
        nac = NugetArtifactCoordinates.parse(nuspecPath);
        assertEquals(ARTIFACT_ID, nac.getId(),
                     "Failed to convert path to artifact coordinates!");
        assertEquals(ARTIFACT_VERSION, nac.getVersion(), "Failed to convert path to artifact coordinates!");
    }

}

@@ -2792,32 +2792,53 @@
 als(ARTIFACT_ID,
+%0A                    
  nac.getId(),%0A  
@@ -2946,34 +2946,76 @@
 VERSION,
- nac.getVersion(),
+%0A                     nac.getVersion(),%0A                    
  %22Failed
@@ -3064,32 +3064,41 @@
 );%0A%0A        
-String n
+Path relArtifactN
 upkgSha512Pa
@@ -3134,16 +3134,108 @@
 sha512%22)
+;%0A        String nupkgSha512Path = FilenameUtils.separatorsToUnix(relArtifactNupkgSha512Path
 .toStrin
@@ -3229,32 +3229,33 @@
 2Path.toString()
+)
 ;%0A        nac = 
@@ -3328,32 +3328,53 @@
 als(ARTIFACT_ID,
+%0A                    
  nac.getId(),%0A  
@@ -3482,34 +3482,76 @@
 VERSION,
- nac.getVersion(),
+%0A                     nac.getVersion(),%0A                    
  %22Failed
@@ -3604,24 +3604,33 @@
         
-String n
+Path relArtifactN
 uspecPat
@@ -3680,16 +3680,98 @@
 uspec%22))
+;%0A        String nuspecPath = FilenameUtils.separatorsToUnix(relArtifactNuspecPath
 .toStrin
@@ -3773,16 +3773,17 @@
 String()
+)
 ;%0A      
@@ -3867,16 +3867,37 @@
 FACT_ID,
+%0A                    
  nac.get
@@ -4013,34 +4013,76 @@
 VERSION,
- nac.getVersion(),
+%0A                     nac.getVersion(),%0A                    
  %22Failed

Fix artifact paths.