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

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