package eu.venusc.cdmi;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.parser.ParseException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ContainerOperationsTest extends CDMIConnectionWrapper implements
		CDMIResponseStatus {

	ContainerOperations cops = null;
	static String containerName = null;
	static String baseContainer = null;
	static Random random = new Random();

	public ContainerOperationsTest(String name) throws MalformedURLException {
		super(name);				
		cops = cdmiConnection.getContainerProxy();
	}

	@Before
	public void setUp() throws IOException {		
		baseContainer = "/";
		containerName = "libcdmi-java" + random.nextInt();
	}

	@After
	public void tearDown() throws IOException, ParseException,
			CDMIOperationException {

		String[] children = cops.getChildren(baseContainer);

		for (int i = 0; i < children.length; i++) {
			HttpResponse response = cops.delete(baseContainer + "/"
					+ children[i]);
			int responseCode = response.getStatusLine().getStatusCode();
			if (responseCode != REQUEST_DELETED)
				fail("Container " + baseContainer + " could not be cleaned up.");
		}

	}


	@Test
	public void testCreate() throws ClientProtocolException, IOException,
			CDMIOperationException {

		HttpResponse response = cops.create(containerName +"/", parameters);
		int responseCode = response.getStatusLine().getStatusCode();
		assertEquals("Creating container " + containerName+"/", responseCode,
				REQUEST_CREATED);

	}

	@Test
	public void testGetChildren() throws ClientProtocolException, IOException,
			CDMIOperationException, ParseException {

		Set<String> set = new HashSet<String>();

		// create containers
		for (int i = 0; i < 3; i++) {

			HttpResponse response = cops.create(containerName + i + "/",
					parameters);
			int responseCode = response.getStatusLine().getStatusCode();

			if (responseCode != REQUEST_CREATED)
				fail("Could not create  the container: " + containerName + "/");
			set.add(containerName + i + "/");
		}

		String[] children = cops.getChildren(baseContainer);

		Set<String> childSet = new HashSet<String>();
		for (int i = 0; i < children.length; i++) {
			childSet.add(children[i]);
		}

		assertEquals("Getting the container children: ", set, childSet);
		childSet.clear();

	}


	@Test
	public void testDelete() throws ClientProtocolException, IOException,
			CDMIOperationException {

		HttpResponse response = null;
		int responseCode = 0;

		// Create a container
		response = cops.create(containerName + "/", parameters);
		responseCode = response.getStatusLine().getStatusCode();

		if (responseCode != REQUEST_CREATED)
			fail("Could not create the container: " + containerName + "/");
		// delete the container
		response = cops.delete(containerName + "/");
		responseCode = response.getStatusLine().getStatusCode();
		assertEquals("Container deleted: ", REQUEST_DELETED, responseCode);
	}
}

@@ -709,20 +709,16 @@
 r(name);
-%09%09%09%09
 %0A%09%09cops 
@@ -809,18 +809,16 @@
 eption %7B
-%09%09
 %0A%09%09baseC
@@ -1096,44 +1096,21 @@
 %0A%09%09%09
-HttpResponse response = cops.delete(
+String url = 
 base
@@ -1124,22 +1124,17 @@
 er + %22/%22
-%0A%09%09%09%09%09
+ 
 + childr
@@ -1134,24 +1134,68 @@
  children%5Bi%5D
+;%0A%09%09%09HttpResponse response = cops.delete(url
 );%0A%09%09%09int re
@@ -1249,16 +1249,17 @@
 Code();%0A
+%0A
 %09%09%09if (r
@@ -1262,33 +1262,75 @@
 f (responseCode 
-!
+== REQUEST_NOT_FOUND%0A%09%09%09%09%09%7C%7C responseCode =
 = REQUEST_DELETE
@@ -1328,24 +1328,46 @@
 ST_DELETED)%0A
+%09%09%09%09continue;%0A%09%09%09else%0A
 %09%09%09%09fail(%22Co
@@ -1378,29 +1378,19 @@
 ner %22 + 
-baseContainer
+url
  + %22 cou
@@ -1410,24 +1410,25 @@
 aned up.%22);%0A
+%0A
 %09%09%7D%0A%0A%09%7D%0A%0A%0A%09@
@@ -1420,25 +1420,24 @@
 ;%0A%0A%09%09%7D%0A%0A%09%7D%0A%0A
-%0A
 %09@Test%0A%09publ
@@ -1584,16 +1584,17 @@
 erName +
+ 
 %22/%22, par
@@ -1718,17 +1718,19 @@
 inerName
-+
+ + 
 %22/%22, res
@@ -2570,17 +2570,16 @@
 );%0A%0A%09%7D%0A%0A
-%0A
 %09@Test%0A%09

fix to a tearDown method