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

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