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

Last change on this file was 2233, checked in by pharms, 7 years ago
  • solved some findbugs issues
  • 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;
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);
[2021]125        File outdir = new File(OUTPUT_DIR);
126        if( !outdir.exists() ) {
127            outdir.mkdirs();
128        }
[1898]129    }
[1926]130
[1625]131    @After
132    public void tearDown() throws Exception {
[1929]133        if (DELETE_OUTPUTS) {
134            deleteFiles(new File(OUTPUT_DIR));
135        }
[2021]136       
[1625]137    }
[1603]138
[1931]139    @Test
[1927]140    public void testCreateUMLTransitionSequence_ITA_1() throws Exception {
141        TestData testdata = ita_1;
[1929]142
[1931]143        /*
144         * Properties properties = loadProperties(testdata); //Collection<List<Event>> sequences =
145         * loadAndPreprocessUsageJournal(testdata, properties);
146         *
[1979]147         * Model model = ModelUtils.loadModel(new
148         * File(ClassLoader.getSystemResource(testdata.dslModelFile).getFile()));
[1931]149         *
150         * StateMachine stateMachine = (StateMachine)
151         * model.getPackagedElement("StateMachineTransportService", true,
152         * UMLPackage.Literals.STATE_MACHINE, true);
153         *
154         *
155         * Collection<List<Event>> umlSequences = new LinkedList<>();
156         *
157         * // remove everything but transport from sequences for (List<Event> sequence : sequences)
158         * { for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { Event
159         * event = eventIter.next(); if
160         * (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
161         * eventIter.remove(); } } umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence,
162         * stateMachine)); }
163         */
164
[1603]165        Model model =
[1979]166            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
167                .getFile()));
[1603]168
169        StateMachine stateMachine =
[1929]170            (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
171                                                    UMLPackage.Literals.STATE_MACHINE, true);
[1603]172
[1931]173        List<Event> manuallyCreatedSequence = new LinkedList<>();
174        manuallyCreatedSequence
175            .add(new Event(new SimpleSOAPEventType("transportInstructionRequest",
176                                                   "TransportService", "Logistics_Environment",
[1994]177                                                   null, null, null)));
[1931]178        manuallyCreatedSequence
179            .add(new Event(new SimpleSOAPEventType("transportInstructionConfirmationRequest",
180                                                   "materialSupplierService",
[1994]181                                                   "Logistics_Environment", null, null, null)));
[1929]182
[1942]183        // TODO make test case run
[1979]184        // UMLUtils.createUMLTransitionSequence(manuallyCreatedSequence, stateMachine);
[1603]185    }
[1625]186
[1603]187    @Test
[1927]188    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception {
[1942]189        // TODO make test run
[1979]190        /*
191         * TestData testdata = ita_1;
192         *
193         * assertTrue("test currently not working", false);
194         *
195         * Properties properties = loadProperties(testdata); Collection<List<Event>> sequences =
196         * loadAndPreprocessUsageJournal(testdata, properties); Model model =
197         * ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
198         * StateMachine stateMachine = (StateMachine)
199         * model.getPackagedElement("StateMachineTransportService", true,
200         * UMLPackage.Literals.STATE_MACHINE, true);
201         *
202         * Collection<List<Event>> umlSequences = new LinkedList<>();
203         *
204         * // remove everything but transport from sequences for (List<Event> sequence : sequences)
205         * { for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { Event
206         * event = eventIter.next(); if
207         * (!"TransportService".equals(SOAPUtils.getServiceNameFromEvent(event))) {
208         * eventIter.remove(); } } umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence,
209         * stateMachine)); }
210         *
211         * UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
212         *
213         * ModelUtils.writeModelToFile(model, OUTPUT_DIR + "ita_v2_result.uml");
214         */
[1603]215    }
[1625]216
217    @Test
[1927]218    public void testCreateInteractionFromEventSequence_DEDA_1() throws Exception {
219        createInteractionFromEventSequenceWorkflow(deda_1);
220    }
[1929]221
[1927]222    @Test
223    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception {
224        createInteractionFromEventSequenceWorkflow(deda_2);
225    }
[1929]226
[1927]227    @Test
228    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception {
229        createInteractionFromEventSequenceWorkflow(ita_1);
230    }
[1759]231
[1927]232    @Test
233    public void testCalculateUsageScore_DEDA_1() throws Exception {
234        calculateUsageScoreWorkflow(deda_1);
235    }
[1929]236
[1927]237    @Test
238    public void testCalculateUsageScore_DEDA_2() throws Exception {
239        calculateUsageScoreWorkflow(deda_2);
240    }
[1625]241
[1927]242    @Test
243    public void testCalculateUsageScore_ITA_1() throws Exception {
244        calculateUsageScoreWorkflow(ita_1);
[1625]245    }
[1926]246
[1835]247    @Test
[1927]248    public void testCreateScheduling_DEDA_1() throws Exception {
249        createSchedulingWorkflow(deda_1);
[1835]250    }
[1929]251
[1927]252    @Test
253    public void testCreateScheduling_DEDA_2() throws Exception {
254        createSchedulingWorkflow(deda_2);
255    }
[1929]256
[1927]257    @Test
[1998]258    public void testCreateScheduling_ITA_1() throws Exception {
[1927]259        createSchedulingWorkflow(ita_1);
260    }
[1926]261
[1896]262    @Test
[1927]263    public void testValidateModelWithLog_DEDA_1() throws Exception {
264        validateModelWithLogWorkflow(deda_1);
[1643]265    }
[1926]266
[1763]267    @Test
[1927]268    public void testValidateModelWithLog_DEDA_2() throws Exception {
269        validateModelWithLogWorkflow(deda_2);
[1763]270    }
[1926]271
[1898]272    @Test
[1927]273    public void testValidateModelWithLog_ITA_1() throws Exception {
274        validateModelWithLogWorkflow(ita_1);
275    }
[1994]276   
277    @Test
278    public void testSerialization_ITA_1() throws Exception {
279        TestData testdata = ita_1;
280        Properties properties = loadProperties(testdata);
281        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[2006]282        IStochasticProcess model = createUsageProfile(testdata, properties, sequences);
[1994]283        byte[] serialized = SerializationUtils.serialize(model);
284        SerializationUtils.deserialize(serialized);
285    }
[1929]286
[1927]287    private void validateModelWithLogWorkflow(TestData testdata) throws Exception {
288        Properties properties = loadProperties(testdata);
289        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]290        Model model =
[1979]291            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
292                .getFile()));
[1929]293
[1927]294        // run validation
[1929]295        int violations =
296            UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context"));
[1926]297        if (violations == 0) {
[1900]298            System.out.println("No problems found.");
299        }
[1926]300        else {
301            System.out.println(violations + " violations found.");
302        }
[1898]303    }
[1929]304
[1927]305    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception {
306        Properties properties = loadProperties(testdata);
307        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]308        Model model =
[1979]309            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
310                .getFile()));
[1929]311
[1927]312        // create a test case for each observed sequence
[2009]313        UMLUtils.createInteractionFromEventSequence(sequences, model,  properties.getProperty("testcases.prefix"), properties.getProperty("test.context"), false);
[1929]314
315        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile);
[1927]316    }
[1929]317
[1927]318    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
319        Properties properties = loadProperties(testdata);
320        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1898]321        Model model =
[1979]322            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
323                .getFile()));
[2006]324        IStochasticProcess usageProfile = createUsageProfile(testdata, properties, sequences);
[1929]325        Collection<List<Event>> generatedSequences =
326            createRandomSequences(usageProfile, properties);
327
[1927]328        int i = 1;
[2009]329        List<Interaction> interactions = UMLUtils.createInteractionFromEventSequence(generatedSequences, model, properties.getProperty("testcases.prefix"), properties.getProperty("test.context"), Boolean.parseBoolean(properties.getProperty("testcases.data.random", "false")));
[1927]330        int[] lengths = new int[generatedSequences.size()];
331        for (List<Event> sequence : generatedSequences) {
332            lengths[i - 1] = sequence.size();
333            i++;
[1926]334        }
[1929]335        for (int j = 0; j < interactions.size(); j++) {
336            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile);
[1927]337            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
[1900]338        }
[1898]339    }
[1929]340
[1927]341    private void createSchedulingWorkflow(TestData testdata) throws Exception {
342        Properties properties = loadProperties(testdata);
343        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
[1929]344        Model model =
[1979]345            ModelUtils.loadModel(new File(ClassLoader.getSystemResource(testdata.dslModelFile)
346                .getFile()));
[2006]347        IStochasticProcess usageProfile = createUsageProfile(testdata, properties, sequences);
[1929]348        Collection<List<Event>> generatedSequences =
349            createRandomSequences(usageProfile, properties);
[2009]350        UMLUtils.createInteractionFromEventSequence(generatedSequences, model, properties.getProperty("testcases.prefix"), properties.getProperty("test.context"), Boolean.parseBoolean(properties.getProperty("testcases.data.random", "false")));
351       
[1929]352        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context"));
353
354        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile);
[1914]355    }
[1929]356
[1927]357    private Properties loadProperties(TestData testdata) throws Exception {
358        Properties properties = new Properties();
[1929]359        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile)
360            .getFile()));
[1927]361        return properties;
362    }
[1929]363
364    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata,
365                                                                  Properties properties)
366        throws Exception
367    {
[1927]368        // load usage journal
[1914]369        HTTPLogParser parser =
[1927]370            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
[1926]371                .getFile()));
[1929]372        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile)
373            .getFile()));
[1927]374        Collection<List<Event>> sequences = parser.getSequences();
[1914]375
[1992]376        sequences = SOAPUtils.removeNonSOAPEvents(sequences);
[1993]377        sequences = SOAPUtils.sortAndConvertSequences(sequences, true, true);
[1992]378        sequences = SOAPUtils.normalizeOperationNames(sequences, properties
[1979]379                    .getProperty("methodName.prefixToRemove"), properties
380                    .getProperty("methodName.suffixToRemove"));
[1992]381        sequences = SOAPUtils.removeCallsToIgnoredServices(sequences, properties.getProperty("test.ignored.services"));
382       
383        return sequences;
[1898]384    }
385
[2006]386    private IStochasticProcess createUsageProfile(TestData testdata, Properties properties,
[1931]387                                                  Collection<List<Event>> sequences)
[1929]388        throws Exception
389    {
[2006]390        TrieBasedModel usageProfile = new HighOrderMarkovModel(Integer.parseInt(properties.getProperty("usageprofile.markovorder", "1")), new Random(1));
[1929]391        usageProfile.train(sequences);
392        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile);
393        SerializationUtils.serialize(usageProfile, fos);
394        fos.close();
[1927]395        return usageProfile;
[1908]396    }
[1929]397
398    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile,
399                                                          Properties properties) throws Exception
400    {
[1927]401        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
[2071]402        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlength", "1"));
[2027]403        long maxIterValidSequence = Long.parseLong(properties.getProperty("testcases.maxIterValid", "100"));
[1929]404        int testCaseMaxLength =
[2009]405            Integer.parseInt(properties.getProperty("testcases.maxlength", "100"));
[1927]406        int maxIter = numberOfTestCases * 100;
[1929]407        RandomWalkGenerator testGenerator =
408            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true,
[2027]409                                    maxIter, maxIterValidSequence);
[2009]410        return SOAPUtils.dropInvalidResponseRequestPairs(testGenerator.generateTestSuite(usageProfile));
[1927]411    }
[1929]412
[1625]413    private void deleteFiles(File file) {
414        if (file.exists()) {
415            if (file.isDirectory()) {
[2233]416                File[] children = file.listFiles();
417                if (children != null) {
418                    for (File child : children) {
419                        deleteFiles(child);
420                    }
[1625]421                }
422            }
[1603]423
[1625]424            try {
425                file.delete();
426            }
427            catch (Exception e) {
428                // ignore and delete as much as possible
429            }
430        }
431    }
432
[1603]433}
Note: See TracBrowser for help on using the repository browser.