Search in sources :

Example 81 with IUnifiedRepository

use of org.pentaho.platform.api.repository2.unified.IUnifiedRepository in project pentaho-platform by pentaho.

the class UnifiedRepositoryTestUtils method stubGetChildren.

/**
 * Stubs a {@code getChildren} call. {@code childrenNames} is zero or more file/folder names. A folder is
 * indicated by a trailing forward slash.
 *
 * <p>
 * Example:
 * </p>
 *
 * <pre>
 * stubGetChildren( repo, &quot;/public&quot;, &quot;hello/&quot;, &quot;file1.txt&quot; );
 * </pre>
 */
public static void stubGetChildren(final IUnifiedRepository repo, final String path, final String... childrenNames) {
    List<RepositoryFile> children = new ArrayList<RepositoryFile>(childrenNames.length);
    for (String childName : childrenNames) {
        if (childName.startsWith(RepositoryFile.SEPARATOR)) {
            throw new IllegalArgumentException("child names must not begin with a forward slash");
        }
        final String fullChildPath = path + RepositoryFile.SEPARATOR + (childName.endsWith(RepositoryFile.SEPARATOR) ? StringUtils.substringBefore(childName, RepositoryFile.SEPARATOR) : childName);
        RepositoryFile child = null;
        if (childName.endsWith(RepositoryFile.SEPARATOR)) {
            child = makeFolderObject(fullChildPath, true);
        } else {
            child = makeFileObject(fullChildPath, true);
        }
        children.add(child);
    }
    doReturn(children).when(repo).getChildren(makeIdObject(path));
}
Also used : ArrayList(java.util.ArrayList) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 82 with IUnifiedRepository

use of org.pentaho.platform.api.repository2.unified.IUnifiedRepository in project pentaho-platform by pentaho.

the class UnifiedRepositoryTestUtils method stubGetChildren.

/**
 * Stubs a {@code getChildren} call. {@code childrenNames} is zero or more file/folder names. A folder is
 * indicated by a trailing forward slash.
 *
 * <p>
 * Example:
 * </p>
 *
 * <pre>
 * stubGetChildren( repo, &quot;/public&quot;, &quot;hello/&quot;, &quot;file1.txt&quot; );
 * </pre>
 */
public static void stubGetChildren(final IUnifiedRepository repo, RepositoryRequest request, final String... childrenNames) {
    List<RepositoryFile> children = new ArrayList<RepositoryFile>(childrenNames.length);
    for (String childName : childrenNames) {
        if (childName.startsWith(RepositoryFile.SEPARATOR)) {
            throw new IllegalArgumentException("child names must not begin with a forward slash");
        }
        final String fullChildPath = request.getPath() + RepositoryFile.SEPARATOR + (childName.endsWith(RepositoryFile.SEPARATOR) ? StringUtils.substringBefore(childName, RepositoryFile.SEPARATOR) : childName);
        RepositoryFile child = null;
        if (childName.endsWith(RepositoryFile.SEPARATOR)) {
            child = makeFolderObject(fullChildPath, true);
        } else {
            child = makeFileObject(fullChildPath, true);
        }
        children.add(child);
    }
    doReturn(children).when(repo).getChildren(request);
}
Also used : ArrayList(java.util.ArrayList) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 83 with IUnifiedRepository

use of org.pentaho.platform.api.repository2.unified.IUnifiedRepository in project pentaho-platform by pentaho.

the class RepositoryFileIoTest method testReadNonExistentFile.

@Test(expected = FileNotFoundException.class)
public void testReadNonExistentFile() throws IOException {
    final String fileName = "doesnotexist";
    final String filePath = publicDirPath + "/" + fileName;
    IUnifiedRepository repo = mock(IUnifiedRepository.class);
    // simulate file does not exist
    doReturn(null).when(repo).getFile(filePath);
    mp.defineInstance(IUnifiedRepository.class, repo);
    RepositoryFileReader reader = new RepositoryFileReader(createFile(fileName));
    reader.close();
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Test(org.junit.Test)

Example 84 with IUnifiedRepository

use of org.pentaho.platform.api.repository2.unified.IUnifiedRepository in project pentaho-platform by pentaho.

the class RepositoryFileIoTest method testWriteFileAtNewDir.

@Test(expected = FileNotFoundException.class)
public void testWriteFileAtNewDir() throws IOException {
    final String fileName = "test.txt";
    final String intermediateDir = "newdir";
    final String filePath = publicDirPath + "/" + intermediateDir + "/" + fileName;
    IUnifiedRepository repo = mock(IUnifiedRepository.class);
    // simulate path does not exist
    doReturn(null).when(repo).getFile(publicDirPath + "/" + intermediateDir);
    mp.defineInstance(IUnifiedRepository.class, repo);
    RepositoryFileWriter writer = new RepositoryFileWriter(filePath, "UTF-8");
    writer.write("test123");
    writer.close();
// test should fail because 'newdir' does not exist and the file writer
// should not create missing dirs
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Test(org.junit.Test)

Example 85 with IUnifiedRepository

use of org.pentaho.platform.api.repository2.unified.IUnifiedRepository in project pentaho-platform by pentaho.

the class RepositoryFileIoTest method testWriteToPath.

@Test
public void testWriteToPath() throws IOException {
    final String fileName = "test-file1.txt";
    final String filePath = publicDirPath + "/" + fileName;
    IUnifiedRepository repo = mock(IUnifiedRepository.class);
    RepositoryFile existingFile = new RepositoryFile.Builder("123", fileName).build();
    // simulate file already exists
    doReturn(existingFile).when(repo).getFile(filePath);
    mp.defineInstance(IUnifiedRepository.class, repo);
    RepositoryFileWriter writer = new RepositoryFileWriter(filePath, "UTF-8");
    writer.write("test123");
    writer.close();
    verify(repo).updateFile(argThat(isLikeFile(existingFile)), argThat(hasData("test123", "UTF-8", "text/plain")), anyString());
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Test(org.junit.Test)

Aggregations

IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)88 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)61 Test (org.junit.Test)51 Matchers.anyString (org.mockito.Matchers.anyString)37 ArrayList (java.util.ArrayList)18 List (java.util.List)10 StringObjectId (org.pentaho.di.repository.StringObjectId)10 Serializable (java.io.Serializable)9 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)7 RepositoryFileTree (org.pentaho.platform.api.repository2.unified.RepositoryFileTree)7 DataNode (org.pentaho.platform.api.repository2.unified.data.node.DataNode)7 InputStream (java.io.InputStream)6 JobMeta (org.pentaho.di.job.JobMeta)6 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)6 FileSystemBackedUnifiedRepository (org.pentaho.platform.repository2.unified.fs.FileSystemBackedUnifiedRepository)6 HashMap (java.util.HashMap)5 ObjectId (org.pentaho.di.repository.ObjectId)5 IDatasourceMgmtService (org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService)5 Properties (java.util.Properties)4 Log (org.apache.commons.logging.Log)4