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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.uml;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Iterator;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Random;
|
---|
23 | import java.util.logging.Level;
|
---|
24 |
|
---|
25 | import org.apache.commons.lang.SerializationUtils;
|
---|
26 | import org.eclipse.uml2.uml.Interaction;
|
---|
27 | import org.eclipse.uml2.uml.Model;
|
---|
28 | import org.eclipse.uml2.uml.StateMachine;
|
---|
29 | import org.eclipse.uml2.uml.Transition;
|
---|
30 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
31 | import org.junit.After;
|
---|
32 | import org.junit.BeforeClass;
|
---|
33 | import org.junit.Test;
|
---|
34 |
|
---|
35 | import de.fraunhofer.fokus.testing.ModelUtils;
|
---|
36 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
37 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
38 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
39 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
40 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
41 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
42 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
43 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
44 | import de.ugoe.cs.util.console.TextConsole;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * Tests for AutoQUESTs UMLUtils
|
---|
49 | * </p>
|
---|
50 | *
|
---|
51 | * @author Steffen Herbold
|
---|
52 | */
|
---|
53 | public 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 testCalculateUsageScore_1() throws Exception {
|
---|
208 | // parse log file
|
---|
209 | HTTPLogParser parser =
|
---|
210 | new HTTPLogParser(new File(ClassLoader
|
---|
211 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
212 | parser.parseFile(new File(ClassLoader
|
---|
213 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
214 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
215 |
|
---|
216 | Model model =
|
---|
217 | ModelUtils.loadModel(ClassLoader
|
---|
218 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
219 |
|
---|
220 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
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 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
229 | // simpleSOAPSequences.add(httpSequence);
|
---|
230 | }
|
---|
231 |
|
---|
232 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
233 | usageProfile.train(simpleSOAPSequences);
|
---|
234 |
|
---|
235 | Collection<List<Event>> genSeqs =
|
---|
236 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
237 |
|
---|
238 | int i = 1;
|
---|
239 | int[] lengths = new int[genSeqs.size()];
|
---|
240 | for (List<Event> seq : genSeqs) {
|
---|
241 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i, null);
|
---|
242 | lengths[i - 1] = seq.size();
|
---|
243 | i++;
|
---|
244 | }
|
---|
245 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
246 | Interaction interaction =
|
---|
247 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
248 | UMLPackage.Literals.INTERACTION, true);
|
---|
249 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
250 | System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | @Test
|
---|
255 | public void testCreateScheduling_1() throws Exception {
|
---|
256 | Model model =
|
---|
257 | ModelUtils.loadModel(ClassLoader
|
---|
258 | .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
|
---|
259 |
|
---|
260 | IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
|
---|
261 |
|
---|
262 | UMLUtils.createScheduling(model, usageProfile, null);
|
---|
263 |
|
---|
264 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
|
---|
265 | }
|
---|
266 |
|
---|
267 | @Test
|
---|
268 | public void testValidateModelWithLog_1() throws Exception {
|
---|
269 | HTTPLogParser parser =
|
---|
270 | new HTTPLogParser(new File(ClassLoader
|
---|
271 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
272 | .getFile()));
|
---|
273 | parser
|
---|
274 | .parseFile(new File(ClassLoader
|
---|
275 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
276 | .getFile()));
|
---|
277 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
278 |
|
---|
279 |
|
---|
280 | Model model =
|
---|
281 | ModelUtils.loadModel(ClassLoader
|
---|
282 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
283 |
|
---|
284 | int violations = UMLUtils.validateModelWithLog(httpSequences, model, null);
|
---|
285 | if( violations==0 ) {
|
---|
286 | System.out.println("No problems found.");
|
---|
287 | } else {
|
---|
288 | System.out.println(violations + " violations found.");
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | @Test
|
---|
293 | public void testValidateModelWithLog_2() throws Exception {
|
---|
294 | HTTPLogParser parser =
|
---|
295 | new HTTPLogParser(new File(ClassLoader
|
---|
296 | .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
|
---|
297 | .getFile()));
|
---|
298 | parser
|
---|
299 | .parseFile(new File(ClassLoader
|
---|
300 | .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
|
---|
301 | .getFile()));
|
---|
302 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
303 |
|
---|
304 |
|
---|
305 | Model model =
|
---|
306 | ModelUtils.loadModel(ClassLoader
|
---|
307 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
|
---|
308 |
|
---|
309 | int violations = UMLUtils.validateModelWithLog(httpSequences, model, null);
|
---|
310 | if( violations==0 ) {
|
---|
311 | System.out.println("No problems found.");
|
---|
312 | } else {
|
---|
313 | System.out.println(violations + " violations found.");
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Test
|
---|
318 | public void testValidateModelWithLog_HL7_v2() throws Exception {
|
---|
319 | HTTPLogParser parser =
|
---|
320 | new HTTPLogParser(new File(ClassLoader
|
---|
321 | .getSystemResource("hl7_servicenamemap.txt")
|
---|
322 | .getFile()));
|
---|
323 | parser
|
---|
324 | .parseFile(new File(ClassLoader
|
---|
325 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
326 | .getFile()));
|
---|
327 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
328 |
|
---|
329 |
|
---|
330 | Model model =
|
---|
331 | ModelUtils.loadModel(ClassLoader
|
---|
332 | .getSystemResourceAsStream("hl7model_v2.uml"));
|
---|
333 |
|
---|
334 | int violations = UMLUtils.validateModelWithLog(httpSequences, model, "IXSTestSuite_1");
|
---|
335 | if( violations==0 ) {
|
---|
336 | System.out.println("No problems found.");
|
---|
337 | } else {
|
---|
338 | System.out.println(violations + " violations found.");
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Test
|
---|
343 | public void testValidateModelWithLog_ITA_v2() throws Exception {
|
---|
344 | HTTPLogParser parser =
|
---|
345 | new HTTPLogParser(new File(ClassLoader
|
---|
346 | .getSystemResource("ita_v2_servicenamemap.txt")
|
---|
347 | .getFile()));
|
---|
348 | parser
|
---|
349 | .parseFile(new File(ClassLoader
|
---|
350 | .getSystemResource("ita_usagejournal_v2_2.log")
|
---|
351 | .getFile()));
|
---|
352 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
353 |
|
---|
354 |
|
---|
355 | Model model =
|
---|
356 | ModelUtils.loadModel(ClassLoader
|
---|
357 | .getSystemResourceAsStream("ita_model_v2.uml"));
|
---|
358 |
|
---|
359 | int violations = UMLUtils.validateModelWithLog(httpSequences, model, null);
|
---|
360 | if( violations==0 ) {
|
---|
361 | System.out.println("No problems found.");
|
---|
362 | } else {
|
---|
363 | System.out.println(violations + " violations found.");
|
---|
364 | }
|
---|
365 |
|
---|
366 | // remove everything but transport from sequences
|
---|
367 | for (List<Event> httpSequence : httpSequences) {
|
---|
368 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
369 | Event event = eventIter.next();
|
---|
370 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
371 | eventIter.remove();
|
---|
372 | } else {
|
---|
373 | SOAPEventType eventType = (SOAPEventType) event.getType();
|
---|
374 | if( !"TransportService".equals(eventType.getServiceName()) ) {
|
---|
375 | eventIter.remove();
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "foo", "LogisticsTestSuite");
|
---|
381 | }
|
---|
382 |
|
---|
383 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "ita_v2_result.uml");
|
---|
384 |
|
---|
385 | }
|
---|
386 |
|
---|
387 | private void deleteFiles(File file) {
|
---|
388 | if (file.exists()) {
|
---|
389 | if (file.isDirectory()) {
|
---|
390 | for (File child : file.listFiles()) {
|
---|
391 | deleteFiles(child);
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | try {
|
---|
396 | file.delete();
|
---|
397 | }
|
---|
398 | catch (Exception e) {
|
---|
399 | // ignore and delete as much as possible
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | }
|
---|