/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package org.slc.sli.api.security.context.validator;

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

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slc.sli.api.constants.EntityNames;
import org.slc.sli.api.resources.SecurityContextInjector;
import org.slc.sli.domain.Entity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 
 * @author dkornishev
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext-test.xml" })
public class TeacherToTeacherSectionAssociationValidatorTest {

	private static final String CORRECT_ENTITY_TYPE = EntityNames.TEACHER_SECTION_ASSOCIATION;
	private static final String USER_ID = "Master of Magic";
	private static final String SECTION_ID = "Sorcery Node";

	@Resource
	private TeacherToTeacherSectionAssociationValidator val;

	@Resource
	private SecurityContextInjector injector;

	@Resource
	private ValidatorTestHelper vth;

	@Before
	public void init() {
		injector.setEducatorContext(USER_ID);
	}

	@Test(expected = IllegalArgumentException.class)
	public void testValidateWrongType() {
		val.validate(EntityNames.ASSESSMENT, new HashSet<String>(Arrays.asList("Jomolungma")));
	}

	@Test
	public void testSuccessOne() {
		Entity tsa = this.vth.generateTSA(USER_ID, SECTION_ID, false);
		Assert.assertTrue(val.validate(CORRECT_ENTITY_TYPE, Collections.singleton(tsa.getEntityId())));
	}

	@Test
	public void testSuccessMulti() {
		Set<String> ids = new HashSet<String>();

		int random = (int) Math.floor(Math.random() * 100);
		for (int i = 0; i < random; i++) {
			ids.add(this.vth.generateTSA(USER_ID, SECTION_ID, false).getEntityId());
		}

		Assert.assertTrue(val.validate(CORRECT_ENTITY_TYPE, ids));
	}

	@Test
	public void testWrongId() {
		Assert.assertFalse(val.validate(CORRECT_ENTITY_TYPE, Collections.singleton("Hammerhands")));
		Assert.assertFalse(val.validate(CORRECT_ENTITY_TYPE, Collections.singleton("Nagas")));
		Assert.assertFalse(val.validate(CORRECT_ENTITY_TYPE, Collections.singleton("Phantom Warriors")));
	}

	@Test
	public void testHeterogenousList() {
		Assert.assertFalse(val.validate(
				CORRECT_ENTITY_TYPE,
				new HashSet<String>(Arrays
						.asList(this.vth.generateTSA(USER_ID, SECTION_ID, false).getEntityId(), this.vth.generateTSA("Ssss'ra", "Arcanus", false).getEntityId(), this.vth.generateTSA("Kali", "Arcanus", false).getEntityId()))));
	}
}

@@ -800,16 +800,40 @@
 ource;%0A%0A
+import org.junit.After;%0A
 import o
@@ -849,16 +849,16 @@
 Assert;%0A
-
 import o
@@ -930,16 +930,223 @@
 unWith;%0A
+import org.springframework.security.core.context.SecurityContextHolder;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0A%0A
 import o
@@ -1278,150 +1278,16 @@
 ity;
-%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 %0A%0A/**%0A *
  %0A *
@@ -1282,17 +1282,16 @@
 %0A%0A/**%0A *
- 
 %0A * @aut
@@ -1307,17 +1307,16 @@
 ishev%0A *
- 
 %0A */%0A@Ru
@@ -1944,24 +1944,143 @@
 ER_ID);%0A%09%7D%0A%0A
+%09@After%0A%09public void cleanUp() throws Exception %7B%0A%09    SecurityContextHolder.clearContext();%0A%09    vth.resetRepo();%0A%09%7D%0A%0A
 %09@Test(expec
@@ -2545,62 +2545,8 @@
 );%0A%0A
-%09%09int random = (int) Math.floor(Math.random() * 100);%0A
 %09%09fo
@@ -2567,14 +2567,11 @@
 i %3C 
-random
+100
 ; i+

fix inconsistency in calling random() during a teacher -> teacher section association validator unit test