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

Last change on this file since 1931 was 1931, checked in by sherbold, 9 years ago
  • minor update of tests
  • Property svn:mime-type set to text/plain
File size: 18.7 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
[1929]80    private final static TestData ita_1 = new TestData("ita_properties.prop",
81                                                       "ita_usagejournal.log",
82                                                       "ita_usageprofile.dat", "ita_model.uml",
83                                                       "ita_model_testsuite.uml",
84                                                       "ita_model_scheduling.uml");
85
[1927]86    private static class TestData {
87        public final String propertiesFile;
88        public final String usageJournalFile;
89        public final String usageProfileFile;
90        public final String dslModelFile;
91        public final String testSuiteFile;
92        public final String schedulingFile;
93
94        public TestData(String propertiesFile,
[1929]95                        String usageJournalFile,
96                        String usageProfileFile,
97                        String dslModelFile,
98                        String testSuiteFile,
99                        String schedulingFile)
[1927]100        {
101            this.propertiesFile = propertiesFile;
102            this.usageJournalFile = usageJournalFile;
103            this.usageProfileFile = usageProfileFile;
104            this.dslModelFile = dslModelFile;
105            this.testSuiteFile = testSuiteFile;
106            this.schedulingFile = schedulingFile;
[1929]107
[1927]108        }
109
110        @Override
111        public String toString() {
112            StringBuilder strBld = new StringBuilder();
113            strBld.append("Properties    " + propertiesFile + "\n");
114            strBld.append("Usage Journal " + usageJournalFile + "\n");
115            strBld.append("Usage Profile " + usageProfileFile + "\n");
116            strBld.append("DSL Model     " + dslModelFile + "\n");
117            strBld.append("Test Suite    " + testSuiteFile + "\n");
118            strBld.append("Scheduling    " + schedulingFile + "\n");
119            return strBld.toString();
120        }
121    }
122
[1898]123    @BeforeClass
124    public static void setUpBeforeClass() throws Exception {
[1929]125        new TextConsole(Level.SEVERE);
[1898]126    }
[1926]127
[1625]128    @After
129    public void tearDown() throws Exception {
[1929]130        if (DELETE_OUTPUTS) {
131            deleteFiles(new File(OUTPUT_DIR));
132        }
[1625]133    }
[1603]134
[1931]135    @Test
[1927]136    public void testCreateUMLTransitionSequence_ITA_1() throws Exception {
137        TestData testdata = ita_1;
[1929]138
[1931]139        /*
140         * Properties properties = loadProperties(testdata); //Collection<List<Event>> sequences =
141         * loadAndPreprocessUsageJournal(testdata, properties);
142         *
143         * Model model =
144         * ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
145         *
146         * StateMachine stateMachine = (StateMachine)
147         * model.getPackagedElement("StateMachineTransportService", true,
148         * UMLPackage.Literals.STATE_MACHINE, true);
149         *
150         *
151         * Collection<List<Event>> umlSequences = new LinkedList<>();
152         *
153         * // remove everything but transport from sequences for (List<Event> sequence : sequences)
154         * { for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { Event
155         * event = eventIter.next(); if
156         * (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
157         * eventIter.remove(); } } umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence,
158         * stateMachine)); }
159         */
160
[1603]161        Model model =
[1927]162            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
[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",
172                                                   null)));
173        manuallyCreatedSequence
174            .add(new Event(new SimpleSOAPEventType("transportInstructionConfirmationRequest",
175                                                   "materialSupplierService",
176                                                   "Logistics_Environment", null)));
[1929]177
[1931]178        UMLUtils.createUMLTransitionSequence(manuallyCreatedSequence, stateMachine);
[1603]179    }
[1625]180
[1603]181    @Test
[1927]182    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception {
183        TestData testdata = ita_1;
[1931]184       
185        assertTrue("test currently not working", false);
[1929]186
[1927]187        Properties properties = loadProperties(testdata);
188        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1603]189        Model model =
[1927]190            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
[1931]191        StateMachine stateMachine =
192            (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
193                                                    UMLPackage.Literals.STATE_MACHINE, true);
[1603]194
[1931]195        Collection<List<Event>> umlSequences = new LinkedList<>();
196
[1926]197        // remove everything but transport from sequences
[1927]198        for (List<Event> sequence : sequences) {
199            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
[1603]200                Event event = eventIter.next();
[1931]201                if (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
[1603]202                    eventIter.remove();
203                }
204            }
[1931]205            umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence, stateMachine));
[1603]206        }
[1625]207
[1931]208        UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
[1926]209
210        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "ita_v2_result.uml");
[1603]211    }
[1625]212
213    @Test
[1927]214    public void testCreateInteractionFromEventSequence_DEDA_1() throws Exception {
215        createInteractionFromEventSequenceWorkflow(deda_1);
216    }
[1929]217
[1927]218    @Test
219    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception {
220        createInteractionFromEventSequenceWorkflow(deda_2);
221    }
[1929]222
[1927]223    @Test
224    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception {
225        createInteractionFromEventSequenceWorkflow(ita_1);
226    }
[1759]227
[1927]228    @Test
229    public void testCalculateUsageScore_DEDA_1() throws Exception {
230        calculateUsageScoreWorkflow(deda_1);
231    }
[1929]232
[1927]233    @Test
234    public void testCalculateUsageScore_DEDA_2() throws Exception {
235        calculateUsageScoreWorkflow(deda_2);
236    }
[1625]237
[1927]238    @Test
239    public void testCalculateUsageScore_ITA_1() throws Exception {
240        calculateUsageScoreWorkflow(ita_1);
[1625]241    }
[1926]242
[1835]243    @Test
[1927]244    public void testCreateScheduling_DEDA_1() throws Exception {
245        createSchedulingWorkflow(deda_1);
[1835]246    }
[1929]247
[1927]248    @Test
249    public void testCreateScheduling_DEDA_2() throws Exception {
250        createSchedulingWorkflow(deda_2);
251    }
[1929]252
[1927]253    @Test
254    public void testCreateScheduling_ITA() throws Exception {
255        createSchedulingWorkflow(ita_1);
256    }
[1926]257
[1896]258    @Test
[1927]259    public void testValidateModelWithLog_DEDA_1() throws Exception {
260        validateModelWithLogWorkflow(deda_1);
[1643]261    }
[1926]262
[1763]263    @Test
[1927]264    public void testValidateModelWithLog_DEDA_2() throws Exception {
265        validateModelWithLogWorkflow(deda_2);
[1763]266    }
[1926]267
[1898]268    @Test
[1927]269    public void testValidateModelWithLog_ITA_1() throws Exception {
270        validateModelWithLogWorkflow(ita_1);
271    }
[1929]272
[1927]273    private void validateModelWithLogWorkflow(TestData testdata) throws Exception {
274        Properties properties = loadProperties(testdata);
275        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]276        Model model =
277            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
278
[1927]279        // run validation
[1929]280        int violations =
281            UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context"));
[1926]282        if (violations == 0) {
[1900]283            System.out.println("No problems found.");
284        }
[1926]285        else {
286            System.out.println(violations + " violations found.");
287        }
[1898]288    }
[1929]289
[1927]290    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception {
291        Properties properties = loadProperties(testdata);
292        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]293        Model model =
294            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
295
[1927]296        // create a test case for each observed sequence
[1929]297        int i = 0;
298        for (List<Event> sequence : sequences) {
299            UMLUtils.createInteractionFromEventSequence(sequence, model,
300                                                        properties.getProperty("testcases.prefix") +
301                                                            "_" + i,
302                                                        properties.getProperty("test.context"));
[1927]303            i++;
[1926]304        }
[1929]305
306        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile);
[1927]307    }
[1929]308
[1927]309    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
310        Properties properties = loadProperties(testdata);
311        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1898]312        Model model =
[1929]313            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
314        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
315        Collection<List<Event>> generatedSequences =
316            createRandomSequences(usageProfile, properties);
317
[1927]318        int i = 1;
[1929]319        List<Interaction> interactions = new LinkedList<>();
[1927]320        int[] lengths = new int[generatedSequences.size()];
321        for (List<Event> sequence : generatedSequences) {
[1931]322            interactions.add(UMLUtils
323                .createInteractionFromEventSequence(sequence, model,
324                                                    properties.getProperty("testcases.prefix") +
325                                                        "_" + i,
326                                                    properties.getProperty("test.context")));
[1927]327            lengths[i - 1] = sequence.size();
328            i++;
[1926]329        }
[1929]330        for (int j = 0; j < interactions.size(); j++) {
331            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile);
[1927]332            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
[1900]333        }
[1898]334    }
[1929]335
[1927]336    private void createSchedulingWorkflow(TestData testdata) throws Exception {
337        Properties properties = loadProperties(testdata);
338        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]339        Model model =
340            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
341        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences);
342        Collection<List<Event>> generatedSequences =
343            createRandomSequences(usageProfile, properties);
[1927]344        int i = 1;
345        for (List<Event> sequence : generatedSequences) {
[1929]346            UMLUtils.createInteractionFromEventSequence(sequence, model,
347                                                        properties.getProperty("testcases.prefix") +
348                                                            "_" + i,
[1927]349                                                        properties.getProperty("test.context"));
350            i++;
[1926]351        }
352
[1929]353        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context"));
354
355        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile);
[1914]356    }
[1929]357
[1927]358    private Properties loadProperties(TestData testdata) throws Exception {
359        Properties properties = new Properties();
[1929]360        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile)
361            .getFile()));
[1927]362        return properties;
363    }
[1929]364
365    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata,
366                                                                  Properties properties)
367        throws Exception
368    {
[1927]369        // load usage journal
[1914]370        HTTPLogParser parser =
[1927]371            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
[1926]372                .getFile()));
[1929]373        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile)
374            .getFile()));
[1927]375        Collection<List<Event>> sequences = parser.getSequences();
[1914]376
[1929]377        // remove non SOAP events and convert to SimpleSOAPEventType
378        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
[1927]379        for (List<Event> sequence : sequences) {
[1929]380            simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true));
[1926]381        }
[1929]382
[1927]383        // remove calls to ingored services
384        Set<String> ignoredServices = new HashSet<>();
385        String ignoredServicesString = properties.getProperty("test.ignored.services");
[1929]386        if (ignoredServicesString != null) {
387            for (String service : ignoredServicesString.split(",")) {
[1927]388                ignoredServices.add(service.trim());
389            }
[1926]390        }
[1929]391
392        for (List<Event> sequence : simpleSOAPSequences) {
[1927]393            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
[1914]394                Event event = eventIter.next();
[1929]395                SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType();
[1927]396                if (ignoredServices.contains(eventType.getServiceName())) {
[1914]397                    eventIter.remove();
398                }
399            }
400        }
[1929]401        return simpleSOAPSequences;
[1898]402    }
403
[1931]404    private IStochasticProcess createUsageProfile(TestData testdata,
405                                                  Collection<List<Event>> sequences)
[1929]406        throws Exception
407    {
[1927]408        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
[1929]409        usageProfile.train(sequences);
410        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile);
411        SerializationUtils.serialize(usageProfile, fos);
412        fos.close();
[1927]413        return usageProfile;
[1908]414    }
[1929]415
416    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile,
417                                                          Properties properties) throws Exception
418    {
[1927]419        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
420        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1"));
[1929]421        int testCaseMaxLength =
422            Integer.parseInt(properties.getProperty("testcases.maxlenth", "100"));
[1927]423        int maxIter = numberOfTestCases * 100;
[1929]424        RandomWalkGenerator testGenerator =
425            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true,
426                                    maxIter);
[1927]427        return testGenerator.generateTestSuite(usageProfile);
428    }
[1929]429
[1625]430    private void deleteFiles(File file) {
431        if (file.exists()) {
432            if (file.isDirectory()) {
433                for (File child : file.listFiles()) {
434                    deleteFiles(child);
435                }
436            }
[1603]437
[1625]438            try {
439                file.delete();
440            }
441            catch (Exception e) {
442                // ignore and delete as much as possible
443            }
444        }
445    }
446
[1603]447}
Note: See TracBrowser for help on using the repository browser.