use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.
the class FileServiceTest method doSetMetadata.
@Test
public void doSetMetadata() {
String pathId = "path:to:file:file1.ext";
List<StringKeyStringValueDto> stringKeyStringValueDtos = new ArrayList<StringKeyStringValueDto>();
StringKeyStringValueDto stringKeyStringValueDto1 = mock(StringKeyStringValueDto.class);
doReturn("key1").when(stringKeyStringValueDto1).getKey();
doReturn("value1").when(stringKeyStringValueDto1).getValue();
StringKeyStringValueDto stringKeyStringValueDto2 = mock(StringKeyStringValueDto.class);
doReturn("key2").when(stringKeyStringValueDto2).getKey();
doReturn("value2").when(stringKeyStringValueDto2).getValue();
stringKeyStringValueDtos.add(stringKeyStringValueDto1);
stringKeyStringValueDtos.add(stringKeyStringValueDto2);
doReturn("/path/to/file/file1.ext").when(fileService).idToPath(pathId);
doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
RepositoryFileDto file = mock(RepositoryFileDto.class);
doReturn(false).when(file).isFolder();
doReturn(true).when(file).isHidden();
doReturn(file).when(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
List<RepositoryFileAclAceDto> repositoryFileAclAceDtos = new ArrayList<RepositoryFileAclAceDto>();
RepositoryFileAclDto repositoryFileAclDto = mock(RepositoryFileAclDto.class);
doReturn("sessionName").when(repositoryFileAclDto).getOwner();
doReturn(true).when(repositoryFileAclDto).isEntriesInheriting();
doReturn(repositoryFileAclAceDtos).when(repositoryFileAclDto).getAces();
doReturn(repositoryFileAclDto).when(fileService.defaultUnifiedRepositoryWebService).getAcl(anyString());
IPentahoSession pentahoSession = mock(IPentahoSession.class);
doReturn(pentahoSession).when(fileService).getSession();
doReturn("sessionName").when(pentahoSession).getName();
RepositoryFileAclAceDto repositoryFileAclAceDto = mock(RepositoryFileAclAceDto.class);
List<Integer> permissions = new ArrayList<Integer>();
permissions.add(RepositoryFilePermission.ACL_MANAGEMENT.ordinal());
doReturn(permissions).when(repositoryFileAclAceDto).getPermissions();
doReturn("sessionName").when(repositoryFileAclAceDto).getRecipient();
repositoryFileAclAceDtos.add(repositoryFileAclAceDto);
doReturn(repositoryFileAclAceDtos).when(fileService.defaultUnifiedRepositoryWebService).getEffectiveAces(anyString());
Map<String, Serializable> metadata = new HashMap<String, Serializable>();
doReturn(metadata).when(fileService.repository).getFileMetadata(anyString());
RepositoryFile sourceFile = mock(RepositoryFile.class);
doReturn(sourceFile).when(fileService.repository).getFileById(anyString());
RepositoryFileDto destFileDto = mock(RepositoryFileDto.class);
doReturn(destFileDto).when(fileService).toFileDto(sourceFile, null, false);
RepositoryFile destFile = mock(RepositoryFile.class);
doReturn(destFile).when(fileService).toFile(destFileDto);
RepositoryFileAcl acl = mock(RepositoryFileAcl.class);
doReturn(acl).when(fileService.repository).getAcl(acl);
// Test 1 - canManage should be true at start
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 2 - canManage should be false at start
doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 3 - canManage should be false at start
doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 4 - canManage should be false at start
doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 5 - canManage should be false at start
doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 6 - canManage should be false at start
doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 7 - canManage should be false at start
doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 8 - canManage should be false at start
doReturn(true).when(file).isFolder();
doReturn(true).when(file).isHidden();
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
// Test 9
StringKeyStringValueDto stringKeyStringValueDto3 = mock(StringKeyStringValueDto.class);
doReturn("_PERM_HIDDEN").when(stringKeyStringValueDto3).getKey();
doReturn("true").when(stringKeyStringValueDto3).getValue();
stringKeyStringValueDtos.add(stringKeyStringValueDto3);
try {
fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
} catch (GeneralSecurityException e) {
fail();
}
verify(fileService.defaultUnifiedRepositoryWebService, times(9)).getFile(anyString());
verify(fileService.defaultUnifiedRepositoryWebService, times(9)).getAcl(anyString());
verify(repositoryFileAclDto, times(9)).getOwner();
verify(fileService.policy, times(11)).isAllowed(anyString());
verify(fileService.repository, times(9)).getFileMetadata(anyString());
verify(fileService.repository, times(7)).setFileMetadata(anyString(), any(Map.class));
verify(file, times(8)).setHidden(anyBoolean());
verify(fileService.repository, times(8)).getFileById(anyString());
verify(fileService, times(8)).toFileDto(any(RepositoryFile.class), anySet(), anyBoolean());
verify(fileService, times(8)).toFile(any(RepositoryFileDto.class));
verify(destFileDto, times(8)).setHidden(anyBoolean());
verify(fileService.repository, times(8)).getAcl(anyString());
verify(fileService.repository, times(7)).updateFile(any(RepositoryFile.class), any(IRepositoryFileData.class), anyString());
verify(fileService.repository, times(7)).updateAcl(any(RepositoryFileAcl.class));
verify(fileService.repository).updateFolder(any(RepositoryFile.class), anyString());
}
use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.
the class OlapServiceImplTest method testRemoveOlap4jCatalogsWithoutPermission.
/**
* Validates getting a list of remote catalogs.
*/
@Test
public void testRemoveOlap4jCatalogsWithoutPermission() throws Exception {
stubGetChildren(repository, olapFolderPath, "myServer");
// Stub /etc/olap-servers/myServer
final String testServerPath = olapFolderPath + RepositoryFile.SEPARATOR + "myServer";
stubGetFolder(repository, testServerPath);
stubGetChildren(repository, testServerPath, "metadata");
// Stub /etc/olap-servers/myServer/metadata
final String metadataPath = testServerPath + RepositoryFile.SEPARATOR + "metadata";
stubGetFile(repository, metadataPath);
stubGetData(repository, metadataPath + RepositoryFile.SEPARATOR + "myServer", "server", pathPropertyPair("/server/name", "myServer"), pathPropertyPair("/server/user", "myUser"), pathPropertyPair("/server/password", "myPassword"), pathPropertyPair("/server/URL", "myUrl"), pathPropertyPair("/server/className", "someClass"));
// Get a list of catalogs.
final List<String> catalogs = olapService.getCatalogNames(session);
assertEquals(1, catalogs.size());
assertEquals("myServer", catalogs.get(0));
verify(repository).getChildren(eq(makeIdObject(olapFolderPath)));
// Stub the security
accessMock = new DefaultAccessImpl() {
public boolean hasAccess(String path, EnumSet<RepositoryFilePermission> perms, IPentahoSession session) {
if (perms.contains(RepositoryFilePermission.DELETE) && path.equals("myServer")) {
return false;
}
return true;
}
};
// Try to delete it. We expect it to fail.
try {
olapService.removeCatalog("myServer", session);
fail();
} catch (IOlapServiceException e) {
assertEquals(IOlapServiceException.Reason.ACCESS_DENIED, e.getReason());
assertTrue(e.getMessage().contains("OlapServiceImpl.ERROR_0003"));
}
// Make sure we didn't invoke the delete method.
verify(repository, never()).deleteFile((RepositoryFile) anyObject(), anyString());
}
use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.
the class RepositoryImportResourceTest method setUp.
@Before
public void setUp() throws ObjectFactoryException, PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
PentahoSystem.init();
IPentahoSession session = mock(IPentahoSession.class);
doReturn("sampleSession").when(session).getName();
PentahoSessionHolder.setSession(session);
handler = mock(SolutionImportHandler.class);
importer = mock(PentahoPlatformImporter.class);
policy = mock(IAuthorizationPolicy.class);
ITenant tenat = mock(ITenant.class);
resolver = mock(ITenantedPrincipleNameResolver.class);
doReturn(tenat).when(resolver).getTenant(anyString());
doReturn(REAL_USER).when(resolver).getPrincipleName(anyString());
policy = mock(IAuthorizationPolicy.class);
pentahoObjectFactory = mock(IPentahoObjectFactory.class);
iPlatformMimeResolver = mock(NameBaseMimeResolver.class);
iRepositoryImportLogger = mock(IRepositoryImportLogger.class);
catalogService = mock(MondrianCatalogHelper.class);
doReturn("xml").when(iPlatformMimeResolver).resolveMimeForFileName("");
doReturn(iRepositoryImportLogger).when(importer).getRepositoryImportLogger();
// for calling importFile in RepositoryImportResource
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
handler.importFile(any(IPlatformImportBundle.class));
return null;
}
}).when(importer).importFile(any(IPlatformImportBundle.class));
// for calling importFile in PentahoPlatformImporter
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
handler.getImportSession();
return null;
}
}).when(handler).importFile(any(IPlatformImportBundle.class));
// for calling getImportSession in SolutionImportHandler
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
ImportSession importsession = ImportSession.getSession();
importsession.setManifest(mock(ExportManifest.class));
return null;
}
}).when(handler).getImportSession();
when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].equals(IAuthorizationPolicy.class)) {
return policy;
}
if (invocation.getArguments()[0].equals(ITenantedPrincipleNameResolver.class)) {
return resolver;
}
if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
return catalogService;
}
return null;
}
});
PentahoSystem.registerObjectFactory(pentahoObjectFactory);
PentahoSystem.registerObject(iPlatformMimeResolver);
PentahoSystem.registerObject(iRepositoryImportLogger);
PentahoSystem.registerObject(catalogService);
PentahoSystem.registerObject(handler);
PentahoSystem.registerObject(importer);
}
use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.
the class SessionResourceTest method testDoGetCurrentUserDir.
@Test
public void testDoGetCurrentUserDir() {
IPentahoSession mockPentahoSession = mock(IPentahoSession.class);
doReturn(mockPentahoSession).when(sessionResource).getSession();
String username = "username";
doReturn(username).when(mockPentahoSession).getName();
String userHomeFolderPath = "path";
doReturn(userHomeFolderPath).when(sessionResource).getUserHomeFolderPath(username);
String testString = sessionResource.doGetCurrentUserDir();
assertEquals(userHomeFolderPath + "/workspace", testString);
verify(sessionResource, times(1)).getUserHomeFolderPath(username);
verify(sessionResource, times(1)).getSession();
verify(mockPentahoSession, times(1)).getName();
}
use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.
the class FileResourceTest method testDoIsParameterizable.
@Test
public void testDoIsParameterizable() throws Exception {
String path = "path";
doReturn(path).when(fileResource.fileService).idToPath(PATH_ID);
RepositoryFile mockRepositoryFile = mock(RepositoryFile.class);
doReturn(mockRepositoryFile).when(fileResource.repository).getFile(path);
doReturn(true).when(fileResource).hasParameterUi(mockRepositoryFile);
IContentGenerator mockContentGenerator = mock(IContentGenerator.class);
doReturn(mockContentGenerator).when(fileResource).getContentGenerator(mockRepositoryFile);
SimpleParameterProvider mockSimpleParameterProvider = mock(SimpleParameterProvider.class);
doReturn(mockSimpleParameterProvider).when(fileResource).getSimpleParameterProvider();
String repositoryPath = "repositoryPath";
doReturn(repositoryPath).when(mockRepositoryFile).getPath();
String encodedPath = "encodedPath";
doReturn(encodedPath).when(fileResource).encode(repositoryPath);
IPentahoSession mockPentahoSession = mock(IPentahoSession.class);
doReturn(mockPentahoSession).when(fileResource).getSession();
ByteArrayOutputStream mockByteArrayOutputStream = mock(ByteArrayOutputStream.class);
doReturn(mockByteArrayOutputStream).when(fileResource).getByteArrayOutputStream();
doReturn(1).when(mockByteArrayOutputStream).size();
String outputStreamToString = "outputStreamToString";
doReturn(outputStreamToString).when(mockByteArrayOutputStream).toString();
Document mockDocument = mock(Document.class);
doReturn(mockDocument).when(fileResource).parseText(outputStreamToString);
String selectNodesParam = "parameters/parameter";
List<Element> elements = new ArrayList<Element>();
doReturn(elements).when(mockDocument).selectNodes(selectNodesParam);
Element mockElement = mock(Element.class);
doReturn("output-target").when(mockElement).attributeValue("name");
doReturn("true").when(mockElement).attributeValue("is-mandatory");
// Test 1
String testString = fileResource.doIsParameterizable(PATH_ID);
assertEquals(Boolean.FALSE.toString(), testString);
// Test 2
elements.add(mockElement);
testString = fileResource.doIsParameterizable(PATH_ID);
assertEquals(Boolean.TRUE.toString(), testString);
// Test 3
doReturn("false").when(mockElement).attributeValue("is-mandatory");
testString = fileResource.doIsParameterizable(PATH_ID);
assertEquals(Boolean.TRUE.toString(), testString);
// Test 4
Element mockAttribElement = mock(Element.class);
doReturn(mockAttribElement).when(mockElement).selectSingleNode("attribute[@namespace='http://reporting.pentaho" + ".org/namespaces/engine/parameter-attributes/core' and @name='role']");
testString = fileResource.doIsParameterizable(PATH_ID);
assertEquals(Boolean.TRUE.toString(), testString);
verify(fileResource.fileService, times(4)).idToPath(PATH_ID);
verify(fileResource.repository, times(4)).getFile(path);
verify(fileResource, times(4)).hasParameterUi(mockRepositoryFile);
verify(fileResource, times(4)).getContentGenerator(mockRepositoryFile);
verify(mockContentGenerator, times(4)).setOutputHandler(any(SimpleOutputHandler.class));
verify(mockContentGenerator, times(4)).setMessagesList(anyList());
verify(fileResource, times(4)).getSimpleParameterProvider();
verify(mockRepositoryFile, times(4)).getPath();
verify(fileResource, times(4)).encode(repositoryPath);
verify(mockSimpleParameterProvider, times(4)).setParameter("path", encodedPath);
verify(mockSimpleParameterProvider, times(4)).setParameter("renderMode", "PARAMETER");
verify(mockContentGenerator, times(4)).setParameterProviders(anyMap());
verify(fileResource, times(4)).getSession();
verify(mockContentGenerator, times(4)).setSession(mockPentahoSession);
verify(mockContentGenerator, times(4)).createContent();
verify(fileResource, times(4)).getByteArrayOutputStream();
verify(mockDocument, times(4)).selectNodes(selectNodesParam);
verify(mockElement, times(3)).attributeValue("name");
verify(mockElement, times(3)).attributeValue("is-mandatory");
verify(mockAttribElement, times(1)).attributeValue("value");
}
Aggregations