package com.molina.cvmfs.test;

import com.molina.cvmfs.directoryentry.DirectoryEntry;
import com.molina.cvmfs.repository.Repository;
import com.molina.cvmfs.repository.exception.CacheDirectoryNotFound;
import com.molina.cvmfs.repository.exception.FailedToLoadSourceException;
import com.molina.cvmfs.rootfile.exception.RootFileException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class RepositoryTest {

    private static final String TEST_CACHE_PATH = "/tmp/cvmfs_test_cache";

    private Repository repo;

    @Test
    public void initialization()
            throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException {
        repo = new Repository("http://cvmfs-stratum-one.cern.ch/opt/boss", TEST_CACHE_PATH);
        Assert.assertEquals("boss.cern.ch", repo.getFqrn());
        Assert.assertEquals(1, repo.getOpenedCatalogs().size());
        Assert.assertEquals(TEST_CACHE_PATH, repo.getStorageLocation());
        Assert.assertNotNull(repo.getFetcher());
        Assert.assertNotNull(repo.retrieveRootCatalog());
        Assert.assertTrue(repo.unloadCatalogs());
    }

    @Test
    public void retrieveCatalogTree()
            throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException {
        repo = new Repository("http://cvmfs-stratum-one.cern.ch/opt/boss", TEST_CACHE_PATH);
        Assert.assertEquals(1, repo.getOpenedCatalogs().size());
        repo.retrieveCatalogTree();
        Assert.assertFalse(repo.getOpenedCatalogs().isEmpty());
        Assert.assertTrue(repo.unloadCatalogs());
    }

    @Test
    public void lookup() throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException {
        repo = new Repository("http://cvmfs-stratum-one.cern.ch/opt/boss", TEST_CACHE_PATH);
        DirectoryEntry result = repo.lookup("/");
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isDirectory());

        result = repo.lookup("");
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isDirectory());

        result = repo.lookup("/.cvmfsdirtab");
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isFile());

        result = repo.lookup("/.-.");
        Assert.assertNull(result);
    }

    @Test
    public void list() throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException {
        repo = new Repository("http://cvmfs-stratum-one.cern.ch/opt/boss", TEST_CACHE_PATH);
        List<DirectoryEntry> result = repo.listDirectory("/");
        Assert.assertNotNull(result);
        Assert.assertFalse(result.isEmpty());
    }

    @Test
    public void cat() throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException {
        repo = new Repository("http://cvmfs-stratum-one.cern.ch/opt/boss", TEST_CACHE_PATH);
        List<DirectoryEntry> result = repo.listDirectory("/");
        for (DirectoryEntry dirent : result) {
            Assert.assertNotNull(dirent);
            if (dirent.isFile()) {
                File file = repo.getFile("/" + dirent.getName());
                Assert.assertNotNull(file);
                Assert.assertTrue(file.exists());
                Assert.assertTrue(file.isFile());
            }
        }
    }
}

@@ -299,43 +299,200 @@
 fs.r
-ootfile.exception.RootFileException
+epository.exception.RepositoryNotFoundException;%0Aimport com.molina.cvmfs.revision.Revision;%0Aimport com.molina.cvmfs.rootfile.exception.RootFileException;%0Aimport org.apache.commons.io.FileUtils
 ;%0Aim
@@ -863,16 +863,185 @@
  repo;%0A%0A
+    public RepositoryTest() throws IOException %7B%0A        File cache = new File(TEST_CACHE_PATH);%0A        FileUtils.deleteDirectory(cache);%0A        cache.mkdir();%0A    %7D%0A%0A
     @Tes
@@ -1169,32 +1169,61 @@
 ion, IOException
+, RepositoryNotFoundException
  %7B%0A        repo 
@@ -1294,32 +1294,82 @@
 ST_CACHE_PATH);%0A
+        Revision rev = repo.getCurrentRevision();%0A
         Assert.a
@@ -1433,33 +1433,33 @@
 rt.assertEquals(
-1
+0
 , repo.getOpened
@@ -1482,81 +1482,8 @@
 ));%0A
-        Assert.assertEquals(TEST_CACHE_PATH, repo.getStorageLocation());%0A
     
@@ -1558,18 +1558,17 @@
 tNull(re
-po
+v
 .retriev
@@ -1671,41 +1671,16 @@
 oid 
-retrieveCatalogTree()%0A           
+lookup()
  thr
@@ -1758,32 +1758,61 @@
 ion, IOException
+, RepositoryNotFoundException
  %7B%0A        repo 
@@ -1903,440 +1903,47 @@
     
-Assert.assertEquals(1, repo.getOpenedCatalogs().size());%0A        repo.retrieveCatalogTree();%0A        Assert.assertFalse(repo.getOpenedCatalogs().isEmpty());%0A        Assert.assertTrue(repo.unloadCatalogs());%0A    %7D%0A%0A    @Test%0A    public void lookup() throws RootFileException, CacheDirectoryNotFound, FailedToLoadSourceException, IOException %7B%0A        repo = new Repository(%22http://cvmfs-stratum-one.cern.ch/opt/boss%22, TEST_CACHE_PATH
+Revision rev = repo.getCurrentRevision(
 );%0A 
@@ -1967,34 +1967,33 @@
 ntry result = re
-po
+v
 .lookup(%22/%22);%0A  
@@ -2089,34 +2089,33 @@
      result = re
-po
+v
 .lookup(%22%22);%0A   
@@ -2210,34 +2210,33 @@
      result = re
-po
+v
 .lookup(%22/.cvmfs
@@ -2347,18 +2347,17 @@
 ult = re
-po
+v
 .lookup(
@@ -2522,32 +2522,61 @@
 ion, IOException
+, RepositoryNotFoundException
  %7B%0A        repo 
@@ -2647,32 +2647,82 @@
 ST_CACHE_PATH);%0A
+        Revision rev = repo.getCurrentRevision();%0A
         List%3CDir
@@ -2737,34 +2737,33 @@
 try%3E result = re
-po
+v
 .listDirectory(%22
@@ -2972,32 +2972,61 @@
 ion, IOException
+, RepositoryNotFoundException
  %7B%0A        repo 
@@ -3097,32 +3097,82 @@
 ST_CACHE_PATH);%0A
+        Revision rev = repo.getCurrentRevision();%0A
         List%3CDir
@@ -3195,18 +3195,17 @@
 ult = re
-po
+v
 .listDir
@@ -3367,26 +3367,25 @@
 le file = re
-po
+v
 .getFile(%22/%22

Fix tests