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
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,
[1989]305                                                        properties.getProperty("test.context"),
306                                                        false);
[1927]307            i++;
[1926]308        }
[1929]309
310        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile);
[1927]311    }
[1929]312
[1927]313    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
314        Properties properties = loadProperties(testdata);
315        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1898]316        Model model =
[1979]317            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
318                .getFile()));
[1929]319        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
320        Collection<List<Event>> generatedSequences =
321            createRandomSequences(usageProfile, properties);
322
[1927]323        int i = 1;
[1929]324        List<Interaction> interactions = new LinkedList<>();
[1927]325        int[] lengths = new int[generatedSequences.size()];
326        for (List<Event> sequence : generatedSequences) {
[1931]327            interactions.add(UMLUtils
328                .createInteractionFromEventSequence(sequence, model,
329                                                    properties.getProperty("testcases.prefix") +
330                                                        "_" + i,
[1989]331                                                    properties.getProperty("test.context"),
332                                                    true));
[1927]333            lengths[i - 1] = sequence.size();
334            i++;
[1926]335        }
[1929]336        for (int j = 0; j < interactions.size(); j++) {
337            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile);
[1927]338            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
[1900]339        }
[1898]340    }
[1929]341
[1927]342    private void createSchedulingWorkflow(TestData testdata) throws Exception {
343        Properties properties = loadProperties(testdata);
344        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]345        Model model =
[1979]346            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
347                .getFile()));
[1929]348        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
349        Collection<List<Event>> generatedSequences =
350            createRandomSequences(usageProfile, properties);
[1927]351        int i = 1;
352        for (List<Event> sequence : generatedSequences) {
[1929]353            UMLUtils.createInteractionFromEventSequence(sequence, model,
354                                                        properties.getProperty("testcases.prefix") +
355                                                            "_" + i,
[1989]356                                                        properties.getProperty("test.context"),
357                                                        true);
[1927]358            i++;
[1926]359        }
360
[1929]361        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context"));
362
363        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile);
[1914]364    }
[1929]365
[1927]366    private Properties loadProperties(TestData testdata) throws Exception {
367        Properties properties = new Properties();
[1929]368        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile)
369            .getFile()));
[1927]370        return properties;
371    }
[1929]372
373    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata,
374                                                                  Properties properties)
375        throws Exception
376    {
[1927]377        // load usage journal
[1914]378        HTTPLogParser parser =
[1927]379            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
[1926]380                .getFile()));
[1929]381        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile)
382            .getFile()));
[1927]383        Collection<List<Event>> sequences = parser.getSequences();
[1914]384
[1929]385        // remove non SOAP events and convert to SimpleSOAPEventType
[1989]386        Collection<List<Event>> tmpSeqs = SOAPUtils.convertToSimpleSOAPEvent(sequences, true);
387        // normalize method names
388        Collection<List<Event>> simpleSOAPSequences = SOAPUtils.normalizeOperationNames(tmpSeqs, properties
[1979]389                    .getProperty("methodName.prefixToRemove"), properties
390                    .getProperty("methodName.suffixToRemove"));
[1929]391
[1989]392        // remove calls to ignored services
[1927]393        Set<String> ignoredServices = new HashSet<>();
394        String ignoredServicesString = properties.getProperty("test.ignored.services");
[1929]395        if (ignoredServicesString != null) {
396            for (String service : ignoredServicesString.split(",")) {
[1927]397                ignoredServices.add(service.trim());
398            }
[1926]399        }
[1929]400
401        for (List<Event> sequence : simpleSOAPSequences) {
[1927]402            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
[1914]403                Event event = eventIter.next();
[1929]404                SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType();
[1927]405                if (ignoredServices.contains(eventType.getServiceName())) {
[1914]406                    eventIter.remove();
407                }
[1979]408                else if( ignoredServices.contains(eventType.getClientName())) {
409                    eventIter.remove();
410                }
[1914]411            }
412        }
[1929]413        return simpleSOAPSequences;
[1898]414    }
415
[1931]416    private IStochasticProcess createUsageProfile(TestData testdata,
417                                                  Collection<List<Event>> sequences)
[1929]418        throws Exception
419    {
[1927]420        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
[1929]421        usageProfile.train(sequences);
422        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile);
423        SerializationUtils.serialize(usageProfile, fos);
424        fos.close();
[1927]425        return usageProfile;
[1908]426    }
[1929]427
428    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile,
429                                                          Properties properties) throws Exception
430    {
[1927]431        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
432        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1"));
[1929]433        int testCaseMaxLength =
434            Integer.parseInt(properties.getProperty("testcases.maxlenth", "100"));
[1927]435        int maxIter = numberOfTestCases * 100;
[1929]436        RandomWalkGenerator testGenerator =
437            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true,
438                                    maxIter);
[1927]439        return testGenerator.generateTestSuite(usageProfile);
440    }
[1929]441
[1625]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            }
[1603]449
[1625]450            try {
451                file.delete();
452            }
453            catch (Exception e) {
454                // ignore and delete as much as possible
455            }
456        }
457    }
458
[1603]459}
Note: See TracBrowser for help on using the repository browser.