DevDoc/WorkWithIS: TestStorageService.java

File TestStorageService.java, 11.4 kB (added by dumoulin, 3 years ago)

IS use demonstration sended by Stephane CHORLET on 11/21/2008

Line 
1/*
2 *
3 *  Copyright © 2008, Cemagref
4 *
5 *  This program is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU General Public License as
7 *  published by the Free Software Foundation; either version 3 of
8 *  the License, or (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public
16 *  License along with this program; if not, write to the Free
17 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 *  MA  02110-1301  USA
19 */
20package org.simexplorer.isimporttest;
21
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.InputStream;
26import java.io.OutputStream;
27import java.util.ArrayList;
28import java.util.Date;
29import java.util.HashMap;
30import java.util.List;
31import java.util.Map;
32import java.util.UUID;
33
34import junit.framework.TestCase;
35
36import org.apache.commons.logging.Log;
37import org.apache.commons.logging.LogFactory;
38import org.codelutin.util.MD5;
39import org.codelutin.util.Resource;
40
41import com.healthmarketscience.rmiio.SerializableInputStream;
42
43import fr.cemagref.simexplorer.is.entities.attachment.Attachment;
44import fr.cemagref.simexplorer.is.entities.composite.Attachments;
45import fr.cemagref.simexplorer.is.entities.composite.Codes;
46import fr.cemagref.simexplorer.is.entities.composite.Components;
47import fr.cemagref.simexplorer.is.entities.composite.ConstantValues;
48import fr.cemagref.simexplorer.is.entities.composite.Constants;
49import fr.cemagref.simexplorer.is.entities.composite.Descriptors;
50import fr.cemagref.simexplorer.is.entities.composite.ExplorationDatas;
51import fr.cemagref.simexplorer.is.entities.composite.Libraries;
52import fr.cemagref.simexplorer.is.entities.composite.Structures;
53import fr.cemagref.simexplorer.is.entities.data.Code;
54import fr.cemagref.simexplorer.is.entities.data.Component;
55import fr.cemagref.simexplorer.is.entities.data.Constant;
56import fr.cemagref.simexplorer.is.entities.data.ConstantValue;
57import fr.cemagref.simexplorer.is.entities.data.Descriptor;
58import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication;
59import fr.cemagref.simexplorer.is.entities.data.ExplorationData;
60import fr.cemagref.simexplorer.is.entities.data.Library;
61import fr.cemagref.simexplorer.is.entities.data.LoggableElement;
62import fr.cemagref.simexplorer.is.entities.data.Result;
63import fr.cemagref.simexplorer.is.entities.data.RuntimeType;
64import fr.cemagref.simexplorer.is.entities.metadata.MetaData;
65import fr.cemagref.simexplorer.is.entities.metadata.Version;
66import fr.cemagref.simexplorer.is.factories.ContentTypeFactory;
67import fr.cemagref.simexplorer.is.factories.LoggableElementFactory;
68import fr.cemagref.simexplorer.is.service.StorageService;
69import fr.cemagref.simexplorer.is.service.random.ElementGenerator;
70import fr.cemagref.simexplorer.is.ui.swing.SimExplorer;
71
72public class TestStorageService extends TestCase {
73        private static final Log log = LogFactory.getLog(TestStorageService.class);
74
75        /** storage service. */
76        private StorageService storageService = null;
77
78        /** user id */
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         * Creates an EA, save it and reload it from storage, and compares equality
95         * between the created one and the reloaded one.
96         *
97         * @throws Exception
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         * Creates a minimal EA with no ED or Components, save it and reload it from
119         * storage, and compares equality between the created one and the reloaded
120         * one.
121         *
122         * @throws Exception
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         * Creates a random EA, save it and reload it from storage, and compares
144         * equality between the created one and the reloaded one.
145         *
146         * @throws Exception
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         * Creates a random EA, save it, changes its version and re save it, and
163         * compares equality between the created one and the reloaded one.
164         *
165         * @throws Exception
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                // normal: metadata is excluded from comparison
181                assertEquals(ea, ea2);
182        }
183
184        /**
185         * Load an EA from storage
186         *
187         * @param uuid
188         * @param version
189         * @return EA
190         * @throws Exception
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         * Serialize ea to file
200         *
201         * @param ea
202         * @param file
203         * @throws Exception
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         * Save an EA to storage. If computeAttachments is false attachments will be
222         * removed from EA and all its LE children
223         *
224         * @param ea
225         * @param computeAttachments
226         * @throws Exception
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                        // you must empty attachments otherwise you get
246                        // simexplorer.service.attachmentnotfund
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         * Set common attributes (metadata, descriptors and attachments) to le
259         *
260         * @param le
261         * @throws Exception
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}

logo cemagref

logo iscpif

logo lifegrid

logo region auvergne

logo patres project