package i5.las2peer.security;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.junit.Test;

import i5.las2peer.api.security.AgentAccessDeniedException;
import i5.las2peer.api.security.AgentException;
import i5.las2peer.p2p.AgentAlreadyRegisteredException;
import i5.las2peer.p2p.LocalNode;
import i5.las2peer.p2p.NodeException;
import i5.las2peer.serialization.MalformedXMLException;
import i5.las2peer.serialization.SerializationException;
import i5.las2peer.testing.MockAgentFactory;
import i5.las2peer.tools.CryptoException;

public class AgentContextTest {

	@Test
	public void testRequestAgent() throws MalformedXMLException, IOException, InternalSecurityException,
			CryptoException, SerializationException, AgentException, NodeException, AgentAccessDeniedException {
		LocalNode node = LocalNode.newNode();

		GroupAgentImpl group1 = MockAgentFactory.getGroup1();
		GroupAgentImpl groupA = MockAgentFactory.getGroupA();
		GroupAgentImpl groupSuper = GroupAgentImpl.createGroupAgent(new AgentImpl[] { group1, groupA });
		try {
			node.storeAgent(group1);
		} catch (AgentAlreadyRegisteredException e) {
		}
		try {
			node.storeAgent(groupA);
		} catch (AgentAlreadyRegisteredException e) {
		}
		node.storeAgent(groupSuper);

		node.launch();

		UserAgentImpl eve = MockAgentFactory.getEve();
		eve.unlock("evespass");

		AgentContext context = new AgentContext(node, eve);

		try {
			GroupAgentImpl a = (GroupAgentImpl) context.requestAgent(group1.getIdentifier());
			assertEquals(a.getIdentifier(), group1.getIdentifier());
		} catch (Exception e) {
			e.printStackTrace();
			fail("exception thrown: " + e);
		}

		try {
			context.requestAgent(groupA.getIdentifier());
			fail("exception expected");
		} catch (Exception e) {
		}

		try {
			GroupAgentImpl a = (GroupAgentImpl) context.requestAgent(groupSuper.getIdentifier());
			assertEquals(a.getIdentifier(), groupSuper.getIdentifier());
		} catch (Exception e) {
			e.printStackTrace();
			fail("exception thrown: " + e);
		}
	}

	@Test
	public void testHasAccess() throws MalformedXMLException, IOException, InternalSecurityException, CryptoException,
			SerializationException, AgentException, NodeException, AgentAccessDeniedException {
		LocalNode node = LocalNode.newNode();

		GroupAgentImpl group1 = MockAgentFactory.getGroup1();
		GroupAgentImpl groupA = MockAgentFactory.getGroupA();
		GroupAgentImpl groupSuper = GroupAgentImpl.createGroupAgent(new AgentImpl[] { group1, groupA });
		try {
			node.storeAgent(group1);
		} catch (AgentAlreadyRegisteredException e) {
		}
		try {
			node.storeAgent(groupA);
		} catch (AgentAlreadyRegisteredException e) {
		}
		node.storeAgent(groupSuper);

		node.launch();

		UserAgentImpl eve = MockAgentFactory.getEve();
		eve.unlock("evespass");

		AgentContext context = new AgentContext(node, eve);

		try {
			boolean result = context.hasAccess(group1.getIdentifier());
			assertTrue(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail("exception thrown: " + e);
		}

		try {
			boolean result = context.hasAccess(groupA.getIdentifier());
			assertFalse(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail("exception thrown: " + e);
		}

		try {
			boolean result = context.hasAccess(groupSuper.getIdentifier());
			assertTrue(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail("exception thrown: " + e);
		}
	}
}

@@ -963,32 +963,111 @@
 ode.newNode();%0A%0A
+%09%09UserAgentImpl adam = MockAgentFactory.getAdam();%0A%09%09adam.unlock(%22adamspass%22);%0A
 %09%09GroupAgentImpl
@@ -1098,32 +1098,55 @@
 ry.getGroup1();%0A
+%09%09group1.unlock(adam);%0A
 %09%09GroupAgentImpl
@@ -1177,32 +1177,55 @@
 ry.getGroupA();%0A
+%09%09groupA.unlock(adam);%0A
 %09%09GroupAgentImpl
@@ -1299,32 +1299,61 @@
 up1, groupA %7D);%0A
+%09%09groupSuper.unlock(group1);%0A
 %09%09try %7B%0A%09%09%09node.
@@ -2560,16 +2560,95 @@
 ode();%0A%0A
+%09%09UserAgentImpl adam = MockAgentFactory.getAdam();%0A%09%09adam.unlock(%22adamspass%22);%0A
 %09%09GroupA
@@ -2691,24 +2691,47 @@
 etGroup1();%0A
+%09%09group1.unlock(adam);%0A
 %09%09GroupAgent
@@ -2774,16 +2774,39 @@
 oupA();%0A
+%09%09groupA.unlock(adam);%0A
 %09%09GroupA
@@ -2896,16 +2896,45 @@
 upA %7D);%0A
+%09%09groupSuper.unlock(group1);%0A
 %09%09try %7B%0A

fix AgentContextTest