diff --git a/source/java/org/alfresco/repo/search/impl/lucene/ADMLuceneCategoryTest.java b/source/java/org/alfresco/repo/search/impl/lucene/ADMLuceneCategoryTest.java index 08fc9ea5b9..4328804a99 100644 --- a/source/java/org/alfresco/repo/search/impl/lucene/ADMLuceneCategoryTest.java +++ b/source/java/org/alfresco/repo/search/impl/lucene/ADMLuceneCategoryTest.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Random; import javax.transaction.UserTransaction; @@ -35,6 +36,8 @@ import org.alfresco.repo.dictionary.IndexTokenisationMode; import org.alfresco.repo.dictionary.M2Aspect; import org.alfresco.repo.dictionary.M2Model; import org.alfresco.repo.dictionary.M2Property; +import org.alfresco.repo.jscript.CategoryNode; +import org.alfresco.repo.jscript.ClasspathScriptLocation; import org.alfresco.repo.search.IndexerAndSearcher; import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer; import org.alfresco.repo.security.authentication.AuthenticationUtil; @@ -45,6 +48,8 @@ import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.ScriptLocation; +import org.alfresco.service.cmr.repository.ScriptService; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.search.CategoryService; import org.alfresco.service.cmr.search.ResultSet; @@ -70,7 +75,9 @@ public class ADMLuceneCategoryTest extends TestCase static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); NodeService nodeService; + NodeService publicNodeService; DictionaryService dictionaryService; + ScriptService scriptService; private NodeRef rootNodeRef; private NodeRef n1; private NodeRef n2; @@ -133,6 +140,7 @@ public class ADMLuceneCategoryTest extends TestCase public void setUp() throws Exception { nodeService = (NodeService)ctx.getBean("dbNodeService"); + publicNodeService = (NodeService)ctx.getBean("NodeService"); dictionaryService = (DictionaryService)ctx.getBean("dictionaryService"); luceneFTS = (FullTextSearchIndexer) ctx.getBean("LuceneFullTextSearchIndexer"); dictionaryDAO = (DictionaryDAO) ctx.getBean("dictionaryDAO"); @@ -142,6 +150,7 @@ public class ADMLuceneCategoryTest extends TestCase categoryService = (CategoryService) ctx.getBean("categoryService"); serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); tenantService = (TenantService) ctx.getBean("tenantService"); + scriptService = (ScriptService) ctx.getBean("scriptService"); AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); @@ -383,7 +392,7 @@ public class ADMLuceneCategoryTest extends TestCase searcher.setNodeService(nodeService); searcher.setDictionaryService(dictionaryService); searcher.setTenantService(tenantService); - searcher.setNamespacePrefixResolver(getNamespacePrefixReolsver("")); + searcher.setNamespacePrefixResolver(getNamespacePrefixResolver("")); searcher.setQueryLanguages(indexerAndSearcher.getQueryLanguages()); return searcher; } @@ -599,7 +608,8 @@ public class ADMLuceneCategoryTest extends TestCase LuceneCategoryServiceImpl impl = new LuceneCategoryServiceImpl(); impl.setNodeService(nodeService); - impl.setNamespacePrefixResolver(getNamespacePrefixReolsver("")); + impl.setPublicNodeService(publicNodeService); + impl.setNamespacePrefixResolver(getNamespacePrefixResolver("")); impl.setIndexerAndSearcher(indexerAndSearcher); impl.setTenantService(tenantService); impl.setDictionaryService(dictionaryService); @@ -640,10 +650,57 @@ public class ADMLuceneCategoryTest extends TestCase Collection aspects = impl.getClassificationAspects(); assertEquals(7, aspects.size()); + + // Add an additional subcategory, and check it shows up + result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY); + assertEquals(3, result.size()); + + String newName = "TestSub"; + impl.createCategory(catACBase, newName); + + result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY); + assertEquals(4, result.size()); + + + // Tidy up tx.rollback(); } - private NamespacePrefixResolver getNamespacePrefixReolsver(String defaultURI) + /** + * JavaScript tests for the Category Service. + * Note that this test lives here because that's where the Java + * CategeoryService tests are, and this also has all the required + * SetUp and TearDown to make it easier. + */ + public void testJavascriptAPI() throws Exception + { + TransactionService transactionService = serviceRegistry.getTransactionService(); + UserTransaction tx = transactionService.getUserTransaction(); + tx.begin(); + buildBaseIndex(); + + ServiceRegistry services = (ServiceRegistry)ctx.getBean("ServiceRegistry"); + + // Call the test + Map model = new HashMap(); + model.put("catRoot", new CategoryNode(catRoot, services)); + model.put("catACBase", new CategoryNode(catACBase, services)); + model.put("catACOne", new CategoryNode(catACOne, services)); + model.put("catACTwo", new CategoryNode(catACTwo, services)); + model.put("catACThree", new CategoryNode(catACThree, services)); + model.put("catRBase", new CategoryNode(catRBase, services)); + model.put("catROne", new CategoryNode(catROne, services)); + model.put("catRTwo", new CategoryNode(catRTwo, services)); + model.put("catRThree", new CategoryNode(catRThree, services)); + + ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/search/impl/lucene/test_categoryService.js"); + this.scriptService.executeScript(location, model); + + // Tidy up + tx.rollback(); + } + + private NamespacePrefixResolver getNamespacePrefixResolver(String defaultURI) { DynamicNamespacePrefixResolver nspr = new DynamicNamespacePrefixResolver(null); nspr.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI); diff --git a/source/java/org/alfresco/repo/search/impl/lucene/LuceneCategoryServiceImpl.java b/source/java/org/alfresco/repo/search/impl/lucene/LuceneCategoryServiceImpl.java index 16b41bda45..8eb399b1d1 100644 --- a/source/java/org/alfresco/repo/search/impl/lucene/LuceneCategoryServiceImpl.java +++ b/source/java/org/alfresco/repo/search/impl/lucene/LuceneCategoryServiceImpl.java @@ -187,8 +187,13 @@ public class LuceneCategoryServiceImpl implements CategoryService break; } - resultSet = indexerAndSearcher.getSearcher(categoryRef.getStoreRef(), false).query(categoryRef.getStoreRef(), "lucene", luceneQuery.toString(), null); + // Get a searcher that will include Categories added in this transaction + SearchService searcher = indexerAndSearcher.getSearcher(categoryRef.getStoreRef(), true); + + // Perform the search + resultSet = searcher.query(categoryRef.getStoreRef(), "lucene", luceneQuery.toString(), null); + // Convert from search results to the required Child Assocs return resultSetToChildAssocCollection(resultSet); } finally diff --git a/source/java/org/alfresco/repo/search/impl/lucene/test_categoryService.js b/source/java/org/alfresco/repo/search/impl/lucene/test_categoryService.js new file mode 100644 index 0000000000..e52fe6d686 --- /dev/null +++ b/source/java/org/alfresco/repo/search/impl/lucene/test_categoryService.js @@ -0,0 +1,69 @@ +// Checks the finding and adding of Root Categories +function testRootCategories() +{ + // TODO + // CategoryNode[] getAllCategoryNodes(String aspect) + // string[] getAllClassificationAspects() + // CategoryNode createRootCategory(string aspect, string name) + // CategoryNode[] getRootCategories(string aspect) +} + +// Checks that we can correctly query a test category +// that ADMLuceneCategoryTest setup for us +function testCategoryListings() +{ + // Check things are correctly detected as categories + test.assertEquals(true, catACBase.isCategory); + test.assertEquals(true, catACOne.isCategory); + test.assertEquals(true, catACTwo.isCategory); + test.assertEquals(true, catACThree.isCategory); + + // These tests are taken from testCategoryServiceImpl() + + //result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.IMMEDIATE); + test.assertEquals(1, catACBase.immediateCategoryMembers.length); + + //result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.IMMEDIATE); + test.assertEquals(3, catACBase.immediateMembersAndSubCategories.length); + + //result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE); + test.assertEquals(2, catACBase.immediateSubCategories.length); + + //result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.ANY); + test.assertEquals(14, catACBase.categoryMembers.length); + + //result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.ANY); + test.assertEquals(17, catACBase.membersAndSubCategories.length); + + //result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY); + test.assertEquals(3, catACBase.subCategories.length); +} + +// Tests that we can add and remove sub-categories +function testSubCategories() +{ + // catACBase starts with 3 + test.assertEquals(3, catACBase.subCategories.length); + + // Add a 4th + var testCat = catACBase.createSubCategory("testCat"); + test.assertEquals(4, catACBase.subCategories.length); + + // Delete it + testCat.removeCategory(); + test.assertEquals(3, catACBase.subCategories.length); + + // Add 2 more, as parent/child + var testCatA = catACBase.createSubCategory("testCatA"); + var testCatB = testCatA.createSubCategory("testCatB"); + test.assertEquals(5, catACBase.subCategories.length); + + // Delete the parent + testCatA.removeCategory(); + test.assertEquals(3, catACBase.subCategories.length); +} + +// Execute Tests +testRootCategories(); +testCategoryListings(); +testSubCategories(); \ No newline at end of file