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.io.FileOutputStream;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.InputStream;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.LinkedList;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Random;
|
---|
27 |
|
---|
28 | import org.eclipse.emf.common.util.URI;
|
---|
29 | import org.eclipse.emf.ecore.EPackage;
|
---|
30 | import org.eclipse.emf.ecore.EcorePackage;
|
---|
31 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
32 | import org.eclipse.emf.ecore.resource.ResourceSet;
|
---|
33 | import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
|
---|
34 | import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
|
---|
35 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
---|
36 | import org.eclipse.emf.ecore.util.EcoreUtil;
|
---|
37 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
|
---|
38 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
|
---|
39 | import org.eclipse.uml2.uml.Interaction;
|
---|
40 | import org.eclipse.uml2.uml.Model;
|
---|
41 | import org.eclipse.uml2.uml.StateMachine;
|
---|
42 | import org.eclipse.uml2.uml.Transition;
|
---|
43 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
44 | import org.eclipse.uml2.uml.resource.UMLResource;
|
---|
45 | import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
|
---|
46 | import org.junit.After;
|
---|
47 | import org.junit.Test;
|
---|
48 |
|
---|
49 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
50 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
51 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
52 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
53 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
54 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
55 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Tests for AutoQUESTs UMLUtils
|
---|
60 | * </p>
|
---|
61 | *
|
---|
62 | * @author Steffen Herbold
|
---|
63 | */
|
---|
64 | public class UMLUtilsTest {
|
---|
65 |
|
---|
66 | private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
|
---|
67 |
|
---|
68 | /**
|
---|
69 | *
|
---|
70 | */
|
---|
71 | @After
|
---|
72 | public void tearDown() throws Exception {
|
---|
73 | deleteFiles(new File(OUTPUT_DIR));
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Test(expected = java.lang.RuntimeException.class)
|
---|
77 | public void testCreateUMLTransitionSequence_1() throws Exception {
|
---|
78 | // parse log file
|
---|
79 | HTTPLogParser parser = new HTTPLogParser();
|
---|
80 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
81 | .getFile()));
|
---|
82 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
83 | Model model =
|
---|
84 | loadModelFromInputStream(ClassLoader
|
---|
85 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
86 |
|
---|
87 | StateMachine stateMachine =
|
---|
88 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
89 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
90 |
|
---|
91 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
92 | for (List<Event> httpSequence : httpSequences) {
|
---|
93 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
94 | Event event = eventIter.next();
|
---|
95 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
96 | eventIter.remove();
|
---|
97 | }
|
---|
98 | else {
|
---|
99 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
100 | eventIter.remove();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Test
|
---|
110 | public void testConvertStateMachineToUsageProfile_1() throws Exception {
|
---|
111 | // parse log file
|
---|
112 | HTTPLogParser parser = new HTTPLogParser();
|
---|
113 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
114 | .getFile()));
|
---|
115 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
116 | Model model =
|
---|
117 | loadModelFromInputStream(ClassLoader
|
---|
118 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
119 |
|
---|
120 | StateMachine stateMachine =
|
---|
121 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
122 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
123 |
|
---|
124 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
125 | for (List<Event> httpSequence : httpSequences) {
|
---|
126 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
127 | Event event = eventIter.next();
|
---|
128 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
129 | eventIter.remove();
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
133 | eventIter.remove();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | List<List<Transition>> matchingSequences =
|
---|
139 | UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
140 | if (matchingSequences.size() >= 1) {
|
---|
141 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
142 | for (Transition transition : matchingSequences.get(0)) {
|
---|
143 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
144 | }
|
---|
145 | umlSequences.add(umlEventSequence);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
|
---|
149 |
|
---|
150 | writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Test
|
---|
154 | public void testCreateInteractionFromEventSequence_1() throws Exception {
|
---|
155 | // parse log file
|
---|
156 | HTTPLogParser parser =
|
---|
157 | new HTTPLogParser(new File(ClassLoader
|
---|
158 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
159 | .getFile()));
|
---|
160 | parser
|
---|
161 | .parseFile(new File(ClassLoader
|
---|
162 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
163 | .getFile()));
|
---|
164 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
165 | Model model =
|
---|
166 | loadModelFromInputStream(ClassLoader
|
---|
167 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
168 |
|
---|
169 | for (List<Event> httpSequence : httpSequences) {
|
---|
170 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
171 | Event event = eventIter.next();
|
---|
172 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
173 | eventIter.remove();
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence");
|
---|
178 | }
|
---|
179 | // writeModelToFile(model, "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Test
|
---|
183 | public void testCalculateUsageScore_1() throws Exception {
|
---|
184 | // parse log file
|
---|
185 | HTTPLogParser parser =
|
---|
186 | new HTTPLogParser(new File(ClassLoader
|
---|
187 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
188 | parser.parseFile(new File(ClassLoader
|
---|
189 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
190 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
191 | Model model =
|
---|
192 | loadModelFromInputStream(ClassLoader
|
---|
193 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
194 |
|
---|
195 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
196 | for (List<Event> httpSequence : httpSequences) {
|
---|
197 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
198 | Event event = eventIter.next();
|
---|
199 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
200 | eventIter.remove();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
204 | // simpleSOAPSequences.add(httpSequence);
|
---|
205 | }
|
---|
206 |
|
---|
207 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
208 | usageProfile.train(simpleSOAPSequences);
|
---|
209 |
|
---|
210 | Collection<List<Event>> genSeqs =
|
---|
211 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
212 |
|
---|
213 | int i = 1;
|
---|
214 | int[] lengths = new int[genSeqs.size()];
|
---|
215 | for (List<Event> seq : genSeqs) {
|
---|
216 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i);
|
---|
217 | lengths[i - 1] = seq.size();
|
---|
218 | i++;
|
---|
219 | }
|
---|
220 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
221 | Interaction interaction =
|
---|
222 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
223 | UMLPackage.Literals.INTERACTION, true);
|
---|
224 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
225 | System.out.format("usage score %02d: %.2f \t %d\n",j+1, usageScore, lengths[j]);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private static void writeModelToFile(Model model, String filename) throws IOException {
|
---|
230 | final ResourceSet resourceSet = new ResourceSetImpl();
|
---|
231 | UMLResourcesUtil.init(resourceSet);
|
---|
232 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
233 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new ResourceFactoryImpl() {
|
---|
234 | @Override
|
---|
235 | public Resource createResource(URI uri) {
|
---|
236 | return new XMIResourceImpl(uri) {
|
---|
237 | @Override
|
---|
238 | public boolean useUUIDs() {
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 | };
|
---|
242 | }
|
---|
243 | });
|
---|
244 |
|
---|
245 | Resource resource = resourceSet.createResource(URI.createURI("binresource"));
|
---|
246 |
|
---|
247 | resource.getContents().add(model);
|
---|
248 | FileOutputStream fos;
|
---|
249 | File file = new File(filename);
|
---|
250 | if (file.getParent() != null) {
|
---|
251 | file.getParentFile().mkdirs();
|
---|
252 | }
|
---|
253 | fos = new FileOutputStream(file);
|
---|
254 | resource.save(fos, null);
|
---|
255 | }
|
---|
256 |
|
---|
257 | private static Model loadModelFromInputStream(InputStream inputStream) {
|
---|
258 | ResourceSet resourceSet = new ResourceSetImpl();
|
---|
259 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
260 | .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
|
---|
261 | resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
262 | resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
|
---|
263 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
|
---|
264 | .put(Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
|
---|
265 | Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
|
---|
266 |
|
---|
267 | UMLResourcesUtil.init(resourceSet);
|
---|
268 |
|
---|
269 | uriMap.put(URI.createURI("pathmap://Papyrus.profile.uml"),
|
---|
270 | URI.createURI("file:/D:/.../Papyrus.profile.uml/"));
|
---|
271 |
|
---|
272 | EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
|
---|
273 |
|
---|
274 | Resource resource = new XMIResourceImpl();
|
---|
275 |
|
---|
276 | try {
|
---|
277 | // resource.load(inputStream, options);
|
---|
278 | resource.load(inputStream, null);
|
---|
279 | }
|
---|
280 | catch (IOException e) {
|
---|
281 | // TODO Auto-generated catch block
|
---|
282 | e.printStackTrace();
|
---|
283 | }
|
---|
284 |
|
---|
285 | return (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
|
---|
286 | }
|
---|
287 |
|
---|
288 | private void deleteFiles(File file) {
|
---|
289 | if (file.exists()) {
|
---|
290 | if (file.isDirectory()) {
|
---|
291 | for (File child : file.listFiles()) {
|
---|
292 | deleteFiles(child);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | try {
|
---|
297 | file.delete();
|
---|
298 | }
|
---|
299 | catch (Exception e) {
|
---|
300 | // ignore and delete as much as possible
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | }
|
---|