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

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