Author: echatellier Date: 2012-01-19 18:26:37 +0100 (Thu, 19 Jan 2012) New Revision: 1343 Url: http://nuiton.org/repositories/revision/wikitty/1343 Log: Ajout d'un jeu d'import pour les test d'import (CSV) Added: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java trunk/wikitty-api/src/test/resources/csv/importbooks.csv Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/test/CategoryImpl.java trunk/wikitty-api/src/test/xmi/wikittytest.zargo Added: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java (rev 0) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java 2012-01-19 17:26:37 UTC (rev 1343) @@ -0,0 +1,207 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.wikitty; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Locale; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assume; +import org.junit.Before; +import org.junit.BeforeClass; +import org.nuiton.util.ApplicationConfig; +import org.nuiton.wikitty.addons.WikittyImportExportService; +import org.nuiton.wikitty.addons.WikittyImportExportService.FORMAT; +import org.nuiton.wikitty.services.WikittyServiceInMemory; +import org.nuiton.wikitty.test.CatalogNode; +import org.nuiton.wikitty.test.CatalogNodeImpl; +import org.nuiton.wikitty.test.Category; +import org.nuiton.wikitty.test.CategoryImpl; +import org.nuiton.wikitty.test.Product; +import org.nuiton.wikitty.test.ProductImpl; + +/** + * Abstract test for wikitty client. + * + * Just init object, and load defaut and import data. Asserts are subclassed. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public abstract class WikittyClientAbstractTest { + + static private Log log = LogFactory.getLog(WikittyClientAbstractTest.class); + + protected static ApplicationConfig wikittyConfig; + + protected WikittyClient wikittyClient; + + protected static DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH); + + /** + * Certains test utilisent des fonctionnalités avancés des moteurs de + * recherche et de facetisation et ne peuvent pas fonctionner si + * l'instance in memory ne supporte pas la fonctionnalité testée. + */ + protected void assumeTrueSearchEngineCanRunTest() { + boolean isInMomory = wikittyClient.getWikittyService() instanceof WikittyServiceInMemory; + if (isInMomory) { + log.warn("Not yet implemented in memory, skipping"); + } + Assume.assumeTrue(!isInMomory); + } + + /** + * Init {@link ApplicationConfig} class. + */ + @BeforeClass + public static void setUpConfig() { + // can't call parse method here + wikittyConfig = new ApplicationConfig( + WikittyConfigOption.class, WikittyConfigAction.class, + null, null); + } + + /** + * Override to method in sub tests to change wikitty client implementation. + * + * @return wikitty client implementation to use in current test case instance + */ + protected WikittyClient getWikittyClient() { + WikittyService wikittyService = new WikittyServiceInMemory(wikittyConfig); + WikittyClient client = new WikittyClient(wikittyConfig, wikittyService); + return client; + } + + /** + * Clear all data and add some test data for all tests. + * @throws ParseException + */ + @Before + public void setUpTestData() throws ParseException { + wikittyClient = getWikittyClient(); + wikittyClient.clear(); + addTestDataInClient(wikittyClient); + } + + /** + * Add some wikitty in client. + * + * In test case, product are books. + * + * @param wikittyClient wikitty client + * @throws ParseException + */ + protected void addTestDataInClient(WikittyClient wikittyClient) throws ParseException { + + // Categories + Category sf = new CategoryImpl("sf", "science fiction"); + Category history = new CategoryImpl("history", "history"); + Category society = new CategoryImpl("society", "société"); + Category fantastic = new CategoryImpl("fan", "fantastique"); + Category fantaisie = new CategoryImpl("hf", "heroique/fantaisie"); + wikittyClient.store(sf, society, fantastic, fantaisie, history); + + // Product (multiple inheritance) + Product book42 = new ProductImpl("Answer to life the universe and everything"); + book42.setPrice(4200); + book42.setPicturePrice(420); + book42.setPriceFromProduct(42); + book42.addColors("white", "black"); + book42.setDate(df.parse("December 25, 1983")); + book42.setCategory(sf.getWikittyId()); + + Product bookIndignez = new ProductImpl("Indignez-vous !"); + bookIndignez.setPrice(1); + bookIndignez.setPicturePrice(2); + bookIndignez.setPriceFromProduct(15); + bookIndignez.setCategory(sf.getWikittyId()); + bookIndignez.addColors("white"); + bookIndignez.setDate(df.parse("April 12, 2011")); + bookIndignez.setCategory(society.getWikittyId()); + + Product bookLotr = new ProductImpl("Le seigneur des anneaux"); + bookLotr.setPrice(11); + bookLotr.setPicturePrice(12); + bookLotr.setPriceFromProduct(100); + bookLotr.setCategory(sf.getWikittyId()); + bookLotr.setDate(df.parse("July 9, 1953")); + bookLotr.addColors("red", "blue"); + + Product bookLan = new ProductImpl("Lanfeust"); + bookLan.setPrice(0); + bookLan.setPicturePrice(5); + bookLan.setPriceFromProduct(14); + bookLan.setCategory(fantastic.getWikittyId()); + bookLan.addColors("red", "yellow"); + bookLan.setDate(df.parse("January 12, 2002")); + wikittyClient.store(book42, bookIndignez, bookLotr, bookLan); + + // some tree nodes + CatalogNode rootNode = new CatalogNodeImpl("rootnode"); + CatalogNode livreNode = new CatalogNodeImpl("Livres"); + livreNode.setParent(rootNode.getWikittyId()); + CatalogNode bdNode = new CatalogNodeImpl("Bande dessinées"); + bdNode.setParent(livreNode.getWikittyId()); + bdNode.addAttachment(bookLan.getWikittyId()); + CatalogNode newsNode = new CatalogNodeImpl("Nouvelles"); + newsNode.setParent(livreNode.getWikittyId()); + newsNode.addAttachment(bookIndignez.getWikittyId()); + CatalogNode romanNode = new CatalogNodeImpl("Roman"); + romanNode.setParent(livreNode.getWikittyId()); + romanNode.addAttachment(book42.getWikittyId(), bookLotr.getWikittyId()); + CatalogNode otherNode = new CatalogNodeImpl("Everything else"); + otherNode.setParent(rootNode.getWikittyId()); + wikittyClient.store(rootNode, livreNode, bdNode, newsNode, romanNode, otherNode); + } + + /** + * Import books from csv files. + * @throws IOException + */ + protected void importBooks() throws IOException { + WikittyImportExportService ieService = new WikittyImportExportService(wikittyClient); + + InputStreamReader reader = null; + try { + InputStream is = WikittyClientAbstractTest.class.getResourceAsStream("/csv/importbooks.csv"); + reader = new InputStreamReader(is); + ieService.syncImport(FORMAT.CSV, reader); + reader.close(); + } finally { + IOUtils.closeQuietly(reader); + } + } +} Property changes on: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientAbstractTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL 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-19 17:17:50 UTC (rev 1342) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyClientTest.java 2012-01-19 17:26:37 UTC (rev 1343) @@ -25,15 +25,10 @@ package org.nuiton.wikitty; -import java.text.DateFormat; -import java.text.ParseException; -import java.util.Locale; +import java.io.IOException; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; -import org.nuiton.util.ApplicationConfig; import org.nuiton.wikitty.addons.WikittyImportExportService; import org.nuiton.wikitty.addons.WikittyImportExportService.FORMAT; import org.nuiton.wikitty.entities.Element; @@ -42,13 +37,7 @@ import org.nuiton.wikitty.query.WikittyQueryMaker; import org.nuiton.wikitty.query.WikittyQueryParser; import org.nuiton.wikitty.query.WikittyQueryResult; -import org.nuiton.wikitty.services.WikittyServiceInMemory; -import org.nuiton.wikitty.test.CatalogNode; -import org.nuiton.wikitty.test.CatalogNodeImpl; -import org.nuiton.wikitty.test.Category; -import org.nuiton.wikitty.test.CategoryImpl; import org.nuiton.wikitty.test.Product; -import org.nuiton.wikitty.test.ProductImpl; /** * Wikitty client test to test for client use. @@ -62,118 +51,9 @@ * Last update : $Date$ * By : $Author$ */ -public class WikittyClientTest { +public class WikittyClientTest extends WikittyClientAbstractTest { - protected static ApplicationConfig wikittyConfig; - - protected WikittyClient wikittyClient; - - protected static DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH); - /** - * Init {@link ApplicationConfig} class. - */ - @BeforeClass - public static void setUpConfig() { - // can't call parse method here - wikittyConfig = new ApplicationConfig( - WikittyConfigOption.class, WikittyConfigAction.class, - null, null); - } - - /** - * Override to method in sub tests to change wikitty client implementation. - * - * @return wikitty client implementation to use in current test case instance - */ - protected WikittyClient getWikittyClient() { - WikittyService wikittyService = new WikittyServiceInMemory(wikittyConfig); - WikittyClient client = new WikittyClient(wikittyConfig, wikittyService); - return client; - } - - /** - * Clear all data and add some test data for all tests. - * @throws ParseException - */ - @Before - public void setUpTestData() throws ParseException { - wikittyClient = getWikittyClient(); - wikittyClient.clear(); - addTestDataInClient(wikittyClient); - } - - /** - * Add some wikitty in client. - * - * In test case, product are books. - * - * @param wikittyClient wikitty client - * @throws ParseException - */ - protected void addTestDataInClient(WikittyClient wikittyClient) throws ParseException { - - // Categories - Category sf = new CategoryImpl("science fiction"); - Category society = new CategoryImpl("société"); - Category fantastic = new CategoryImpl("fantastique"); - Category fantaisie = new CategoryImpl("heroique/fantaisie"); - wikittyClient.store(sf, society, fantastic, fantaisie); - - // Product (multiple inheritance) - Product book42 = new ProductImpl("Answer to life the universe and everything"); - book42.setPrice(4200); - book42.setPicturePrice(420); - book42.setPriceFromProduct(42); - book42.addColors("white", "black"); - book42.setDate(df.parse("December 25, 1983")); - book42.setCategory(sf.getWikittyId()); - - Product bookIndignez = new ProductImpl("Indignez-vous !"); - bookIndignez.setPrice(1); - bookIndignez.setPicturePrice(2); - bookIndignez.setPriceFromProduct(15); - bookIndignez.setCategory(sf.getWikittyId()); - bookIndignez.addColors("white"); - bookIndignez.setDate(df.parse("April 12, 2011")); - bookIndignez.setCategory(society.getWikittyId()); - - Product bookLotr = new ProductImpl("Le seigneur des anneaux"); - bookLotr.setPrice(11); - bookLotr.setPicturePrice(12); - bookLotr.setPriceFromProduct(100); - bookLotr.setCategory(sf.getWikittyId()); - bookLotr.setDate(df.parse("July 9, 1953")); - bookLotr.addColors("red", "blue"); - - Product bookLan = new ProductImpl("Lanfeust"); - bookLan.setPrice(0); - bookLan.setPicturePrice(5); - bookLan.setPriceFromProduct(14); - bookLan.setCategory(fantastic.getWikittyId()); - bookLan.addColors("red", "yellow"); - bookLan.setDate(df.parse("January 12, 2002")); - wikittyClient.store(book42, bookIndignez, bookLotr, bookLan); - - // some tree nodes - CatalogNode rootNode = new CatalogNodeImpl("rootnode"); - CatalogNode livreNode = new CatalogNodeImpl("Livres"); - livreNode.setParent(rootNode.getWikittyId()); - CatalogNode bdNode = new CatalogNodeImpl("Bande dessinées"); - bdNode.setParent(livreNode.getWikittyId()); - bdNode.addAttachment(bookLan.getWikittyId()); - CatalogNode newsNode = new CatalogNodeImpl("Nouvelles"); - newsNode.setParent(livreNode.getWikittyId()); - newsNode.addAttachment(bookIndignez.getWikittyId()); - CatalogNode romanNode = new CatalogNodeImpl("Roman"); - romanNode.setParent(livreNode.getWikittyId()); - romanNode.addAttachment(book42.getWikittyId(), bookLotr.getWikittyId()); - CatalogNode otherNode = new CatalogNodeImpl("Everything else"); - otherNode.setParent(rootNode.getWikittyId()); - wikittyClient.store(rootNode, livreNode, bdNode, newsNode, romanNode, otherNode); - } - - /** * Test a query with query maker. */ @Test @@ -240,11 +120,52 @@ * Test le resultat attendu d'un export XML. */ @Test - public void testExport() { + public void testExportXml() { + assumeTrueSearchEngineCanRunTest(); // facets + WikittyImportExportService ieService = new WikittyImportExportService(wikittyClient); WikittyQuery query = new WikittyQueryMaker().eq(Element.EXTENSION, WikittyTreeNode.EXT_WIKITTYTREENODE).end(); String xmlExport = ieService.syncExportAllByQuery(FORMAT.XML, query); - System.out.println(xmlExport); - + + // extension definition is present in xml export + Assert.assertTrue(xmlExport.contains("<extension name='WikittyTreeNode' version='2.0'>")); + // some data too + Assert.assertTrue(xmlExport.contains("<WikittyTreeNode.name>Livres</WikittyTreeNode.name>")); + Assert.assertTrue(xmlExport.contains("extensions='WikittyTreeNode[2.0],CatalogNode[2.0]'")); + Assert.assertTrue(xmlExport.contains("<WikittyTreeNode.attachment>")); } + + /** + * Test le resultat attendu d'un export CSV. + */ + @Test + public void testExportCSV() { + assumeTrueSearchEngineCanRunTest(); // facets + + WikittyImportExportService ieService = new WikittyImportExportService(wikittyClient); + WikittyQuery query = new WikittyQueryMaker().eq(Element.EXTENSION, WikittyTreeNode.EXT_WIKITTYTREENODE).end(); + String csvExport = ieService.syncExportAllByQuery(FORMAT.CSV, query); + System.out.println(csvExport); + + // extension definition is present in xml export + Assert.assertTrue(csvExport.startsWith("\"Wikitty.Id\",\"Wikitty.Ext\",\"WikittyTreeNode.attachment\",\"WikittyTreeNode.name\",\"WikittyTreeNode.parent\"")); + // some data too + Assert.assertTrue(csvExport.contains("\"WikittyTreeNode,CatalogNode\",,\"Everything else\"")); + Assert.assertTrue(csvExport.contains("\"WikittyTreeNode,CatalogNode\",,\"rootnode\"")); + Assert.assertTrue(csvExport.contains("\"WikittyTreeNode,CatalogNode\",,\"Livres\"")); + Assert.assertTrue(csvExport.contains("Nouvelles")); + } + + /** + * Test l'import csv des livres. + * @throws IOException + */ + @Test + public void testImportCSV() throws IOException { + importBooks(); // 13 books + + WikittyQuery query = new WikittyQueryMaker().eq(Element.EXTENSION, Product.EXT_PRODUCT).end(); + WikittyQueryResult<Product> results = wikittyClient.findAllByQuery(Product.class, query); + Assert.assertEquals(4 + 13, results.getTotalResult()); + } } Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/test/CategoryImpl.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/test/CategoryImpl.java 2012-01-19 17:17:50 UTC (rev 1342) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/test/CategoryImpl.java 2012-01-19 17:26:37 UTC (rev 1343) @@ -20,8 +20,9 @@ } - public CategoryImpl(String name) { + public CategoryImpl(String code, String name) { this(); + setCode(code); setName(name); } Added: trunk/wikitty-api/src/test/resources/csv/importbooks.csv =================================================================== --- trunk/wikitty-api/src/test/resources/csv/importbooks.csv (rev 0) +++ trunk/wikitty-api/src/test/resources/csv/importbooks.csv 2012-01-19 17:26:37 UTC (rev 1343) @@ -0,0 +1,14 @@ +"Wikitty.Id","Wikitty.Ext","Product.category","Product.colors","Product.date","Product.name","Product.price","ProductDocumentation.documentation","ProductDocumentation.price","ProductPicture.picture","ProductPicture.price" +"4d221e31-ff9b-44f0-8545-f9884435f30d","ProductPicture,ProductDocumentation,Product","Category.code=sf","(black)","1997-01-11T23:00:00.000+0000Z","Harry Potter à l'école des sorciers","29",,"0",,"2" +"a6121143-f5a4-4567-8c2a-f5e3f5241a56","ProductPicture,ProductDocumentation,Product","Category.code=love","(pink)","1953-07-08T23:00:00.000+0000Z","Roméo et Juliette","30",,"11",,"10" +"36b8e47d-5d38-4818-b915-6a318f7b74d8","ProductPicture,ProductDocumentation,Product","Category.code=history","(white),(black)","1983-12-24T23:00:00.000+0000Z","Oliver Twist","42",,"2",,"5" +"94f4c6ca-49cd-4e4f-954f-cee8654582be","ProductPicture,ProductDocumentation,Product","Category.code=history","(white)","2011-04-11T22:00:00.000+0000Z","Guerre et Paix","15",,"1",,"2" +"aa3dc36f-cb12-4bad-a405-ae1146975567","ProductPicture,ProductDocumentation,Product","Category.code=history","(pink)","2011-04-11T22:00:00.000+0000Z","Les Quatre filles du Dr March","15",,"1",,"2" +"6ee97a12-0f2b-4d6f-8d28-ad40e8d4e178","ProductPicture,ProductDocumentation,Product","Category.code=history","(white)","2011-04-11T22:00:00.000+0000Z","Les Chevaliers de La Table Ronde","15",,"1",,"2" +"db9dc782-e650-4fd4-83ac-3c1c5c136cde","ProductPicture,ProductDocumentation,Product","Category.code=sf","(white)","2011-04-08T22:00:00.000+0000Z","Da vinci code","49",,"1",,"2" +"584adc1e-726d-4348-9a57-77153d245b34","ProductPicture,ProductDocumentation,Product","Category.code=sf","(red)","2011-02-11T22:00:00.000+0000Z","Le Symbole perdu","49",,"0",,"2" +"0f30c93f-7ad5-4f03-8714-b2e62f2446af","ProductPicture,ProductDocumentation,Product","Category.code=sf","(blue)","1998-05-27T22:00:00.000+0000Z","Harry Potter et la Chambre des secrets","30",,"0",,"10" +"d55f4c0a-af3c-45bc-ade9-8512b80121dd","ProductPicture,ProductDocumentation,Product","Category.code=sf","(red)","1999-04-13T22:00:00.000+0000Z","Harry Potter et le Prisonnier d'Azkaban","23",,"0",,"3" +"28feed78-6179-4232-bed3-ef7e0c4c71a0","ProductPicture,ProductDocumentation,Product","Category.code=sf","(black)","2000-01-14T22:00:00.000+0000Z","Harry Potter et la Coupe de feu","51",,"0",,"2" +"76ab5768-593e-471f-8101-da39e440d686","ProductPicture,ProductDocumentation,Product","Category.code=sf","(yellow)","2003-04-30T22:00:00.000+0000Z","Harry Potter et l'Ordre du phénix","52",,"0",,"7" +"ce57b748-3f73-4d38-a401-47b405cbfb34","ProductPicture,ProductDocumentation,Product","Category.code=sf","(white)","2005-01-02T22:00:00.000+0000Z","Harry Potter et le Prince de sang-mêlé","19",,"0",,"5" Modified: trunk/wikitty-api/src/test/xmi/wikittytest.zargo =================================================================== (Binary files differ)