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

Last change on this file since 1979 was 1979, checked in by sherbold, 9 years ago
  • updated tests
  • Property svn:mime-type set to text/plain
File size: 19.5 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
[1931]30import static org.junit.Assert.*;
[1926]31
[1643]32import org.eclipse.uml2.uml.Interaction;
[1603]33import org.eclipse.uml2.uml.Model;
34import org.eclipse.uml2.uml.StateMachine;
35import org.eclipse.uml2.uml.UMLPackage;
36import org.junit.After;
[1898]37import org.junit.BeforeClass;
[1603]38import org.junit.Test;
39
[1759]40import de.fraunhofer.fokus.testing.ModelUtils;
[1603]41import de.ugoe.cs.autoquest.eventcore.Event;
42import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
[1926]43import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
[1929]44import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
[1643]45import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
46import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
[1763]47import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
[1929]48import de.ugoe.cs.util.SerializationUtils;
[1898]49import de.ugoe.cs.util.console.TextConsole;
[1603]50
51/**
52 * <p>
53 * Tests for AutoQUESTs UMLUtils
54 * </p>
55 *
56 * @author Steffen Herbold
57 */
58public class UMLUtilsTest {
[1625]59
[1606]60    private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
[1929]61
62    private final static boolean DELETE_OUTPUTS = false;
63
[1927]64    // for RLUS
65    private final static TestData deda_1 = new TestData("deda_rlus_properties.prop",
[1929]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");
[1625]71
[1927]72    // for IXS
73    private final static TestData deda_2 = new TestData("deda_ixs_properties.prop",
[1929]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");
[1927]79
[1979]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");
[1929]86
[1927]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,
[1929]96                        String usageJournalFile,
97                        String usageProfileFile,
98                        String dslModelFile,
99                        String testSuiteFile,
100                        String schedulingFile)
[1927]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;
[1929]108
[1927]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
[1898]124    @BeforeClass
125    public static void setUpBeforeClass() throws Exception {
[1979]126        new TextConsole(Level.FINE);
[1898]127    }
[1926]128
[1625]129    @After
130    public void tearDown() throws Exception {
[1929]131        if (DELETE_OUTPUTS) {
132            deleteFiles(new File(OUTPUT_DIR));
133        }
[1625]134    }
[1603]135
[1931]136    @Test
[1927]137    public void testCreateUMLTransitionSequence_ITA_1() throws Exception {
138        TestData testdata = ita_1;
[1929]139
[1931]140        /*
141         * Properties properties = loadProperties(testdata); //Collection<List<Event>> sequences =
142         * loadAndPreprocessUsageJournal(testdata, properties);
143         *
[1979]144         * Model model = ModelUtils.loadModel(new
145         * File(ClassLoader.getSystemResource(testdata.dslModelFile).getFile()));
[1931]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
[1603]162        Model model =
[1979]163            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
164                .getFile()));
[1603]165
166        StateMachine stateMachine =
[1929]167            (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
168                                                    UMLPackage.Literals.STATE_MACHINE, true);
[1603]169
[1931]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)));
[1929]179
[1942]180        // TODO make test case run
[1979]181        // UMLUtils.createUMLTransitionSequence(manuallyCreatedSequence, stateMachine);
[1603]182    }
[1625]183
[1603]184    @Test
[1927]185    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception {
[1942]186        // TODO make test run
[1979]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         */
[1603]212    }
[1625]213
214    @Test
[1927]215    public void testCreateInteractionFromEventSequence_DEDA_1() throws Exception {
216        createInteractionFromEventSequenceWorkflow(deda_1);
217    }
[1929]218
[1927]219    @Test
220    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception {
221        createInteractionFromEventSequenceWorkflow(deda_2);
222    }
[1929]223
[1927]224    @Test
225    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception {
226        createInteractionFromEventSequenceWorkflow(ita_1);
227    }
[1759]228
[1927]229    @Test
230    public void testCalculateUsageScore_DEDA_1() throws Exception {
231        calculateUsageScoreWorkflow(deda_1);
232    }
[1929]233
[1927]234    @Test
235    public void testCalculateUsageScore_DEDA_2() throws Exception {
236        calculateUsageScoreWorkflow(deda_2);
237    }
[1625]238
[1927]239    @Test
240    public void testCalculateUsageScore_ITA_1() throws Exception {
241        calculateUsageScoreWorkflow(ita_1);
[1625]242    }
[1926]243
[1835]244    @Test
[1927]245    public void testCreateScheduling_DEDA_1() throws Exception {
246        createSchedulingWorkflow(deda_1);
[1835]247    }
[1929]248
[1927]249    @Test
250    public void testCreateScheduling_DEDA_2() throws Exception {
251        createSchedulingWorkflow(deda_2);
252    }
[1929]253
[1927]254    @Test
255    public void testCreateScheduling_ITA() throws Exception {
256        createSchedulingWorkflow(ita_1);
257    }
[1926]258
[1896]259    @Test
[1927]260    public void testValidateModelWithLog_DEDA_1() throws Exception {
261        validateModelWithLogWorkflow(deda_1);
[1643]262    }
[1926]263
[1763]264    @Test
[1927]265    public void testValidateModelWithLog_DEDA_2() throws Exception {
266        validateModelWithLogWorkflow(deda_2);
[1763]267    }
[1926]268
[1898]269    @Test
[1927]270    public void testValidateModelWithLog_ITA_1() throws Exception {
271        validateModelWithLogWorkflow(ita_1);
272    }
[1929]273
[1927]274    private void validateModelWithLogWorkflow(TestData testdata) throws Exception {
275        Properties properties = loadProperties(testdata);
276        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]277        Model model =
[1979]278            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
279                .getFile()));
[1929]280
[1927]281        // run validation
[1929]282        int violations =
283            UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context"));
[1926]284        if (violations == 0) {
[1900]285            System.out.println("No problems found.");
286        }
[1926]287        else {
288            System.out.println(violations + " violations found.");
289        }
[1898]290    }
[1929]291
[1927]292    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception {
293        Properties properties = loadProperties(testdata);
294        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]295        Model model =
[1979]296            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
297                .getFile()));
[1929]298
[1927]299        // create a test case for each observed sequence
[1929]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"));
[1927]306            i++;
[1926]307        }
[1929]308
309        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile);
[1927]310    }
[1929]311
[1927]312    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
313        Properties properties = loadProperties(testdata);
314        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1898]315        Model model =
[1979]316            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
317                .getFile()));
[1929]318        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
319        Collection<List<Event>> generatedSequences =
320            createRandomSequences(usageProfile, properties);
321
[1927]322        int i = 1;
[1929]323        List<Interaction> interactions = new LinkedList<>();
[1927]324        int[] lengths = new int[generatedSequences.size()];
325        for (List<Event> sequence : generatedSequences) {
[1931]326            interactions.add(UMLUtils
327                .createInteractionFromEventSequence(sequence, model,
328                                                    properties.getProperty("testcases.prefix") +
329                                                        "_" + i,
330                                                    properties.getProperty("test.context")));
[1927]331            lengths[i - 1] = sequence.size();
332            i++;
[1926]333        }
[1929]334        for (int j = 0; j < interactions.size(); j++) {
335            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile);
[1927]336            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
[1900]337        }
[1898]338    }
[1929]339
[1927]340    private void createSchedulingWorkflow(TestData testdata) throws Exception {
341        Properties properties = loadProperties(testdata);
342        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]343        Model model =
[1979]344            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
345                .getFile()));
[1929]346        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
347        Collection<List<Event>> generatedSequences =
348            createRandomSequences(usageProfile, properties);
[1927]349        int i = 1;
350        for (List<Event> sequence : generatedSequences) {
[1929]351            UMLUtils.createInteractionFromEventSequence(sequence, model,
352                                                        properties.getProperty("testcases.prefix") +
353                                                            "_" + i,
[1927]354                                                        properties.getProperty("test.context"));
355            i++;
[1926]356        }
357
[1929]358        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context"));
359
360        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile);
[1914]361    }
[1929]362
[1927]363    private Properties loadProperties(TestData testdata) throws Exception {
364        Properties properties = new Properties();
[1929]365        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile)
366            .getFile()));
[1927]367        return properties;
368    }
[1929]369
370    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata,
371                                                                  Properties properties)
372        throws Exception
373    {
[1927]374        // load usage journal
[1914]375        HTTPLogParser parser =
[1927]376            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
[1926]377                .getFile()));
[1929]378        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile)
379            .getFile()));
[1927]380        Collection<List<Event>> sequences = parser.getSequences();
[1914]381
[1929]382        // remove non SOAP events and convert to SimpleSOAPEventType
383        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
[1927]384        for (List<Event> sequence : sequences) {
[1979]385            List<Event> simpleSOAPSequence = SOAPUtils.convertToSimpleSOAPEvent(sequence, true);
386            List<Event> simpleSOAPSequenceWithNormalizedMethodNames =
387                SOAPUtils.normalizeOperationNames(simpleSOAPSequence, properties
388                    .getProperty("methodName.prefixToRemove"), properties
389                    .getProperty("methodName.suffixToRemove"));
390            simpleSOAPSequences.add(simpleSOAPSequenceWithNormalizedMethodNames);
[1926]391        }
[1929]392
[1927]393        // remove calls to ingored services
394        Set<String> ignoredServices = new HashSet<>();
395        String ignoredServicesString = properties.getProperty("test.ignored.services");
[1929]396        if (ignoredServicesString != null) {
397            for (String service : ignoredServicesString.split(",")) {
[1927]398                ignoredServices.add(service.trim());
399            }
[1926]400        }
[1929]401
402        for (List<Event> sequence : simpleSOAPSequences) {
[1927]403            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
[1914]404                Event event = eventIter.next();
[1929]405                SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType();
[1927]406                if (ignoredServices.contains(eventType.getServiceName())) {
[1914]407                    eventIter.remove();
408                }
[1979]409                else if( ignoredServices.contains(eventType.getClientName())) {
410                    eventIter.remove();
411                }
[1914]412            }
413        }
[1929]414        return simpleSOAPSequences;
[1898]415    }
416
[1931]417    private IStochasticProcess createUsageProfile(TestData testdata,
418                                                  Collection<List<Event>> sequences)
[1929]419        throws Exception
420    {
[1927]421        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
[1929]422        usageProfile.train(sequences);
423        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile);
424        SerializationUtils.serialize(usageProfile, fos);
425        fos.close();
[1927]426        return usageProfile;
[1908]427    }
[1929]428
429    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile,
430                                                          Properties properties) throws Exception
431    {
[1927]432        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
433        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1"));
[1929]434        int testCaseMaxLength =
435            Integer.parseInt(properties.getProperty("testcases.maxlenth", "100"));
[1927]436        int maxIter = numberOfTestCases * 100;
[1929]437        RandomWalkGenerator testGenerator =
438            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true,
439                                    maxIter);
[1927]440        return testGenerator.generateTestSuite(usageProfile);
441    }
[1929]442
[1625]443    private void deleteFiles(File file) {
444        if (file.exists()) {
445            if (file.isDirectory()) {
446                for (File child : file.listFiles()) {
447                    deleteFiles(child);
448                }
449            }
[1603]450
[1625]451            try {
452                file.delete();
453            }
454            catch (Exception e) {
455                // ignore and delete as much as possible
456            }
457        }
458    }
459
[1603]460}
Note: See TracBrowser for help on using the repository browser.