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

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