/*
 * #%L
 * Course Signup Implementation
 * %%
 * Copyright (C) 2010 - 2013 University of Oxford
 * %%
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *             http://opensource.org/licenses/ecl2
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package uk.ac.ox.oucs.vle;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;
import uk.ac.ox.oucs.vle.CourseSignupService.Status;


public class TestCourseSignupServiceSignup extends OnSampleData {

	@Test
	public void testSignupGood() {
		service.signup("course-1", Collections.singleton("comp-1"), "test.user.1@dept.ox.ac.uk", null);
	}

	@Test
	public void testSignupGoodSet() {
		Set<String> componentIds = new HashSet<String>();
		componentIds.add("comp-1");
		componentIds.add("comp-3");
		service.signup("course-1", componentIds, "test.user.1@dept.ox.ac.uk", null);
	}

	@Test
	public void testSignupMultiple() {
		// TODO This affects all further tests.
		for (int i = 1; i<=15; i++) {
			proxy.setCurrentUser(proxy.findUserById("id"+i));
			service.signup("course-1", Collections.singleton("comp-3"), "test.user.1@dept.ox.ac.uk", null);
		}
	}

	@Test
	public void testSignupSignupSingle() {
		service.signup("course-1", Collections.singleton("comp-1"), "test.user.1@dept.ox.ac.uk", null);
		dao.flush();
		List<CourseSignup> signups = service.getMySignups(Collections.singleton(Status.PENDING));
		assertEquals(1, signups.size());
		try {
			service.signup("course-1", Collections.singleton("comp-1"), "test.user.2@dept.ox.ac.uk", null);
			fail("Shouldn't be able to signup twice.");
		} catch (Exception e) {}
		dao.flush();
		signups = service.getMySignups(Collections.singleton(Status.PENDING));
		assertEquals("test.user.1@dept.ox.ac.uk", signups.get(0).getSupervisor().getEmail());
	}

	@Test
	public void testSignupWithdrawSignupSingle() {
		service.signup("course-1", Collections.singleton("comp-1"), "test.user.1@dept.ox.ac.uk", null);
		dao.flush();
		List<CourseSignup> signups = service.getMySignups(Collections.singleton(Status.PENDING));
		assertEquals(1, signups.size());
		service.withdraw(signups.get(0).getId());
		dao.flush();
		service.signup("course-1", Collections.singleton("comp-1"), "test.user.2@dept.ox.ac.uk", null);
		dao.flush();
		signups = service.getMySignups(Collections.singleton(Status.PENDING));
		assertEquals("test.user.2@dept.ox.ac.uk", signups.get(0).getSupervisor().getEmail());
	}

	@Test
	public void testSignupWithdrawSignupMultiple() {
		
	}

	@Test
	public void testSignupFuture() {
		try{
			// Isn't open yet.
			service.signup("course-1", Collections.singleton("comp-5"), "test.user.1@dept.ox.ac.uk", null);
			fail("Should throw exception");
		} catch (IllegalStateException ise) {}
	}

	@Test
	public void testSignupPast() {
		try {
			// Is now closed.
			service.signup("course-1", Collections.singleton("comp-6"), "test.user.1@dept.ox.ac.uk", null);
			fail("Should throw exception");
		} catch (IllegalStateException ise) {}
	}

	@Test
	public void testSignupBadUser() {
		try {
			service.signup("course-1", Collections.singleton("comp-1"), "nosuchuser@ox.ac.uk", null);
			fail("Should throw exception");
		} catch (IllegalArgumentException iae) {}
	}

	@Test
	public void testSignupFull() {
		service.signup("course-1", Collections.singleton("comp-8"), "test.user.1@dept.ox.ac.uk", null);
		List<CourseSignup> signups = service.getMySignups(Collections.singleton(Status.WAITING));
		assertEquals(1, signups.size());
	}

	@Test
	public void testSignupWrongCourse() {
		try {
			service.signup("course-3", Collections.singleton("comp-1"), "test.user.1@dept.ox.ac.uk", null);
			fail("Should throw exception");
		} catch (IllegalArgumentException iae) {}
	}
}

@@ -811,16 +811,41 @@
 l.Set;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -981,16 +981,111 @@
 Data %7B%0A%0A
+%09@Before%0A%09public void resetUser() %7B%0A%09%09proxy.setCurrentUser(proxy.findUserById(%22current%22));%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -1501,50 +1501,8 @@
 ) %7B%0A
-%09%09// TODO This affects all further tests.%0A
 %09%09fo

Make sure tests can be run in any order.