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

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