package org.owasp.appsensor.analysis.impl;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.owasp.appsensor.ClientObjectFactory;
import org.owasp.appsensor.DetectionPoint;
import org.owasp.appsensor.Interval;
import org.owasp.appsensor.ServerObjectFactory;
import org.owasp.appsensor.StatisticalEvent;
import org.owasp.appsensor.Threshold;
import org.owasp.appsensor.User;

public class ReferenceStatisticalEventAnalysisEngineTest {

	private static User bob = new User("bob", "1.2.3.4");
	
	private static Interval fiveMinutes = new Interval(5, Interval.MINUTES);
//	private static Interval thirtySeconds = new Interval(30, Interval.SECONDS);
	
	private static Threshold threeInFiveMinutes = new Threshold(3, fiveMinutes);
	
	private static DetectionPoint detectionPoint1 = new DetectionPoint();
	
//	private static Collection<Response> responses1 = new ArrayList<Response>();
	
	private static Collection<String> detectionSystems1 = new ArrayList<String>();
	
	private static String detectionSystem1 = "localhostme";
	
	@BeforeClass
	public static void doSetup() {
//		responses1.add(new Response(bob, "log", detectionPoint1, detectionSystem1));
//		responses1.add(new Response(bob, "logout", detectionPoint1, detectionSystem1));
//		responses1.add(new Response(bob, "disableUser", detectionPoint1, detectionSystem1));
//		responses1.add(new Response(bob, "disableComponentForAllUsers", detectionPoint1, detectionSystem1, thirtySeconds));
		
		detectionPoint1.setId("IE1");
		detectionPoint1.setThreshold(threeInFiveMinutes);
//		detectionPoint1.setResponses(responses1);
		
		detectionSystems1.add(detectionSystem1);
	}
	
	@Test
	public void testAttackCreation() throws Exception {
//		StatisticalEvent tmp = new StatisticalEvent(bob, detectionPoint1, "localhostme");
//		Gson gson = new GsonBuilder().setPrettyPrinting().create();
//		System.err.println(gson.toJson(tmp));
		
		assertEquals(0, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(0, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(1, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(0, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(2, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(0, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(3, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(1, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(4, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(1, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(5, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(1, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(6, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(2, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
		
		ClientObjectFactory.getEventManager().addEvent(new StatisticalEvent(bob, detectionPoint1, "localhostme"));
		
		assertEquals(7, ServerObjectFactory.getEventStore().findEvents(bob, detectionPoint1, detectionSystems1).size());
		assertEquals(2, ServerObjectFactory.getAttackStore().findAttacks(bob, detectionPoint1, detectionSystems1).size());
	}
	
}

@@ -682,87 +682,8 @@
 S);%0A
-//%09private static Interval thirtySeconds = new Interval(30, Interval.SECONDS);%0A
 %09%0A%09p
@@ -837,89 +837,8 @@
 ;%0A%09%0A
-//%09private static Collection%3CResponse%3E responses1 = new ArrayList%3CResponse%3E();%0A%09%0A
 %09pri
@@ -1024,385 +1024,8 @@
 ) %7B%0A
-//%09%09responses1.add(new Response(bob, %22log%22, detectionPoint1, detectionSystem1));%0A//%09%09responses1.add(new Response(bob, %22logout%22, detectionPoint1, detectionSystem1));%0A//%09%09responses1.add(new Response(bob, %22disableUser%22, detectionPoint1, detectionSystem1));%0A//%09%09responses1.add(new Response(bob, %22disableComponentForAllUsers%22, detectionPoint1, detectionSystem1, thirtySeconds));%0A%09%09%0A
 %09%09de
@@ -1108,54 +1108,8 @@
 s);%0A
-//%09%09detectionPoint1.setResponses(responses1);%0A
 %09%09%0A%09

Update test to remove responses being added - they are now being pulled from config appropriately.