package org.owasp.appsensor.handler;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;

import org.junit.Test;
import org.owasp.appsensor.AppSensorServer;
import org.owasp.appsensor.DetectionPoint;
import org.owasp.appsensor.Event;
import org.owasp.appsensor.User;

/**
 * Test basic soap event handling. Add a number of events matching 
 * the known set of criteria and ensure the attacks are triggered at 
 * the appropriate points and that the expected responses are returned 
 * from the soap handler
 * 
 * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/
 */
public class ReferenceSoapRequestHandlerTest {
	
	private static User bob = new User("bob");
	
	private static DetectionPoint detectionPoint1 = new DetectionPoint("IE1");
	
	private static String SERVICE_URL = "http://localhost:8080/appsensor/services/SoapRequestHandler";
	
    @SuppressWarnings("rawtypes")
	@Test
    public void test() throws Exception {
		System.err.println("Starting service");
		AppSensorServer.bootstrap();
    	Endpoint endpoint = Endpoint.publish(SERVICE_URL, new ReferenceSoapRequestHandler());
		
        Service soapHandlerService = Service.create(
                new URL(SERVICE_URL + "?wsdl"),
                new QName("https://www.owasp.org/index.php/OWASP_AppSensor_Project/wsdl", "SoapRequestHandlerService"));

        assertNotNull(soapHandlerService);

        SoapRequestHandler requestHandler = soapHandlerService.getPort(SoapRequestHandler.class);
        
        // HandlerChain installation
        Binding binding = ((BindingProvider) requestHandler).getBinding();
        List<Handler> handlerChain = binding.getHandlerChain();
        if (handlerChain == null) {
        	handlerChain = new ArrayList<Handler>();
        }
        handlerChain.add(new RegisterClientApplicationIdentificationHandler());
        binding.setHandlerChain(handlerChain);
        
        long startMillis = new Date().getTime() - 1000;	//current time - 1 second - account for clock drift
        
        assertEquals(0, requestHandler.getResponses(startMillis).size());
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        
        assertEquals(0, requestHandler.getResponses(startMillis).size());
        //this is 11th
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        assertEquals(1, requestHandler.getResponses(startMillis).size());
        
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        
        assertEquals(1, requestHandler.getResponses(startMillis).size());
        //this is 22nd
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        assertEquals(2, requestHandler.getResponses(startMillis).size());
        
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        requestHandler.addEvent(new Event(bob, detectionPoint1, "localhostme"));
        
        assertEquals(2, requestHandler.getResponses(startMillis).size());
        
        endpoint.stop();
        System.err.println("Stopped service");
    }
}



@@ -176,31 +176,8 @@
 st;%0A
-import java.util.Date;%0A
 impo
@@ -389,24 +389,55 @@
 r.Handler;%0A%0A
+import org.joda.time.DateTime;%0A
 import org.j
@@ -600,16 +600,59 @@
 or.User;
+%0Aimport org.owasp.appsensor.util.DateUtils;
 %0A%0A/**%0A *
@@ -2280,107 +2280,185 @@
     
-long startMillis = new Date().getTime() - 1000;%09//
+DateTime currentTimestamp = DateUtils.getCurrentTimestamp();%0A        
 current
- time - 1 second - account for clock drift
+Timestamp = currentTimestamp.minusSeconds(1);%0A        String timestamp = currentTimestamp.toString();
 %0A   
@@ -2511,35 +2511,33 @@
 etResponses(
-startMillis
+timestamp
 ).size());%0A 
@@ -3402,35 +3402,33 @@
 etResponses(
-startMillis
+timestamp
 ).size());%0A 
@@ -3578,35 +3578,33 @@
 etResponses(
-startMillis
+timestamp
 ).size());%0A 
@@ -4478,35 +4478,33 @@
 etResponses(
-startMillis
+timestamp
 ).size());%0A 
@@ -4654,35 +4654,33 @@
 etResponses(
-startMillis
+timestamp
 ).size());%0A 
@@ -4995,19 +4995,17 @@
 ses(
-startMillis
+timestamp
 ).si

test code updated to support rfc 3339 date / time