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

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