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

Last change on this file since 1835 was 1835, checked in by sherbold, 10 years ago
  • updated UMLUtils and UMLUtilsTest to work with a first version of the MIDAS logistics pilot. This also required changes in the service name maps. They now must point to the component for a service, not the interface.
  • Property svn:mime-type set to text/plain
File size: 11.3 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");
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");
197        }
198
199        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_2_result.uml");
200       
201    }
202
203    @Test
204    public void testCalculateUsageScore_1() throws Exception {
205        // parse log file
206        HTTPLogParser parser =
207            new HTTPLogParser(new File(ClassLoader
208                .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
209        parser.parseFile(new File(ClassLoader
210            .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
211        Collection<List<Event>> httpSequences = parser.getSequences();
212
213        Model model =
214            ModelUtils.loadModel(ClassLoader
215                .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
216
217        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
218        for (List<Event> httpSequence : httpSequences) {
219            for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
220                Event event = eventIter.next();
221                if (!(event.getType() instanceof SOAPEventType)) {
222                    eventIter.remove();
223                }
224            }
225            simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
226            // simpleSOAPSequences.add(httpSequence);
227        }
228
229        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
230        usageProfile.train(simpleSOAPSequences);
231
232        Collection<List<Event>> genSeqs =
233            (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
234
235        int i = 1;
236        int[] lengths = new int[genSeqs.size()];
237        for (List<Event> seq : genSeqs) {
238            UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i);
239            lengths[i - 1] = seq.size();
240            i++;
241        }
242        for (int j = 0; j < genSeqs.size(); j++) {
243            Interaction interaction =
244                (Interaction) model.getPackagedElement("seq_" + j, true,
245                                                       UMLPackage.Literals.INTERACTION, true);
246            double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
247            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
248        }
249    }
250   
251    @Test
252    public void testCreateScheduling_1() throws Exception {
253        Model model =
254                ModelUtils.loadModel(ClassLoader
255                    .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
256       
257        IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
258       
259        UMLUtils.createScheduling(model, usageProfile);
260       
261        ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
262    }
263
264    private void deleteFiles(File file) {
265        if (file.exists()) {
266            if (file.isDirectory()) {
267                for (File child : file.listFiles()) {
268                    deleteFiles(child);
269                }
270            }
271
272            try {
273                file.delete();
274            }
275            catch (Exception e) {
276                // ignore and delete as much as possible
277            }
278        }
279    }
280
281}
Note: See TracBrowser for help on using the repository browser.