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

Last change on this file since 1927 was 1927, checked in by sherbold, 9 years ago
  • refactored tests
  • Property svn:mime-type set to text/plain
File size: 16.7 KB
Line 
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;
18import java.io.FileInputStream;
19import java.util.Collection;
20import java.util.HashSet;
21import java.util.Iterator;
22import java.util.LinkedList;
23import java.util.List;
24import java.util.Properties;
25import java.util.Random;
26import java.util.Set;
27import java.util.logging.Level;
28
29//import static org.junit.Assert.*;
30
31import org.apache.commons.lang.SerializationUtils;
32import org.eclipse.uml2.uml.Interaction;
33import org.eclipse.uml2.uml.Model;
34import org.eclipse.uml2.uml.StateMachine;
35import org.eclipse.uml2.uml.UMLPackage;
36import org.junit.After;
37import org.junit.BeforeClass;
38import org.junit.Test;
39
40import de.fraunhofer.fokus.testing.ModelUtils;
41import de.ugoe.cs.autoquest.eventcore.Event;
42import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
43import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
44import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
45import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
46import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
47import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
48import de.ugoe.cs.util.console.TextConsole;
49
50/**
51 * <p>
52 * Tests for AutoQUESTs UMLUtils
53 * </p>
54 *
55 * @author Steffen Herbold
56 */
57public class UMLUtilsTest {
58
59    private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
60   
61    // for RLUS
62    private final static TestData deda_1 = new TestData("deda_rlus_properties.prop",
63                                                      "deda_usagejournal.log",
64                                                      "deda_rlus_usageprofile.dat",
65                                                      "deda_model.uml",
66                                                      "deda_rlus_model_testsuite.uml",
67                                                      "deda_rlus_model_scheduling.uml");
68
69    // for IXS
70    private final static TestData deda_2 = new TestData("deda_ixs_properties.prop",
71                                                           "deda_usagejournal.log",
72                                                           "deda_ixs_usageprofile.dat",
73                                                           "deda_model.uml",
74                                                           "deda_ixs_model_testsuite.uml",
75                                                           "deda_ixs_model_scheduling.uml");
76
77    private final static TestData ita_1 = new TestData("ita_properties.prop", "ita_usagejournal.log",
78                                                     "ita_usageprofile.dat", "ita_model.uml",
79                                                     "ita_model_testsuite.uml",
80                                                     "ita_model_scheduling.uml");
81   
82    private static class TestData {
83        public final String propertiesFile;
84        public final String usageJournalFile;
85        public final String usageProfileFile;
86        public final String dslModelFile;
87        public final String testSuiteFile;
88        public final String schedulingFile;
89
90        public TestData(String propertiesFile,
91                String usageJournalFile,
92                String usageProfileFile,
93                String dslModelFile,
94                String testSuiteFile,
95                String schedulingFile)
96        {
97            this.propertiesFile = propertiesFile;
98            this.usageJournalFile = usageJournalFile;
99            this.usageProfileFile = usageProfileFile;
100            this.dslModelFile = dslModelFile;
101            this.testSuiteFile = testSuiteFile;
102            this.schedulingFile = schedulingFile;
103           
104        }
105
106        @Override
107        public String toString() {
108            StringBuilder strBld = new StringBuilder();
109            strBld.append("Properties    " + propertiesFile + "\n");
110            strBld.append("Usage Journal " + usageJournalFile + "\n");
111            strBld.append("Usage Profile " + usageProfileFile + "\n");
112            strBld.append("DSL Model     " + dslModelFile + "\n");
113            strBld.append("Test Suite    " + testSuiteFile + "\n");
114            strBld.append("Scheduling    " + schedulingFile + "\n");
115            return strBld.toString();
116        }
117    }
118
119    @BeforeClass
120    public static void setUpBeforeClass() throws Exception {
121        new TextConsole(Level.FINE);
122    }
123
124    @After
125    public void tearDown() throws Exception {
126        deleteFiles(new File(OUTPUT_DIR));
127    }
128
129    @Test(expected = java.lang.RuntimeException.class)
130    public void testCreateUMLTransitionSequence_ITA_1() throws Exception {
131        TestData testdata = ita_1;
132       
133        Properties properties = loadProperties(testdata);
134        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
135        Model model =
136            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
137
138        StateMachine stateMachine =
139                (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
140                                                        UMLPackage.Literals.STATE_MACHINE, true);
141
142           
143        Collection<List<Event>> umlSequences = new LinkedList<>();
144       
145        // remove everything but transport from sequences
146        for (List<Event> sequence : sequences) {
147            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
148                Event event = eventIter.next();
149                SOAPEventType eventType = (SOAPEventType) event.getType();
150                if (!"TransportService".equals(eventType.getServiceName())) {
151                    eventIter.remove();
152                }
153            }
154            umlSequences.add(UMLUtils.createUMLTransitionSequence(sequence, stateMachine));
155        }
156    }
157
158    @Test
159    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception {
160        TestData testdata = ita_1;
161       
162        Properties properties = loadProperties(testdata);
163        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
164
165        Model model =
166            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
167
168        // remove everything but transport from sequences
169        for (List<Event> sequence : sequences) {
170            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
171                Event event = eventIter.next();
172                SOAPEventType eventType = (SOAPEventType) event.getType();
173                if (!"TransportService".equals(eventType.getServiceName())) {
174                    eventIter.remove();
175                }
176            }
177        }
178
179        StateMachine stateMachine =
180            (StateMachine) model.getPackagedElement("StateMachineTransportService", true,
181                                                    UMLPackage.Literals.STATE_MACHINE, true);
182
183        UMLUtils.convertStateMachineToUsageProfile(sequences, stateMachine);
184
185        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "ita_v2_result.uml");
186    }
187
188    @Test
189    public void testCreateInteractionFromEventSequence_DEDA_1() throws Exception {
190        createInteractionFromEventSequenceWorkflow(deda_1);
191    }
192   
193    @Test
194    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception {
195        createInteractionFromEventSequenceWorkflow(deda_2);
196    }
197   
198    @Test
199    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception {
200        createInteractionFromEventSequenceWorkflow(ita_1);
201    }
202
203    @Test
204    public void testCalculateUsageScore_DEDA_1() throws Exception {
205        calculateUsageScoreWorkflow(deda_1);
206    }
207   
208    @Test
209    public void testCalculateUsageScore_DEDA_2() throws Exception {
210        calculateUsageScoreWorkflow(deda_2);
211    }
212
213   
214    @Test
215    public void testCalculateUsageScore_ITA_1() throws Exception {
216        calculateUsageScoreWorkflow(ita_1);
217    }
218
219    @Test
220    public void testCreateScheduling_DEDA_1() throws Exception {
221        createSchedulingWorkflow(deda_1);
222    }
223   
224    @Test
225    public void testCreateScheduling_DEDA_2() throws Exception {
226        createSchedulingWorkflow(deda_2);
227    }
228   
229    @Test
230    public void testCreateScheduling_ITA() throws Exception {
231        createSchedulingWorkflow(ita_1);
232    }
233
234    @Test
235    public void testValidateModelWithLog_DEDA_1() throws Exception {
236        validateModelWithLogWorkflow(deda_1);
237    }
238   
239
240    @Test
241    public void testValidateModelWithLog_DEDA_2() throws Exception {
242        validateModelWithLogWorkflow(deda_2);
243    }
244
245    @Test
246    public void testValidateModelWithLog_ITA_1() throws Exception {
247        validateModelWithLogWorkflow(ita_1);
248    }
249   
250    private void validateModelWithLogWorkflow(TestData testdata) throws Exception {
251        Properties properties = loadProperties(testdata);
252        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
253        Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
254       
255        // run validation
256        int violations = UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context"));
257        if (violations == 0) {
258            System.out.println("No problems found.");
259        }
260        else {
261            System.out.println(violations + " violations found.");
262        }
263    }
264   
265    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception {
266        Properties properties = loadProperties(testdata);
267        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
268        Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
269       
270        // create a test case for each observed sequence
271        int i=0;
272        for( List<Event> sequence : sequences ) {
273            UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i,
274                    properties.getProperty("test.context"));
275            i++;
276        }
277    }
278   
279    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception {
280        Properties properties = loadProperties(testdata);
281        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
282        Model model =
283            ModelUtils.loadModel(ClassLoader
284                .getSystemResourceAsStream(testdata.dslModelFile));
285        IStochasticProcess usageProfile = createUsageProfile(sequences);
286        Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties);
287       
288        int i = 1;
289        int[] lengths = new int[generatedSequences.size()];
290        for (List<Event> sequence : generatedSequences) {
291            UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i,
292                                                        properties.getProperty("test.context"));
293            lengths[i - 1] = sequence.size();
294            i++;
295        }
296        for (int j = 0; j < generatedSequences.size(); j++) {
297            Interaction interaction =
298                (Interaction) model.getPackagedElement(properties.getProperty("testcases.prefix") + j, true,
299                                                       UMLPackage.Literals.INTERACTION, true);
300            double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
301            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
302        }
303    }
304   
305    private void createSchedulingWorkflow(TestData testdata) throws Exception {
306        Properties properties = loadProperties(testdata);
307        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties);
308        Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile));
309        IStochasticProcess usageProfile = createUsageProfile(sequences);
310        Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties);
311        int i = 1;
312        for (List<Event> sequence : generatedSequences) {
313            UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i,
314                                                        properties.getProperty("test.context"));
315            i++;
316        }
317       
318        UMLUtils.createScheduling(model, usageProfile, null);
319
320        //ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
321    }
322   
323    private Properties loadProperties(TestData testdata) throws Exception {
324        Properties properties = new Properties();
325        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile).getFile()));
326        return properties;
327    }
328   
329    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, Properties properties) throws Exception {
330        // load usage journal
331        HTTPLogParser parser =
332            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile)
333                .getFile()));
334        parser
335            .parseFile(new File(ClassLoader
336                .getSystemResource(testdata.usageJournalFile)
337                .getFile()));
338        Collection<List<Event>> sequences = parser.getSequences();
339
340        // remove non SOAP events
341        for (List<Event> sequence : sequences) {
342            SOAPUtils.removeNonSOAPEvents(sequence);
343        }
344       
345        // remove calls to ingored services
346        Set<String> ignoredServices = new HashSet<>();
347        String ignoredServicesString = properties.getProperty("test.ignored.services");
348        if( ignoredServicesString!=null ) {
349            for( String service : ignoredServicesString.split(",") ) {
350                ignoredServices.add(service.trim());
351            }
352        }
353       
354        for (List<Event> sequence : sequences) {
355            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
356                Event event = eventIter.next();
357                SOAPEventType eventType = (SOAPEventType) event.getType();
358                if (ignoredServices.contains(eventType.getServiceName())) {
359                    eventIter.remove();
360                }
361            }
362        }
363        return sequences;
364    }
365   
366    private IStochasticProcess createUsageProfile(Collection<List<Event>> sequences) throws Exception {
367        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
368        for (List<Event> sequence : sequences) {
369            simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true));
370        }
371
372        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
373        usageProfile.train(simpleSOAPSequences);
374        return usageProfile;
375    }
376   
377    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, Properties properties) throws Exception {
378        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number"));
379        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1"));
380        int testCaseMaxLength = Integer.parseInt(properties.getProperty("testcases.maxlenth", "100"));
381        int maxIter = numberOfTestCases * 100;
382        RandomWalkGenerator testGenerator = new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, maxIter);
383        return testGenerator.generateTestSuite(usageProfile);
384    }
385   
386    private void deleteFiles(File file) {
387        if (file.exists()) {
388            if (file.isDirectory()) {
389                for (File child : file.listFiles()) {
390                    deleteFiles(child);
391                }
392            }
393
394            try {
395                file.delete();
396            }
397            catch (Exception e) {
398                // ignore and delete as much as possible
399            }
400        }
401    }
402
403}
Note: See TracBrowser for help on using the repository browser.