From e98f70c70bbfc5bc154bb148c6bcf7c857191e6e Mon Sep 17 00:00:00 2001 From: rkv Date: Fri, 16 Nov 2012 09:52:14 +0000 Subject: [PATCH] Unit test is created for ScenarioService.getScenarioInfo method. --- Workspace/Siman-Common/src/log4j.xml | 16 +- .../src/org/splat/dal/dao/som/Database.java | 20 +- .../splat/service/ScenarioServiceImpl.java | 57 +++-- .../technical/ProjectSettingsServiceImpl.java | 50 ++-- .../Siman-Common/src/test/siman.properties | 2 +- Workspace/Siman-Common/src/test/som.xml | 218 ++++++++++++++++++ .../src/test/splat/common/BaseTest.java | 3 +- .../splat/service/TestScenarioService.java | 190 +++++++++++---- 8 files changed, 460 insertions(+), 96 deletions(-) create mode 100644 Workspace/Siman-Common/src/test/som.xml diff --git a/Workspace/Siman-Common/src/log4j.xml b/Workspace/Siman-Common/src/log4j.xml index f31a8af..b54fe31 100644 --- a/Workspace/Siman-Common/src/log4j.xml +++ b/Workspace/Siman-Common/src/log4j.xml @@ -46,17 +46,29 @@ + + + + + + + + + + + + - + diff --git a/Workspace/Siman-Common/src/org/splat/dal/dao/som/Database.java b/Workspace/Siman-Common/src/org/splat/dal/dao/som/Database.java index ec6639d..91f4ce1 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/dao/som/Database.java +++ b/Workspace/Siman-Common/src/org/splat/dal/dao/som/Database.java @@ -11,6 +11,7 @@ import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.Properties; import org.apache.log4j.Logger; @@ -24,7 +25,7 @@ import org.springframework.transaction.annotation.Transactional; public class Database extends org.splat.dal.dao.kernel.AbstractDatabase { - public final static Logger LOG = org.splat.dal.dao.som.Database.LOG; + public final static Logger LOG = Logger.getLogger(org.splat.dal.dao.som.Database.class); private transient int _uplevel = 0; // Level of database upgrade private RepositoryService _repositoryService; @@ -49,11 +50,19 @@ public class Database extends org.splat.dal.dao.kernel.AbstractDatabase { DatabaseMetaData dbmdata = connex.getMetaData(); String dbname = connex.getCatalog(); ResultSet table = null; + Statement st = connex.createStatement(); + _uplevel = -1; // Database not initialized try { table = dbmdata.getTables(dbname, null, "study", null); if (table.next()) { - return; + table = dbmdata.getTables(dbname, null, "doctype", null); + if (table.next()) { + if (st.execute("SELECT name FROM doctype") && + st.getResultSet().next() ) { + _uplevel = 0; + } + } } } catch (SQLException e) { LOG.debug("Can't check the database version: ", e); @@ -62,8 +71,10 @@ public class Database extends org.splat.dal.dao.kernel.AbstractDatabase { if (table != null) { table.close(); } + if (st != null) { + st.close(); + } } - _uplevel = -1; // Database not initialized } } @@ -109,6 +120,9 @@ public class Database extends org.splat.dal.dao.kernel.AbstractDatabase { // ============================================================================================================================== public boolean isInitialized() { + if (LOG.isDebugEnabled()) { + LOG.debug("The database is initialized: " + (_uplevel >= 0)); + } return (_uplevel >= 0); } diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java index d8f560d..77f04df 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java @@ -148,7 +148,8 @@ public class ScenarioServiceImpl implements ScenarioService { * @param publicationService * the publicationService to set */ - public void setPublicationService(final PublicationService publicationService) { + public void setPublicationService( + final PublicationService publicationService) { _publicationService = publicationService; } @@ -171,33 +172,45 @@ public class ScenarioServiceImpl implements ScenarioService { _stepService = stepService; } - /** + /** * {@inheritDoc} + * * @see org.splat.service.ScenarioService#getScenarioInfo(long) */ @Transactional public List getScenarioInfo(final long scenarioId) { List res = new ArrayList(); Scenario scen = getScenarioDAO().get(scenarioId); + if (LOG.isDebugEnabled()) { + LOG.debug("Scenario[" + scenarioId + "]: Number of publications: " + + scen.getDocums().size()); + } Step[] steps = getProjectElementService().getSteps(scen); StepDTO stepDTO; DocumentDTO docDTO; - for (Step step: steps) { + for (Step step : steps) { stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class); res.add(stepDTO); - for (Publication tag: step.getDocuments()) { - docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value().getTitle()); + if (LOG.isDebugEnabled()) { + LOG.debug("Step[" + stepDTO.getNumber() + + "]: Number of documents: " + + step.getDocuments().size()); + } + for (Publication tag : step.getDocuments()) { + docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value() + .getTitle()); char aState = tag.getIsnew(); docDTO.addFile(tag.value().getFile().getRelativePath(), aState); - for(Relation rel: tag.value().getRelations(ConvertsRelation.class)) { - File aFile = ((ConvertsRelation)rel).getTo(); + for (Relation rel : tag.value().getRelations( + ConvertsRelation.class)) { + File aFile = ((ConvertsRelation) rel).getTo(); docDTO.addFile(aFile.getRelativePath(), aState); } } } return res; } - + /** * Create a new study with one scenario and "product" simulation context. * @@ -216,9 +229,11 @@ public class ScenarioServiceImpl implements ScenarioService { * if some property occurs several times */ @Transactional - public Study createStudy(final Study.Properties sprop, final Scenario.Properties oprop, - final SimulationContext.Properties cprop) throws MissedPropertyException, - InvalidPropertyException, MultiplyDefinedException { + public Study createStudy(final Study.Properties sprop, + final Scenario.Properties oprop, + final SimulationContext.Properties cprop) + throws MissedPropertyException, InvalidPropertyException, + MultiplyDefinedException { Study study = getStudyService().createStudy(sprop); addScenario(study, oprop); if (cprop.getIndex() == 0) { // Input of new project context @@ -241,8 +256,9 @@ public class ScenarioServiceImpl implements ScenarioService { */ @Transactional public KnowledgeElement addKnowledgeElement(final Scenario aScenarioDTO, - final KnowledgeElement.Properties kprop) throws MissedPropertyException, - InvalidPropertyException, MultiplyDefinedException { + final KnowledgeElement.Properties kprop) + throws MissedPropertyException, InvalidPropertyException, + MultiplyDefinedException { KnowledgeElement kelm = null; try { long aScenarioId = aScenarioDTO.getIndex(); @@ -266,11 +282,11 @@ public class ScenarioServiceImpl implements ScenarioService { } else if (aScenarioDTO.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed aScenarioDTO.getKnowledgeElementsList().add(kelm); } - - // Load the workflow for the parent study to take into account + + // Load the workflow for the parent study to take into account // all study actors durng reindexing. getStudyService().loadWorkflow(aScenario.getOwnerStudy()); - + // Update the lucene index of knowledge elements. getIndexService().add(kelm); if (LOG.isDebugEnabled()) { @@ -404,9 +420,9 @@ public class ScenarioServiceImpl implements ScenarioService { * @see org.splat.service.StudyService#addScenario(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Scenario.Properties) */ @Transactional - public Scenario addScenario(final Study aStudy, final Scenario.Properties sprop) - throws MissedPropertyException, InvalidPropertyException, - MultiplyDefinedException { + public Scenario addScenario(final Study aStudy, + final Scenario.Properties sprop) throws MissedPropertyException, + InvalidPropertyException, MultiplyDefinedException { if (sprop.getManager() == null) { sprop.setManager(aStudy.getAuthor()); } @@ -488,7 +504,8 @@ public class ScenarioServiceImpl implements ScenarioService { * @param knowledgeElementDAO * the knowledgeElementDAO to set */ - public void setKnowledgeElementDAO(final KnowledgeElementDAO knowledgeElementDAO) { + public void setKnowledgeElementDAO( + final KnowledgeElementDAO knowledgeElementDAO) { _knowledgeElementDAO = knowledgeElementDAO; } diff --git a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java index cb7c193..66790ae 100644 --- a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsServiceImpl.java @@ -549,6 +549,9 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { * Create in the database document types defined in the custom configuration. */ private void createDocumentTypes() { + if (LOG.isDebugEnabled()) { + LOG.debug("Creating documents types..."); + } DocumentType.Properties tprop = new DocumentType.Properties(); Map> mapsteps = new HashMap>(); Map mapresult = new HashMap(); @@ -588,33 +591,38 @@ public class ProjectSettingsServiceImpl implements ProjectSettingsService { for (Iterator i = tset.iterator(); i.hasNext();) { type = i.next(); slist = mapsteps.get(type); - uses = _mapuse.get(type); - step = mapresult.get(type); - - tprop.clear(); - tprop.setName(type).setStep( - slist.toArray(new ProjectSettingsService.Step[slist - .size()])); - if (uses != null) { - tdoc = maptype.get(uses); - if (tdoc == null) { - LOG.warn("Undefined \"" + uses + "\" document type."); - } else { - tprop.setUses(tdoc); + if (slist != null) { + uses = _mapuse.get(type); + step = mapresult.get(type); + + tprop.clear(); + tprop.setName(type).setStep( + slist.toArray(new ProjectSettingsService.Step[slist + .size()])); + if (uses != null) { + tdoc = maptype.get(uses); + if (tdoc == null) { + LOG.warn("Undefined \"" + uses + "\" document type."); + } else { + tprop.setUses(tdoc); + } } + if (step != null) { + tprop.setResult(step); + } + + tprop.disableCheck(); + tdoc = getDocumentTypeService().createType(tprop); // Creation of Document Types + getDocumentTypeService().approve(tdoc); + maptype.put(type, tdoc); } - if (step != null) { - tprop.setResult(step); - } - - tprop.disableCheck(); - tdoc = getDocumentTypeService().createType(tprop); // Creation of Document Types - getDocumentTypeService().approve(tdoc); - maptype.put(type, tdoc); } } catch (Exception error) { LOG.warn("Error creating document types, reason:", error); // Should not happen } + if (LOG.isDebugEnabled()) { + LOG.debug("Documents types are created: " + maptype.size()); + } } /** diff --git a/Workspace/Siman-Common/src/test/siman.properties b/Workspace/Siman-Common/src/test/siman.properties index e2220c9..41ca64c 100644 --- a/Workspace/Siman-Common/src/test/siman.properties +++ b/Workspace/Siman-Common/src/test/siman.properties @@ -3,7 +3,7 @@ schema.version = D-0.3 wapp.version = D-0.5 wapp.root = D:/users/rkv/SIMAN/SIMAN_SRC/Workspace/Siman/WebContent/ wapp.login = conf/login.conf -wapp.configuration = conf/som.xml +wapp.configuration = som.xml wapp.customization = conf/my.xml wapp.website = http://www.salome-platform.org wapp.onlinehelp = http://docs.salome-platform.org/salome_6_3_1/gui/GUI_index.html diff --git a/Workspace/Siman-Common/src/test/som.xml b/Workspace/Siman-Common/src/test/som.xml new file mode 100644 index 0000000..0aeae57 --- /dev/null +++ b/Workspace/Siman-Common/src/test/som.xml @@ -0,0 +1,218 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
+
+
+ + + + + + + +
+
+
+
+
+
+ + +
+
+ + + +
+
+
+
+ + +
+ + +
+
+
+ + + + + +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Workspace/Siman-Common/src/test/splat/common/BaseTest.java b/Workspace/Siman-Common/src/test/splat/common/BaseTest.java index 2cdcff0..519e4bc 100644 --- a/Workspace/Siman-Common/src/test/splat/common/BaseTest.java +++ b/Workspace/Siman-Common/src/test/splat/common/BaseTest.java @@ -95,7 +95,8 @@ public class BaseTest extends TestListingAndOrder { */ @Autowired @Test(enabled = false) - public void setSessionFactory(@Qualifier("simanSessionFactory") SessionFactory aSessionFactory) { + public void setSessionFactory(@Qualifier("simanSessionFactory") + final SessionFactory aSessionFactory) { _hibernateTemplate = new HibernateTemplate(aSessionFactory); } diff --git a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java index 3dee9cc..bd31ec3 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java @@ -8,14 +8,15 @@ *****************************************************************************/ package test.splat.service; +import java.io.FileNotFoundException; import java.io.IOException; +import java.sql.SQLException; import java.util.Date; import java.util.List; import org.splat.dal.bo.kernel.User; import org.splat.dal.bo.som.Document; import org.splat.dal.bo.som.DocumentType; -import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.Publication; import org.splat.dal.bo.som.Scenario; import org.splat.dal.bo.som.Study; @@ -29,8 +30,11 @@ import org.splat.service.ProjectElementService; import org.splat.service.PublicationService; import org.splat.service.ScenarioService; import org.splat.service.StepService; +import org.splat.service.dto.DocumentDTO; +import org.splat.service.dto.FileDTO; import org.splat.service.dto.StepDTO; -import org.splat.som.Step; +import org.splat.service.technical.ProjectSettingsService; +import org.splat.service.technical.ProjectSettingsService.Step; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.orm.hibernate3.HibernateTemplate; @@ -45,6 +49,7 @@ import test.splat.common.BaseTest; * @author Roman Kozlov (RKV) * */ +@Test(sequential = true, groups = { "service", "functional", "business" }) public class TestScenarioService extends BaseTest { /** @@ -88,6 +93,13 @@ public class TestScenarioService extends BaseTest { @Qualifier("projectElementService") private transient ProjectElementService _projectElementService; + /** + * The ProjectSettingsService. Later injected by Spring. + */ + @Autowired + @Qualifier("projectSettings") + private transient ProjectSettingsService _projectSettings; + /** * The DocumentTypeService. Later injected by Spring. */ @@ -124,11 +136,13 @@ public class TestScenarioService extends BaseTest { * if a mandatory property is not defined for an object to be created * @throws IOException * if scenario creation is failed - * + * @throws SQLException + * if scenario creation is failed */ @Test public void testGetScenarioInfo() throws InvalidPropertyException, - MissedPropertyException, MultiplyDefinedException, IOException { + MissedPropertyException, MultiplyDefinedException, IOException, + SQLException { LOG.debug(">>>>> BEGIN testGetScenarioInfo()"); long scenarioId = createScenario(); // Call DAO's create method for a good transient knowledge element. @@ -136,6 +150,59 @@ public class TestScenarioService extends BaseTest { Assert.assertNotNull(steps, "List of steps must not be null."); Assert.assertTrue(steps.size() > 0, "No steps are read."); + List projSteps = _projectSettings.getStepsOf(Scenario.class); + Assert.assertEquals(steps.size(), projSteps.size(), + "Not all steps are listed."); + int docIndex = 0; + for (StepDTO step : steps) { + LOG.debug("check the step " + step.getNumber() + ":\n" + step); + Assert.assertNotNull(step, "Step DTO must not be null."); + Assert.assertNotNull(step.getKey(), "Step name must not be null."); + Assert.assertFalse(step.getKey().isEmpty(), + "Step name must not empty."); + Assert.assertTrue(step.getNumber() > 0, + "Step number must be positive integer."); + Assert.assertNotNull(step.getDocs(), + "Step documents list must not be null."); + + Step aProjStep = null; + for (Step projStep : projSteps) { + if (projStep.getNumber() == step.getNumber()) { + aProjStep = projStep; + break; + } + } + + List dtypes = _documentTypeService + .selectTypesOf(aProjStep); + for (DocumentType dtype : dtypes) { + Assert.assertTrue(step.getDocs().size() > 0, + "Step documents list must not be empty."); + String docName = "document" + docIndex; + docIndex++; + for (DocumentDTO doc : step.getDocs()) { + if (docName.equals(doc.getTitle())) { + Assert.assertTrue(doc.getId() > 0, + "Document id must be positive integer."); + Assert.assertEquals(doc.getTitle(), docName); + Assert.assertNotNull(doc.getFiles(), + "Document files list must not be null."); + Assert.assertTrue(doc.getFiles().size() > 1, + "Document must have more then 1 attached file."); + + for (FileDTO file : doc.getFiles()) { + Assert.assertNotNull(file.getPath(), + "File path must not be null."); + Assert.assertFalse(file.getPath().isEmpty(), + "File path must not be empty."); + Assert.assertEquals(file.getState(), 'Y', + "File state must be actual ('Y')."); + } + } + } + } + } + // Call DAO's get method for a not existing id. try { steps = _scenarioService.getScenarioInfo(-1L); @@ -161,12 +228,24 @@ public class TestScenarioService extends BaseTest { * if a mandatory property is not defined for an object to be created * @throws IOException * if document creation is failed + * @throws SQLException + * if project settings loading is failed */ private long createScenario() throws InvalidPropertyException, - MissedPropertyException, MultiplyDefinedException, IOException { + MissedPropertyException, MultiplyDefinedException, IOException, + SQLException { // Create a scenario for tests HibernateTemplate ht = getHibernateTemplate(); + // Load workflow customization + try { + _projectSettings.configure("src/test/som.xml"); + } catch (FileNotFoundException e) { + Assert.fail("Can't find som.xml: ", e); + } + List steps = _projectSettings.getStepsOf(Scenario.class); + Assert.assertTrue(steps.size() > 0, "No steps are created."); + // Create a test user User.Properties uprop = new User.Properties(); uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser") @@ -175,7 +254,7 @@ public class TestScenarioService extends BaseTest { "noreply@salome-platform.org"); uprop.disableCheck(); User anAuthor = new User(uprop); - ht.update(anAuthor); + ht.save(anAuthor); // Create a test study Study.Properties stprops = new Study.Properties().setReference( @@ -189,56 +268,71 @@ public class TestScenarioService extends BaseTest { Scenario aScenario = new Scenario(sprops); aStudy.getScenariiList().add(aScenario); ht.saveOrUpdate(anAuthor); - ht.update(aStudy); + ht.saveOrUpdate(aStudy); ht.saveOrUpdate(aScenario); Document.Properties dprop = new Document.Properties().setAuthor( anAuthor).setDate(new Date()); - Step[] steps = _projectElementService.getSteps(aScenario); - Assert.assertTrue(steps.length > 0, "No steps are created."); + // Create documents int i = 0; for (Step step : steps) { - List dtypes = _documentTypeService.selectTypesOf(step - .getStep()); - dprop.setName("document" + i++).setType(dtypes.get(0)); - // Create a document published in the scenario - Publication pub = _stepService.createDocument(step, dprop); - // Attach a file - _publicationService.attach(pub, "brep"); // TODO: test for different formats + LOG.debug("Create scenario step: " + i); + + org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario); + List dtypes = _documentTypeService + .selectTypesOf(step); + for (DocumentType dtype : dtypes) { + dprop.setName("document" + i++).setType(dtype).setFormat( + dtype.getName()); + // Create a document published in the scenario + Publication pub = _stepService.createDocument(aScStep, dprop); + Assert.assertNotNull(pub.getOwner(), + "The publication must be attached to the scenario."); + Assert.assertEquals(pub.getOwner().getIndex(), aScenario + .getIndex(), + "The publication was not attached to the scenario."); + + aScenario.add(pub); + ht.saveOrUpdate(pub); + + // Attach a file + ht.saveOrUpdate(pub.value()); + ht.saveOrUpdate(_publicationService.attach(pub, "med")); + } + if (dtypes.size() <= 0) { + LOG.debug("No document types are found for scenario step " + i); + } } - return aScenario.getIndex(); - } + Assert.assertNotNull(ht.find("from Document"), + "No documents in the database."); + Assert.assertTrue(ht.find("from Document").size() > 0, + "No documents in the database."); - /** - * Check that given objects are equal. - * - * @param anActual - * the object to check - * @param anExpected - * the expected object - */ - private void compareObjects(final KnowledgeElement anActual, - final KnowledgeElement anExpected) { - Assert.assertNotNull(anActual, - "Created object is not found in the database."); - Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(), - "Knowledge Element author is not saved."); - Assert.assertEquals(anActual.getDate(), anExpected.getDate(), - "Knowledge Element date is not saved."); - Assert - .assertEquals(anActual.getOwnerScenario(), anExpected - .getOwnerScenario(), - "Knowledge Element scenario is not saved."); - Assert.assertEquals(anActual.getProgressState(), anExpected - .getProgressState(), "Knowledge Element state is not saved."); - Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(), - "Knowledge Element title is not saved."); - Assert.assertEquals(anActual.getType(), anExpected.getType(), - "Knowledge Element type is not saved."); - Assert.assertEquals(anActual.getValue(), anExpected.getValue(), - "Knowledge Element value is not saved."); - Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(), - "Knowledge Element index is not saved."); + Assert.assertNotNull(ht.find("from Publication where owner=" + + aScenario.getIndex()), "No publications in the database."); + Assert.assertTrue( + ht.find("from Publication where owner=" + aScenario.getIndex()) + .size() > 0, "No publications in the database."); + + for (Publication p : (List) ht + .find("from Publication where owner=" + aScenario.getIndex())) { + LOG.debug("Publication found: [id=" + p.getIndex() + ", owner=" + + p.getOwner().getIndex() + ", doc=" + p.value().getIndex() + + "]"); + Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(), + "The publication was not attached to the scenario."); + } + + ht.evict(aScenario); + Scenario aScen = ht.load(Scenario.class, aScenario.getIndex()); + Assert.assertNotNull(aScen, "Scenario was not saved in the database."); + Assert.assertTrue(aScen.getDocums().size() > 0, + "No publications in the scenario."); + + Assert.assertTrue(i > 0, + "More then one document must be in the database"); + + return aScenario.getIndex(); } } -- 2.39.2