package edu.msu.nscl.olog.api;

import static org.junit.Assert.*;

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

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import edu.msu.nscl.olog.api.OlogClientImpl.OlogClientBuilder;
import static edu.msu.nscl.olog.api.LogBuilder.*;
import static edu.msu.nscl.olog.api.PropertyBuilder.*;
import static edu.msu.nscl.olog.api.TagBuilder.*;
import static edu.msu.nscl.olog.api.LogbookBuilder.*;

public class ErrorIT {

	private static LogbookBuilder validLogbook = logbook("Valid Logbook");
	private static LogbookBuilder validLogbook2 = logbook("Valid Logbook2");
	private static TagBuilder validTag = tag("Valid Tag");
	private static PropertyBuilder validProperty = property("Valid Property")
			.attribute("Valid Attribute");
	private static Log validLog;

	private static LogbookBuilder inValidLogbook = logbook("InValid Logbook");
	private static TagBuilder inValidTag = tag("InValid Tag");
	private static PropertyBuilder inValidProperty = property("InValid Property");

	private static OlogClient client;

	@BeforeClass
	public static void setup() {
		try {
			client = OlogClientBuilder.serviceURL()
					.withHTTPAuthentication(true).create();
			client.set(validTag);
			client.set(validLogbook.owner("test"));
			client.set(validLogbook2.owner("test"));
			client.set(validProperty);
			validLog = client.set(log()
					.description("Valid Log Entry for error condition tests.")
					.appendToLogbook(validLogbook).level("Info"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDown() {
		client.deleteTag(validTag.build().getName());
		client.deleteLogbook(validLogbook.build().getName());
		client.deleteLogbook(validLogbook2.build().getName());
		client.deleteProperty(validProperty.build().getName());
		client.delete(validLog.getId());
	}

	@Test(expected = OlogException.class)
	public void setLogWithNoLogbook() {
		LogBuilder log = log().description("log with no logbook").level("Info");
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setLogWithNoLevel() {
		LogBuilder log = log().description("log with no level")
				.appendToLogbook(validLogbook);
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setLogWithInvalidLogbook() {
		LogBuilder log = log().description("log with invalid logbook")
				.level("Info").appendToLogbook(inValidLogbook);
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setLogWithInvalidTag() {
		LogBuilder log = log().description("log with invalid tag")
				.level("Info").appendToLogbook(validLogbook)
				.appendTag(inValidTag);
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setLogWithInvalidProperty() {
		LogBuilder log = log().description("log with invalid property")
				.level("Info").appendToLogbook(validLogbook)
				.appendProperty(inValidProperty);
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setLogWithInvalidAttribute() {
		LogBuilder log = log()
				.description("log with invalid attribute")
				.level("Info")
				.appendToLogbook(validLogbook)
				.appendProperty(validProperty.attribute("invalidAttribute", ""));
		client.set(log);
	}

	@Test(expected = OlogException.class)
	public void setTagOnInvalidLog() {
		Collection<Long> logIds = new ArrayList<Long>();
		logIds.add(validLog.getId());
		logIds.add(12345L);
		client.set(validTag, logIds);
		Log queryLog = client.getLog(validLog.getId());
		assertTrue(
				"invalid request to add tag to logs partially executed, unexpected modification of validLog",
				validLog.equals(queryLog));
	}

	@Test(expected = OlogException.class)
	public void setInvalidTagOnLog() {
		client.update(inValidTag, validLog.getId());
		assertTrue("invalid request to add invalid tag to log",
				validLog.equals(client.getLog(validLog.getId())));
	}

	@Test(expected = OlogException.class)
	public void setLogbookOnInvalidLog() {
		Collection<Long> logIds = new ArrayList();
		logIds.add(validLog.getId());
		logIds.add(12345L);
		client.set(validLogbook2, logIds);
		Log queryLog = client.getLog(validLog.getId());
		assertTrue(
				"invalid request to add logbook2 to logs partially executed, unexpected modification of validLog",
				validLog.equals(queryLog));
	}

}

@@ -4467,14 +4467,333 @@
 ));%0D%0A%09%7D%0D
+%0A%09%0D%0A%09@Test%0D%0A%09public void deleteNonExistingLog()%7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void deleteNonExistingTagFromLog()%7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void deleteNonExistingLogbookFromLog()%7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void deleteNonExistingPropertyFromLog()%7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void deleteOnlyLogbookFromLog()%7B%0D%0A%09%09%0D%0A%09%7D%0D
 %0A%0D%0A%7D%0D%0A

define new error condition tests