[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;
|
---|
[1898] | 23 | import java.util.logging.Level;
|
---|
[1603] | 24 |
|
---|
[1763] | 25 | import org.apache.commons.lang.SerializationUtils;
|
---|
[1643] | 26 | import org.eclipse.uml2.uml.Interaction;
|
---|
[1603] | 27 | import org.eclipse.uml2.uml.Model;
|
---|
| 28 | import org.eclipse.uml2.uml.StateMachine;
|
---|
| 29 | import org.eclipse.uml2.uml.Transition;
|
---|
| 30 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
| 31 | import org.junit.After;
|
---|
[1898] | 32 | import org.junit.BeforeClass;
|
---|
[1603] | 33 | import org.junit.Test;
|
---|
| 34 |
|
---|
[1759] | 35 | import de.fraunhofer.fokus.testing.ModelUtils;
|
---|
[1603] | 36 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 37 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
|
---|
[1643] | 38 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
[1603] | 39 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
| 40 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
[1643] | 41 | import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator;
|
---|
| 42 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
[1763] | 43 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
[1898] | 44 | import de.ugoe.cs.util.console.Console;
|
---|
| 45 | import de.ugoe.cs.util.console.TextConsole;
|
---|
[1603] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * <p>
|
---|
| 49 | * Tests for AutoQUESTs UMLUtils
|
---|
| 50 | * </p>
|
---|
| 51 | *
|
---|
| 52 | * @author Steffen Herbold
|
---|
| 53 | */
|
---|
| 54 | public class UMLUtilsTest {
|
---|
[1625] | 55 |
|
---|
[1606] | 56 | private final static String OUTPUT_DIR = "target/tmp/test-outputs/";
|
---|
[1625] | 57 |
|
---|
[1898] | 58 | @BeforeClass
|
---|
| 59 | public static void setUpBeforeClass() throws Exception {
|
---|
| 60 | Console.getInstance().registerTraceListener(new TextConsole(Level.INFO));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1625] | 63 | @After
|
---|
| 64 | public void tearDown() throws Exception {
|
---|
| 65 | deleteFiles(new File(OUTPUT_DIR));
|
---|
| 66 | }
|
---|
[1603] | 67 |
|
---|
[1625] | 68 | @Test(expected = java.lang.RuntimeException.class)
|
---|
| 69 | public void testCreateUMLTransitionSequence_1() throws Exception {
|
---|
[1603] | 70 | // parse log file
|
---|
| 71 | HTTPLogParser parser = new HTTPLogParser();
|
---|
| 72 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
| 73 | .getFile()));
|
---|
| 74 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
| 75 | Model model =
|
---|
[1759] | 76 | ModelUtils.loadModel(ClassLoader
|
---|
[1603] | 77 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
| 78 |
|
---|
| 79 | StateMachine stateMachine =
|
---|
| 80 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
| 81 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
| 82 |
|
---|
| 83 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
| 84 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 85 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 86 | Event event = eventIter.next();
|
---|
| 87 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 88 | eventIter.remove();
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
| 92 | eventIter.remove();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | umlSequences.add(UMLUtils.createUMLTransitionSequence(httpSequence, stateMachine));
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[1625] | 100 |
|
---|
[1603] | 101 | @Test
|
---|
[1625] | 102 | public void testConvertStateMachineToUsageProfile_1() throws Exception {
|
---|
[1603] | 103 | // parse log file
|
---|
| 104 | HTTPLogParser parser = new HTTPLogParser();
|
---|
| 105 | parser.parseFile(new File(ClassLoader.getSystemResource("createSequence_1_usagedata.log")
|
---|
| 106 | .getFile()));
|
---|
| 107 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
| 108 | Model model =
|
---|
[1759] | 109 | ModelUtils.loadModel(ClassLoader
|
---|
[1603] | 110 | .getSystemResourceAsStream("createSequence_1_model.uml"));
|
---|
| 111 |
|
---|
| 112 | StateMachine stateMachine =
|
---|
| 113 | (StateMachine) model.getPackagedElement("PatientIDBehavior", true,
|
---|
| 114 | UMLPackage.Literals.STATE_MACHINE, true);
|
---|
| 115 |
|
---|
| 116 | Collection<List<Event>> umlSequences = new LinkedList<>();
|
---|
| 117 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 118 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 119 | Event event = eventIter.next();
|
---|
| 120 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 121 | eventIter.remove();
|
---|
| 122 | }
|
---|
| 123 | else {
|
---|
| 124 | if (!event.getType().toString().contains("ixsmq")) {
|
---|
| 125 | eventIter.remove();
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[1625] | 130 | List<List<Transition>> matchingSequences =
|
---|
| 131 | UMLUtils.determineMatchingTransitionSequences(httpSequence, stateMachine);
|
---|
[1603] | 132 | if (matchingSequences.size() >= 1) {
|
---|
| 133 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
| 134 | for (Transition transition : matchingSequences.get(0)) {
|
---|
| 135 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
| 136 | }
|
---|
| 137 | umlSequences.add(umlEventSequence);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | UMLUtils.convertStateMachineToUsageProfile(umlSequences, stateMachine);
|
---|
[1625] | 141 |
|
---|
[1759] | 142 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "convertStateMachineToUsageProfile_1.uml");
|
---|
[1603] | 143 | }
|
---|
[1625] | 144 |
|
---|
| 145 | @Test
|
---|
| 146 | public void testCreateInteractionFromEventSequence_1() throws Exception {
|
---|
| 147 | // parse log file
|
---|
| 148 | HTTPLogParser parser =
|
---|
| 149 | new HTTPLogParser(new File(ClassLoader
|
---|
[1643] | 150 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
| 151 | .getFile()));
|
---|
| 152 | parser
|
---|
| 153 | .parseFile(new File(ClassLoader
|
---|
| 154 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
| 155 | .getFile()));
|
---|
[1625] | 156 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1759] | 157 |
|
---|
[1625] | 158 | Model model =
|
---|
[1759] | 159 | ModelUtils.loadModel(ClassLoader
|
---|
[1625] | 160 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
| 161 |
|
---|
| 162 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 163 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 164 | Event event = eventIter.next();
|
---|
| 165 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 166 | eventIter.remove();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[1896] | 170 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", null);
|
---|
[1625] | 171 | }
|
---|
[1752] | 172 |
|
---|
[1759] | 173 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
[1625] | 174 | }
|
---|
[1835] | 175 |
|
---|
| 176 | @Test
|
---|
| 177 | public void testCreateInteractionFromEventSequence_2() throws Exception {
|
---|
| 178 | // parse log file
|
---|
| 179 | HTTPLogParser parser =
|
---|
| 180 | new HTTPLogParser(new File(ClassLoader
|
---|
| 181 | .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
|
---|
| 182 | .getFile()));
|
---|
| 183 | parser
|
---|
| 184 | .parseFile(new File(ClassLoader
|
---|
| 185 | .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
|
---|
| 186 | .getFile()));
|
---|
| 187 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1625] | 188 |
|
---|
[1835] | 189 | Model model =
|
---|
| 190 | ModelUtils.loadModel(ClassLoader
|
---|
| 191 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
|
---|
| 192 |
|
---|
| 193 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 194 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 195 | Event event = eventIter.next();
|
---|
| 196 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 197 | eventIter.remove();
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[1896] | 201 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", null);
|
---|
[1835] | 202 | }
|
---|
| 203 |
|
---|
[1898] | 204 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_1_result.uml");
|
---|
[1835] | 205 | }
|
---|
[1896] | 206 |
|
---|
| 207 | @Test
|
---|
| 208 | public void testHL7v2_1() throws Exception {
|
---|
| 209 | // parse log file
|
---|
| 210 | HTTPLogParser parser =
|
---|
| 211 | new HTTPLogParser(new File(ClassLoader
|
---|
| 212 | .getSystemResource("hl7_servicenamemap.txt")
|
---|
| 213 | .getFile()));
|
---|
| 214 | parser
|
---|
| 215 | .parseFile(new File(ClassLoader
|
---|
| 216 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
| 217 | .getFile()));
|
---|
| 218 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1835] | 219 |
|
---|
[1896] | 220 |
|
---|
| 221 | Model model =
|
---|
| 222 | ModelUtils.loadModel(ClassLoader
|
---|
| 223 | .getSystemResourceAsStream("hl7model_v2.uml"));
|
---|
| 224 |
|
---|
| 225 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 226 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 227 | Event event = eventIter.next();
|
---|
| 228 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 229 | eventIter.remove();
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | UMLUtils.createInteractionFromEventSequence(httpSequence, model, "testSequence", "RLUSTestSuite_1");
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateInteractionFromEventSequence_2_result.uml");
|
---|
| 237 |
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[1643] | 240 | @Test
|
---|
| 241 | public void testCalculateUsageScore_1() throws Exception {
|
---|
| 242 | // parse log file
|
---|
| 243 | HTTPLogParser parser =
|
---|
| 244 | new HTTPLogParser(new File(ClassLoader
|
---|
| 245 | .getSystemResource("testCalculateUsageScore_1_properties.txt").getFile()));
|
---|
| 246 | parser.parseFile(new File(ClassLoader
|
---|
| 247 | .getSystemResource("testCalculateUsageScore_1_usagedata.log").getFile()));
|
---|
| 248 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1759] | 249 |
|
---|
[1643] | 250 | Model model =
|
---|
[1759] | 251 | ModelUtils.loadModel(ClassLoader
|
---|
[1643] | 252 | .getSystemResourceAsStream("testCalculateUsageScore_1_model.uml"));
|
---|
| 253 |
|
---|
| 254 | Collection<List<Event>> simpleSOAPSequences = new LinkedList<>();
|
---|
| 255 | for (List<Event> httpSequence : httpSequences) {
|
---|
| 256 | for (Iterator<Event> eventIter = httpSequence.iterator(); eventIter.hasNext();) {
|
---|
| 257 | Event event = eventIter.next();
|
---|
| 258 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
| 259 | eventIter.remove();
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | simpleSOAPSequences.add(HTTPUtils.convertToSimpleSOAPEvent(httpSequence));
|
---|
| 263 | // simpleSOAPSequences.add(httpSequence);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1));
|
---|
| 267 | usageProfile.train(simpleSOAPSequences);
|
---|
| 268 |
|
---|
| 269 | Collection<List<Event>> genSeqs =
|
---|
| 270 | (new RandomWalkGenerator(10, 1, 100, true, 500)).generateTestSuite(usageProfile);
|
---|
| 271 |
|
---|
| 272 | int i = 1;
|
---|
| 273 | int[] lengths = new int[genSeqs.size()];
|
---|
| 274 | for (List<Event> seq : genSeqs) {
|
---|
[1896] | 275 | UMLUtils.createInteractionFromEventSequence(seq, model, "seq_" + i, null);
|
---|
[1643] | 276 | lengths[i - 1] = seq.size();
|
---|
| 277 | i++;
|
---|
| 278 | }
|
---|
| 279 | for (int j = 0; j < genSeqs.size(); j++) {
|
---|
| 280 | Interaction interaction =
|
---|
| 281 | (Interaction) model.getPackagedElement("seq_" + j, true,
|
---|
| 282 | UMLPackage.Literals.INTERACTION, true);
|
---|
| 283 | double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile);
|
---|
[1752] | 284 | System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]);
|
---|
[1643] | 285 | }
|
---|
| 286 | }
|
---|
[1763] | 287 |
|
---|
| 288 | @Test
|
---|
| 289 | public void testCreateScheduling_1() throws Exception {
|
---|
| 290 | Model model =
|
---|
| 291 | ModelUtils.loadModel(ClassLoader
|
---|
| 292 | .getSystemResourceAsStream("testCreateScheduling_1_testsuite.uml"));
|
---|
| 293 |
|
---|
| 294 | IStochasticProcess usageProfile = (IStochasticProcess) SerializationUtils.deserialize(ClassLoader.getSystemResourceAsStream("testCreateScheduling_1_usageprofile.dat"));
|
---|
| 295 |
|
---|
[1896] | 296 | UMLUtils.createScheduling(model, usageProfile, null);
|
---|
[1763] | 297 |
|
---|
| 298 | ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");
|
---|
| 299 | }
|
---|
[1898] | 300 |
|
---|
| 301 | @Test
|
---|
| 302 | public void testValidateModelWithLog_1() throws Exception {
|
---|
| 303 | HTTPLogParser parser =
|
---|
| 304 | new HTTPLogParser(new File(ClassLoader
|
---|
| 305 | .getSystemResource("testCreateInteractionFromEventSequence_1_properties.txt")
|
---|
| 306 | .getFile()));
|
---|
| 307 | parser
|
---|
| 308 | .parseFile(new File(ClassLoader
|
---|
| 309 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
| 310 | .getFile()));
|
---|
| 311 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
[1643] | 312 |
|
---|
[1898] | 313 |
|
---|
| 314 | Model model =
|
---|
| 315 | ModelUtils.loadModel(ClassLoader
|
---|
| 316 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_1_model.uml"));
|
---|
| 317 |
|
---|
| 318 | UMLUtils.validateModelWithLog(httpSequences, model, null);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | @Test
|
---|
| 322 | public void testValidateModelWithLog_2() throws Exception {
|
---|
| 323 | HTTPLogParser parser =
|
---|
| 324 | new HTTPLogParser(new File(ClassLoader
|
---|
| 325 | .getSystemResource("testCreateInteractionFromEventSequence_2_properties.txt")
|
---|
| 326 | .getFile()));
|
---|
| 327 | parser
|
---|
| 328 | .parseFile(new File(ClassLoader
|
---|
| 329 | .getSystemResource("testCreateInteractionFromEventSequence_2_usagedata.log")
|
---|
| 330 | .getFile()));
|
---|
| 331 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 | Model model =
|
---|
| 335 | ModelUtils.loadModel(ClassLoader
|
---|
| 336 | .getSystemResourceAsStream("testCreateInteractionFromEventSequence_2_model.uml"));
|
---|
| 337 |
|
---|
| 338 | UMLUtils.validateModelWithLog(httpSequences, model, null);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | @Test
|
---|
| 342 | public void testValidateModelWithLog_HL7_v2() throws Exception {
|
---|
| 343 | HTTPLogParser parser =
|
---|
| 344 | new HTTPLogParser(new File(ClassLoader
|
---|
| 345 | .getSystemResource("hl7_servicenamemap.txt")
|
---|
| 346 | .getFile()));
|
---|
| 347 | parser
|
---|
| 348 | .parseFile(new File(ClassLoader
|
---|
| 349 | .getSystemResource("testCreateInteractionFromEventSequence_1_usagedata.log")
|
---|
| 350 | .getFile()));
|
---|
| 351 | Collection<List<Event>> httpSequences = parser.getSequences();
|
---|
| 352 |
|
---|
| 353 |
|
---|
| 354 | Model model =
|
---|
| 355 | ModelUtils.loadModel(ClassLoader
|
---|
| 356 | .getSystemResourceAsStream("hl7model_v2.uml"));
|
---|
| 357 |
|
---|
| 358 | UMLUtils.validateModelWithLog(httpSequences, model, "IXSTestSuite_1");
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[1625] | 361 | private void deleteFiles(File file) {
|
---|
| 362 | if (file.exists()) {
|
---|
| 363 | if (file.isDirectory()) {
|
---|
| 364 | for (File child : file.listFiles()) {
|
---|
| 365 | deleteFiles(child);
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
[1603] | 368 |
|
---|
[1625] | 369 | try {
|
---|
| 370 | file.delete();
|
---|
| 371 | }
|
---|
| 372 | catch (Exception e) {
|
---|
| 373 | // ignore and delete as much as possible
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[1603] | 378 | }
|
---|