Ignore:
Timestamp:
03/20/15 16:00:10 (9 years ago)
Author:
sherbold
Message:
  • refactored and commented UMLUtils
  • created UTPUtils
  • moved DOM related methods from UMLUtils to SOAPUtils
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtilsTest.java

    r1927 r1929  
    1717import java.io.File; 
    1818import java.io.FileInputStream; 
     19import java.io.FileOutputStream; 
    1920import java.util.Collection; 
    2021import java.util.HashSet; 
     
    2930//import static org.junit.Assert.*; 
    3031 
    31 import org.apache.commons.lang.SerializationUtils; 
     32 
     33 
     34 
    3235import org.eclipse.uml2.uml.Interaction; 
    3336import org.eclipse.uml2.uml.Model; 
     
    4346import de.ugoe.cs.autoquest.plugin.http.SOAPUtils; 
    4447import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; 
     48import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 
    4549import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator; 
    4650import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel; 
    4751import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; 
     52import de.ugoe.cs.util.SerializationUtils; 
    4853import de.ugoe.cs.util.console.TextConsole; 
    4954 
     
    5863 
    5964    private final static String OUTPUT_DIR = "target/tmp/test-outputs/"; 
    60      
     65 
     66    private final static boolean DELETE_OUTPUTS = false; 
     67 
    6168    // for RLUS 
    6269    private final static TestData deda_1 = new TestData("deda_rlus_properties.prop", 
    63                                                       "deda_usagejournal.log", 
    64                                                       "deda_rlus_usageprofile.dat", 
    65                                                       "deda_model.uml", 
    66                                                       "deda_rlus_model_testsuite.uml", 
    67                                                       "deda_rlus_model_scheduling.uml"); 
     70                                                        "deda_usagejournal.log", 
     71                                                        "deda_rlus_usageprofile.dat", 
     72                                                        "deda_model.uml", 
     73                                                        "deda_rlus_model_testsuite.uml", 
     74                                                        "deda_rlus_model_scheduling.uml"); 
    6875 
    6976    // for IXS 
    7077    private final static TestData deda_2 = new TestData("deda_ixs_properties.prop", 
    71                                                            "deda_usagejournal.log", 
    72                                                            "deda_ixs_usageprofile.dat", 
    73                                                            "deda_model.uml", 
    74                                                            "deda_ixs_model_testsuite.uml", 
    75                                                            "deda_ixs_model_scheduling.uml"); 
    76  
    77     private final static TestData ita_1 = new TestData("ita_properties.prop", "ita_usagejournal.log", 
    78                                                      "ita_usageprofile.dat", "ita_model.uml", 
    79                                                      "ita_model_testsuite.uml", 
    80                                                      "ita_model_scheduling.uml"); 
    81      
     78                                                        "deda_usagejournal.log", 
     79                                                        "deda_ixs_usageprofile.dat", 
     80                                                        "deda_model.uml", 
     81                                                        "deda_ixs_model_testsuite.uml", 
     82                                                        "deda_ixs_model_scheduling.uml"); 
     83 
     84    private final static TestData ita_1 = new TestData("ita_properties.prop", 
     85                                                       "ita_usagejournal.log", 
     86                                                       "ita_usageprofile.dat", "ita_model.uml", 
     87                                                       "ita_model_testsuite.uml", 
     88                                                       "ita_model_scheduling.uml"); 
     89 
    8290    private static class TestData { 
    8391        public final String propertiesFile; 
     
    8997 
    9098        public TestData(String propertiesFile, 
    91                 String usageJournalFile, 
    92                 String usageProfileFile, 
    93                 String dslModelFile, 
    94                 String testSuiteFile, 
    95                 String schedulingFile) 
     99                        String usageJournalFile, 
     100                        String usageProfileFile, 
     101                        String dslModelFile, 
     102                        String testSuiteFile, 
     103                        String schedulingFile) 
    96104        { 
    97105            this.propertiesFile = propertiesFile; 
     
    101109            this.testSuiteFile = testSuiteFile; 
    102110            this.schedulingFile = schedulingFile; 
    103              
     111 
    104112        } 
    105113 
     
    119127    @BeforeClass 
    120128    public static void setUpBeforeClass() throws Exception { 
    121         new TextConsole(Level.FINE); 
     129        new TextConsole(Level.SEVERE); 
    122130    } 
    123131 
    124132    @After 
    125133    public void tearDown() throws Exception { 
    126         deleteFiles(new File(OUTPUT_DIR)); 
     134        if (DELETE_OUTPUTS) { 
     135            deleteFiles(new File(OUTPUT_DIR)); 
     136        } 
    127137    } 
    128138 
     
    130140    public void testCreateUMLTransitionSequence_ITA_1() throws Exception { 
    131141        TestData testdata = ita_1; 
    132          
     142 
    133143        Properties properties = loadProperties(testdata); 
    134144        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
     
    137147 
    138148        StateMachine stateMachine = 
    139                 (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 
    140                                                         UMLPackage.Literals.STATE_MACHINE, true); 
    141  
    142             
     149            (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 
     150                                                    UMLPackage.Literals.STATE_MACHINE, true); 
     151 
    143152        Collection<List<Event>> umlSequences = new LinkedList<>(); 
    144          
     153 
    145154        // remove everything but transport from sequences 
    146155        for (List<Event> sequence : sequences) { 
     
    159168    public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception { 
    160169        TestData testdata = ita_1; 
    161          
     170 
    162171        Properties properties = loadProperties(testdata); 
    163172        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
     
    190199        createInteractionFromEventSequenceWorkflow(deda_1); 
    191200    } 
    192      
     201 
    193202    @Test 
    194203    public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception { 
    195204        createInteractionFromEventSequenceWorkflow(deda_2); 
    196205    } 
    197      
     206 
    198207    @Test 
    199208    public void testCreateInteractionFromEventSequence_ITA_1() throws Exception { 
     
    205214        calculateUsageScoreWorkflow(deda_1); 
    206215    } 
    207      
     216 
    208217    @Test 
    209218    public void testCalculateUsageScore_DEDA_2() throws Exception { 
     
    211220    } 
    212221 
    213      
    214222    @Test 
    215223    public void testCalculateUsageScore_ITA_1() throws Exception { 
     
    221229        createSchedulingWorkflow(deda_1); 
    222230    } 
    223      
     231 
    224232    @Test 
    225233    public void testCreateScheduling_DEDA_2() throws Exception { 
    226234        createSchedulingWorkflow(deda_2); 
    227235    } 
    228      
     236 
    229237    @Test 
    230238    public void testCreateScheduling_ITA() throws Exception { 
     
    236244        validateModelWithLogWorkflow(deda_1); 
    237245    } 
    238      
    239246 
    240247    @Test 
     
    247254        validateModelWithLogWorkflow(ita_1); 
    248255    } 
    249      
     256 
    250257    private void validateModelWithLogWorkflow(TestData testdata) throws Exception { 
    251258        Properties properties = loadProperties(testdata); 
    252259        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
    253         Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
    254          
     260        Model model = 
     261            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
     262 
    255263        // run validation 
    256         int violations = UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 
     264        int violations = 
     265            UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 
    257266        if (violations == 0) { 
    258267            System.out.println("No problems found."); 
     
    262271        } 
    263272    } 
    264      
     273 
    265274    private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception { 
    266275        Properties properties = loadProperties(testdata); 
    267276        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
    268         Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
    269          
     277        Model model = 
     278            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
     279 
    270280        // create a test case for each observed sequence 
    271         int i=0; 
    272         for( List<Event> sequence : sequences ) { 
    273             UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 
    274                     properties.getProperty("test.context")); 
     281        int i = 0; 
     282        for (List<Event> sequence : sequences) { 
     283            UMLUtils.createInteractionFromEventSequence(sequence, model, 
     284                                                        properties.getProperty("testcases.prefix") + 
     285                                                            "_" + i, 
     286                                                        properties.getProperty("test.context")); 
    275287            i++; 
    276288        } 
    277     } 
    278      
     289 
     290        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile); 
     291    } 
     292 
    279293    private void calculateUsageScoreWorkflow(TestData testdata) throws Exception { 
    280294        Properties properties = loadProperties(testdata); 
    281295        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
    282296        Model model = 
    283             ModelUtils.loadModel(ClassLoader 
    284                 .getSystemResourceAsStream(testdata.dslModelFile)); 
    285         IStochasticProcess usageProfile = createUsageProfile(sequences); 
    286         Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties); 
    287          
     297            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
     298        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 
     299        Collection<List<Event>> generatedSequences = 
     300            createRandomSequences(usageProfile, properties); 
     301 
    288302        int i = 1; 
     303        List<Interaction> interactions = new LinkedList<>(); 
    289304        int[] lengths = new int[generatedSequences.size()]; 
    290305        for (List<Event> sequence : generatedSequences) { 
    291             UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 
    292                                                         properties.getProperty("test.context")); 
     306            interactions.add(UMLUtils.createInteractionFromEventSequence(sequence, model, 
     307                                                        properties.getProperty("testcases.prefix") + 
     308                                                            "_" + i, 
     309                                                        properties.getProperty("test.context"))); 
    293310            lengths[i - 1] = sequence.size(); 
    294311            i++; 
    295312        } 
    296         for (int j = 0; j < generatedSequences.size(); j++) { 
    297             Interaction interaction = 
    298                 (Interaction) model.getPackagedElement(properties.getProperty("testcases.prefix") + j, true, 
    299                                                        UMLPackage.Literals.INTERACTION, true); 
    300             double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile); 
     313        for (int j = 0; j < interactions.size(); j++) { 
     314            double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile); 
    301315            System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]); 
    302316        } 
    303317    } 
    304      
     318 
    305319    private void createSchedulingWorkflow(TestData testdata) throws Exception { 
    306320        Properties properties = loadProperties(testdata); 
    307321        Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 
    308         Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
    309         IStochasticProcess usageProfile = createUsageProfile(sequences); 
    310         Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties); 
     322        Model model = 
     323            ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 
     324        IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 
     325        Collection<List<Event>> generatedSequences = 
     326            createRandomSequences(usageProfile, properties); 
    311327        int i = 1; 
    312328        for (List<Event> sequence : generatedSequences) { 
    313             UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 
     329            UMLUtils.createInteractionFromEventSequence(sequence, model, 
     330                                                        properties.getProperty("testcases.prefix") + 
     331                                                            "_" + i, 
    314332                                                        properties.getProperty("test.context")); 
    315333            i++; 
    316334        } 
    317          
    318         UMLUtils.createScheduling(model, usageProfile, null); 
    319  
    320         //ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml"); 
    321     } 
    322      
     335 
     336        UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context")); 
     337 
     338        ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile); 
     339    } 
     340 
    323341    private Properties loadProperties(TestData testdata) throws Exception { 
    324342        Properties properties = new Properties(); 
    325         properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile).getFile())); 
     343        properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile) 
     344            .getFile())); 
    326345        return properties; 
    327346    } 
    328      
    329     private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, Properties properties) throws Exception { 
     347 
     348    private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, 
     349                                                                  Properties properties) 
     350        throws Exception 
     351    { 
    330352        // load usage journal 
    331353        HTTPLogParser parser = 
    332354            new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile) 
    333355                .getFile())); 
    334         parser 
    335             .parseFile(new File(ClassLoader 
    336                 .getSystemResource(testdata.usageJournalFile) 
    337                 .getFile())); 
     356        parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile) 
     357            .getFile())); 
    338358        Collection<List<Event>> sequences = parser.getSequences(); 
    339359 
    340         // remove non SOAP events 
     360        // remove non SOAP events and convert to SimpleSOAPEventType 
     361        Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 
    341362        for (List<Event> sequence : sequences) { 
    342             SOAPUtils.removeNonSOAPEvents(sequence); 
    343         } 
    344          
     363            simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 
     364        } 
     365 
    345366        // remove calls to ingored services 
    346367        Set<String> ignoredServices = new HashSet<>(); 
    347368        String ignoredServicesString = properties.getProperty("test.ignored.services"); 
    348         if( ignoredServicesString!=null ) { 
    349             for( String service : ignoredServicesString.split(",") ) { 
     369        if (ignoredServicesString != null) { 
     370            for (String service : ignoredServicesString.split(",")) { 
    350371                ignoredServices.add(service.trim()); 
    351372            } 
    352373        } 
    353          
    354         for (List<Event> sequence : sequences) { 
     374 
     375        for (List<Event> sequence : simpleSOAPSequences) { 
    355376            for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 
    356377                Event event = eventIter.next(); 
    357                 SOAPEventType eventType = (SOAPEventType) event.getType(); 
     378                SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType(); 
    358379                if (ignoredServices.contains(eventType.getServiceName())) { 
    359380                    eventIter.remove(); 
     
    361382            } 
    362383        } 
    363         return sequences; 
    364     } 
    365      
    366     private IStochasticProcess createUsageProfile(Collection<List<Event>> sequences) throws Exception { 
    367         Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 
    368         for (List<Event> sequence : sequences) { 
    369             simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 
    370         } 
    371  
     384        return simpleSOAPSequences; 
     385    } 
     386 
     387    private IStochasticProcess createUsageProfile(TestData testdata, Collection<List<Event>> sequences) 
     388        throws Exception 
     389    { 
    372390        FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1)); 
    373         usageProfile.train(simpleSOAPSequences); 
     391        usageProfile.train(sequences); 
     392        FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile); 
     393        SerializationUtils.serialize(usageProfile, fos); 
     394        fos.close(); 
    374395        return usageProfile; 
    375396    } 
    376      
    377     private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, Properties properties) throws Exception { 
     397 
     398    private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, 
     399                                                          Properties properties) throws Exception 
     400    { 
    378401        int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number")); 
    379402        int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1")); 
    380         int testCaseMaxLength = Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 
     403        int testCaseMaxLength = 
     404            Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 
    381405        int maxIter = numberOfTestCases * 100; 
    382         RandomWalkGenerator testGenerator = new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, maxIter); 
     406        RandomWalkGenerator testGenerator = 
     407            new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, 
     408                                    maxIter); 
    383409        return testGenerator.generateTestSuite(usageProfile); 
    384410    } 
    385      
     411 
    386412    private void deleteFiles(File file) { 
    387413        if (file.exists()) { 
Note: See TracChangeset for help on using the changeset viewer.