Changeset 2003


Ignore:
Timestamp:
07/14/15 10:57:19 (9 years ago)
Author:
sherbold
Message:
  • extended SOAP utils with function that removes all invalid request/response pairs from a sequence of SimpleSOAPEvents
Location:
trunk
Files:
2 edited

Legend:

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

    r1988 r2003  
    2020import java.util.Collection; 
    2121import java.util.Iterator; 
     22import java.util.LinkedList; 
    2223import java.util.List; 
    23  
     24import java.util.logging.Level; 
     25 
     26import org.junit.BeforeClass; 
    2427import org.junit.Test; 
    2528 
    2629import de.ugoe.cs.autoquest.eventcore.Event; 
     30import de.ugoe.cs.autoquest.eventcore.EventUtils; 
    2731import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; 
    2832import de.ugoe.cs.autoquest.plugin.http.SOAPUtils; 
    2933import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; 
    3034import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 
     35import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType.CallType; 
     36import de.ugoe.cs.util.console.Console; 
     37import de.ugoe.cs.util.console.TextConsole; 
    3138 
    3239/** 
     
    3845 */ 
    3946public class SOAPUtilsTest { 
     47     
     48    @BeforeClass 
     49    public static void setupBeforeClass() { 
     50        new TextConsole(Level.FINEST); 
     51    } 
    4052 
    4153    @Test 
     
    8294            } 
    8395        } 
    84  
    85     } 
    86  
     96    } 
     97     
     98    @Test 
     99    public void testDropInvalidResponseRequestPairs_1() throws Exception { 
     100        List<Event> sequence = new LinkedList<Event>(); 
     101        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     102        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     103        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     104        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     105        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     106        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     107         
     108        List<Event> expectedSequence = new LinkedList<>(sequence); 
     109         
     110        List<Event> originalSequence = new LinkedList<>(sequence); 
     111         
     112        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     113        Console.traceln(Level.INFO, getCurrentMethodName()); 
     114        EventUtils.traceSequence(Level.INFO, validSequence); 
     115         
     116        assertEquals(expectedSequence, validSequence); 
     117        assertEquals(originalSequence, sequence); 
     118    } 
     119     
     120    @Test 
     121    public void testDropInvalidResponseRequestPairs_2() throws Exception { 
     122        List<Event> sequence = new LinkedList<Event>(); 
     123        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     124        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     125        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     126        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     127        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     128        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     129         
     130        List<Event> expectedSequence = new LinkedList<Event>(); 
     131        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     132        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     133         
     134        List<Event> originalSequence = new LinkedList<>(sequence); 
     135         
     136        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     137        Console.traceln(Level.INFO, getCurrentMethodName()); 
     138        EventUtils.traceSequence(Level.INFO, validSequence); 
     139         
     140        assertEquals(expectedSequence, validSequence); 
     141        assertEquals(originalSequence, sequence); 
     142    } 
     143     
     144    @Test 
     145    public void testDropInvalidResponseRequestPairs_3() throws Exception { 
     146        List<Event> sequence = new LinkedList<Event>(); 
     147        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     148        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     149        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     150        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     151        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     152         
     153        List<Event> expectedSequence = new LinkedList<Event>(); 
     154        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     155        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     156         
     157        List<Event> originalSequence = new LinkedList<>(sequence); 
     158         
     159        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     160        Console.traceln(Level.INFO, getCurrentMethodName()); 
     161        EventUtils.traceSequence(Level.INFO, validSequence); 
     162         
     163        assertEquals(expectedSequence, validSequence); 
     164        assertEquals(originalSequence, sequence); 
     165    } 
     166     
     167    @Test 
     168    public void testDropInvalidResponseRequestPairs_4() throws Exception { 
     169        List<Event> sequence = new LinkedList<Event>(); 
     170        sequence.add(Event.STARTEVENT); 
     171        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     172        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     173        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     174        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     175        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     176        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     177        sequence.add(Event.ENDEVENT); 
     178         
     179        List<Event> expectedSequence = new LinkedList<>(sequence); 
     180         
     181        List<Event> originalSequence = new LinkedList<>(sequence); 
     182         
     183        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     184        Console.traceln(Level.INFO, getCurrentMethodName()); 
     185        EventUtils.traceSequence(Level.INFO, validSequence); 
     186         
     187        assertEquals(expectedSequence, validSequence); 
     188        assertEquals(originalSequence, sequence); 
     189    } 
     190     
     191    @Test 
     192    public void testDropInvalidResponseRequestPairs_5() throws Exception { 
     193        List<Event> sequence = new LinkedList<Event>(); 
     194        sequence.add(Event.STARTEVENT); 
     195        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     196        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     197        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     198        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     199        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     200        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     201        sequence.add(Event.ENDEVENT); 
     202         
     203        List<Event> expectedSequence = new LinkedList<Event>(); 
     204        expectedSequence.add(Event.STARTEVENT); 
     205        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     206        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     207        expectedSequence.add(Event.ENDEVENT); 
     208         
     209        List<Event> originalSequence = new LinkedList<>(sequence); 
     210         
     211        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     212        Console.traceln(Level.INFO, getCurrentMethodName() ); 
     213        EventUtils.traceSequence(Level.INFO, validSequence); 
     214         
     215        assertEquals(expectedSequence, validSequence); 
     216        assertEquals(originalSequence, sequence); 
     217    } 
     218     
     219    @Test 
     220    public void testDropInvalidResponseRequestPairs_6() throws Exception { 
     221        List<Event> sequence = new LinkedList<Event>(); 
     222        sequence.add(Event.STARTEVENT); 
     223        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     224        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     225        sequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     226        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.REQUEST))); 
     227        sequence.add(new Event(new SimpleSOAPEventType("op2", "foo", "bar", null, null, CallType.RESPONSE))); 
     228        sequence.add(Event.ENDEVENT); 
     229         
     230        List<Event> expectedSequence = new LinkedList<Event>(); 
     231        expectedSequence.add(Event.STARTEVENT); 
     232        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.REQUEST))); 
     233        expectedSequence.add(new Event(new SimpleSOAPEventType("op1", "foo", "bar", null, null, CallType.RESPONSE))); 
     234        expectedSequence.add(Event.ENDEVENT); 
     235         
     236        List<Event> originalSequence = new LinkedList<>(sequence); 
     237         
     238        List<Event> validSequence = SOAPUtils.dropInvalidResponseRequestPairs(sequence); 
     239        Console.traceln(Level.INFO, getCurrentMethodName()); 
     240        EventUtils.traceSequence(Level.INFO, validSequence); 
     241         
     242        assertEquals(expectedSequence, validSequence); 
     243        assertEquals(originalSequence, sequence); 
     244    } 
     245 
     246    private  String getCurrentMethodName() { 
     247         StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace(); 
     248         return stackTraceElements[1].toString(); 
     249    } 
     250     
    87251} 
  • trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/SOAPUtils.java

    r1994 r2003  
    2323import java.util.List; 
    2424import java.util.Set; 
     25import java.util.Stack; 
     26import java.util.logging.Level; 
    2527 
    2628import javax.xml.transform.Transformer; 
     
    4143import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 
    4244import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType.CallType; 
     45import de.ugoe.cs.util.console.Console; 
    4346 
    4447/** 
     
    690693    /** 
    691694     * <p> 
     695     * Drops all requests without responses as well as invalid request/response orders. Only the 
     696     * part of the sequence before the first invalid occurrence is kept. 
     697     * </p> 
     698     *  
     699     * @param sequences 
     700     *            sequences where the invalid pairs are dropped 
     701     * @return sequences with only valid and complete interactions 
     702     */ 
     703    public static Collection<List<Event>> dropInvalidResponseRequestPairs(Collection<List<Event>> sequences) 
     704    { 
     705        Collection<List<Event>> validSequences = new LinkedList<>(); 
     706        int i = 0; 
     707        for (List<Event> sequence : sequences) { 
     708            List<Event> validSequence = dropInvalidResponseRequestPairs(sequence); 
     709            if (validSequence.isEmpty() || 
     710                (validSequence.size() <= 2 && validSequence.get(0) == Event.STARTEVENT)) 
     711            { 
     712                Console.traceln(Level.INFO, "dropped sequence " + i + 
     713                    ": empty after removal of invalid request/response pairs"); 
     714            } 
     715            validSequences.add(validSequence); 
     716            i++; 
     717        } 
     718        return validSequences; 
     719    } 
     720 
     721    /** 
     722     * <p> 
     723     * Drops all requests without responses as well as invalid request/response orders. Only the 
     724     * part of the sequence before the first invalid occurrence is kept. 
     725     * </p> 
     726     *  
     727     * @param sequence 
     728     *            sequence where the invalid pairs are dropped 
     729     * @return sequence with only valid and complete interactions 
     730     */ 
     731    public static List<Event> dropInvalidResponseRequestPairs(List<Event> sequence) { 
     732        Stack<SimpleSOAPEventType> unrespondedRequests = new Stack<>(); 
     733        boolean hasStartEvent = false; 
     734        int lastValidIndex = 0; 
     735        int i = 0; 
     736        for (Event event : sequence) { 
     737            if (event == Event.STARTEVENT) { 
     738                hasStartEvent = true; 
     739                lastValidIndex = i; 
     740            } 
     741            else if (event.getType() instanceof SimpleSOAPEventType) { 
     742                SimpleSOAPEventType currentEventType = (SimpleSOAPEventType) event.getType(); 
     743                if (SOAPUtils.isSOAPRequest(event)) { 
     744                    unrespondedRequests.push(currentEventType); 
     745                } 
     746                else if (SOAPUtils.isSOAPResponse(event)) { 
     747                    if (unrespondedRequests.empty()) { 
     748                        break; // found a response without previous request; sequence invalid from 
     749                               // here 
     750                    } 
     751                    else { 
     752                        SimpleSOAPEventType lastRequest = unrespondedRequests.peek(); 
     753                        if (isRequestResponseMatch(lastRequest, currentEventType)) { 
     754                            unrespondedRequests.pop(); 
     755                            if (unrespondedRequests.empty()) { 
     756                                lastValidIndex = i; 
     757                            } 
     758                        } 
     759                    } 
     760 
     761                } 
     762            } 
     763            i++; 
     764        } 
     765        List<Event> validSequence = new LinkedList<>(sequence.subList(0, lastValidIndex + 1)); 
     766        if (hasStartEvent) { 
     767            validSequence.add(Event.ENDEVENT); 
     768        } 
     769        return validSequence; 
     770    } 
     771 
     772    /** 
     773     * <p> 
     774     * Checks if two {@link SimpleSOAPEventType} types are equal except their {@link CallType}, 
     775     * which must be {@value CallType#REQUEST} for the first parameter and {@link CallType#RESPONSE} 
     776     * for the second parameter. 
     777     * </p> 
     778     *  
     779     * @param request 
     780     *            request soap event 
     781     * @param response 
     782     *            response soap event 
     783     * @return true if they are a matching request/response pair 
     784     */ 
     785    public static boolean isRequestResponseMatch(SimpleSOAPEventType request, 
     786                                                 SimpleSOAPEventType response) 
     787    { 
     788        if (request == null && response == null) { 
     789            return true; 
     790        } 
     791        if ((request != null && response == null) || (request == null && response != null)) { 
     792            return false; 
     793        } 
     794        return request.getCallType().equals(CallType.REQUEST) && 
     795            response.getCallType().equals(CallType.RESPONSE) && 
     796            HTTPUtils.equals(request.getCalledMethod(), response.getCalledMethod()) && 
     797            HTTPUtils.equals(request.getServiceName(), response.getServiceName()) && 
     798            HTTPUtils.equals(request.getClientName(), response.getClientName()); 
     799    } 
     800 
     801    /** 
     802     * <p> 
    692803     * prevent instantiation 
    693804     * </p> 
Note: See TracChangeset for help on using the changeset viewer.