package org.carlspring.strongbox.testing;

import org.carlspring.commons.io.RandomInputStream;
import org.carlspring.strongbox.repository.RepositoryManagementStrategyException;
import org.carlspring.strongbox.services.RepositoryManagementService;
import org.carlspring.strongbox.services.StorageManagementService;
import org.carlspring.strongbox.storage.Storage;
import org.carlspring.strongbox.storage.repository.Repository;

import javax.inject.Inject;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author carlspring
 */
public abstract class TestCaseWithRepositoryManagement extends TestCaseWithRepository
{

    @Inject
    protected StorageManagementService storageManagementService;

    @Inject
    protected RepositoryManagementService repositoryManagementService;
    

    public void createStorage(String storageId)
            throws IOException
    {
        createStorage(new Storage(storageId));
    }

    public void createStorage(Storage storage)
            throws IOException
    {
        configurationManagementService.saveStorage(storage);
        storageManagementService.createStorage(storage);
    }

    public void createRepository(Repository repository)
            throws IOException, JAXBException, RepositoryManagementStrategyException
    {
        configurationManagementService.saveRepository(repository.getStorage().getId(), repository);

        // Create the repository
        repositoryManagementService.createRepository(repository.getStorage().getId(), repository.getId());
    }

    public void createRepositoryWithFile(Repository repository, String path)
            throws IOException, JAXBException, RepositoryManagementStrategyException
    {
        createRepository(repository);
        createFile(repository, path);
    }

    public abstract void createProxyRepository(String storageId,
                                               String repositoryId,
                                               String remoteRepositoryUrl)
            throws IOException,
                   JAXBException,
                   RepositoryManagementStrategyException;

    public void createFile(String storageId,
                           String repositoryId,
                           String path)
            throws IOException
    {
        Repository repository = configurationManagementService.getRepository(storageId, repositoryId);

        createFile(repository, path);
    }

    public void createFile(Repository repository,
                           String path)
            throws IOException
    {
        File file = new File(repository.getBasedir(), path);

        if (!file.getParentFile().exists())
        {
            //noinspection ResultOfMethodCallIgnored
            file.getParentFile().mkdirs();
        }

        createRandomSizeFile(file);
    }

    private void createRandomSizeFile(File file)
            throws IOException
    {
        FileOutputStream fos = new FileOutputStream(file);
        RandomInputStream ris = new RandomInputStream(true, 1000000);

        byte[] buffer = new byte[4096];
        int len;
        while ((len = ris.read(buffer)) > 0)
        {
            fos.write(buffer, 0, len);
        }

        ris.close();

        fos.flush();
        fos.close();
    }

}

@@ -84,24 +84,233 @@
 nputStream;%0A
+import org.carlspring.strongbox.providers.io.RepositoryPath;%0Aimport org.carlspring.strongbox.providers.io.RepositoryPathResolver;%0Aimport org.carlspring.strongbox.providers.repository.HostedRepositoryProvider;%0A
 import org.c
@@ -709,20 +709,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -737,20 +737,16 @@
 java.io.
-File
 OutputSt
@@ -763,30 +763,30 @@
 rt java.
+n
 io.
-IOException
+file.Files
 ;%0A%0A/**%0A 
@@ -1064,16 +1064,171 @@
 ce;%0A    
+%0A    @Inject%0A    protected HostedRepositoryProvider hostedRepositoryProvider;%0A    %0A    @Inject%0A    protected RepositoryPathResolver repositoryPathResolver;
 %0A%0A    pu
@@ -2206,32 +2206,36 @@
 y, path);%0A    %7D%0A
+    
 %0A    public abst
@@ -2989,32 +2989,39 @@
 OException%0A    %7B
+       
 %0A        File fi
@@ -3017,105 +3017,109 @@
     
-File file = new File(repository.getBasedir(), path);%0A%0A        if (!file.getParentFile().exists())
+String repositoryId = repository.getId();%0A        String storageId = repository.getStorage().getId();
 %0A   
@@ -3115,33 +3115,32 @@
 etId();%0A        
-%7B
 %0A            //n
@@ -3136,93 +3136,161 @@
     
-    //noinspection ResultOfMethodCallIgnored%0A            file.getParentFile().mkdirs(
+RepositoryPath repositoryPath = repositoryPathResolver.resolve(storageId, repositoryId, path);%0A        Files.createDirectories(repositoryPath.getParent()
 );%0A 
@@ -3288,34 +3288,32 @@
 ent());%0A        
-%7D%0A
 %0A        createR
@@ -3326,20 +3326,30 @@
 izeFile(
-file
+repositoryPath
 );%0A    %7D
@@ -3392,23 +3392,39 @@
 ile(
-File file)%0A    
+RepositoryPath repositoryPath)%0A
     
@@ -3460,20 +3460,21 @@
         
-File
+try (
 OutputSt
@@ -3488,44 +3488,93 @@
 s = 
-new FileOutputStream(file);%0A        
+hostedRepositoryProvider.getOutputStream(repositoryPath))%0A        %7B%0A            try (
 Rand
@@ -3629,19 +3629,41 @@
 1000000)
-;%0A%0A
+)%0A            %7B%0A%0A        
         
@@ -3702,16 +3702,24 @@
         
+        
 int len;
@@ -3719,16 +3719,24 @@
 nt len;%0A
+        
         
@@ -3772,34 +3772,50 @@
 )) %3E 0)%0A        
-%7B%0A
+        %7B%0A        
             fos.
@@ -3849,19 +3849,16 @@
     
-%7D%0A%0A
         
 ris.
@@ -3857,63 +3857,33 @@
     
-ris.close();%0A%0A        fos.flush();%0A        fos.close();
+%7D%0A            %7D%0A        %7D
 %0A   

test artifact resolution fixed for Raw Layout