source: trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtilsTest.java @ 1994

Last change on this file since 1994 was 1994, checked in by sherbold, 9 years ago
  • fixed bug in the serialization of SimpleSOAPEventType. It is a workaround around a JDK bug ... who would have thought that HashMaps? have a serialization bug?!
  • Property svn:mime-type set to text/plain
File size: 19.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.uml;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileOutputStream;
20import java.util.Collection;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Properties;
24import java.util.Random;
25import java.util.logging.Level;
26
27import static org.junit.Assert.*;
28
29import org.eclipse.uml2.uml.Interaction;
30import org.eclipse.uml2.uml.Model;
31import org.eclipse.uml2.uml.StateMachine;
32import org.eclipse.uml2.uml.UMLPackage;
33import org.junit.After;
34import org.junit.BeforeClass;
35import org.junit.Test;
36
37import de.fraunhofer.fokus.testing.ModelUtils;
38import de.ugoe.cs.autoquest.eventcore.Event;
39import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
40import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
41import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
42import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
43import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
44import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
45import de.ugoe.cs.util.SerializationUtils;
46import de.ugoe.cs.util.console.TextConsole;
47
48/**
49 * <p>
50 * Tests for AutoQUESTs UMLUtils
51 * </p>
52 *
53 * @author Steffen Herbold
54 */
55public class UMLUtilsTest {
56
57    private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
58
59    private final static boolean DELETE_OUTPUTS = false;
60
61    // for RLUS
62    private final static TestData deda_1 = new TestData("deda_rlus_properties.prop",
63                                                        "deda_usagejournal.log",
64                                                        "deda_rlus_usageprofile.dat",
65                                                        "deda_model.uml",
66                                                        "deda_rlus_model_testsuite.uml",
67                                                        "deda_rlus_model_scheduling.uml");
68
69    // for IXS
70    private final static TestData deda_2 = new TestData("deda_ixs_properties.prop",
71                                                        "deda_usagejournal.log",
72                                                        "deda_ixs_usageprofile.dat",
73                                                        "deda_model.uml",
74                                                        "deda_ixs_model_testsuite.uml",
75                                                        "deda_ixs_model_scheduling.uml");
76
77    private final static TestData ita_1 = new TestData("ita_imported_properties.prop",
78                                                       "ita_imported_usagejournal.log",
79                                                       "ita_imported_usageprofile.dat",
80                                                       "ita_imported_model.uml",
81                                                       "ita_imported_model_testsuite.uml",
82                                                       "ita_imported_model_scheduling.uml");
83
84    private static class TestData {
85        public final String propertiesFile;
86        public final String usageJournalFile;
87        public final String usageProfileFile;
88        public final String dslModelFile;
89        public final String testSuiteFile;
90        public final String schedulingFile;
91
92        public TestData(String propertiesFile,
93                        String usageJournalFile,
94                        String usageProfileFile,
95                        String dslModelFile,
96                        String testSuiteFile,
97                        String schedulingFile)
98        {
99            this.propertiesFile = propertiesFile;
100            this.usageJournalFile = usageJournalFile;
101            this.usageProfileFile = usageProfileFile;
102            this.dslModelFile = dslModelFile;
103            this.testSuiteFile = testSuiteFile;
104            this.schedulingFile = schedulingFile;
105
106        }
107
108        @Override
109        public String toString() {
110            StringBuilder strBld = new StringBuilder();
111            strBld.append("Properties    " + propertiesFile + "\n");
112            strBld.append("Usage Journal " + usageJournalFile + "\n");
113            strBld.append("Usage Profile " + usageProfileFile + "\n");
114            strBld.append("DSL Model     " + dslModelFile + "\n");
115            strBld.append("Test Suite    " + testSuiteFile + "\n");
116            strBld.append("Scheduling    " + schedulingFile + "\n");
117            return strBld.toString();
118        }
119    }
120
121    @BeforeClass
122    public static void setUpBeforeClass() throws Exception {
123        new TextConsole(Level.FINE);
124    }
125
126    @After
127    public void tearDown() throws Exception {
128        if (DELETE_OUTPUTS) {
129            deleteFiles(new File(OUTPUT_DIR));
130        }
131    }
132
133    @Test
134    public void testCreateUMLTransitionSequence_ITA_1() throws Exception {
135        TestData testdata = ita_1;
136
137        /*
138         * Properties properties = loadProperties(testdata); //Collection<List<Event>> sequences =
139         * loadAndPreprocessUsageJournal(testdata, properties);
140         *
141         * Model model = ModelUtils.loadModel(new
142         * File(ClassLoader.getSystemResource(testdata.dslModelFile).getFile()));
143         *
144         * StateMachine stateMachine = (StateMachine)
145         * model.getPackagedElement("StateMachineTransportService", true,
146         * UMLPackage.Literals.STATE_MACHINE, true);
147         *
148         *
149         * Collection<List<Event>> umlSequences = new LinkedList<>();
150         *
151         * // remove everything but transport from sequences for (List<Event> sequence : sequences)
152         * { for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { Event
153         * event = eventIter.next(); if
154         * (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
155         * eventIter.remove(); } } umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence,
156         * stateMachine)); }
157         */
158
159        Model model =
160            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
161                .getFile()));
162
163        StateMachine stateMachine =
164            (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
165                                                    UMLPackage.Literals.STATE_MACHINE, true);
166
167        List<Event> manuallyCreatedSequence = new LinkedList<>();
168        manuallyCreatedSequence
169            .add(new Event(new SimpleSOAPEventType("transportInstructionRequest",
170                                                   "TransportService", "Logistics_Environment",
171                                                   null, null, null)));
172        manuallyCreatedSequence
173            .add(new Event(new SimpleSOAPEventType("transportInstructionConfirmationRequest",
174                                                   "materialSupplierService",
175                                                   "Logistics_Environment", null, null, null)));
176
177        // TODO make test case run
178        // UMLUtils.createUMLTransitionSequence(manuallyCreatedSequence, stateMachine);
179    }
180
181    @Test
182    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception {
183        // TODO make test run
184        /*
185         * TestData testdata = ita_1;
186         *
187         * assertTrue("test currently not working", false);
188         *
189         * Properties properties = loadProperties(testdata); Collection<List<Event>> sequences =
190         * loadAndPreprocessUsageJournal(testdata, properties); Model model =
191         * ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
192         * StateMachine stateMachine = (StateMachine)
193         * model.getPackagedElement("StateMachineTransportService", true,
194         * UMLPackage.Literals.STATE_MACHINE, true);
195         *
196         * Collection<List<Event>> umlSequences = new LinkedList<>();
197         *
198         * // remove everything but transport from sequences for (List<Event> sequence : sequences)
199         * { for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { Event
200         * event = eventIter.next(); if
201         * (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
202         * eventIter.remove(); } } umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence,
203         * stateMachine)); }
204         *
205         * UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
206         *
207         * ModelUtils.writeModelToFile(model, OUTPUT_DIR + "ita_v2_result.uml");
208         */
209    }
210
211    @Test
212    public void testCreateInteractionFromEventSequence_DEDA_1() throws Exception {
213        createInteractionFromEventSequenceWorkflow(deda_1);
214    }
215
216    @Test
217    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception {
218        createInteractionFromEventSequenceWorkflow(deda_2);
219    }
220
221    @Test
222    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception {
223        createInteractionFromEventSequenceWorkflow(ita_1);
224    }
225
226    @Test
227    public void testCalculateUsageScore_DEDA_1() throws Exception {
228        calculateUsageScoreWorkflow(deda_1);
229    }
230
231    @Test
232    public void testCalculateUsageScore_DEDA_2() throws Exception {
233        calculateUsageScoreWorkflow(deda_2);
234    }
235
236    @Test
237    public void testCalculateUsageScore_ITA_1() throws Exception {
238        calculateUsageScoreWorkflow(ita_1);
239    }
240
241    @Test
242    public void testCreateScheduling_DEDA_1() throws Exception {
243        createSchedulingWorkflow(deda_1);
244    }
245
246    @Test
247    public void testCreateScheduling_DEDA_2() throws Exception {
248        createSchedulingWorkflow(deda_2);
249    }
250
251    @Test
252    public void testCreateScheduling_ITA() throws Exception {
253        createSchedulingWorkflow(ita_1);
254    }
255
256    @Test
257    public void testValidateModelWithLog_DEDA_1() throws Exception {
258        validateModelWithLogWorkflow(deda_1);
259    }
260
261    @Test
262    public void testValidateModelWithLog_DEDA_2() throws Exception {
263        validateModelWithLogWorkflow(deda_2);
264    }
265
266    @Test
267    public void testValidateModelWithLog_ITA_1() throws Exception {
268        validateModelWithLogWorkflow(ita_1);
269    }
270   
271    @Test
272    public void testSerialization_ITA_1() throws Exception {
273        TestData testdata = ita_1;
274        Properties properties = loadProperties(testdata);
275        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
276        IStochasticProcess model = createUsageProfile(testdata, sequences);
277        byte[] serialized = SerializationUtils.serialize(model);
278        SerializationUtils.deserialize(serialized);
279    }
280
281    private void validateModelWithLogWorkflow(TestData testdata) throws Exception {
282        Properties properties = loadProperties(testdata);
283        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
284        Model model =
285            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
286                .getFile()));
287
288        // run validation
289        int violations =
290            UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context"));
291        if (violations == 0) {
292            System.out.println("No problems found.");
293        }
294        else {
295            System.out.println(violations + " violations found.");
296        }
297    }
298
299    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception {
300        Properties properties = loadProperties(testdata);
301        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
302        Model model =
303            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
304                .getFile()));
305
306        // create a test case for each observed sequence
307        int i = 0;
308        for (List<Event> sequence : sequences) {
309            UMLUtils.createInteractionFromEventSequence(sequence, model,
310                                                        properties.getProperty("testcases.prefix") +
311                                                            "_" + i,
312                                                        properties.getProperty("test.context"),
313                                                        false);
314            i++;
315        }
316
317        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile);
318    }
319
320    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
321        Properties properties = loadProperties(testdata);
322        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
323        Model model =
324            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
325                .getFile()));
326        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
327        Collection<List<Event>> generatedSequences =
328            createRandomSequences(usageProfile, properties);
329
330        int i = 1;
331        List<Interaction> interactions = new LinkedList<>();
332        int[] lengths = new int[generatedSequences.size()];
333        for (List<Event> sequence : generatedSequences) {
334            interactions.add(UMLUtils
335                .createInteractionFromEventSequence(sequence, model,
336                                                    properties.getProperty("testcases.prefix") +
337                                                        "_" + i,
338                                                    properties.getProperty("test.context"),
339                                                    Boolean.parseBoolean(properties.getProperty("testcases.data.random", "false"))));
340            lengths[i - 1] = sequence.size();
341            i++;
342        }
343        for (int j = 0; j < interactions.size(); j++) {
344            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile);
345            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
346        }
347    }
348
349    private void createSchedulingWorkflow(TestData testdata) throws Exception {
350        Properties properties = loadProperties(testdata);
351        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
352        Model model =
353            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
354                .getFile()));
355        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
356        Collection<List<Event>> generatedSequences =
357            createRandomSequences(usageProfile, properties);
358        int i = 1;
359        for (List<Event> sequence : generatedSequences) {
360            UMLUtils.createInteractionFromEventSequence(sequence, model,
361                                                        properties.getProperty("testcases.prefix") +
362                                                            "_" + i,
363                                                        properties.getProperty("test.context"),
364                                                        true);
365            i++;
366        }
367
368        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context"));
369
370        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile);
371    }
372
373    private Properties loadProperties(TestData testdata) throws Exception {
374        Properties properties = new Properties();
375        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile)
376            .getFile()));
377        return properties;
378    }
379
380    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata,
381                                                                  Properties properties)
382        throws Exception
383    {
384        // load usage journal
385        HTTPLogParser parser =
386            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
387                .getFile()));
388        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile)
389            .getFile()));
390        Collection<List<Event>> sequences = parser.getSequences();
391
392        sequences = SOAPUtils.removeNonSOAPEvents(sequences);
393        sequences = SOAPUtils.sortAndConvertSequences(sequences, true, true);
394        sequences = SOAPUtils.normalizeOperationNames(sequences, properties
395                    .getProperty("methodName.prefixToRemove"), properties
396                    .getProperty("methodName.suffixToRemove"));
397        sequences = SOAPUtils.removeCallsToIgnoredServices(sequences, properties.getProperty("test.ignored.services"));
398       
399        return sequences;
400    }
401
402    private IStochasticProcess createUsageProfile(TestData testdata,
403                                                  Collection<List<Event>> sequences)
404        throws Exception
405    {
406        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
407        usageProfile.train(sequences);
408        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile);
409        SerializationUtils.serialize(usageProfile, fos);
410        fos.close();
411        return usageProfile;
412    }
413
414    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile,
415                                                          Properties properties) throws Exception
416    {
417        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
418        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1"));
419        int testCaseMaxLength =
420            Integer.parseInt(properties.getProperty("testcases.maxlenth", "100"));
421        int maxIter = numberOfTestCases * 100;
422        RandomWalkGenerator testGenerator =
423            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true,
424                                    maxIter);
425        return testGenerator.generateTestSuite(usageProfile);
426    }
427
428    private void deleteFiles(File file) {
429        if (file.exists()) {
430            if (file.isDirectory()) {
431                for (File child : file.listFiles()) {
432                    deleteFiles(child);
433                }
434            }
435
436            try {
437                file.delete();
438            }
439            catch (Exception e) {
440                // ignore and delete as much as possible
441            }
442        }
443    }
444
445}
Note: See TracBrowser for help on using the repository browser.