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.apache.commons.lang.SerializationUtils;
|
---|
25 | import org.eclipse.uml2.uml.Interaction;
|
---|
26 | import org.eclipse.uml2.uml.Model;
|
---|
27 | import org.eclipse.uml2.uml.StateMachine;
|
---|
28 | import org.eclipse.uml2.uml.Transition;
|
---|
29 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
30 | import org.junit.After;
|
---|
31 | import org.junit.Test;
|
---|
32 |
|
---|
33 | import de.fraunhofer.fokus.testing.ModelUtils;
|
---|
34 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
35 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
36 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
37 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
38 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
39 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
40 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
41 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * <p>
|
---|
45 | * Tests for AutoQUESTs UMLUtils
|
---|
46 | * </p>
|
---|
47 | *
|
---|
48 | * @author Steffen Herbold
|
---|
49 | */
|
---|
50 | public 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 testCalculateUsageScore_1() throws Exception {
|
---|
172 | // parse log file
|
---|
173 | HTTPLogParser parser =
|
---|
174 | new HTTPLogParser(new File(ClassLoader
|
---|
175 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
176 | parser.parseFile(new File(ClassLoader
|
---|
177 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
178 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
179 |
|
---|
180 | Model model =
|
---|
181 | ModelUtils.loadModel(ClassLoader
|
---|
182 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
183 |
|
---|
184 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
185 | for (List<Event> httpSequence : httpSequences) {
|
---|
186 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
187 | Event event = eventIter.next();
|
---|
188 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
189 | eventIter.remove();
|
---|
190 | }
|
---|
191 | }
|
---|
192 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
193 | // simpleSOAPSequences.add(httpSequence);
|
---|
194 | }
|
---|
195 |
|
---|
196 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
197 | usageProfile.train(simpleSOAPSequences);
|
---|
198 |
|
---|
199 | Collection<List<Event>> genSeqs =
|
---|
200 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
201 |
|
---|
202 | int i = 1;
|
---|
203 | int[] lengths = new int[genSeqs.size()];
|
---|
204 | for (List<Event> seq : genSeqs) {
|
---|
205 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i);
|
---|
206 | lengths[i - 1] = seq.size();
|
---|
207 | i++;
|
---|
208 | }
|
---|
209 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
210 | Interaction interaction =
|
---|
211 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
212 | UMLPackage.Literals.INTERACTION, true);
|
---|
213 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
214 | System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | @Test
|
---|
219 | public void testCreateScheduling_1() throws Exception {
|
---|
220 | Model model =
|
---|
221 | ModelUtils.loadModel(ClassLoader
|
---|
222 | .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
|
---|
223 |
|
---|
224 | IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
|
---|
225 |
|
---|
226 | UMLUtils.createScheduling(model, usageProfile);
|
---|
227 |
|
---|
228 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void deleteFiles(File file) {
|
---|
232 | if (file.exists()) {
|
---|
233 | if (file.isDirectory()) {
|
---|
234 | for (File child : file.listFiles()) {
|
---|
235 | deleteFiles(child);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | try {
|
---|
240 | file.delete();
|
---|
241 | }
|
---|
242 | catch (Exception e) {
|
---|
243 | // ignore and delete as much as possible
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | }
|
---|