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

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