Files
alfresco-community-repo/source/test-java/org/alfresco/repo/tenant/AbstractTenantRoutingContentStoreTest.java
Raluca Munteanu 8674e2bfc8 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 12:48:49 +00:00

161 lines
6.3 KiB
Java

package org.alfresco.repo.tenant;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.test_category.OwnJVMTestsCategory;
import org.alfresco.util.ApplicationContextHelper;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;
/**
* Test checking the behaviour of the MT {@link ContentStore} routing
*
* @author Alfresco
* @since 4.2.1
*/
@Category(OwnJVMTestsCategory.class)
public class AbstractTenantRoutingContentStoreTest extends TestCase
{
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private ContentService contentService;
private ServiceRegistry serviceRegistry;
private AbstractTenantRoutingContentStore fileContentStore;
private boolean isNullEntry = false;
@Override
public void setUp() throws Exception
{
AuthenticationUtil.setRunAsUserSystem();
serviceRegistry = (ServiceRegistry) ctx.getBean("ServiceRegistry");
contentService = serviceRegistry.getContentService();
fileContentStore = (AbstractTenantRoutingContentStore) ctx.getBean("fileContentStore");
}
public void testGetAllStores() throws Exception
{
final RetryingTransactionCallback<Object> txnWork = new RetryingTransactionCallback<Object>()
{
public Boolean execute() throws Exception
{
// The listener
final TestAfterRollbackTxnListener listener = new TestAfterRollbackTxnListener();
AlfrescoTransactionSupport.bindListener(listener);
NodeRef content = createContent(serviceRegistry);
ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("The quick brown fox jumps over the lazy dog");
throw new AlfrescoRuntimeException("Some error that causes RollBack. The transaction will be closed");
}
};
try
{
serviceRegistry.getTransactionService().getRetryingTransactionHelper().doInTransaction(txnWork, false, false);
}
catch (AlfrescoRuntimeException e)
{
// Expected
}
assertFalse("getAllStores method returned the list with null entry", isNullEntry);
}
// helper methods and listener
private void checkStores(List<ContentStore> stores)
{
// check that list is not empty
assertEquals(false, stores.isEmpty());
// check for null entry
for (ContentStore store : stores)
{
if (store == null)
{
isNullEntry = true;
}
}
}
private NodeRef createContent(ServiceRegistry serviceRegistry) throws Exception
{
AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
authenticationService.authenticate("admin", "admin".toCharArray());
SearchService searchService = serviceRegistry.getSearchService();
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
try
{
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home\"");
NodeRef companyHome = resultSet.getNodeRef(0);
serviceRegistry.getFileFolderService().create(companyHome, "Test", ContentModel.TYPE_FOLDER);
resultSet.close();
}
catch (Exception e)
{
}
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home/cm:Test\"");
NodeRef node = resultSet.getNodeRef(0);
resultSet.close();
// assign name
String name = "TestContent(" + Thread.currentThread().getName() + " " + System.currentTimeMillis() + ")";
Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
contentProps.put(ContentModel.PROP_NAME, name);
// create content node
NodeService nodeService = serviceRegistry.getNodeService();
ChildAssociationRef association = nodeService.createNode(node, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name),
ContentModel.TYPE_CONTENT, contentProps);
NodeRef content = association.getChildRef();
// add titled aspect (for Web Client display)
Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
titledProps.put(ContentModel.PROP_TITLE, name);
titledProps.put(ContentModel.PROP_DESCRIPTION, name);
nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps);
return content;
}
private class TestAfterRollbackTxnListener extends TransactionListenerAdapter
{
@Override
public void afterRollback()
{
checkStores(fileContentStore.getAllStores());
}
}
}