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

Last change on this file since 1989 was 1989, checked in by sherbold, 9 years ago

*updated tests

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