| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | package org.simexplorer.isimporttest; |
|---|
| 21 | |
|---|
| 22 | import java.io.File; |
|---|
| 23 | import java.io.FileInputStream; |
|---|
| 24 | import java.io.FileOutputStream; |
|---|
| 25 | import java.io.InputStream; |
|---|
| 26 | import java.io.OutputStream; |
|---|
| 27 | import java.util.ArrayList; |
|---|
| 28 | import java.util.Date; |
|---|
| 29 | import java.util.HashMap; |
|---|
| 30 | import java.util.List; |
|---|
| 31 | import java.util.Map; |
|---|
| 32 | import java.util.UUID; |
|---|
| 33 | |
|---|
| 34 | import junit.framework.TestCase; |
|---|
| 35 | |
|---|
| 36 | import org.apache.commons.logging.Log; |
|---|
| 37 | import org.apache.commons.logging.LogFactory; |
|---|
| 38 | import org.codelutin.util.MD5; |
|---|
| 39 | import org.codelutin.util.Resource; |
|---|
| 40 | |
|---|
| 41 | import com.healthmarketscience.rmiio.SerializableInputStream; |
|---|
| 42 | |
|---|
| 43 | import fr.cemagref.simexplorer.is.entities.attachment.Attachment; |
|---|
| 44 | import fr.cemagref.simexplorer.is.entities.composite.Attachments; |
|---|
| 45 | import fr.cemagref.simexplorer.is.entities.composite.Codes; |
|---|
| 46 | import fr.cemagref.simexplorer.is.entities.composite.Components; |
|---|
| 47 | import fr.cemagref.simexplorer.is.entities.composite.ConstantValues; |
|---|
| 48 | import fr.cemagref.simexplorer.is.entities.composite.Constants; |
|---|
| 49 | import fr.cemagref.simexplorer.is.entities.composite.Descriptors; |
|---|
| 50 | import fr.cemagref.simexplorer.is.entities.composite.ExplorationDatas; |
|---|
| 51 | import fr.cemagref.simexplorer.is.entities.composite.Libraries; |
|---|
| 52 | import fr.cemagref.simexplorer.is.entities.composite.Structures; |
|---|
| 53 | import fr.cemagref.simexplorer.is.entities.data.Code; |
|---|
| 54 | import fr.cemagref.simexplorer.is.entities.data.Component; |
|---|
| 55 | import fr.cemagref.simexplorer.is.entities.data.Constant; |
|---|
| 56 | import fr.cemagref.simexplorer.is.entities.data.ConstantValue; |
|---|
| 57 | import fr.cemagref.simexplorer.is.entities.data.Descriptor; |
|---|
| 58 | import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; |
|---|
| 59 | import fr.cemagref.simexplorer.is.entities.data.ExplorationData; |
|---|
| 60 | import fr.cemagref.simexplorer.is.entities.data.Library; |
|---|
| 61 | import fr.cemagref.simexplorer.is.entities.data.LoggableElement; |
|---|
| 62 | import fr.cemagref.simexplorer.is.entities.data.Result; |
|---|
| 63 | import fr.cemagref.simexplorer.is.entities.data.RuntimeType; |
|---|
| 64 | import fr.cemagref.simexplorer.is.entities.metadata.MetaData; |
|---|
| 65 | import fr.cemagref.simexplorer.is.entities.metadata.Version; |
|---|
| 66 | import fr.cemagref.simexplorer.is.factories.ContentTypeFactory; |
|---|
| 67 | import fr.cemagref.simexplorer.is.factories.LoggableElementFactory; |
|---|
| 68 | import fr.cemagref.simexplorer.is.service.StorageService; |
|---|
| 69 | import fr.cemagref.simexplorer.is.service.random.ElementGenerator; |
|---|
| 70 | import fr.cemagref.simexplorer.is.ui.swing.SimExplorer; |
|---|
| 71 | |
|---|
| 72 | public class TestStorageService extends TestCase { |
|---|
| 73 | private static final Log log = LogFactory.getLog(TestStorageService.class); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | private StorageService storageService = null; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | private String token = null; |
|---|
| 80 | |
|---|
| 81 | private static String version = "1.0"; |
|---|
| 82 | private static String versionPlusUn = "1.1"; |
|---|
| 83 | |
|---|
| 84 | static { |
|---|
| 85 | SimExplorer.init(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public TestStorageService() { |
|---|
| 89 | storageService = SimExplorer.getContext().getStorageService(false); |
|---|
| 90 | token = SimExplorer.getContext().getToken(); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | public void testEA() throws Exception { |
|---|
| 100 | log.debug("TEST EA"); |
|---|
| 101 | ExplorationApplication ea = new ExplorationApplication(); |
|---|
| 102 | update(ea, "demo"); |
|---|
| 103 | |
|---|
| 104 | Components c = createComponents(); |
|---|
| 105 | ExplorationDatas ed = createExplorationDatas(); |
|---|
| 106 | ea.setComponents(c); |
|---|
| 107 | ea.setExplorations(ed); |
|---|
| 108 | |
|---|
| 109 | printEa(ea, "/tmp/ea.xml"); |
|---|
| 110 | saveEa(ea, true); |
|---|
| 111 | |
|---|
| 112 | ExplorationApplication ea2 = loadEA(ea.getMetaData().getUuid(), ea.getMetaData().getVersion().toString()); |
|---|
| 113 | printEa(ea2, "/tmp/ea2.xml"); |
|---|
| 114 | assertEquals(ea, ea2); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | public void testMinimalEA() throws Exception { |
|---|
| 125 | log.debug("TEST MINIMAL EA"); |
|---|
| 126 | ExplorationApplication ea = new ExplorationApplication(); |
|---|
| 127 | update(ea, "minimal"); |
|---|
| 128 | |
|---|
| 129 | Components c = new Components(); |
|---|
| 130 | ExplorationDatas ed = new ExplorationDatas(); |
|---|
| 131 | ea.setComponents(c); |
|---|
| 132 | ea.setExplorations(ed); |
|---|
| 133 | |
|---|
| 134 | printEa(ea, "/tmp/ea-minimal.xml"); |
|---|
| 135 | saveEa(ea, true); |
|---|
| 136 | |
|---|
| 137 | ExplorationApplication ea2 = loadEA(ea.getMetaData().getUuid(), ea.getMetaData().getVersion().toString()); |
|---|
| 138 | printEa(ea2, "/tmp/ea2-minimal.xml"); |
|---|
| 139 | assertEquals(ea, ea2); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | public void testRandomEA() throws Exception { |
|---|
| 149 | log.debug("TEST RANDOM EA"); |
|---|
| 150 | ExplorationApplication ea = new ElementGenerator().generateRandomEA(); |
|---|
| 151 | update(ea, "random"); |
|---|
| 152 | |
|---|
| 153 | printEa(ea, "/tmp/ea-random.xml"); |
|---|
| 154 | saveEa(ea, false); |
|---|
| 155 | |
|---|
| 156 | ExplorationApplication ea2 = loadEA(ea.getMetaData().getUuid(), ea.getMetaData().getVersion().toString()); |
|---|
| 157 | printEa(ea2, "/tmp/ea2-random.xml"); |
|---|
| 158 | assertTrue(ea.equals(ea2)); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | public void testVersion() throws Exception { |
|---|
| 168 | log.debug("TEST VERSION"); |
|---|
| 169 | ExplorationApplication ea = new ElementGenerator().generateRandomEA(); |
|---|
| 170 | update(ea, "version"); |
|---|
| 171 | |
|---|
| 172 | saveEa(ea, false); |
|---|
| 173 | assertEquals(ea.getMetaData().getVersion().toString(), version); |
|---|
| 174 | |
|---|
| 175 | ea.getMetaData().setVersion(ea.getMetaData().getVersion().increment(1)); |
|---|
| 176 | saveEa(ea, true); |
|---|
| 177 | |
|---|
| 178 | ExplorationApplication ea2 = loadEA(ea.getMetaData().getUuid(), ea.getMetaData().getVersion().toString()); |
|---|
| 179 | assertEquals(ea2.getMetaData().getVersion().toString(), versionPlusUn); |
|---|
| 180 | |
|---|
| 181 | assertEquals(ea, ea2); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | private ExplorationApplication loadEA(String uuid, String version) throws Exception { |
|---|
| 193 | log.debug("uuid: " + uuid); |
|---|
| 194 | log.debug("version: " + version); |
|---|
| 195 | return (ExplorationApplication) storageService.getLoggableElement(token, uuid, version); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | private void printEa(ExplorationApplication ea, String file) throws Exception { |
|---|
| 206 | OutputStream os = new FileOutputStream(file); |
|---|
| 207 | InputStream is = LoggableElementFactory.getStream(ea); |
|---|
| 208 | byte[] buffer = new byte[2048]; |
|---|
| 209 | |
|---|
| 210 | int b = is.read(buffer); |
|---|
| 211 | while (b > -1) { |
|---|
| 212 | os.write(buffer, 0, b); |
|---|
| 213 | b = is.read(buffer); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | is.close(); |
|---|
| 217 | os.close(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | private void saveEa(ExplorationApplication ea, boolean computeAttachments) throws Exception { |
|---|
| 229 | log.debug(""); |
|---|
| 230 | Map<Attachment, SerializableInputStream> realAttachments = new HashMap<Attachment, SerializableInputStream>(); |
|---|
| 231 | |
|---|
| 232 | if (computeAttachments) { |
|---|
| 233 | List<Attachment> attachments = new ArrayList<Attachment>(); |
|---|
| 234 | attachments.addAll(ea.getAttachments()); |
|---|
| 235 | |
|---|
| 236 | List<LoggableElement> children = ea.getLEChildren(); |
|---|
| 237 | for (LoggableElement child : children) { |
|---|
| 238 | attachments.addAll(child.getAttachments()); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | for (Attachment element : attachments) { |
|---|
| 242 | realAttachments.put(element, new SerializableInputStream(new FileInputStream(element.getFileName()))); |
|---|
| 243 | } |
|---|
| 244 | } else { |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | ea.setAttachments(new Attachments()); |
|---|
| 248 | for (LoggableElement child : ea.getLEChildren()) { |
|---|
| 249 | child.setAttachments(new Attachments()); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | SerializableInputStream xmlStream = new SerializableInputStream(LoggableElementFactory.getStream(ea)); |
|---|
| 254 | storageService.saveElement(token, xmlStream, realAttachments); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | private void update(LoggableElement le, String desc) throws Exception { |
|---|
| 264 | MetaData md = new MetaData(); |
|---|
| 265 | md.setUuid(UUID.randomUUID().toString()); |
|---|
| 266 | md.setVersion(Version.valueOf(version)); |
|---|
| 267 | md.setCreationDate(new Date()); |
|---|
| 268 | md.setLatest(true); |
|---|
| 269 | |
|---|
| 270 | Descriptors ds = createDescriptors(); |
|---|
| 271 | Attachments as = createAttachments(); |
|---|
| 272 | |
|---|
| 273 | le.setName(le.getClass().getSimpleName()); |
|---|
| 274 | le.setDescription(desc); |
|---|
| 275 | le.setDescriptors(ds); |
|---|
| 276 | le.setAttachments(as); |
|---|
| 277 | le.setMetaData(md); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | private Components createComponents() throws Exception { |
|---|
| 281 | Codes codes = createCodes(); |
|---|
| 282 | Constants constants = createConstants(); |
|---|
| 283 | Libraries libraries = createLibraries(); |
|---|
| 284 | RuntimeType rt = new RuntimeType(Object.class); |
|---|
| 285 | Components subComponents = new Components(); |
|---|
| 286 | Structures structures = new Structures(); |
|---|
| 287 | |
|---|
| 288 | Component c = new Component(); |
|---|
| 289 | update(c, "Component"); |
|---|
| 290 | c.setCodes(codes); |
|---|
| 291 | c.setConstants(constants); |
|---|
| 292 | c.setLibraries(libraries); |
|---|
| 293 | c.setType(rt); |
|---|
| 294 | c.setSubComponents(subComponents); |
|---|
| 295 | c.setStructures(structures); |
|---|
| 296 | |
|---|
| 297 | Components cs = new Components(); |
|---|
| 298 | cs.add(c); |
|---|
| 299 | return cs; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | private ExplorationDatas createExplorationDatas() throws Exception { |
|---|
| 303 | Result result = new Result(); |
|---|
| 304 | ConstantValues cvs = createConstantValues(); |
|---|
| 305 | |
|---|
| 306 | ExplorationData ed = new ExplorationData(); |
|---|
| 307 | ed.setResult(result); |
|---|
| 308 | ed.setConstantValues(cvs); |
|---|
| 309 | update(ed, "ExplorationData"); |
|---|
| 310 | |
|---|
| 311 | ExplorationDatas eds = new ExplorationDatas(); |
|---|
| 312 | eds.add(ed); |
|---|
| 313 | return eds; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | private Codes createCodes() { |
|---|
| 317 | Code c = new Code(); |
|---|
| 318 | c.setCode("println 'hello'"); |
|---|
| 319 | c.setLanguage("groovy"); |
|---|
| 320 | |
|---|
| 321 | Codes cs = new Codes(); |
|---|
| 322 | cs.add(c); |
|---|
| 323 | return cs; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | private Libraries createLibraries() throws Exception { |
|---|
| 327 | Library l = new Library(); |
|---|
| 328 | update(l, "Library"); |
|---|
| 329 | |
|---|
| 330 | Libraries ls = new Libraries(); |
|---|
| 331 | ls.add(l); |
|---|
| 332 | return ls; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | private ConstantValues createConstantValues() { |
|---|
| 336 | ConstantValues cvs = new ConstantValues(); |
|---|
| 337 | |
|---|
| 338 | Constants cs = createConstants(); |
|---|
| 339 | for (int i = 0; i < cs.size(); i++) { |
|---|
| 340 | ConstantValue cv = new ConstantValue(); |
|---|
| 341 | cv.setConstant(cs.get(i)); |
|---|
| 342 | cv.setValue(String.valueOf(i + 1)); |
|---|
| 343 | cvs.add(cv); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | return cvs; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | private Constants createConstants() { |
|---|
| 350 | Constant c1 = new Constant(); |
|---|
| 351 | c1.setName("un"); |
|---|
| 352 | c1.setType(Integer.class); |
|---|
| 353 | |
|---|
| 354 | Constant c2 = new Constant(); |
|---|
| 355 | c2.setName("deux"); |
|---|
| 356 | c2.setType(Integer.class); |
|---|
| 357 | |
|---|
| 358 | Constants cs = new Constants(); |
|---|
| 359 | cs.add(c1); |
|---|
| 360 | cs.add(c2); |
|---|
| 361 | return cs; |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | private Descriptors createDescriptors() { |
|---|
| 365 | Descriptors ds = new Descriptors(); |
|---|
| 366 | ds.add(new Descriptor("category", "demo")); |
|---|
| 367 | ds.add(new Descriptor("dependencies", "no")); |
|---|
| 368 | return ds; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | private Attachments createAttachments() throws Exception { |
|---|
| 372 | Attachment a = new Attachment(); |
|---|
| 373 | a.setContentType(ContentTypeFactory.getContentTypeInstance("RawType")); |
|---|
| 374 | File f = new File(Resource.getURL("1.txt").getFile()); |
|---|
| 375 | a.setDataHash(MD5.asHex(MD5.getHash(f))); |
|---|
| 376 | a.setFileName(f.getAbsolutePath()); |
|---|
| 377 | |
|---|
| 378 | Attachments as = new Attachments(); |
|---|
| 379 | as.add(a); |
|---|
| 380 | return as; |
|---|
| 381 | } |
|---|
| 382 | } |
|---|