Author: glandais Date: 2008-02-29 16:43:11 +0000 (Fri, 29 Feb 2008) New Revision: 1259 Added: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceGenerator.java Log: EA generator, shared by tests, Web and Swing Added: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceGenerator.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceGenerator.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceGenerator.java 2008-02-29 16:43:11 UTC (rev 1259) @@ -0,0 +1,156 @@ +package fr.cemagref.simexplorer.is.service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.healthmarketscience.rmiio.SerializableInputStream; + +import fr.cemagref.simexplorer.is.entities.attachment.Attachment; +import fr.cemagref.simexplorer.is.entities.data.Code; +import fr.cemagref.simexplorer.is.entities.data.Component; +import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; +import fr.cemagref.simexplorer.is.entities.data.LoggableElement; +import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.factories.LoggableElementFactory; +import fr.cemagref.simexplorer.is.storage.ElementGenerator; +import fr.cemagref.simexplorer.is.storage.VersionGenerator; +import fr.cemagref.simexplorer.is.storage.ElementGenerator.RandomStream; + +/** + * The Class StorageServiceGenerator. Aimed to generate random elements for a service. + */ +public class StorageServiceGenerator { + + /** The Constant log. */ + private static final Log log = LogFactory.getLog(StorageServiceGenerator.class); + + /** The element generator. */ + private static ElementGenerator elementGenerator = new ElementGenerator(); + + /** + * Save ea. + * + * @param ea + * the ea + * @param computeAttachments + * the compute attachments + * + * @return the meta data + * + * @throws Exception + * the exception + */ + private MetaData saveEa(StorageService storageService, String token, ExplorationApplication ea, + boolean computeAttachments, Map<Attachment, ElementGenerator.RandomStream> attachments) throws Exception { + + if (computeAttachments) { + List<Attachment> attachmentsKeys = new ArrayList<Attachment>(); + + attachmentsKeys.addAll(ea.getAttachments()); + List<LoggableElement> children = ea.getChildren(); + for (LoggableElement child : children) { + attachmentsKeys.addAll(child.getAttachments()); + } + + for (Attachment attachment : attachmentsKeys) { + RandomStream randomStream = elementGenerator.generateTextStream(); + attachment.setDataHash(randomStream.getMd5()); + attachments.put(attachment, randomStream); + } + } + + SerializableInputStream xmlStream = new SerializableInputStream(LoggableElementFactory.getStream(ea)); + + Map<Attachment, SerializableInputStream> realAttachments = new HashMap<Attachment, SerializableInputStream>(); + for (Map.Entry<Attachment, ElementGenerator.RandomStream> element : attachments.entrySet()) { + realAttachments.put(element.getKey(), new SerializableInputStream(element.getValue().getStream())); + } + + return storageService.saveElement(token, xmlStream, realAttachments); + } + + /** + * Test version update. + * + * @param firstVersion + * the first version + * @param secondVersion + * the second version + * @param different + * the different + * @param versions + * the versions + * + * @return the string + * + * @throws Exception + * the exception + */ + public String generateTwoExplorationApplications(StorageService storageService, String token, String firstVersion, + String secondVersion, boolean different) throws Exception { + log.info("-----------------------------"); + + ExplorationApplication ea; + + Map<Attachment, ElementGenerator.RandomStream> attachments = new HashMap<Attachment, RandomStream>(); + + ea = elementGenerator.generateRandomEA(); + + StringBuffer task = new StringBuffer(); + task.append(firstVersion).append("A + ").append(secondVersion); + if (different) { + task.append("B"); + } else { + task.append("A"); + } + + log.info(task.toString()); + + log.info("UUID : " + ea.getMetaData().getUuid()); + log.info("Name : " + ea.getMetaData().getName()); + + ea.getMetaData().setVersion(firstVersion); + + log.info("Saving " + firstVersion + "A"); + MetaData mde1 = saveEa(storageService, token, ea, true, attachments); + + ea.getMetaData().setVersion(secondVersion); + + if (different) { + Component component = ea.getComponents().iterator().next(); + Code code = new Code(); + code.setCode("regreg"); + code.setLanguage("regreg"); + component.getCodes().add(code); + } + + if (different) { + log.info("Saving " + secondVersion + "B"); + } else { + log.info("Saving " + secondVersion + "A"); + } + MetaData mde2 = saveEa(storageService, token, ea, false, attachments); + + log.info("A : " + mde1.getHash()); + if (different) { + log.info("B : " + mde2.getHash()); + } + + return ea.getMetaData().getUuid(); + } + + public void generateLotsExplorationApplication(StorageService storageService, String token, int c) throws Exception { + Random r = new Random(); + for (int i = 0; i < c; i++) { + generateTwoExplorationApplications(storageService, token, VersionGenerator.getInstance().generateVersion().toString(), + VersionGenerator.getInstance().generateVersion().toString(), r.nextBoolean()); + } + } + +}