Author: echatellier Date: 2012-01-26 14:32:44 +0100 (Thu, 26 Jan 2012) New Revision: 1371 Url: http://nuiton.org/repositories/revision/wikitty/1371 Log: Move all tests from StorageTest (can't be removed yet) Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/conform/StorageTest.java Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java 2012-01-26 13:28:55 UTC (rev 1370) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java 2012-01-26 13:32:44 UTC (rev 1371) @@ -59,6 +59,10 @@ * * Just init object, and load defaut and import data. Asserts are subclassed. * + * Abstract test defini and use to data type: + * - Business entity : generated from modele + * - Manual extension : movies + * * @author chatellier * @version $Revision$ * @@ -203,39 +207,51 @@ client.store(rootNode, livreNode, bdNode, newsNode, romanNode, otherNode); } + /** Manual extension name : media. */ + protected static final String MEDIA_EXTENSION_NAME = "Media"; + /** Manual extension media. */ + protected static final WikittyExtension MEDIA_EXTENSION = ExtensionFactory.create(MEDIA_EXTENSION_NAME, "6.0") + .addField("type", WikittyTypes.STRING) + .extension(); + protected static final String MOVIE_EXTENSION_NAME = "Movies"; + /** Manual extension movies. */ + protected static final WikittyExtension MOVIE_EXTENSION = ExtensionFactory.create(MOVIE_EXTENSION_NAME, "2.0", MEDIA_EXTENSION_NAME) + .addField("name", WikittyTypes.STRING) + .addField("authors", WikittyTypes.STRING).maxOccur(Integer.MAX_VALUE) + .addField("category", WikittyTypes.WIKITTY) + .addField("date", WikittyTypes.DATE) + .extension(); + /** * Create new "movies" extension. * * @param wikittyClient client */ protected void addManualExtension(WikittyClient client) { - // create extension - WikittyExtension movieExtension = ExtensionFactory.create("Movies", "1.0") - .addField("name", WikittyTypes.STRING) - .addField("author", WikittyTypes.STRING).maxOccur(Integer.MAX_VALUE) - .addField("category", WikittyTypes.WIKITTY) - .addField("year", WikittyTypes.STRING) - .extension(); - client.storeExtension(movieExtension); + client.storeExtension(MOVIE_EXTENSION); + // create wikitty movies Wikitty dieHardMovie = new WikittyImpl(); - dieHardMovie.addExtension(movieExtension); - dieHardMovie.setField("Movies", "name", "Die hard 4"); - dieHardMovie.addToField("Movies", "author", "Willis"); - dieHardMovie.addToField("Movies", "year", "2009"); + dieHardMovie.addExtension(MEDIA_EXTENSION); + dieHardMovie.addExtension(MOVIE_EXTENSION); + dieHardMovie.setField(MOVIE_EXTENSION_NAME, "name", "Die hard 4"); + dieHardMovie.addToField(MOVIE_EXTENSION_NAME, "authors", "Willis"); + dieHardMovie.setField(MOVIE_EXTENSION_NAME, "date", "04/02/2009"); Wikitty edgarMovie = new WikittyImpl(); - edgarMovie.addExtension(movieExtension); - edgarMovie.setField("Movies", "name", "J. Edgar"); - edgarMovie.addToField("Movies", "author", "Eastwood"); - edgarMovie.addToField("Movies", "year", "2011"); + edgarMovie.addExtension(MEDIA_EXTENSION); + edgarMovie.addExtension(MOVIE_EXTENSION); + edgarMovie.setField(MOVIE_EXTENSION_NAME, "name", "J. Edgar"); + edgarMovie.addToField(MOVIE_EXTENSION_NAME, "authors", "Eastwood"); + edgarMovie.setField("Movies", "date", "25/12/2011"); Wikitty dnrMovie = new WikittyImpl(); - dnrMovie.addExtension(movieExtension); - dnrMovie.setField("Movies", "name", "The Dark Knight Rises"); - dnrMovie.addToField("Movies", "author", "Nolan"); - dnrMovie.addToField("Movies", "year", "2012"); + dnrMovie.addExtension(MEDIA_EXTENSION); + dnrMovie.addExtension(MOVIE_EXTENSION); + dnrMovie.setField(MOVIE_EXTENSION_NAME, "name", "The Dark Knight Rises"); + dnrMovie.addToField(MOVIE_EXTENSION_NAME, "authors", "Nolan"); + dnrMovie.setField(MOVIE_EXTENSION_NAME, "date", "13/03/2012"); client.store(dieHardMovie, edgarMovie, dnrMovie); } Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java 2012-01-26 13:28:55 UTC (rev 1370) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java 2012-01-26 13:32:44 UTC (rev 1371) @@ -28,11 +28,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Assert; import org.junit.Test; import org.nuiton.wikitty.addons.WikittyI18nTestUtil; @@ -41,6 +47,7 @@ import org.nuiton.wikitty.addons.WikittyImportExportService.FORMAT; import org.nuiton.wikitty.addons.WikittyLabelUtil; import org.nuiton.wikitty.entities.Element; +import org.nuiton.wikitty.entities.ElementField; import org.nuiton.wikitty.entities.ExtensionFactory; import org.nuiton.wikitty.entities.Wikitty; import org.nuiton.wikitty.entities.WikittyExtension; @@ -49,11 +56,15 @@ import org.nuiton.wikitty.entities.WikittyLabel; import org.nuiton.wikitty.entities.WikittyLabelImpl; import org.nuiton.wikitty.entities.WikittyTreeNode; +import org.nuiton.wikitty.entities.WikittyTreeNodeHelper; +import org.nuiton.wikitty.entities.WikittyTreeNodeImpl; import org.nuiton.wikitty.entities.WikittyTypes; import org.nuiton.wikitty.query.WikittyQuery; import org.nuiton.wikitty.query.WikittyQueryMaker; import org.nuiton.wikitty.query.WikittyQueryParser; import org.nuiton.wikitty.query.WikittyQueryResult; +import org.nuiton.wikitty.query.WikittyQueryResultTreeNode; +import org.nuiton.wikitty.services.WikittyEvent; import org.nuiton.wikitty.test.Category; import org.nuiton.wikitty.test.Product; @@ -63,6 +74,9 @@ * This test is designed to be overridden and change {@link WikittyClient} * implementation. * + * Test begin with 'testStorage' focus on store/restore. + * Test begin with 'testFind' or 'testSearch' focus on search engine. + * * @author chatellier * @version $Revision$ * @@ -71,7 +85,547 @@ */ public class WikittyClientTest extends WikittyClientAbstractTest { + static private Log log = LogFactory.getLog(WikittyClientTest.class); + + @Test + public void testStorageRestoreBasics() { + // create basic wikitty + Wikitty w = new WikittyImpl(); + w.addExtension(MEDIA_EXTENSION); + w.addExtension(MOVIE_EXTENSION); + + String id = w.getId(); + w = wikittyClient.store(w); + w = wikittyClient.restore(id); + Assert.assertTrue(w.hasExtension(MOVIE_EXTENSION_NAME)); + Assert.assertTrue(w.hasField("Movies", "name")); + Assert.assertTrue(w.hasField("Movies", "authors")); + Assert.assertTrue(w.hasField("Movies", "category")); + Assert.assertTrue(w.hasField("Movies", "date")); + } + + @Test + public void testStorageExtensionMethod() { + // store extension + List<String> extIds = wikittyClient.getAllExtensionIds(); + log.debug("extIds: " + extIds); + Assert.assertEquals(8, extIds.size()); + Assert.assertTrue(extIds.contains("Movies[2.0]")); + WikittyExtension ext = wikittyClient.restoreExtension("Movies[2.0]"); + Assert.assertEquals("Movies", ext.getName()); + Assert.assertEquals("2.0", ext.getVersion()); + Assert.assertEquals(MOVIE_EXTENSION, ext); + } + /** + * Test que la suppression d'une extension est possible. + */ + @Test + public void testStorageExtensionDelete() { + // new one + WikittyExtension volatileExt = new WikittyExtension("VolatileExt", + "4.0", // version + WikittyUtil.buildFieldMapExtension( // building field map + "String name unique=\"true\"")); + wikittyClient.storeExtension(volatileExt); + + // test existance + List<String> extIds = wikittyClient.getAllExtensionIds(); + Assert.assertTrue(extIds.contains("VolatileExt[4.0]")); + + // delete + wikittyClient.deleteExtension("VolatileExt"); + + // restest existance + extIds = wikittyClient.getAllExtensionIds(); + Assert.assertFalse(extIds.contains("VolatileExt[4.0]")); + } + + /** + * Test que la suppression echoue si l'extension est utilisee. + */ + @Test(expected=WikittyException.class) + public void testStorageExtensionDeleteUsed() { + // try to delete it (throws WikittyException) + wikittyClient.deleteExtension(MOVIE_EXTENSION_NAME); + } + + @Test + public void testStorageExtensionRequires() { + wikittyClient.storeExtension(MEDIA_EXTENSION); + wikittyClient.storeExtension(MOVIE_EXTENSION); + List<String> extIds = wikittyClient.getAllExtensionsRequires(MEDIA_EXTENSION_NAME); + log.debug("extIds: " + extIds); + Assert.assertEquals(1, extIds.size()); + + WikittyExtension ext = wikittyClient.restoreExtension(extIds.get(0)); + Assert.assertEquals("Movies", ext.getName()); + Assert.assertEquals("2.0", ext.getVersion()); + Assert.assertEquals(MOVIE_EXTENSION, ext); + } + + /** + * Test seulement la methode restore(). + * + * @throws ParseException + */ + @Test + public void testStorageRestoreLists() throws ParseException { + + // create some wikitty + Wikitty gf1Movie = new WikittyImpl(); + gf1Movie.addExtension(MEDIA_EXTENSION); + gf1Movie.addExtension(MOVIE_EXTENSION); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather"); + gf1Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("March 15, 1972"))); + + Wikitty gf2Movie = new WikittyImpl(); + gf2Movie.addExtension(MEDIA_EXTENSION); + gf2Movie.addExtension(MOVIE_EXTENSION); + gf2Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather 2"); + gf2Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf2Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("October 12, 1974"))); + + Wikitty gf3Movie = new WikittyImpl(); + gf3Movie.addExtension(MEDIA_EXTENSION); + gf3Movie.addExtension(MOVIE_EXTENSION); + gf3Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather 3"); + gf3Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf3Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("July 30, 1990"))); + + wikittyClient.store(gf1Movie, gf2Movie, gf3Movie); + + List<String> ids = new ArrayList<String>(); + ids.add(gf1Movie.getId()); + ids.add(gf2Movie.getId()); + ids.add(gf3Movie.getId()); + + List<Wikitty> wikitties = wikittyClient.restore(ids); + + Assert.assertEquals("The godfather", wikitties.get(0).getFieldAsString(MOVIE_EXTENSION_NAME, "name")); + Assert.assertEquals("The godfather 2", wikitties.get(1).getFieldAsString(MOVIE_EXTENSION_NAME, "name")); + Assert.assertEquals("The godfather 3", wikitties.get(2).getFieldAsString(MOVIE_EXTENSION_NAME, "name")); + } + + @Test + public void testStorageRestoreSingle() throws Exception { + Wikitty w = new WikittyImpl(); + String id = w.getId(); + w = wikittyClient.store(w); + w = wikittyClient.restore(id); + Assert.assertEquals(id, w.getId()); + } + + @Test(expected=WikittyException.class) + public void testStorageRestoreNull() throws Exception { + wikittyClient.store((Wikitty)null); + } + + @Test + public void testFieldConstraint() throws Exception { + // Store ext + WikittyExtension ext = new WikittyExtension("TestConstraint", "1", + WikittyUtil.buildFieldMapExtension( + "String id notNull=true", + "String ext[0-n] unique=true", + "String other[0-n] unique=true notNull=true" + )); + wikittyClient.storeExtension(ext); + + // store wikitty + Wikitty w = new WikittyImpl(); + w.addExtension(ext); + + try { + w = wikittyClient.store(w); + Assert.fail("not null contraint don't work on String"); + } catch (WikittyException eee) { + eee.printStackTrace(); + // ok id must not be null + } + + w.setField("TestConstraint", "id", "toto"); + try { + w = wikittyClient.store(w); + Assert.fail("not null contraint don't work in Collection"); + } catch (WikittyException eee) { + eee.printStackTrace(); + // ok id must not be null + } + + w.addToField("TestConstraint", "other", "titi"); + w = wikittyClient.store(w); + + w.addToField("TestConstraint", "ext", "tata"); + w.addToField("TestConstraint", "ext", "titi"); // titi ne doit pas s'ajouter + w = wikittyClient.store(w); + + Wikitty w2 = wikittyClient.restore(w.getId()); + w2.addToField("TestConstraint", "ext", "tata"); // tata ne doit pas s'ajouter + w2.addToField("TestConstraint", "ext", "toto"); + + w2 = wikittyClient.store(w2); + Wikitty w3 = wikittyClient.restore(w.getId()); + + Set set = new HashSet(Arrays.asList("tata", "titi", "toto")); + Assert.assertEquals(set, w3.getFieldAsSet("TestConstraint", "ext", String.class)); + } + + @Test + public void testSearchByExampleFacet() throws Exception { + + // create some wikitty + Wikitty gf1Movie = new WikittyImpl(); + gf1Movie.addExtension(MEDIA_EXTENSION); + gf1Movie.addExtension(MOVIE_EXTENSION); + gf1Movie.setField(MEDIA_EXTENSION_NAME, "type", "movie"); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather"); + gf1Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("March 15, 1972"))); + + Wikitty gf2Movie = new WikittyImpl(); + gf2Movie.addExtension(MEDIA_EXTENSION); + gf2Movie.addExtension(MOVIE_EXTENSION); + gf2Movie.setField(MEDIA_EXTENSION_NAME, "type", "movie"); + gf2Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather 2"); + gf2Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf2Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("October 12, 1974"))); + + Wikitty gf3Movie = new WikittyImpl(); + gf3Movie.addExtension(MEDIA_EXTENSION); + gf3Movie.addExtension(MOVIE_EXTENSION); + gf3Movie.setField(MEDIA_EXTENSION_NAME, "type", "movie"); + gf3Movie.setField(MOVIE_EXTENSION_NAME, "name", "Fly me to the moon"); + gf3Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Pixar"); + gf3Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("October 12, 1974"))); + + // search test + Wikitty w = new WikittyImpl(); + w.addExtension(MEDIA_EXTENSION); + w.addExtension(MOVIE_EXTENSION); + w.setField(MEDIA_EXTENSION_NAME, "type", "movie"); + WikittyQuery query = new WikittyQueryMaker().wikitty(w).end(); + query.addFacetField(new ElementField(MOVIE_EXTENSION_NAME + ".authors")); + query.addFacetField(new ElementField(MOVIE_EXTENSION_NAME + ".date")); + WikittyQueryResult<String> result = wikittyClient.findAllByQuery(query); + + Assert.assertTrue(result.getFacetNames().contains(MOVIE_EXTENSION_NAME + ".date")); + Assert.assertTrue(result.getFacetNames().contains(MOVIE_EXTENSION_NAME + ".authors")); + + // with must have 2 topic: Pixar and Coppola + Assert.assertEquals(2, result.getTopic(MOVIE_EXTENSION_NAME + ".authors").size()); + Assert.assertEquals("Coppola", result.getTopic(MOVIE_EXTENSION_NAME + ".authors").get(0).getFacetName()); + Assert.assertEquals(2, result.getTopic(MOVIE_EXTENSION_NAME + ".authors").get(0).getCount()); + Assert.assertEquals("Pixar", result.getTopic(MOVIE_EXTENSION_NAME + ".authors").get(1).getFacetName()); + Assert.assertEquals(1, result.getTopic(MOVIE_EXTENSION_NAME + ".authors").get(1).getCount()); + // with must have 2 topic: March 15, 1972 and October 12, 1974 + Assert.assertEquals(2, result.getTopic(MOVIE_EXTENSION_NAME + ".date").size()); + + // essai de facettiser sur les extensions + query = new WikittyQueryMaker().keyword("*").end(); + query.setFirst(0); + query.setLimit(0); + query.addFacetField(Element.EXTENSION); + result = wikittyClient.findAllByQuery(query); + Assert.assertEquals(1, result.getFacetNames().size()); + Assert.assertNotNull(result.getFacets().get(Element.EXTENSION)); + } + + /** + * Test qu'une recherche fonctionne et que la même recherche apres + * suppression ne trouve plus l'element. + * + * @throws ParseException + */ + @Test + public void testStorageFindDelete() throws ParseException { + + // create some wikitty + Wikitty gf1Movie = new WikittyImpl(); + gf1Movie.addExtension(MEDIA_EXTENSION); + gf1Movie.addExtension(MOVIE_EXTENSION); + gf1Movie.setField(MEDIA_EXTENSION_NAME, "type", "movie"); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "name", "The godfather"); + gf1Movie.addToField(MOVIE_EXTENSION_NAME, "authors", "Coppola"); + gf1Movie.setField(MOVIE_EXTENSION_NAME, "date", WikittyUtil.formatDate(df.parse("March 15, 1972"))); + + Wikitty newWik = wikittyClient.store(gf1Movie); + Assert.assertTrue(WikittyUtil.versionGreaterThan(gf1Movie.getId(), newWik.getId())); + + // search test + Wikitty w = new WikittyImpl(); + w.addExtension(MOVIE_EXTENSION); + w.setField(MOVIE_EXTENSION_NAME, "name", "The godfather"); + WikittyQuery query = new WikittyQueryMaker().wikitty(w).end(); + Wikitty resultFind = wikittyClient.findByQuery(Wikitty.class, query); + + Assert.assertEquals(w.getFieldAsString(MOVIE_EXTENSION_NAME, "name"), + resultFind.getFieldAsString(MOVIE_EXTENSION_NAME, "name")); + + Assert.assertEquals(gf1Movie.getId(), resultFind.getId()); + // test equals implantation method + Assert.assertEquals(gf1Movie, resultFind); + + Assert.assertEquals(gf1Movie.getFieldAsString(MOVIE_EXTENSION_NAME, "name"), + resultFind.getFieldAsString(MOVIE_EXTENSION_NAME, "name")); + Assert.assertEquals(gf1Movie.getFieldAsDate(MOVIE_EXTENSION_NAME, "date"), + resultFind.getFieldAsDate(MOVIE_EXTENSION_NAME, "date")); + Assert.assertEquals(gf1Movie.getFieldAsSet(MOVIE_EXTENSION_NAME, "authors", String.class), + resultFind.getFieldAsSet(MOVIE_EXTENSION_NAME, "authors", String.class)); + + // test to find deleted wikitty + // test if solr index is coherent with database + WikittyQuery query1 = new WikittyQueryMaker().ideq(gf1Movie.getId()).end(); + Wikitty searchedWikitty1 = wikittyClient.findByQuery(Wikitty.class, query1); + Assert.assertNotNull(searchedWikitty1); + + wikittyClient.delete(gf1Movie.getId()); + Wikitty deletedObject = wikittyClient.restore(gf1Movie.getId()); + Assert.assertNull(deletedObject); + + // test to find deleted wikitty + // test if solr index is coherent with database + Wikitty searchedWikitty2 = wikittyClient.findByQuery(Wikitty.class, query1); + Assert.assertNull(searchedWikitty2); + } + + @Test + public void testStorageAndLabel() throws Exception { + + // create some wikitty to permit search test + WikittyQuery query1 = new WikittyQueryMaker().exteq(MOVIE_EXTENSION_NAME).end(); + List<Wikitty> wikitties = wikittyClient.findAllByQuery(Wikitty.class, query1).getAll(); + long ts = new Date().getTime(); + + // labelisation test + Wikitty w1 = wikitties.get(0); + WikittyLabelUtil.addLabel(wikittyClient, w1.getId(), "titi"+ts); + WikittyLabelUtil.addLabel(wikittyClient, w1.getId(), "toto"+ts); + + Wikitty w2 = wikitties.get(1); + WikittyLabelUtil.addLabel(wikittyClient, w2.getId(), "tata"+ts); + WikittyLabelUtil.addLabel(wikittyClient, w2.getId(), "titi"+ts); + + Wikitty w3 = wikitties.get(2); + WikittyLabelUtil.addLabel(wikittyClient, w3.getId(), "tutu"+ts); + WikittyLabelUtil.addLabel(wikittyClient, w3.getId(), "titi"+ts); + + Wikitty wt = WikittyLabelUtil.findByLabel(wikittyClient, "toto"+ts ); + Assert.assertEquals(w1, wt); + + Set<String> labels = WikittyLabelUtil.findAllAppliedLabels(wikittyClient, w2.getId()); + Assert.assertEquals(new HashSet(Arrays.asList("tata"+ts, "titi"+ts)), labels); + } + + @Test + public void testStorageAndClassification() throws Exception { + // create some wikitty to permit search test + WikittyQuery query1 = new WikittyQueryMaker().exteq(MOVIE_EXTENSION_NAME).end(); + List<Wikitty> wikitties = wikittyClient.findAllByQuery(Wikitty.class, query1).getAll(); + List<Wikitty> wikittyNodes = new ArrayList<Wikitty>(); + + WikittyTreeNodeImpl root = new WikittyTreeNodeImpl(); + wikittyNodes.add(root.getWikitty()); + root.setName("MyCategoryRoot"); + for ( int i = 0; i < 3; i++ ) { + WikittyTreeNodeImpl leaf = new WikittyTreeNodeImpl(); + wikittyNodes.add( leaf.getWikitty() ); + leaf.setName( "cat-"+i ); + leaf.setParent( root.getWikittyId() ); + // root.addChild( leaf.getWikittyId() ); + + for ( int j = 0; j < 5; j++ ) { + WikittyTreeNodeImpl subLeaf = new WikittyTreeNodeImpl(); + subLeaf.setName( "subcat-"+i+"-"+j ); + subLeaf.setParent( leaf.getWikittyId() ); + wikittyNodes.add( subLeaf.getWikitty() ); + // leaf.addChild( subLeaf.getWikittyId() ); + } + } + wikittyNodes = wikittyClient.storeWikitty(wikittyNodes); // store treeNodes. + + Wikitty dieHard = wikitties.get(0); + Wikitty edgar = wikitties.get(1); + Wikitty knight = wikitties.get(2); + + assign( dieHard, root, "cat-1/subcat-1-4" ); + assign( edgar, root, "cat-1" ); + assign( knight, root, "cat-2/subcat-2-4" ); + + WikittyQueryResultTreeNode<WikittyTreeNode> t = wikittyClient.findTreeNode( + WikittyTreeNode.class, + root.getWikitty().getId(), 0, false, null); + Assert.assertEquals("MyCategoryRoot", t.getObject().getName()); + } + + /** + * @deprecated remove this undocumented method + */ + @Deprecated + protected void assign(Wikitty wikitty, WikittyTreeNodeImpl root, String path) { + String[] nodeNames = path.split("/"); + WikittyTreeNodeImpl currentNode = root; + outerloop : for( String nodeName : nodeNames ) { + WikittyTreeNodeImpl node = new WikittyTreeNodeImpl(); + node.setName(nodeName); + + WikittyQuery query = new WikittyQueryMaker().wikitty(node.getWikitty()).end(); + List<String> wikittiesId = wikittyClient.findAllByQuery(query).getAll(); + List<Wikitty> wikitties = wikittyClient.restore(wikittiesId); + for ( Wikitty child : wikitties ) { + if (!child.hasExtension(WikittyTreeNode.EXT_WIKITTYTREENODE)) { + continue; + } + node = new WikittyTreeNodeImpl(child); + if ( node.getName().equals(nodeName) ) { + currentNode = node; + continue outerloop; + } + } + Assert.fail( "Unable to find node " + nodeName + " inside " + currentNode.getName() ); + } + currentNode.addAttachment(wikitty.getId()); + wikittyClient.store(currentNode.getWikitty()); + } + + /** + * Test de la methode cast du wikitty client. + */ + @Test + public void testStorageCastTo() { + WikittyTreeNode node = new WikittyTreeNodeImpl(); + node.setName("nodeName"); + + // cast, different business object, same wikitty + WikittyLabel label = wikittyClient.castTo(WikittyLabel.class, node); + label.addLabels("testlabel"); + + label = wikittyClient.store(label); + String wikittyId = label.getWikittyId(); + + WikittyTreeNode node2 = wikittyClient.restore(WikittyTreeNode.class, wikittyId); + Assert.assertEquals("nodeName", node2.getName()); + + WikittyLabel label2 = wikittyClient.castTo(WikittyLabel.class, node); + List<String> labels = new ArrayList<String>(label2.getLabels()); + Assert.assertFalse(labels.isEmpty()); + Assert.assertEquals("testlabel", labels.get(0)); + } + + /** + * Test les differents cas du forcage de version lors du store. + */ + @Test + public void testStorageVersionForce() { + + // store 1 + Wikitty myWikitty = new WikittyImpl(); + myWikitty.addExtension(WikittyTreeNodeImpl.extensions); + WikittyTreeNodeHelper.setName(myWikitty, "name"); + myWikitty = wikittyClient.store(myWikitty); + Assert.assertEquals("1.0", myWikitty.getVersion()); + + // store 2 : no modification + myWikitty = wikittyClient.store(myWikitty); + Assert.assertEquals("1.0", myWikitty.getVersion()); + + // store 3 : modification + WikittyTreeNodeHelper.setName(myWikitty, "new name"); + myWikitty = wikittyClient.store(myWikitty); + Assert.assertEquals("2.0", myWikitty.getVersion()); + + // store 4 : new wikitty with same wikitty id (obsolete) + Wikitty myNewWikitty = new WikittyImpl(myWikitty.getId()); + myNewWikitty.addExtension(WikittyTreeNodeImpl.extensions); + WikittyTreeNodeHelper.setName(myNewWikitty, "new wikitty"); + try { + myWikitty = wikittyClient.store(myNewWikitty); + Assert.fail("Test must throw WikittyObsoleteException"); + } + catch (WikittyObsoleteException ex) { + if (log.isTraceEnabled()) { + log.trace("Wikitty obsolete", ex); + } + } + + // store 4 : same but with force (increased by force) + WikittyEvent event = wikittyClient.getWikittyService().store( + null, Collections.singletonList(myWikitty), true); + event.update(myWikitty); + Assert.assertEquals("3.0", myWikitty.getVersion()); + + // store 5 : fix version (not increased by force) + myNewWikitty.setVersion("11.0"); + event = wikittyClient.getWikittyService().store( + null, Collections.singletonList(myNewWikitty), true); + event.update(myNewWikitty); + Assert.assertEquals("11.0", myNewWikitty.getVersion()); + } + + /** + * Test qu'une sauvegarde de wikitty précédemment supprimé fonctionne. + */ + @Test + public void testStoragePreviouslyDeleted() { + + // store 1 + Wikitty myWikitty = new WikittyImpl(); + myWikitty.addExtension(WikittyTreeNodeImpl.extensions); + WikittyTreeNodeHelper.setName(myWikitty, "name"); + + myWikitty = wikittyClient.store(myWikitty); + + // delete + wikittyClient.delete(myWikitty.getId()); + Wikitty restoredWikitty = wikittyClient.restore(myWikitty.getId()); + Assert.assertNull(restoredWikitty); + + // store again + myWikitty = wikittyClient.store(myWikitty); + restoredWikitty = wikittyClient.restore(myWikitty.getId()); + Assert.assertNotNull(restoredWikitty); + } + + @Test + public void testStorageBinaryField() { + String extName = "BinaryExt"; + byte[] bytes = "Coucou le monde".getBytes(); + + WikittyExtension BinaryExt = new WikittyExtension(extName, + "1.0", // version + WikittyUtil.buildFieldMapExtension( // building field map + "String name unique=\"true\"", + "Binary content")); + Wikitty w = new WikittyImpl(); + w.addExtension(BinaryExt); + w.setField(extName, "name", "LeBin"); + w.setField(extName, "content", bytes); + + w = wikittyClient.store(w); + + Wikitty restoredWikitty = wikittyClient.restore(w.getId()); + Assert.assertNotNull(restoredWikitty); + Assert.assertEquals("LeBin", restoredWikitty.getFieldAsString(extName, "name")); + Assert.assertEquals(bytes, restoredWikitty.getFieldAsBytes(extName, "content")); + } + + @Test + public void testStoreUnmodifiedEntity() { + WikittyLabel wikitty1 = new WikittyLabelImpl(); + WikittyLabel wikitty2 = new WikittyLabelImpl(); + + List<WikittyLabel> toStore = new ArrayList<WikittyLabel>(); + Collections.addAll(toStore, wikitty1, wikitty2); + List<WikittyLabel> stored = wikittyClient.store(toStore); + + stored.get(0).addLabels("lbl"); + + stored = wikittyClient.store(toStore); + Assert.assertEquals(Collections.singleton("lbl"), stored.get(0).getLabels()); + } + + /** * Test a query with query maker. */ @Test @@ -254,7 +808,7 @@ Assert.assertEquals(1, results.getTotalResult()); // test normal import - WikittyQuery query2 = new WikittyQueryMaker().eq("Client.name", "Toto").end(); + WikittyQuery query2 = new WikittyQueryMaker().eq("Client.name", "Entreprise dupont").end(); WikittyQueryResult<String> results2 = wikittyClient.findAllByQuery(query2); Assert.assertEquals(1, results2.getTotalResult()); @@ -295,7 +849,7 @@ // aa w = wikittyClient.restore("fbcc8aed-7f67-4e3c-a9aa-221373765f8d"); - Assert.assertEquals("Toto", w.getFieldAsString("Client", "name")); + Assert.assertEquals("Entreprise dupont", w.getFieldAsString("Client", "name")); } /** @@ -907,9 +1461,9 @@ assumeTrueSearchEngineCanRunTest(); // sorting WikittyQuery query1 = WikittyQueryParser.parse("*.name=*"); - // FIXME uncomment query1.setSortAscending("*.name"); + query1.setSortAscending(new ElementField("*.name")); WikittyQueryResult<Product> results1 = wikittyClient.findAllByQuery(Product.class, query1); - Assert.assertEquals(18, results1.getTotalResult()); + Assert.assertEquals(14, results1.getTotalResult()); } /** @@ -943,7 +1497,7 @@ } Collections.sort(expected); WikittyQuery query2 = new WikittyQueryMaker().exteq(sortableExtName).end(); - // FIXME uncomment query2.setSortAscending(WikittyUtil.getFQFieldName(sortableExtName, numFieldName)); + query2.setSortAscending(new ElementField(WikittyUtil.getFQFieldName(sortableExtName, numFieldName))); WikittyQueryResult<Wikitty> results2 = wikittyClient.findAllByQuery(Wikitty.class, query2); List<Integer> resulted = new ArrayList<Integer>(); Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/conform/StorageTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/conform/StorageTest.java 2012-01-26 13:28:55 UTC (rev 1370) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/conform/StorageTest.java 2012-01-26 13:32:44 UTC (rev 1371) @@ -24,674 +24,6 @@ */ package org.nuiton.wikitty.conform; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Assert; -import org.junit.Test; -import org.nuiton.wikitty.entities.WikittyLabelImpl; -import org.nuiton.wikitty.search.Criteria; -import org.nuiton.wikitty.entities.WikittyLabel; -import org.nuiton.wikitty.search.PagedResult; -import org.nuiton.wikitty.entities.WikittyTreeNode; -import org.nuiton.wikitty.entities.WikittyTreeNodeImpl; -import org.nuiton.wikitty.entities.Wikitty; -import org.nuiton.wikitty.WikittyException; -import org.nuiton.wikitty.entities.WikittyExtension; -import org.nuiton.wikitty.entities.WikittyImpl; -import org.nuiton.wikitty.addons.WikittyLabelUtil; -import org.nuiton.wikitty.WikittyObsoleteException; -import org.nuiton.wikitty.services.WikittyEvent; -import org.nuiton.wikitty.WikittyUtil; -import org.nuiton.wikitty.entities.WikittyTreeNodeHelper; -import org.nuiton.wikitty.search.operators.Element; -import org.nuiton.wikitty.search.Search; -import org.nuiton.wikitty.search.TreeNodeResult; - public abstract class StorageTest extends AbstractTestConformance { - private final static Log log = LogFactory.getLog(StorageTest.class); - - @Test - public void testStoreRestoreBasics() throws Exception { - Wikitty w = createBasicWikitty(); - String id = w.getId(); - w = getProxy().store(w); - w = getProxy().restore(id); - assertTrue(w.hasExtension(EXTNAME)); - for ( int i = 0; i < 3; i++ ) { - assertTrue( w.hasField(EXTNAME, "fieldName" + i)); - } - } - - @Test - public void testExtensionMethod() throws Exception { - { - getProxy().storeExtension(EXT_TEST); - List<String> extIds = getProxy().getAllExtensionIds(); - log.debug("extIds: " + extIds); - assertEquals(1, extIds.size()); - WikittyExtension ext = getProxy().restoreExtension(extIds.get(0)); - assertEquals(EXT_TEST.getName(), ext.getName()); - assertEquals(EXT_TEST.getVersion(), ext.getVersion()); - assertEquals(EXT_TEST, ext); - } - { - // test de la suppression de l'extension - getProxy().deleteExtension(EXT_TEST.getName()); - List<String> extIds = getProxy().getAllExtensionIds(); - assertEquals(0, extIds.size()); - } - { - // test que la suppression echoue si l'extension est utilisee - getProxy().storeExtension(EXT_TEST); - - Wikitty w = new WikittyImpl(); - w.addExtension(EXT_TEST); - w = getProxy().store(w); - - try { - getProxy().deleteExtension(EXT_TEST.getName()); - assertTrue(false); // il doit y avoir une exception, donc on ne passe pas la - } catch (WikittyException eee) { - assertTrue(true); - } - List<String> extIds = getProxy().getAllExtensionIds(); - assertEquals(1, extIds.size()); - } - } - - @Test - public void testExtensionRequires() throws Exception { - getProxy().storeExtension(Arrays.asList(EXT_REQUIRED, EXT_REQUIRES)); - List<String> extIds = getProxy().getAllExtensionsRequires(EXTREQUIRED); - log.debug("extIds: " + extIds); - assertEquals(1, extIds.size()); - - WikittyExtension ext = getProxy().restoreExtension(extIds.get(0)); - assertEquals(EXT_REQUIRES.getName(), ext.getName()); - assertEquals(EXT_REQUIRES.getVersion(), ext.getVersion()); - assertEquals(EXT_REQUIRES, ext); - } - - @Test - public void testStoreRestoreLists() throws Exception { - List<Wikitty> wikitties = new ArrayList<Wikitty>(); - List<String> ids = new ArrayList<String>(); - String[] wValues = new String[] { - // wikitty[ 0 ] - "fieldName0 = hello," + - "fieldName1 = 123456," + - "fieldName2 = " + format("23/01/1982"), - - // wikitty[ 1 ] - "fieldName0 = Bonzai !," + - "fieldName1 = 111111," + - "fieldName2 = " + format("26/09/2009"), - - // wikitty[ 2 ] - "fieldName0 = ho ho ho !," + - "fieldName1 = 987654," + - "fieldName2 = " + format("25/12/2029") - }; - for ( String wValue : wValues ) { - Wikitty w = createWikitty( wValue, EXTNAME, EXT_TEST ); - wikitties.add( w ); - ids.add( w.getId() ); - } - wikitties = getProxy().storeWikitty(wikitties); - - wikitties = getProxy().restore(ids); - int wIndex = 0; - for ( String wValue : wValues ) { - Wikitty w = wikitties.get(wIndex); - int i = 0; - for( Entry<String, String> keyValuePair : getKeyPairs(wValue) ) { - String fieldName = "fieldName" + i; - assertEquals(fieldName, keyValuePair.getKey()); - String errorMsg = "Error check field '" + fieldName + "' on wikitty[" + i + "]"; - switch( i ) { - case 0: // check fieldName0 - assertEquals( errorMsg, - keyValuePair.getValue(), w.getFieldAsString(EXTNAME, fieldName) ); - break; - case 1: // check fieldName1 - assertEquals( errorMsg, - Integer.parseInt(keyValuePair.getValue()), - w.getFieldAsInt(EXTNAME, fieldName) ); - break; - case 2: // check fieldName2 - assertEquals( errorMsg, - WikittyUtil.parseDate(keyValuePair.getValue()), - w.getFieldAsDate(EXTNAME, fieldName) ); - break; - default: - fail( "this structure should only get 3 elements, unexpected : " + keyValuePair.getKey() ); - } - i++; - } - wIndex++; - } - } - - @Test - public void testStoreRestoreNullEmpty() throws Exception { - Wikitty w = null; - // should be ignored (?) - try { - w = getProxy().store(w); - fail("store(null) Must throw an exception !"); - } catch (Exception e) { - // OK ! - } - - w = new WikittyImpl(); - String id = w.getId(); - w = getProxy().store(w); - w = getProxy().restore(id); - assertEquals( id, w.getId() ); - } - - @Test - public void testFieldConstraint() throws Exception { - WikittyExtension ext = new WikittyExtension("TestConstraint", "1", - WikittyUtil.buildFieldMapExtension( - "String id notNull=true", - "String ext[0-n] unique=true", - "String other[0-n] unique=true notNull=true" - )); - - getProxy().storeExtension(ext); - Wikitty w = new WikittyImpl(); - w.addExtension(ext); - - try { - w = getProxy().store(w); - Assert.fail("not null contraint don't work on String"); - } catch (WikittyException eee) { - eee.printStackTrace(); - // ok id must not be null - } - - w.setField("TestConstraint", "id", "toto"); - try { - w = getProxy().store(w); - Assert.fail("not null contraint don't work in Collection"); - } catch (WikittyException eee) { - eee.printStackTrace(); - // ok id must not be null - } - - w.addToField("TestConstraint", "other", "titi"); - w = getProxy().store(w); - - w.addToField("TestConstraint", "ext", "tata"); - w.addToField("TestConstraint", "ext", "titi"); - w = getProxy().store(w); - - Wikitty w2 = getProxy().restore(w.getId()); - w2.addToField("TestConstraint", "ext", "tata"); // tata ne doit pas s'ajouter - w2.addToField("TestConstraint", "ext", "toto"); - - w2 = getProxy().store(w2); - Wikitty w3 = getProxy().restore(w.getId()); - - HashSet set = new HashSet(Arrays.asList("tata", "titi", "toto")); - assertEquals(set, w3.getFieldAsSet("TestConstraint", "ext", String.class)); - } - - @Test - public void testStoreFindAll() throws Exception { - - // create some wikitty to permit search test - List<Wikitty> wikitties = new ArrayList<Wikitty>(); - String[] wValues = new String[] { - // wikitty[ 0 ] - "fieldName0 = table," + - "fieldName1 = 003309," + - "fieldName2 = " + format("23/01/1982"), - - // wikitty[ 1 ] - "fieldName0 = chaise," + - "fieldName1 = 003309," + - "fieldName2 = " + format("26/09/2009"), - - // wikitty[ 2 ] - "fieldName0 = bureau," + - "fieldName1 = 223322," + - "fieldName2 = " + format("25/12/2029") - }; - for ( String wValue : wValues ) { - Wikitty w = createWikitty( wValue, EXTNAME, EXT_TEST ); - wikitties.add( w ); - } - wikitties = getProxy().storeWikitty(wikitties); - - // search test - Wikitty w = new WikittyImpl(); - w.addExtension(EXT_TEST); - w.setField(EXT_TEST.getName(), "fieldName1", "003309"); - Criteria criteria = Search.query(w).criteria() - .addSortDescending(EXT_TEST.getName() + ".fieldName0"); - PagedResult<Wikitty> resultFind = getProxy().findAllByCriteria(criteria); - - assertEquals(2, resultFind.size()); - List<Wikitty> wikittyFind = resultFind.getAll(); - assertEquals(wikitties.subList(0, 2), wikittyFind); - } - - /** - * Test ignored because WikittyServiceInMemory don't support facets. - * - * @throws Exception - */ - @Test - public void testStoreFindAllFacet() throws Exception { - - // create some wikitty to permit search test - List<Wikitty> wikitties = new ArrayList<Wikitty>(); - String[] wValues = new String[] { - // wikitty[ 0 ] - "fieldName0 = table," + - "fieldName1 = 663300," + - "fieldName2 = " + format("23/01/1982"), - - // wikitty[ 1 ] - "fieldName0 = chaise," + - "fieldName1 = 663300," + - "fieldName2 = " + format("26/09/2009"), - - // wikitty[ 2 ] - "fieldName0 = bureau," + - "fieldName1 = 223322," + - "fieldName2 = " + format("25/12/2029") - }; - for ( String wValue : wValues ) { - Wikitty w = createWikitty( wValue, EXTNAME, EXT_TEST ); - wikitties.add( w ); - } - wikitties = getProxy().storeWikitty(wikitties); - - // search test - Wikitty w = new WikittyImpl(); - w.addExtension(EXT_TEST); - w.setField(EXT_TEST.getName(), "fieldName1", "663300"); - Criteria criteria = Search.query(w).criteria() - .setFirstIndex(0).setEndIndex(Criteria.ALL_ELEMENTS) - .addFacetField(EXT_TEST.getName()+".fieldName0") - .addFacetField(EXT_TEST.getName()+".fieldName1"); - PagedResult<String> resultFind = getProxy().findAllIdByCriteria(criteria); - - List<String> facetNames = new ArrayList<String>(resultFind.getFacetNames()); - Collections.sort(facetNames); - assertEquals(Arrays.asList(EXT_TEST.getName()+".fieldName0", - EXT_TEST.getName()+".fieldName1"), facetNames); - - // with must have 2 topic: table and chaise - assertEquals(2, resultFind.getTopic(EXT_TEST.getName()+".fieldName0").size()); - // with must have 1 topic: 663300 - assertEquals(1, resultFind.getTopic(EXT_TEST.getName()+".fieldName1").size()); - - - // essai de facettiser sur les extensions - criteria = Search.query().keyword("*").criteria() - .setFirstIndex(0).setEndIndex(0) - .addFacetField(Element.ELT_EXTENSION); - resultFind = getProxy().findAllIdByCriteria(criteria); - assertEquals(1, resultFind.getFacetNames().size()); - assertNotNull(resultFind.getFacets().get(Element.ELT_EXTENSION)); - } - - @Test - public void testStoreFindDelete() throws Exception { - - // create some wikitty to permit search test - List<Wikitty> wikitties = new ArrayList<Wikitty>(); - String[] wValues = new String[] { - // wikitty[ 0 ] - "fieldName0 = table," + - "fieldName1 = 003301," + - "fieldName2 = " + format("23/01/1982"), - - // wikitty[ 1 ] - "fieldName0 = chaiseICI," + - "fieldName1 = 113312," + - "fieldName2 = " + format("26/09/2009"), - - // wikitty[ 2 ] - "fieldName0 = bureau," + - "fieldName1 = 223323," + - "fieldName2 = " + format("25/12/2029") - }; - for ( String wValue : wValues ) { - Wikitty w = createWikitty( wValue, EXTNAME, EXT_TEST ); - wikitties.add( w ); - } - Map<String, String> oldIdVersion = new HashMap<String, String>(); - for( Wikitty w : wikitties ) { - oldIdVersion.put(w.getId(), w.getVersion()); - } - WikittyEvent response = getWikittyService().store(null, wikitties, false); - for (Wikitty w : wikitties) { - response.update(w); - String v1 = w.getVersion(); - String v2 = oldIdVersion.get( w.getId() ); - assertNotNull( v1 ); - assertNotNull( v2 ); - assertTrue(WikittyUtil.versionGreaterThan(v1, v2)); - } - - // search test - Wikitty w = new WikittyImpl(); - w.addExtension(EXT_TEST); - w.setField(EXT_TEST.getName(), "fieldName0", "chaiseICI"); - Criteria criteria = Search.query(w).criteria(); - Wikitty resultFind = getProxy().findByCriteria(criteria); - - Wikitty wikittySource = wikitties.get(1); - assertEquals(w.getFieldAsString(EXT_TEST.getName(), "fieldName0"), - resultFind.getFieldAsString(EXT_TEST.getName(), "fieldName0")); - - assertEquals(wikittySource.getId(), resultFind.getId()); - // test equals implantation method - assertEquals(wikittySource, resultFind); - - assertEquals(wikittySource.getFieldAsString(EXT_TEST.getName(), "fieldName0"), - resultFind.getFieldAsString(EXT_TEST.getName(), "fieldName0")); - assertEquals(wikittySource.getFieldAsString(EXT_TEST.getName(), "fieldName1"), - resultFind.getFieldAsString(EXT_TEST.getName(), "fieldName1")); - assertEquals(wikittySource.getFieldAsString(EXT_TEST.getName(), "fieldName2"), - resultFind.getFieldAsString(EXT_TEST.getName(), "fieldName2")); - - - // test to find deleted wikitty - // test if solr index is coherent with database - Criteria criteria1 = Search.query().eq(Element.ELT_ID, wikittySource.getId()).criteria(); - Wikitty searchedWikitty1 = getProxy().findByCriteria(criteria1); - Assert.assertNotNull(searchedWikitty1); - - getProxy().delete(wikittySource.getId()); - Wikitty deletedObject = getProxy().restore(wikittySource.getId()); - assertNull(deletedObject); - - // test to find deleted wikitty - // test if solr index is coherent with database - Criteria criteria2 = Search.query().eq(Element.ELT_ID, wikittySource.getId()).criteria(); - Wikitty searchedWikitty2 = getProxy().findByCriteria(criteria2); - Assert.assertNull(searchedWikitty2); - } - - /** - * Test ignored because WikittyServiceInMemory don't support labels. - * - * @throws Exception - */ - @Test - public void testStoreAndLabel() throws Exception { - - // create some wikitty to permit search test - List<Wikitty> wikitties = createSampleWikitty(getWikittyService()); - long ts = new Date().getTime(); - - // labelisation test - Wikitty w1 = wikitties.get(0); - WikittyLabelUtil.addLabel(getProxy(), w1.getId(), "titi"+ts); - WikittyLabelUtil.addLabel(getProxy(), w1.getId(), "toto"+ts); - - Wikitty w2 = wikitties.get(1); - WikittyLabelUtil.addLabel(getProxy(), w2.getId(), "tata"+ts); - WikittyLabelUtil.addLabel(getProxy(), w2.getId(), "titi"+ts); - - Wikitty w3 = wikitties.get(2); - WikittyLabelUtil.addLabel(getProxy(), w3.getId(), "tutu"+ts); - WikittyLabelUtil.addLabel(getProxy(), w3.getId(), "titi"+ts); - - Wikitty wt = WikittyLabelUtil.findByLabel(getProxy(), "toto"+ts ); - - assertEquals(w1, wt); - - Set<String> labels = WikittyLabelUtil.findAllAppliedLabels(getProxy(), w2.getId()); - - assertEquals(new HashSet(Arrays.asList("tata"+ts, "titi"+ts)), labels); - } - - @Test - public void testStoreAndClassification() throws Exception { - // create some wikitty to permit search test - List<Wikitty> wikitties = createSampleWikitty(getWikittyService()); - List<Wikitty> wikittyNodes = new ArrayList<Wikitty>(); - - WikittyTreeNodeImpl root = new WikittyTreeNodeImpl(); - wikittyNodes.add(root.getWikitty()); - root.setName("MyCategoryRoot"); - for ( int i = 0; i < 3; i++ ) { - WikittyTreeNodeImpl leaf = new WikittyTreeNodeImpl(); - wikittyNodes.add( leaf.getWikitty() ); - leaf.setName( "cat-"+i ); - leaf.setParent( root.getWikittyId() ); - // root.addChild( leaf.getWikittyId() ); - - for ( int j = 0; j < 5; j++ ) { - WikittyTreeNodeImpl subLeaf = new WikittyTreeNodeImpl(); - subLeaf.setName( "subcat-"+i+"-"+j ); - subLeaf.setParent( leaf.getWikittyId() ); - wikittyNodes.add( subLeaf.getWikitty() ); - // leaf.addChild( subLeaf.getWikittyId() ); - } - } - wikittyNodes = getProxy().storeWikitty(wikittyNodes); // store treeNodes. - - Wikitty table = wikitties.get(0); - Wikitty chaise = wikitties.get(1); - Wikitty bureau = wikitties.get(2); - - assign( chaise, root, "cat-1/subcat-1-4" ); - assign( table, root, "cat-1" ); - assign( bureau, root, "cat-2/subcat-2-4" ); - - TreeNodeResult<WikittyTreeNode> t = getProxy().findTreeNode( - WikittyTreeNode.class, - root.getWikitty().getId(), 0, false, null); - - Assert.assertEquals("MyCategoryRoot", t.getObject().getName()); - - - } - - protected void assign(Wikitty wikitty, WikittyTreeNodeImpl root, String path) { - String[] nodeNames = path.split("/"); - WikittyTreeNodeImpl currentNode = root; - outerloop : for( String nodeName : nodeNames ) { - WikittyTreeNodeImpl node = new WikittyTreeNodeImpl(); - node.setName(nodeName); - - Criteria criteria = Search.query(node.getWikitty()).criteria() - .setFirstIndex(0).setEndIndex(Criteria.ALL_ELEMENTS); - List<String> wikittiesId = getProxy().findAllIdByCriteria(criteria).getAll(); - List<Wikitty> wikitties = getProxy().restore(wikittiesId); - for ( Wikitty child : wikitties ) { - if (!child.hasExtension(WikittyTreeNode.EXT_WIKITTYTREENODE)) { - continue; - } - node = new WikittyTreeNodeImpl(child); - if ( node.getName().equals(nodeName) ) { - currentNode = node; - continue outerloop; - } - } - fail( "Unable to find node " + nodeName + " inside " + currentNode.getName() ); - } - currentNode.addAttachment(wikitty.getId()); - getProxy().store(currentNode.getWikitty()); - } - - /** - * Test la creation d'une entité business par introspection avec un wikitty. - */ - @Test - public void testWikittyUtilNewInstanceWithWikitty() { - - Wikitty w = new WikittyImpl(); - w.addExtension(WikittyTreeNodeImpl.extensions); - - WikittyTreeNode treeNode = WikittyUtil.newInstance( - null, getWikittyService(), WikittyTreeNode.class, w); - Assert.assertNotNull(treeNode); - } - - /** - * Test de la methode cast du proxy. - */ - @Test - public void testCast() { - - WikittyTreeNode node = new WikittyTreeNodeImpl(); - node.setName("nodeName"); - - // cast, different business object, same wikitty - WikittyLabel label = getProxy().cast(node, WikittyLabel.class); - label.addLabels("toto"); - - label = getProxy().store(label); - String wikittyId = label.getWikittyId(); - - WikittyTreeNode node2 = getProxy().restore(WikittyTreeNode.class, wikittyId); - Assert.assertEquals("nodeName", node2.getName()); - - WikittyLabel label2 = getProxy().cast(node, WikittyLabel.class); - List<String> labels = new ArrayList<String>(label2.getLabels()); - Assert.assertFalse(labels.isEmpty()); - Assert.assertEquals("toto", labels.get(0)); - } - - /** - * Test les differents cas du forage de version lors du store. - */ - @Test - public void testStorageVersionForce() { - - // store 1 - Wikitty myWikitty = new WikittyImpl(); - myWikitty.addExtension(WikittyTreeNodeImpl.extensions); - WikittyTreeNodeHelper.setName(myWikitty, "name"); - myWikitty = getProxy().store(myWikitty); - Assert.assertEquals("1.0", myWikitty.getVersion()); - - // store 2 : no modification - myWikitty = getProxy().store(myWikitty); - Assert.assertEquals("1.0", myWikitty.getVersion()); - - // store 3 : modification - WikittyTreeNodeHelper.setName(myWikitty, "new name"); - myWikitty = getProxy().store(myWikitty); - Assert.assertEquals("2.0", myWikitty.getVersion()); - - // store 4 : new wikitty with same wikitty id (obsolete) - Wikitty myNewWikitty = new WikittyImpl(myWikitty.getId()); - myNewWikitty.addExtension(WikittyTreeNodeImpl.extensions); - WikittyTreeNodeHelper.setName(myNewWikitty, "new wikitty"); - try { - myWikitty = getProxy().store(myNewWikitty); - Assert.fail("Test must throw WikittyObsoleteException"); - } - catch (WikittyObsoleteException ex) { - if (log.isTraceEnabled()) { - log.trace("Wikitty obsolete", ex); - } - } - - // store 4 : same but with force (increased by force) - WikittyEvent event = getWikittyService().store( - null, Collections.singletonList(myWikitty), true); - event.update(myWikitty); - Assert.assertEquals("3.0", myWikitty.getVersion()); - - // store 5 : fix version (not increased by force) - myNewWikitty.setVersion("11.0"); - event = getWikittyService().store( - null, Collections.singletonList(myNewWikitty), true); - event.update(myNewWikitty); - Assert.assertEquals("11.0", myNewWikitty.getVersion()); - } - - /** - * Test qu'une sauvegarde de wikitty précédemment supprimé fonctionne. - */ - @Test - public void testStoragePreviouslyDeleted() { - - // store 1 - Wikitty myWikitty = new WikittyImpl(); - myWikitty.addExtension(WikittyTreeNodeImpl.extensions); - WikittyTreeNodeHelper.setName(myWikitty, "name"); - - myWikitty = getProxy().store(myWikitty); - - // delete - getProxy().delete(myWikitty.getId()); - Wikitty restoredWikitty = getProxy().restore(myWikitty.getId()); - Assert.assertNull(restoredWikitty); - - // store again - myWikitty = getProxy().store(myWikitty); - restoredWikitty = getProxy().restore(myWikitty.getId()); - Assert.assertNotNull(restoredWikitty); - } - - @Test - public void testBinaryFieldStorage() { - String extName = "BinaryExt"; - byte[] bytes = "Coucou le monde".getBytes(); - - WikittyExtension BinaryExt = new WikittyExtension(extName, - "1.0", // version - WikittyUtil.buildFieldMapExtension( // building field map - "String name unique=\"true\"", - "Binary content")); - Wikitty w = new WikittyImpl(); - w.addExtension(BinaryExt); - w.setField(extName, "name", "LeBin"); - w.setField(extName, "content", bytes); - - w = getProxy().store(w); - - Wikitty restoredWikitty = getProxy().restore(w.getId()); - Assert.assertNotNull(restoredWikitty); - Assert.assertEquals("LeBin", restoredWikitty.getFieldAsString(extName, "name")); - Assert.assertEquals(bytes, restoredWikitty.getFieldAsBytes(extName, "content")); - } - - @Test - public void testStoreUnmodifiedEntity() { - WikittyLabel wikitty1 = new WikittyLabelImpl(); - WikittyLabel wikitty2 = new WikittyLabelImpl(); - - List<WikittyLabel> toStore = new ArrayList<WikittyLabel>(); - Collections.addAll(toStore, wikitty1, wikitty2); - List<WikittyLabel> stored = getProxy().store(toStore); - - stored.get(0).addLabels("lbl"); - - try { - getProxy().store(toStore); - } catch (Exception eee) { - Assert.fail(); - } - } }