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

Last change on this file since 1900 was 1900, checked in by sherbold, 9 years ago
  • updated model validation to report violation counts
  • code documentation
  • Property svn:mime-type set to text/plain
File size: 15.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.util.Collection;
19import java.util.Iterator;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Random;
23import java.util.logging.Level;
24
25import org.apache.commons.lang.SerializationUtils;
26import org.eclipse.uml2.uml.Interaction;
27import org.eclipse.uml2.uml.Model;
28import org.eclipse.uml2.uml.StateMachine;
29import org.eclipse.uml2.uml.Transition;
30import org.eclipse.uml2.uml.UMLPackage;
31import org.junit.After;
32import org.junit.BeforeClass;
33import org.junit.Test;
34
35import de.fraunhofer.fokus.testing.ModelUtils;
36import de.ugoe.cs.autoquest.eventcore.Event;
37import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
38import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
39import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
40import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
41import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
42import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
43import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
44import de.ugoe.cs.util.console.TextConsole;
45
46/**
47 * <p>
48 * Tests for AutoQUESTs UMLUtils
49 * </p>
50 *
51 * @author Steffen Herbold
52 */
53public class UMLUtilsTest {
54
55    private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
56
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59        new TextConsole(Level.INFO);
60    }
61   
62    @After
63    public void tearDown() throws Exception {
64        deleteFiles(new File(OUTPUT_DIR));
65    }
66
67    @Test(expected = java.lang.RuntimeException.class)
68    public void testCreateUMLTransitionSequence_1() throws Exception {
69        // parse log file
70        HTTPLogParser parser = new HTTPLogParser();
71        parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
72            .getFile()));
73        Collection<List<Event>> httpSequences = parser.getSequences();
74        Model model =
75            ModelUtils.loadModel(ClassLoader
76                .getSystemResourceAsStream("createSequence_1_model.uml"));
77
78        StateMachine stateMachine =
79            (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
80                                                    UMLPackage.Literals.STATE_MACHINE, true);
81
82        Collection<List<Event>> umlSequences = new LinkedList<>();
83        for (List<Event> httpSequence : httpSequences) {
84            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
85                Event event = eventIter.next();
86                if (!(event.getType() instanceof SOAPEventType)) {
87                    eventIter.remove();
88                }
89                else {
90                    if (!event.getType().toString().contains("ixsmq")) {
91                        eventIter.remove();
92                    }
93                }
94            }
95
96            umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
97        }
98    }
99
100    @Test
101    public void testConvertStateMachineToUsageProfile_1() throws Exception {
102        // parse log file
103        HTTPLogParser parser = new HTTPLogParser();
104        parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
105            .getFile()));
106        Collection<List<Event>> httpSequences = parser.getSequences();
107        Model model =
108            ModelUtils.loadModel(ClassLoader
109                .getSystemResourceAsStream("createSequence_1_model.uml"));
110
111        StateMachine stateMachine =
112            (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
113                                                    UMLPackage.Literals.STATE_MACHINE, true);
114
115        Collection<List<Event>> umlSequences = new LinkedList<>();
116        for (List<Event> httpSequence : httpSequences) {
117            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
118                Event event = eventIter.next();
119                if (!(event.getType() instanceof SOAPEventType)) {
120                    eventIter.remove();
121                }
122                else {
123                    if (!event.getType().toString().contains("ixsmq")) {
124                        eventIter.remove();
125                    }
126                }
127            }
128
129            List<List<Transition>> matchingSequences =
130                UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
131            if (matchingSequences.size() >= 1) {
132                List<Event> umlEventSequence = new LinkedList<>();
133                for (Transition transition : matchingSequences.get(0)) {
134                    umlEventSequence.add(new Event(new UMLTransitionType(transition)));
135                }
136                umlSequences.add(umlEventSequence);
137            }
138        }
139        UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
140
141        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
142    }
143
144    @Test
145    public void testCreateInteractionFromEventSequence_1() throws Exception {
146        // parse log file
147        HTTPLogParser parser =
148            new HTTPLogParser(new File(ClassLoader
149                .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
150                .getFile()));
151        parser
152            .parseFile(new File(ClassLoader
153                .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
154                .getFile()));
155        Collection<List<Event>> httpSequences = parser.getSequences();
156
157        Model model =
158            ModelUtils.loadModel(ClassLoader
159                .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
160
161        for (List<Event> httpSequence : httpSequences) {
162            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
163                Event event = eventIter.next();
164                if (!(event.getType() instanceof SOAPEventType)) {
165                    eventIter.remove();
166                }
167            }
168
169            UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", null);
170        }
171
172        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
173    }
174   
175    @Test
176    public void testCreateInteractionFromEventSequence_2() throws Exception {
177        // parse log file
178        HTTPLogParser parser =
179            new HTTPLogParser(new File(ClassLoader
180                .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
181                .getFile()));
182        parser
183            .parseFile(new File(ClassLoader
184                .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
185                .getFile()));
186        Collection<List<Event>> httpSequences = parser.getSequences();
187
188        Model model =
189            ModelUtils.loadModel(ClassLoader
190                .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
191
192        for (List<Event> httpSequence : httpSequences) {
193            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
194                Event event = eventIter.next();
195                if (!(event.getType() instanceof SOAPEventType)) {
196                    eventIter.remove();
197                }
198            }
199
200            UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", null);
201        }
202
203        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
204    }
205   
206    @Test
207    public void testHL7v2_1() throws Exception {
208        // parse log file
209        HTTPLogParser parser =
210            new HTTPLogParser(new File(ClassLoader
211                .getSystemResource("hl7_servicenamemap.txt")
212                .getFile()));
213        parser
214            .parseFile(new File(ClassLoader
215                .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
216                .getFile()));
217        Collection<List<Event>> httpSequences = parser.getSequences();
218
219       
220        Model model =
221            ModelUtils.loadModel(ClassLoader
222                .getSystemResourceAsStream("hl7model_v2.uml"));
223
224        for (List<Event> httpSequence : httpSequences) {
225            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
226                Event event = eventIter.next();
227                if (!(event.getType() instanceof SOAPEventType)) {
228                    eventIter.remove();
229                }
230            }
231
232            UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", "RLUSTestSuite_1");
233        }
234
235        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_2_result.uml");
236       
237    }
238
239    @Test
240    public void testCalculateUsageScore_1() throws Exception {
241        // parse log file
242        HTTPLogParser parser =
243            new HTTPLogParser(new File(ClassLoader
244                .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
245        parser.parseFile(new File(ClassLoader
246            .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
247        Collection<List<Event>> httpSequences = parser.getSequences();
248
249        Model model =
250            ModelUtils.loadModel(ClassLoader
251                .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
252
253        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
254        for (List<Event> httpSequence : httpSequences) {
255            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
256                Event event = eventIter.next();
257                if (!(event.getType() instanceof SOAPEventType)) {
258                    eventIter.remove();
259                }
260            }
261            simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
262            // simpleSOAPSequences.add(httpSequence);
263        }
264
265        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
266        usageProfile.train(simpleSOAPSequences);
267
268        Collection<List<Event>> genSeqs =
269            (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
270
271        int i = 1;
272        int[] lengths = new int[genSeqs.size()];
273        for (List<Event> seq : genSeqs) {
274            UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i, null);
275            lengths[i - 1] = seq.size();
276            i++;
277        }
278        for (int j = 0; j < genSeqs.size(); j++) {
279            Interaction interaction =
280                (Interaction) model.getPackagedElement("seq_" + j, true,
281                                                       UMLPackage.Literals.INTERACTION, true);
282            double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
283            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
284        }
285    }
286   
287    @Test
288    public void testCreateScheduling_1() throws Exception {
289        Model model =
290                ModelUtils.loadModel(ClassLoader
291                    .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
292       
293        IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
294       
295        UMLUtils.createScheduling(model, usageProfile, null);
296       
297        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
298    }
299   
300    @Test
301    public void testValidateModelWithLog_1() throws Exception {
302        HTTPLogParser parser =
303                new HTTPLogParser(new File(ClassLoader
304                    .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
305                    .getFile()));
306        parser
307            .parseFile(new File(ClassLoader
308                .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
309                .getFile()));
310        Collection<List<Event>> httpSequences = parser.getSequences();
311
312       
313        Model model =
314            ModelUtils.loadModel(ClassLoader
315                .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
316       
317        int violations = UMLUtils.validateModelWithLog(httpSequences, model, null);
318        if( violations==0 ) {
319            System.out.println("No problems found.");
320        } else {
321                System.out.println(violations + " violations found.");
322        }
323    }
324   
325    @Test
326    public void testValidateModelWithLog_2() throws Exception {
327        HTTPLogParser parser =
328                new HTTPLogParser(new File(ClassLoader
329                    .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
330                    .getFile()));
331        parser
332            .parseFile(new File(ClassLoader
333                .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
334                .getFile()));
335        Collection<List<Event>> httpSequences = parser.getSequences();
336
337       
338        Model model =
339            ModelUtils.loadModel(ClassLoader
340                .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
341       
342        int violations = UMLUtils.validateModelWithLog(httpSequences, model, null);
343        if( violations==0 ) {
344            System.out.println("No problems found.");
345        } else {
346            System.out.println(violations + " violations found.");
347        }
348    }
349   
350    @Test
351    public void testValidateModelWithLog_HL7_v2() throws Exception {
352        HTTPLogParser parser =
353                new HTTPLogParser(new File(ClassLoader
354                    .getSystemResource("hl7_servicenamemap.txt")
355                    .getFile()));
356        parser
357            .parseFile(new File(ClassLoader
358                .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
359                .getFile()));
360        Collection<List<Event>> httpSequences = parser.getSequences();
361
362       
363        Model model =
364            ModelUtils.loadModel(ClassLoader
365                .getSystemResourceAsStream("hl7model_v2.uml"));
366       
367        int violations = UMLUtils.validateModelWithLog(httpSequences, model, "IXSTestSuite_1");
368        if( violations==0 ) {
369            System.out.println("No problems found.");
370        } else {
371            System.out.println(violations + " violations found.");
372        }
373    }
374
375    private void deleteFiles(File file) {
376        if (file.exists()) {
377            if (file.isDirectory()) {
378                for (File child : file.listFiles()) {
379                    deleteFiles(child);
380                }
381            }
382
383            try {
384                file.delete();
385            }
386            catch (Exception e) {
387                // ignore and delete as much as possible
388            }
389        }
390    }
391
392}
Note: See TracBrowser for help on using the repository browser.