[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;
|
---|
| 18 | import java.util.Collection;
|
---|
| 19 | import java.util.Iterator;
|
---|
| 20 | import java.util.LinkedList;
|
---|
| 21 | import java.util.List;
|
---|
[1643] | 22 | import java.util.Random;
|
---|
[1603] | 23 |
|
---|
[1763] | 24 | import org.apache.commons.lang.SerializationUtils;
|
---|
[1643] | 25 | import org.eclipse.uml2.uml.Interaction;
|
---|
[1603] | 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 |
|
---|
[1759] | 33 | import de.fraunhofer.fokus.testing.ModelUtils;
|
---|
[1603] | 34 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 35 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
[1643] | 36 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
[1603] | 37 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
| 38 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
[1643] | 39 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
| 40 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
[1763] | 41 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
[1603] | 42 |
|
---|
| 43 | /**
|
---|
| 44 | * <p>
|
---|
| 45 | * Tests for AutoQUESTs UMLUtils
|
---|
| 46 | * </p>
|
---|
| 47 | *
|
---|
| 48 | * @author Steffen Herbold
|
---|
| 49 | */
|
---|
| 50 | public class UMLUtilsTest {
|
---|
[1625] | 51 |
|
---|
[1606] | 52 | private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
|
---|
[1625] | 53 |
|
---|
[1603] | 54 | /**
|
---|
| 55 | *
|
---|
| 56 | */
|
---|
[1625] | 57 | @After
|
---|
| 58 | public void tearDown() throws Exception {
|
---|
| 59 | deleteFiles(new File(OUTPUT_DIR));
|
---|
| 60 | }
|
---|
[1603] | 61 |
|
---|
[1625] | 62 | @Test(expected = java.lang.RuntimeException.class)
|
---|
| 63 | public void testCreateUMLTransitionSequence_1() throws Exception {
|
---|
[1603] | 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 =
|
---|
[1759] | 70 | ModelUtils.loadModel(ClassLoader
|
---|
[1603] | 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 | }
|
---|
[1625] | 94 |
|
---|
[1603] | 95 | @Test
|
---|
[1625] | 96 | public void testConvertStateMachineToUsageProfile_1() throws Exception {
|
---|
[1603] | 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 =
|
---|
[1759] | 103 | ModelUtils.loadModel(ClassLoader
|
---|
[1603] | 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 |
|
---|
[1625] | 124 | List<List<Transition>> matchingSequences =
|
---|
| 125 | UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
[1603] | 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);
|
---|
[1625] | 135 |
|
---|
[1759] | 136 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
[1603] | 137 | }
|
---|
[1625] | 138 |
|
---|
| 139 | @Test
|
---|
| 140 | public void testCreateInteractionFromEventSequence_1() throws Exception {
|
---|
| 141 | // parse log file
|
---|
| 142 | HTTPLogParser parser =
|
---|
| 143 | new HTTPLogParser(new File(ClassLoader
|
---|
[1643] | 144 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
| 145 | .getFile()));
|
---|
| 146 | parser
|
---|
| 147 | .parseFile(new File(ClassLoader
|
---|
| 148 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
| 149 | .getFile()));
|
---|
[1625] | 150 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1759] | 151 |
|
---|
[1625] | 152 | Model model =
|
---|
[1759] | 153 | ModelUtils.loadModel(ClassLoader
|
---|
[1625] | 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 | }
|
---|
[1752] | 166 |
|
---|
[1759] | 167 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
[1625] | 168 | }
|
---|
[1835] | 169 |
|
---|
| 170 | @Test
|
---|
| 171 | public void testCreateInteractionFromEventSequence_2() throws Exception {
|
---|
| 172 | // parse log file
|
---|
| 173 | HTTPLogParser parser =
|
---|
| 174 | new HTTPLogParser(new File(ClassLoader
|
---|
| 175 | .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
|
---|
| 176 | .getFile()));
|
---|
| 177 | parser
|
---|
| 178 | .parseFile(new File(ClassLoader
|
---|
| 179 | .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
|
---|
| 180 | .getFile()));
|
---|
| 181 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1625] | 182 |
|
---|
[1835] | 183 |
|
---|
| 184 | Model model =
|
---|
| 185 | ModelUtils.loadModel(ClassLoader
|
---|
| 186 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
|
---|
| 187 |
|
---|
| 188 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 189 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 190 | Event event = eventIter.next();
|
---|
| 191 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 192 | eventIter.remove();
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence");
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_2_result.uml");
|
---|
| 200 |
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[1643] | 203 | @Test
|
---|
| 204 | public void testCalculateUsageScore_1() throws Exception {
|
---|
| 205 | // parse log file
|
---|
| 206 | HTTPLogParser parser =
|
---|
| 207 | new HTTPLogParser(new File(ClassLoader
|
---|
| 208 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
| 209 | parser.parseFile(new File(ClassLoader
|
---|
| 210 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
| 211 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1759] | 212 |
|
---|
[1643] | 213 | Model model =
|
---|
[1759] | 214 | ModelUtils.loadModel(ClassLoader
|
---|
[1643] | 215 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
| 216 |
|
---|
| 217 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
| 218 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 219 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 220 | Event event = eventIter.next();
|
---|
| 221 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 222 | eventIter.remove();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
| 226 | // simpleSOAPSequences.add(httpSequence);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
| 230 | usageProfile.train(simpleSOAPSequences);
|
---|
| 231 |
|
---|
| 232 | Collection<List<Event>> genSeqs =
|
---|
| 233 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
| 234 |
|
---|
| 235 | int i = 1;
|
---|
| 236 | int[] lengths = new int[genSeqs.size()];
|
---|
| 237 | for (List<Event> seq : genSeqs) {
|
---|
| 238 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i);
|
---|
| 239 | lengths[i - 1] = seq.size();
|
---|
| 240 | i++;
|
---|
| 241 | }
|
---|
| 242 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
| 243 | Interaction interaction =
|
---|
| 244 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
| 245 | UMLPackage.Literals.INTERACTION, true);
|
---|
| 246 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
[1752] | 247 | System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
|
---|
[1643] | 248 | }
|
---|
| 249 | }
|
---|
[1763] | 250 |
|
---|
| 251 | @Test
|
---|
| 252 | public void testCreateScheduling_1() throws Exception {
|
---|
| 253 | Model model =
|
---|
| 254 | ModelUtils.loadModel(ClassLoader
|
---|
| 255 | .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
|
---|
| 256 |
|
---|
| 257 | IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
|
---|
| 258 |
|
---|
| 259 | UMLUtils.createScheduling(model, usageProfile);
|
---|
| 260 |
|
---|
| 261 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
|
---|
| 262 | }
|
---|
[1643] | 263 |
|
---|
[1625] | 264 | private void deleteFiles(File file) {
|
---|
| 265 | if (file.exists()) {
|
---|
| 266 | if (file.isDirectory()) {
|
---|
| 267 | for (File child : file.listFiles()) {
|
---|
| 268 | deleteFiles(child);
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
[1603] | 271 |
|
---|
[1625] | 272 | try {
|
---|
| 273 | file.delete();
|
---|
| 274 | }
|
---|
| 275 | catch (Exception e) {
|
---|
| 276 | // ignore and delete as much as possible
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[1603] | 281 | }
|
---|