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