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 |
|
---|
24 | import org.eclipse.uml2.uml.Interaction;
|
---|
25 | import org.eclipse.uml2.uml.Model;
|
---|
26 | import org.eclipse.uml2.uml.StateMachine;
|
---|
27 | import org.eclipse.uml2.uml.Transition;
|
---|
28 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
29 | import org.junit.After;
|
---|
30 | import org.junit.Test;
|
---|
31 |
|
---|
32 | import de.fraunhofer.fokus.testing.ModelUtils;
|
---|
33 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
34 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
35 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
36 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
37 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
38 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
39 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * <p>
|
---|
43 | * Tests for AutoQUESTs UMLUtils
|
---|
44 | * </p>
|
---|
45 | *
|
---|
46 | * @author Steffen Herbold
|
---|
47 | */
|
---|
48 | public class UMLUtilsTest {
|
---|
49 |
|
---|
50 | private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
|
---|
51 |
|
---|
52 | /**
|
---|
53 | *
|
---|
54 | */
|
---|
55 | @After
|
---|
56 | public void tearDown() throws Exception {
|
---|
57 | deleteFiles(new File(OUTPUT_DIR));
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test(expected = java.lang.RuntimeException.class)
|
---|
61 | public void testCreateUMLTransitionSequence_1() throws Exception {
|
---|
62 | // parse log file
|
---|
63 | HTTPLogParser parser = new HTTPLogParser();
|
---|
64 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
65 | .getFile()));
|
---|
66 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
67 | Model model =
|
---|
68 | ModelUtils.loadModel(ClassLoader
|
---|
69 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
70 |
|
---|
71 | StateMachine stateMachine =
|
---|
72 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
73 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
74 |
|
---|
75 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
76 | for (List<Event> httpSequence : httpSequences) {
|
---|
77 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
78 | Event event = eventIter.next();
|
---|
79 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
80 | eventIter.remove();
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
84 | eventIter.remove();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Test
|
---|
94 | public void testConvertStateMachineToUsageProfile_1() throws Exception {
|
---|
95 | // parse log file
|
---|
96 | HTTPLogParser parser = new HTTPLogParser();
|
---|
97 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
98 | .getFile()));
|
---|
99 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
100 | Model model =
|
---|
101 | ModelUtils.loadModel(ClassLoader
|
---|
102 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
103 |
|
---|
104 | StateMachine stateMachine =
|
---|
105 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
106 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
107 |
|
---|
108 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
109 | for (List<Event> httpSequence : httpSequences) {
|
---|
110 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
111 | Event event = eventIter.next();
|
---|
112 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
113 | eventIter.remove();
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
117 | eventIter.remove();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | List<List<Transition>> matchingSequences =
|
---|
123 | UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
124 | if (matchingSequences.size() >= 1) {
|
---|
125 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
126 | for (Transition transition : matchingSequences.get(0)) {
|
---|
127 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
128 | }
|
---|
129 | umlSequences.add(umlEventSequence);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
|
---|
133 |
|
---|
134 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Test
|
---|
138 | public void testCreateInteractionFromEventSequence_1() throws Exception {
|
---|
139 | // parse log file
|
---|
140 | HTTPLogParser parser =
|
---|
141 | new HTTPLogParser(new File(ClassLoader
|
---|
142 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
143 | .getFile()));
|
---|
144 | parser
|
---|
145 | .parseFile(new File(ClassLoader
|
---|
146 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
147 | .getFile()));
|
---|
148 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
149 |
|
---|
150 | Model model =
|
---|
151 | ModelUtils.loadModel(ClassLoader
|
---|
152 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
153 |
|
---|
154 | for (List<Event> httpSequence : httpSequences) {
|
---|
155 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
156 | Event event = eventIter.next();
|
---|
157 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
158 | eventIter.remove();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence");
|
---|
163 | }
|
---|
164 |
|
---|
165 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Test
|
---|
169 | public void testCalculateUsageScore_1() throws Exception {
|
---|
170 | // parse log file
|
---|
171 | HTTPLogParser parser =
|
---|
172 | new HTTPLogParser(new File(ClassLoader
|
---|
173 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
174 | parser.parseFile(new File(ClassLoader
|
---|
175 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
176 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
177 |
|
---|
178 | Model model =
|
---|
179 | ModelUtils.loadModel(ClassLoader
|
---|
180 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
181 |
|
---|
182 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
183 | for (List<Event> httpSequence : httpSequences) {
|
---|
184 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
185 | Event event = eventIter.next();
|
---|
186 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
187 | eventIter.remove();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
191 | // simpleSOAPSequences.add(httpSequence);
|
---|
192 | }
|
---|
193 |
|
---|
194 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
195 | usageProfile.train(simpleSOAPSequences);
|
---|
196 |
|
---|
197 | Collection<List<Event>> genSeqs =
|
---|
198 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
199 |
|
---|
200 | int i = 1;
|
---|
201 | int[] lengths = new int[genSeqs.size()];
|
---|
202 | for (List<Event> seq : genSeqs) {
|
---|
203 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i);
|
---|
204 | lengths[i - 1] = seq.size();
|
---|
205 | i++;
|
---|
206 | }
|
---|
207 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
208 | Interaction interaction =
|
---|
209 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
210 | UMLPackage.Literals.INTERACTION, true);
|
---|
211 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
212 | System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | private void deleteFiles(File file) {
|
---|
217 | if (file.exists()) {
|
---|
218 | if (file.isDirectory()) {
|
---|
219 | for (File child : file.listFiles()) {
|
---|
220 | deleteFiles(child);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | try {
|
---|
225 | file.delete();
|
---|
226 | }
|
---|
227 | catch (Exception e) {
|
---|
228 | // ignore and delete as much as possible
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | }
|
---|