text:
package org.sagebionetworks.evaluation.manager;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sagebionetworks.evaluation.dao.EvaluationDAO;
import org.sagebionetworks.evaluation.dao.EvaluationSubmissionsDAO;
import org.sagebionetworks.evaluation.dao.SubmissionDAO;
import org.sagebionetworks.evaluation.dao.SubmissionFileHandleDAO;
import org.sagebionetworks.evaluation.dao.SubmissionStatusDAO;
import org.sagebionetworks.evaluation.model.BatchUploadResponse;
import org.sagebionetworks.evaluation.model.Evaluation;
import org.sagebionetworks.evaluation.model.EvaluationStatus;
import org.sagebionetworks.evaluation.model.EvaluationSubmissions;
import org.sagebionetworks.evaluation.model.Submission;
import org.sagebionetworks.evaluation.model.SubmissionBundle;
import org.sagebionetworks.evaluation.model.SubmissionContributor;
import org.sagebionetworks.evaluation.model.SubmissionStatus;
import org.sagebionetworks.evaluation.model.SubmissionStatusBatch;
import org.sagebionetworks.evaluation.model.SubmissionStatusEnum;
import org.sagebionetworks.ids.IdGenerator;
import org.sagebionetworks.ids.IdType;
import org.sagebionetworks.repo.manager.EntityManager;
import org.sagebionetworks.repo.manager.MessageToUserAndBody;
import org.sagebionetworks.repo.manager.NodeManager;
import org.sagebionetworks.repo.manager.UserProfileManager;
import org.sagebionetworks.repo.manager.file.FileHandleManager;
import org.sagebionetworks.repo.manager.file.FileHandleUrlRequest;
import org.sagebionetworks.repo.model.ACCESS_TYPE;
import org.sagebionetworks.repo.model.ConflictingUpdateException;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.DockerCommitDao;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.Folder;
import org.sagebionetworks.repo.model.InvalidModelException;
import org.sagebionetworks.repo.model.Node;
import org.sagebionetworks.repo.model.ObjectType;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.repo.model.TeamDAO;
import org.sagebionetworks.repo.model.UnauthorizedException;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.UserProfile;
import org.sagebionetworks.repo.model.annotation.AnnotationBase;
import org.sagebionetworks.repo.model.annotation.Annotations;
import org.sagebionetworks.repo.model.annotation.DoubleAnnotation;
import org.sagebionetworks.repo.model.annotation.LongAnnotation;
import org.sagebionetworks.repo.model.annotation.StringAnnotation;
import org.sagebionetworks.repo.model.annotation.v2.AnnotationsV2TestUtils;
import org.sagebionetworks.repo.model.annotation.v2.AnnotationsV2Utils;
import org.sagebionetworks.repo.model.annotation.v2.AnnotationsValueType;
import org.sagebionetworks.repo.model.auth.AuthorizationStatus;
import org.sagebionetworks.repo.model.docker.DockerCommit;
import org.sagebionetworks.repo.model.docker.DockerRepository;
import org.sagebionetworks.repo.model.entitybundle.v2.EntityBundle;
import org.sagebionetworks.repo.model.file.FileHandle;
import org.sagebionetworks.repo.model.file.FileHandleAssociateType;
import org.sagebionetworks.repo.model.file.S3FileHandle;
import org.sagebionetworks.repo.model.message.ChangeType;
import org.sagebionetworks.repo.model.message.MessageToSend;
import org.sagebionetworks.repo.model.message.TransactionalMessenger;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.schema.adapter.JSONObjectAdapter;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl;
import org.sagebionetworks.util.Pair;

import com.google.common.collect.ImmutableSet;

@ExtendWith(MockitoExtension.class)
public class SubmissionManagerTest {

	private Evaluation eval;
	private Submission sub;
	private Submission sub2;
	private Submission subWithId;
	private Submission sub2WithId;
	private SubmissionStatus subStatus;
	private SubmissionBundle submissionBundle;
	private EntityBundle dockerBundle;

	private SubmissionStatusBatch batch;

	@Mock
	private IdGenerator mockIdGenerator;
	@Mock
	private SubmissionDAO mockSubmissionDAO;
	@Mock
	private SubmissionStatusDAO mockSubmissionStatusDAO;
	@Mock
	private SubmissionFileHandleDAO mockSubmissionFileHandleDAO;
	@Mock
	private EvaluationSubmissionsDAO mockEvaluationSubmissionsDAO;
	@Mock
	private EntityManager mockEntityManager;
	@Mock
	private NodeManager mockNodeManager;
	@Mock
	private FileHandleManager mockFileHandleManager;
	@Mock
	private EvaluationPermissionsManager mockEvalPermissionsManager;
	@Mock
	private SubmissionEligibilityManager mockSubmissionEligibilityManager;
	@Mock
	private Node mockNode;
	@Mock
	private Node mockDockerRepoNode;
	@Mock
	private TeamDAO mockTeamDAO;
	@Mock
	private UserProfileManager mockUserProfileManager;
	@Mock
	private EvaluationDAO mockEvaluationDAO;
	@Mock
	private DockerCommitDao mockDockerCommitDao;
	@Mock
	private TransactionalMessenger mockTransactionalMessenger;

	@InjectMocks
	private SubmissionManagerImpl submissionManager;

	private Folder folder;
	private EntityBundle bundle;

    private FileHandle fileHandle1;
    private FileHandle fileHandle2;
	private List<String> handleIds;

	private static final String EVAL_ID = "12";
	private static final Long EVAL_ID_LONG = Long.parseLong("12");
	private static final String OWNER_ID = "34";
	private static final String USER_ID = "56";
	private static final String SUB_ID = "78";
	private static final String SUB2_ID = "87";
	private static final String PROJECT_ID = "syn56";
	private static final String ENTITY_ID = "90";
	private static final String ENTITY2_ID = "99";
	private static final String DOCKER_REPO_ENTITY_ID = "11";
	private static final String ETAG = "etag";
	private static final String HANDLE_ID_2 = "handle2";
	private static final String HANDLE_ID_1 = "handle1";
	private static final String TEST_URL = "http://www.foo.com/bar";
	private static final String TEAM_ID = "999";
	private static final String CHALLENGE_END_POINT = "https://synapse.org/#ENTITY:";
	private static final String NOTIFICATION_UNSUBSCRIBE_END_POINT = "https://synapse.org/#notificationUnsubscribeEndpoint:";
	private static final String DOCKER_REPO_NAME = "docker.synapse.org/syn12345/arepo";
	private static final String DOCKER_REPO_DIGEST = "abcdef012345";

	private static UserInfo ownerInfo;
	private static UserInfo userInfo;

	private static EntityBundle createDockerEntityBundle() {
		EntityBundle bundle = new EntityBundle();
		DockerRepository entity = new DockerRepository();
		entity.setRepositoryName(DOCKER_REPO_NAME);
		bundle.setEntity(entity);
		bundle.setFileHandles(Collections.EMPTY_LIST);
		return bundle;
	}

	private static String createEntityBundleJSON(EntityBundle bundle) throws JSONObjectAdapterException {
		JSONObjectAdapter joa = new JSONObjectAdapterImpl();
		bundle.writeToJSONObject(joa);
		return joa.toJSONString();
	}

    @BeforeEach
    public void setUp() throws Exception {
		// User Info
    	ownerInfo = new UserInfo(false, OWNER_ID);
    	userInfo = new UserInfo(false, USER_ID);

    	// FileHandles
		List<FileHandle> handles = new ArrayList<FileHandle>();
		handleIds = new ArrayList<String>();

		fileHandle1 = new S3FileHandle();
		fileHandle1.setId(HANDLE_ID_1);
		handles.add(fileHandle1);
		handleIds.add(HANDLE_ID_1);
		fileHandle2 = new S3FileHandle();
		fileHandle2.setId(HANDLE_ID_2);
		handles.add(fileHandle2);
		handleIds.add(HANDLE_ID_2);

    	// Objects
		eval = new Evaluation();
		eval.setName("compName");
		eval.setId(EVAL_ID);
		eval.setOwnerId(OWNER_ID);
        eval.setContentSource(PROJECT_ID);
        eval.setStatus(EvaluationStatus.CLOSED); // deprecated, setting doesn't matter
        eval.setCreatedOn(new Date());
        eval.setEtag("compEtag");

        sub = new Submission();
        sub.setEvaluationId(EVAL_ID);
        sub.setCreatedOn(new Date());
        sub.setEntityId(ENTITY_ID);
        sub.setName("subName");
        sub.setUserId(USER_ID);
        sub.setSubmitterAlias("Team Awesome!");
        sub.setVersionNumber(0L);
        dockerBundle = createDockerEntityBundle();
    	sub.setEntityBundleJSON(createEntityBundleJSON(dockerBundle));

        sub2 = new Submission();
        sub2.setEvaluationId(EVAL_ID);
        sub2.setCreatedOn(new Date());
        sub2.setEntityId(ENTITY2_ID);
        sub2.setName("subName");
        sub2.setUserId(OWNER_ID);
        sub2.setSubmitterAlias("Team Even More Awesome!");
        sub2.setVersionNumber(0L);

        subWithId = new Submission();
        subWithId.setId(SUB_ID);
        subWithId.setEvaluationId(EVAL_ID);
        subWithId.setCreatedOn(new Date());
        subWithId.setEntityId(ENTITY_ID);
        subWithId.setName("subName");
        subWithId.setUserId(USER_ID);
        subWithId.setSubmitterAlias("Team Awesome!");
        subWithId.setVersionNumber(0L);

        sub2WithId = new Submission();
        sub2WithId.setId(SUB2_ID);
        sub2WithId.setEvaluationId(EVAL_ID);
        sub2WithId.setCreatedOn(new Date());
        sub2WithId.setEntityId(ENTITY2_ID);
        sub2WithId.setName("subName");
        sub2WithId.setUserId(OWNER_ID);
        sub2WithId.setSubmitterAlias("Team Even More Awesome!");
        sub2WithId.setVersionNumber(0L);

        subStatus = new SubmissionStatus();
        subStatus.setEtag("subEtag");
        subStatus.setId(SUB_ID);
        subStatus.setModifiedOn(new Date());
        subStatus.setScore(0.0);
        subStatus.setStatus(SubmissionStatusEnum.RECEIVED);
        subStatus.setAnnotations(createDummyAnnotations().getFirst());

        submissionBundle = new SubmissionBundle();
        submissionBundle.setSubmission(subWithId);
        submissionBundle.setSubmissionStatus(subStatus);

        batch = new SubmissionStatusBatch();
        List<SubmissionStatus> statuses = new ArrayList<SubmissionStatus>();
        statuses.add(subStatus);
        batch.setStatuses(statuses);
        batch.setIsFirstBatch(true);
        batch.setIsLastBatch(true);

        folder = new Folder();
        bundle = new EntityBundle();
        bundle.setEntity(folder);
        bundle.setFileHandles(handles);
    }

	@Test
	public void testCRUDAsAdmin() throws Exception {
    	when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockSubmissionStatusDAO.get(eq(SUB_ID))).thenReturn(subStatus);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionStatusDAO.getEvaluationIdForBatch((List<SubmissionStatus>)anyObject())).thenReturn(Long.parseLong(EVAL_ID));

    	// by default we say that individual submissions are within quota
    	// (specific tests will change this)
    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getId());
		assertNotNull(subWithId.getId());
		submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		submissionManager.getSubmission(ownerInfo, SUB_ID);
		submissionManager.updateSubmissionStatus(ownerInfo, subStatus);
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		// add another contributor as an admin
		SubmissionContributor submissionContributor = new SubmissionContributor();
		submissionContributor.setPrincipalId("101");
		SubmissionContributor scCreated = submissionManager.addSubmissionContributor(new UserInfo(true), SUB_ID, submissionContributor);
		assertEquals("101", scCreated.getPrincipalId());
		assertNotNull(scCreated.getCreatedOn());

		submissionManager.deleteSubmission(ownerInfo, SUB_ID);
		verify(mockSubmissionDAO).create(any(Submission.class));
		verify(mockSubmissionDAO).delete(eq(SUB_ID));
		verify(mockSubmissionStatusDAO).create(any(SubmissionStatus.class));
		verify(mockTransactionalMessenger).sendMessageAfterCommit(getSubmissionMessage(userInfo, SUB_ID, ChangeType.CREATE));
		verify(mockSubmissionStatusDAO, times(2)).update(any(List.class));
		verify(mockTransactionalMessenger, times(2)).sendMessageAfterCommit(getSubmissionMessage(ownerInfo, SUB_ID, ChangeType.UPDATE));
		verify(mockSubmissionFileHandleDAO).create(eq(SUB_ID), eq(fileHandle1.getId()));
		verify(mockSubmissionFileHandleDAO).create(eq(SUB_ID), eq(fileHandle2.getId()));
		verify(mockEvaluationSubmissionsDAO, times(1)).lockAndGetForEvaluation(EVAL_ID_LONG);
		verify(mockEvaluationSubmissionsDAO, times(1)).updateEtagForEvaluation(EVAL_ID_LONG, true, ChangeType.CREATE);
		verify(mockEvaluationSubmissionsDAO, times(3)).updateEtagForEvaluation(EVAL_ID_LONG, true, ChangeType.UPDATE);
	}

	@Test
	public void testCAsUser_NotAuthorized() throws Exception {
		assertNull(sub.getId());
		assertNotNull(subWithId.getId());
		when(mockEvalPermissionsManager.hasAccess(
				eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.accessDenied(""));

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		});
	}

	@Test
	public void testCRUDAsUser() throws NotFoundException, DatastoreException, JSONObjectAdapterException {
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.UPDATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.DELETE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
    	when(mockSubmissionStatusDAO.getEvaluationIdForBatch((List<SubmissionStatus>)anyObject())).thenReturn(Long.parseLong(EVAL_ID));

    	// by default we say that individual submissions are within quota
    	// (specific tests will change this)
    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getId());
		assertNotNull(subWithId.getId());
		submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.getSubmission(userInfo, SUB_ID);
		});

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.updateSubmissionStatus(userInfo, subStatus);
		});

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(userInfo, EVAL_ID, batch);
		});

		// add another contributor
		SubmissionContributor submissionContributor = new SubmissionContributor();
		submissionContributor.setPrincipalId("101");

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.addSubmissionContributor(userInfo, SUB_ID, submissionContributor);
		});

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.deleteSubmission(userInfo, SUB_ID);
		});

		verify(mockSubmissionDAO).create(any(Submission.class));
		verify(mockSubmissionDAO, never()).delete(eq(SUB_ID));
		verify(mockSubmissionStatusDAO).create(any(SubmissionStatus.class));
		verify(mockTransactionalMessenger).sendMessageAfterCommit(getSubmissionMessage(userInfo, SUB_ID, ChangeType.CREATE));
		verify(mockSubmissionStatusDAO, never()).update(any(List.class));
	}

	@Test
	public void testInsertUserAsContributor_NullContributorList() throws Exception {
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());
    	// by default we say that individual submissions are within quota
    	// (specific tests will change this)
    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getContributors());
		submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		assertEquals(1, sub.getContributors().size());
		SubmissionContributor sc =sub.getContributors().iterator().next();
		assertNotNull(sc.getCreatedOn());
		assertEquals(USER_ID, sc.getPrincipalId());
		verify(mockSubmissionEligibilityManager).
			isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class));
		verify(mockSubmissionEligibilityManager, never()).
			isTeamEligible(anyString(), anyString(),any(List.class), anyString(), any(Date.class));
	}

	@Test
	public void testInsertUserAsContributor_SubmitterIsContributor() throws Exception {
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

    	// by default we say that individual submissions are within quota
    	// (specific tests will change this)
    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

		Set<SubmissionContributor> contributors = new HashSet<SubmissionContributor>();
		SubmissionContributor sc = new SubmissionContributor();
		sc.setPrincipalId(USER_ID);
		contributors.add(sc);
		sub.setContributors(contributors);
		submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		assertEquals(1, sub.getContributors().size());
		sc =sub.getContributors().iterator().next();
		assertNotNull(sc.getCreatedOn());
		assertEquals(USER_ID, sc.getPrincipalId());
		verify(mockSubmissionEligibilityManager).
			isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class));
		verify(mockSubmissionEligibilityManager, never()).
			isTeamEligible(anyString(), anyString(),any(List.class), anyString(), any(Date.class));
	}

	@Test
	public void testTeamSubmission() throws Exception {
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getContributors());
		Set<SubmissionContributor> contributors = new HashSet<SubmissionContributor>();
		SubmissionContributor sc = new SubmissionContributor();
		sc.setPrincipalId(OWNER_ID);
		contributors.add(sc);
		sub.setContributors(contributors);
		sub.setTeamId(TEAM_ID);
		int submissionEligibilityHash = 1234;
		when(mockSubmissionEligibilityManager.isTeamEligible(
				eq(EVAL_ID), eq(TEAM_ID),
				any(List.class), eq(""+submissionEligibilityHash), any(Date.class))).
				thenReturn(AuthorizationStatus.authorized());
		submissionManager.createSubmission(userInfo, sub, ETAG, ""+submissionEligibilityHash, bundle);
		assertEquals(2, sub.getContributors().size());
		boolean foundUser = false;
		boolean foundOwner = false;
		assertEquals(2, sub.getContributors().size());
		for (SubmissionContributor sc2 : sub.getContributors()) {
			assertNotNull(sc2.getCreatedOn());
			if (USER_ID.equals(sc2.getPrincipalId())) foundUser=true;
			if (OWNER_ID.equals(sc2.getPrincipalId())) foundOwner=true;
		}
		assertTrue(foundUser);
		assertTrue(foundOwner);
		verify(mockSubmissionEligibilityManager, never()).
				isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class));
		verify(mockSubmissionEligibilityManager).
			isTeamEligible(eq(EVAL_ID), eq(TEAM_ID),
					any(List.class), eq(""+submissionEligibilityHash), any(Date.class));
	}

	@Test
	public void testContributorListButNoTeamId() throws Exception {
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getContributors());
		Set<SubmissionContributor> contributors = new HashSet<SubmissionContributor>();
		SubmissionContributor sc = new SubmissionContributor();
		sc.setPrincipalId(OWNER_ID);
		contributors.add(sc);
		sub.setContributors(contributors);
		Assertions.assertThrows(InvalidModelException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		});
	}

	@Test
	public void testIndividualSubmissionWithHash() throws Exception {
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		assertNull(sub.getContributors());
		Assertions.assertThrows(InvalidModelException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG, "123456", bundle);
		});
	}

	@Test
	public void testUnauthorizedGet() throws NotFoundException, DatastoreException, JSONObjectAdapterException {
    	when(mockSubmissionDAO.get(eq(SUB2_ID))).thenReturn(sub2WithId);
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.getSubmission(userInfo, SUB2_ID);
		});
	}

	@Test
	public void testUnauthorizedEntity() throws NotFoundException, DatastoreException, JSONObjectAdapterException {
    	when(mockNodeManager.getNode(eq(userInfo), eq(ENTITY2_ID))).thenThrow(new UnauthorizedException());
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			// user should not have access to sub2
			submissionManager.createSubmission(userInfo, sub2, ETAG, null, bundle);
		});
	}

	@Test
	public void testCreateWithErroneousDockerDigest() throws Exception{
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockNode.getNodeType()).thenReturn(EntityType.file);
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

		sub.setDockerDigest("this is some digest");
		submissionManager.createSubmission(userInfo, sub, ETAG, null, bundle);
		Submission retrieved = submissionManager.getSubmission(ownerInfo, SUB_ID);
		assertNull(retrieved.getDockerDigest());
	}

	@Test
	public void testCreateDockerRepoSubmission() throws Exception {
		when(mockIdGenerator.generateNewId(IdType.EVALUATION_SUBMISSION_ID)).thenReturn(Long.parseLong(SUB_ID));
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionDAO.create(eq(sub))).thenReturn(SUB_ID);
    	when(mockDockerRepoNode.getNodeType()).thenReturn(EntityType.dockerrepo);
    	when(mockDockerRepoNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(DOCKER_REPO_ENTITY_ID))).thenReturn(mockDockerRepoNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());    	when(mockSubmissionEligibilityManager.isIndividualEligible(eq(EVAL_ID), any(UserInfo.class), any(Date.class))).
    		thenReturn(AuthorizationStatus.authorized());

    	DockerCommit commit = new DockerCommit();
    	commit.setTag("foo");
    	commit.setDigest(DOCKER_REPO_DIGEST);
    	when(mockDockerCommitDao.
    			listCommitsByOwnerAndDigest(DOCKER_REPO_ENTITY_ID, DOCKER_REPO_DIGEST)).
    			thenReturn(Collections.singletonList(commit));

		sub.setEntityId(DOCKER_REPO_ENTITY_ID);
		sub.setDockerDigest(DOCKER_REPO_DIGEST);
		submissionManager.createSubmission(userInfo, sub, ETAG, null, dockerBundle);
		verify(mockDockerCommitDao).listCommitsByOwnerAndDigest(DOCKER_REPO_ENTITY_ID, DOCKER_REPO_DIGEST);
	}

	@Test
	public void testCreateDockerRepoSubmissionNoDigest() throws Exception {
    	when(mockDockerRepoNode.getNodeType()).thenReturn(EntityType.dockerrepo);
    	when(mockDockerRepoNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(DOCKER_REPO_ENTITY_ID))).thenReturn(mockDockerRepoNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		sub.setEntityId(DOCKER_REPO_ENTITY_ID);
		sub.setDockerDigest(null);
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG, null, dockerBundle);
		});
	}

	@Test
	public void testCreateDockerRepoSubmissionInvalidDigest() throws Exception {
		when(mockDockerRepoNode.getNodeType()).thenReturn(EntityType.dockerrepo);
    	when(mockDockerRepoNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(DOCKER_REPO_ENTITY_ID))).thenReturn(mockDockerRepoNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		sub.setEntityId(DOCKER_REPO_ENTITY_ID);
		sub.setDockerDigest("not a valid digest");
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG, null, dockerBundle);
		});
	}

	@Test
	public void testInvalidScore() throws Exception {
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

    	subStatus.setScore(1.1);
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatus(ownerInfo, subStatus);
		});
	}

	@Test
	public void testInvalidEtag() throws Exception {
    	when(mockNode.getETag()).thenReturn(ETAG);
    	when(mockNodeManager.getNode(any(UserInfo.class), eq(ENTITY_ID))).thenReturn(mockNode);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.SUBMIT))).thenReturn(AuthorizationStatus.authorized());

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.createSubmission(userInfo, sub, ETAG + "modified", null, bundle);
		});
	}

	@Test
	public void testGetAllSubmissions() throws DatastoreException, UnauthorizedException, NotFoundException {
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

		SubmissionStatusEnum statusEnum = SubmissionStatusEnum.SCORED;
		submissionManager.getAllSubmissions(ownerInfo, EVAL_ID, null, 10, 0);
		submissionManager.getAllSubmissions(ownerInfo, EVAL_ID, statusEnum, 10, 0);
		verify(mockSubmissionDAO).getAllByEvaluation(eq(EVAL_ID), anyLong(), anyLong());
		verify(mockSubmissionDAO).getAllByEvaluationAndStatus(eq(EVAL_ID), eq(statusEnum), eq(10L), eq(0L));
	}

	@Test
	public void testGetAllSubmissionsUnauthorized() throws DatastoreException, UnauthorizedException, NotFoundException {
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(USER_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.accessDenied(""));
    	Assertions.assertThrows(UnauthorizedException.class, ()-> {
    		submissionManager.getAllSubmissions(ownerInfo, USER_ID, null, 10, 0);
    	});
	}

	@Test
	public void testGetAllSubmissionBundles() throws Exception {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

		SubmissionStatusEnum statusEnum = SubmissionStatusEnum.SCORED;
		when(mockSubmissionDAO.getAllBundlesByEvaluationAndStatus(EVAL_ID, statusEnum, 10L, 0L)).
			thenReturn(Collections.singletonList(submissionBundle));

		List<SubmissionBundle> queryResults =
				submissionManager.getAllSubmissionBundles(ownerInfo, EVAL_ID, statusEnum, 10L, 0L);
		assertEquals(1L, queryResults.size());
		SubmissionBundle expected = new SubmissionBundle();
		expected.setSubmission(subWithId);
		expected.setSubmissionStatus(subStatus);
		assertEquals(Collections.singletonList(expected), queryResults);
		verify(mockSubmissionDAO).getAllBundlesByEvaluationAndStatus(eq(EVAL_ID), eq(statusEnum), eq(10L), eq(0L));
	}

	@Test
	public void testGetMyOwnSubmissionBundles() throws Exception {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

		when(mockSubmissionDAO.getAllBundlesByEvaluationAndUser(EVAL_ID, ownerInfo.getId().toString(), 10L, 0L)).
			thenReturn(Collections.singletonList(submissionBundle));
		List<SubmissionBundle> queryResults =
				submissionManager.getMyOwnSubmissionBundlesByEvaluation(ownerInfo, EVAL_ID, 10L, 0L);
		SubmissionBundle expected = new SubmissionBundle();
		expected.setSubmission(subWithId);
		expected.setSubmissionStatus(subStatus);
		assertEquals(Collections.singletonList(expected), queryResults);
		verify(mockSubmissionDAO).getAllBundlesByEvaluationAndUser(EVAL_ID, ownerInfo.getId().toString(), 10L, 0L);
	}

	@Test
	public void testGetMyOwnSubmissions() throws Exception {
		submissionManager.getMyOwnSubmissionsByEvaluation(ownerInfo, EVAL_ID, 10, 0);
		verify(mockSubmissionDAO).getAllByEvaluationAndUser(EVAL_ID, ownerInfo.getId().toString(), 10, 0);
	}

	@Test
	public void testGetSubmissionCount() throws DatastoreException, NotFoundException {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

		submissionManager.getSubmissionCount(ownerInfo, EVAL_ID);
		verify(mockSubmissionDAO).getCountByEvaluation(eq(EVAL_ID));
	}

	@Test
	public void testGetRedirectURLForFileHandle()
			throws DatastoreException, NotFoundException {
		when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionFileHandleDAO.getAllBySubmission(eq(SUB_ID))).thenReturn(handleIds);
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());

		FileHandleUrlRequest urlRequest = new FileHandleUrlRequest(ownerInfo, HANDLE_ID_1)
				.withAssociation(FileHandleAssociateType.SubmissionAttachment, SUB_ID);

		when(mockFileHandleManager.getRedirectURLForFileHandle(eq(urlRequest))).thenReturn(TEST_URL);

		String url = submissionManager.getRedirectURLForFileHandle(ownerInfo, SUB_ID, HANDLE_ID_1);

		verify(mockSubmissionFileHandleDAO).getAllBySubmission(eq(SUB_ID));
		verify(mockFileHandleManager).getRedirectURLForFileHandle(eq(urlRequest));

		assertEquals(TEST_URL, url);
	}

	@Test
	public void testGetRedirectURLForFileHandleUnauthorized()
			throws DatastoreException, NotFoundException {
    	when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.getRedirectURLForFileHandle(userInfo, SUB_ID, HANDLE_ID_1);
		});
	}

	@Test
	public void testGetRedirectURLForFileHandleNotFound()
			throws DatastoreException, NotFoundException {
    	when(mockSubmissionDAO.get(eq(SUB2_ID))).thenReturn(sub2WithId);
    	when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionFileHandleDAO.getAllBySubmission(any())).thenReturn(Collections.emptyList());

		Assertions.assertThrows(NotFoundException.class, ()-> {
			// HANDLE_ID_1 is not contained in this Submission
			submissionManager.getRedirectURLForFileHandle(ownerInfo, SUB2_ID, HANDLE_ID_1);
		});
	}

	@Test
	public void testGetSubmissionStatus() throws DatastoreException, NotFoundException {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionDAO.getBundle(any(), anyBoolean())).thenReturn(submissionBundle);

		SubmissionStatus status = submissionManager.getSubmissionStatus(ownerInfo, SUB_ID);

		verify(mockSubmissionDAO).getBundle(SUB_ID, false);
		Annotations annos = status.getAnnotations();
		assertNotNull(annos);

		// check the single private LongAnno
		assertNotNull(annos.getLongAnnos());
		assertEquals(1, annos.getLongAnnos().size());
		LongAnnotation la = annos.getLongAnnos().get(0);
		assertTrue(la.getIsPrivate());

		// check the single StringAnno
		assertNotNull(annos.getStringAnnos());
		assertEquals(1, annos.getStringAnnos().size());

		// check the single DoubleAnno
		assertNotNull(annos.getDoubleAnnos());
		assertEquals(1, annos.getDoubleAnnos().size());
	}

	@Test
	public void testGetSubmissionStatusNoREADAccess() throws Exception {
		when(mockSubmissionDAO.getBundle(any(), anyBoolean())).thenReturn(submissionBundle);
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ))).thenReturn(AuthorizationStatus.accessDenied(""));
    	Assertions.assertThrows(UnauthorizedException.class, ()-> {
    		submissionManager.getSubmissionStatus(userInfo, SUB_ID);
    	});
	}

	@Test
	public void testGetALLSubmissionStatus() throws Exception {
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ))).thenReturn(AuthorizationStatus.authorized());
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
		when(mockSubmissionDAO.getAllBundlesByEvaluation(EVAL_ID, 100L, 0L)).thenReturn(Collections.singletonList(submissionBundle));
		List<SubmissionStatus> actual = submissionManager.getAllSubmissionStatuses(userInfo, EVAL_ID, null, 100L, 0L);
		verify(mockSubmissionDAO).getAllBundlesByEvaluation(EVAL_ID, 100L, 0L);
		List<SubmissionStatus> expected = Collections.singletonList(subStatus);
		assertEquals(expected, actual);
	}

	@Test
	public void testGetALLSubmissionStatusNoREADAccess() throws Exception {
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ))).thenReturn(AuthorizationStatus.accessDenied(""));
    	Assertions.assertThrows(UnauthorizedException.class, ()-> {
    		submissionManager.getAllSubmissionStatuses(userInfo, EVAL_ID, null, 100L, 0L);
    	});
	}

	@Test
	public void testGetSubmissionStatusNoPrivate() throws DatastoreException, NotFoundException {
    	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ))).thenReturn(AuthorizationStatus.authorized());
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
       	when(mockSubmissionDAO.getBundle(any(), anyBoolean())).thenReturn(submissionBundle);

		SubmissionStatus status = submissionManager.getSubmissionStatus(userInfo, SUB_ID);

		verify(mockSubmissionDAO).getBundle(SUB_ID, false);
		Annotations annos = status.getAnnotations();
		assertNotNull(annos);

		// check that the single private LongAnno was removed
		assertNotNull(annos.getLongAnnos());
		assertEquals(0, annos.getLongAnnos().size());

		// check the single StringAnno
		assertNotNull(annos.getStringAnnos());
		assertEquals(1, annos.getStringAnnos().size());

		// check the single DoubleAnno
		assertNotNull(annos.getDoubleAnnos());
		assertEquals(1, annos.getDoubleAnnos().size());
	}

	@Test
	public void testGetSubmissionStatusNoPrivateNoAnnot() throws DatastoreException, NotFoundException {
		when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ))).thenReturn(AuthorizationStatus.authorized());
       	when(mockEvalPermissionsManager.hasAccess(eq(userInfo), eq(EVAL_ID), eq(ACCESS_TYPE.READ_PRIVATE_SUBMISSION))).thenReturn(AuthorizationStatus.accessDenied(""));
       	when(mockSubmissionDAO.getBundle(any(), anyBoolean())).thenReturn(submissionBundle);

 		Annotations annots = subStatus.getAnnotations();
		annots.setStringAnnos(null); // this is the case in PLFM-2586
		annots.setLongAnnos(null);
		annots.setDoubleAnnos(null);
		submissionManager.getSubmissionStatus(userInfo, SUB_ID);
		verify(mockSubmissionDAO).getBundle(SUB_ID, false);
	}

	private static Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> createDummyAnnotations() {
		List<StringAnnotation> stringAnnos = new ArrayList<StringAnnotation>();
		StringAnnotation sa = new StringAnnotation();
		sa.setIsPrivate(false);
		sa.setKey("sa");
		sa.setValue("foo");
		stringAnnos.add(sa);

		List<LongAnnotation> longAnnos = new ArrayList<LongAnnotation>();
		LongAnnotation la = new LongAnnotation();
		la.setIsPrivate(true);
		la.setKey("la");
		la.setValue(42L);
		longAnnos.add(la);

		List<DoubleAnnotation> doubleAnnos = new ArrayList<DoubleAnnotation>();
		DoubleAnnotation doa = new DoubleAnnotation();
		doa.setIsPrivate(false);
		doa.setKey("doa");
		doa.setValue(3.14);
		doubleAnnos.add(doa);

		org.sagebionetworks.repo.model.annotation.Annotations annos = new org.sagebionetworks.repo.model.annotation.Annotations();
		annos.setStringAnnos(stringAnnos);
		annos.setLongAnnos(longAnnos);
		annos.setDoubleAnnos(doubleAnnos);

		org.sagebionetworks.repo.model.annotation.v2.Annotations expected = AnnotationsV2Utils.emptyAnnotations();

		AnnotationsV2TestUtils.putAnnotations(expected, "sa", "foo", AnnotationsValueType.STRING);
		AnnotationsV2TestUtils.putAnnotations(expected, "la", "42", AnnotationsValueType.LONG);
		AnnotationsV2TestUtils.putAnnotations(expected, "doa", "3.14", AnnotationsValueType.DOUBLE);

		return Pair.create(annos, expected);
	}

	@Test
	public void testBadBatch() throws Exception {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionStatusDAO.getEvaluationIdForBatch((List<SubmissionStatus>)anyObject())).thenReturn(Long.parseLong(EVAL_ID));

		// baseline:  all is good
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		batch.setIsFirstBatch(null);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		batch.setIsFirstBatch(true);
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		batch.setIsLastBatch(null);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		batch.setIsLastBatch(true);
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		List<SubmissionStatus> statuses = new ArrayList<SubmissionStatus>(batch.getStatuses());
		batch.getStatuses().clear();

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		batch.setStatuses(null);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		List<SubmissionStatus> tooLong = new ArrayList<SubmissionStatus>();
		for (int i=0; i<501; i++) tooLong.add(new SubmissionStatus());
		batch.setStatuses(tooLong);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		List<SubmissionStatus> hasNull = new ArrayList<SubmissionStatus>();
		hasNull.add(null);
		batch.setStatuses(hasNull);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});

		// back to base-line
		batch.setStatuses(statuses);
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID+"xxx", batch);
		});
	}

	@Test
	public void testConflictingBatchUpdate() throws Exception {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionStatusDAO.getEvaluationIdForBatch((List<SubmissionStatus>)anyObject())).thenReturn(Long.parseLong(EVAL_ID));

		// baseline:  all is OK
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		batch.setIsFirstBatch(false);
		batch.setBatchToken("foo");
		EvaluationSubmissions evalSubs = new EvaluationSubmissions();
		evalSubs.setEtag("foo");
		when(mockEvaluationSubmissionsDAO.lockAndGetForEvaluation(EVAL_ID_LONG)).thenReturn(evalSubs);
		// still OK
		submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);

		// but what if the token is wrong?
		evalSubs.setEtag("bar");
		when(mockEvaluationSubmissionsDAO.lockAndGetForEvaluation(EVAL_ID_LONG)).thenReturn(evalSubs);

		Assertions.assertThrows(ConflictingUpdateException.class, ()-> {
			submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		});
	}

	@Test
	public void testBatchResponseToken() throws Exception {
		when(mockEvalPermissionsManager.hasAccess(eq(ownerInfo), eq(EVAL_ID), any(ACCESS_TYPE.class))).thenReturn(AuthorizationStatus.authorized());
    	when(mockSubmissionStatusDAO.getEvaluationIdForBatch((List<SubmissionStatus>)anyObject())).thenReturn(Long.parseLong(EVAL_ID));

		batch.setIsFirstBatch(true);
		batch.setIsLastBatch(false);
		when(mockEvaluationSubmissionsDAO.updateEtagForEvaluation(EVAL_ID_LONG, false, ChangeType.UPDATE)).thenReturn("foo");
		BatchUploadResponse resp = submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		verify(mockEvaluationSubmissionsDAO).updateEtagForEvaluation(EVAL_ID_LONG, false, ChangeType.UPDATE);
		assertEquals(resp.getNextUploadToken(), "foo");

		// last batch doesn't get a token back
		batch.setIsLastBatch(true);
		resp = submissionManager.updateSubmissionStatusBatch(ownerInfo, EVAL_ID, batch);
		assertNull(resp.getNextUploadToken());
	}

	@Test
	public void testCreateSubmissionNotification() throws Exception {
		assertNull(sub.getContributors());
		Set<SubmissionContributor> contributors = new HashSet<SubmissionContributor>();
		SubmissionContributor sc = new SubmissionContributor();
		sc.setPrincipalId(USER_ID);
		contributors.add(sc);
		SubmissionContributor sc2 = new SubmissionContributor();
		sc2.setPrincipalId("99");
		contributors.add(sc2);
		sub.setContributors(contributors);
		sub.setTeamId(TEAM_ID);
		int submissionEligibilityHash = 1234;

		Team team = new Team();
		team.setName("test team");
		when(mockTeamDAO.get(TEAM_ID)).thenReturn(team);

		Evaluation evaluation = new Evaluation();
		evaluation.setContentSource("syn101");
		String evalQueueName = "express queue";
		evaluation.setName(evalQueueName);
		when(mockEvaluationDAO.get(sub.getEvaluationId())).thenReturn(evaluation);

		UserProfile up = new UserProfile();
		up.setUserName("auser");
		when(mockUserProfileManager.getUserProfile(userInfo.getId().toString())).thenReturn(up);

		List<MessageToUserAndBody> result =
				submissionManager.createSubmissionNotifications(
						userInfo, sub, ""+submissionEligibilityHash,
						CHALLENGE_END_POINT,
						NOTIFICATION_UNSUBSCRIBE_END_POINT);
		assertEquals(1, result.size());
		assertEquals("Team Challenge Submission", result.get(0).getMetadata().getSubject());
		assertEquals("test team <testteam@synapse.org>", result.get(0).getMetadata().getTo());
		assertEquals(Collections.singleton("99"), result.get(0).getMetadata().getRecipients());
		String body = result.get(0).getBody();
		String userId = USER_ID;
		String displayName = "auser";
		String challengeName = "syn101";
		String challengeWebLink = CHALLENGE_END_POINT + "syn101";
		String teamId = TEAM_ID;
		String teamName = "test team";
		String expected = "<html style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-size: 10px;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\">\r\n" +
				"  <body style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;font-size: 14px;line-height: 1.42857143;color: #333333;background-color: #ffffff;\">\r\n" +
				"    <div style=\"margin: 10px;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;\">\r\n" +
				"      <p style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0 0 10px;margin-bottom: 20px;font-size: 16px;font-weight: 300;line-height: 1.4;\">Hello,</p>\r\n" +
				"      <p style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0 0 10px;\">\r\n" +
				"        <strong style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-weight: bold;\"><a href=\"https://www.synapse.org/#!Profile:" + userId + "\">" + displayName + "</a></strong>\r\n" +
				"        has created a submission to\r\n" +
				"        <strong style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-weight: bold;\">" + challengeName + "</strong>\r\n" +
				"        (" + evalQueueName + ")\r\n" +
				"        on behalf of\r\n" +
				"        <strong style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;font-weight: bold;\"><a href=\"https://www.synapse.org/#!Team:" + teamId + "\">" + teamName + "</a></strong>.\r\n" +
				"      </p>\r\n" +
				"      <p style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0 0 10px;\">For further information please <a href=\"" + challengeWebLink + "\" style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;background-color: transparent;color: #337ab7;text-decoration: none;\">visit the challenge page</a>.</p>\r\n" +
				"      <br style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;\">\r\n" +
				"      <p style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0 0 10px;\">\r\n" +
				"        Sincerely,\r\n" +
				"      </p>\r\n" +
				"      <p style=\"-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;margin: 0 0 10px;\">\r\n" +
				"        <img src=\"https://s3.amazonaws.com/static.synapse.org/images/SynapseLogo2.png\" style=\"display: inline;width: 40px;height: 40px;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;border: 0;vertical-align: middle;\"> Synapse Administration\r\n" +
				"      </p>\r\n" +
				"    </div>\r\n" +
				"  </body>\r\n" +
				"</html>\r\n";
		assertEquals(expected, body);
	}

	@Test
	public void testCreateSubmissionNotification_Individual() throws Exception {
		// check that when it's not a team submission no notification is created
		sub.setTeamId(null);
		List<MessageToUserAndBody> result =
				submissionManager.createSubmissionNotifications(userInfo, sub, null,
						CHALLENGE_END_POINT,
						NOTIFICATION_UNSUBSCRIBE_END_POINT);
		assertTrue(result.isEmpty());

	}

	@Test
	public void testBuildSubmissionNotificationBody() {
		String to = "Test team";
		String messageContent = "Content";
		String notificationUnsubscribeEndpoint = "someUnsubscribeEndpoint";
		String recipient1 = "recipient1";
		String recipient2 = "recipient2";

		Set<String> recipients = ImmutableSet.of(recipient1, recipient2);

		// Call under test
		List<MessageToUserAndBody> result = submissionManager.buildSubmissionNotificationMessages(to, recipients, messageContent, notificationUnsubscribeEndpoint);

		assertEquals(2, result.size());

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

		resultRecipients.addAll(result.get(0).getMetadata().getRecipients());
		resultRecipients.addAll(result.get(1).getMetadata().getRecipients());

		assertEquals(recipients, resultRecipients);
	}

	@Test
	public void testProcessCancelRequestWithInvalidUserInfo() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.processUserCancelRequest(null, subWithId.getId());
		});
	}

	@Test
	public void testProcessCancelRequestWithNullId() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			submissionManager.processUserCancelRequest(userInfo, null);
		});
	}

	@Test
	public void testProcessCancelRequestWithUnauthorizedUser() {
		when(mockSubmissionDAO.getCreatedBy(subWithId.getId())).thenReturn(USER_ID);
		UserInfo unauthorizedUser = new UserInfo(false);
		unauthorizedUser.setId(Long.parseLong(USER_ID+1));
		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.processUserCancelRequest(unauthorizedUser, subWithId.getId());
		});
	}

	@Test
	public void testProcessCancelRequestWithAuthorizedUserNonCancellable() {
    	when(mockSubmissionStatusDAO.get(eq(SUB_ID))).thenReturn(subStatus);
		when(mockSubmissionDAO.getCreatedBy(subWithId.getId())).thenReturn(USER_ID);
		when(mockSubmissionDAO.getCreatedBy(subWithId.getId())).thenReturn(USER_ID);
		UserInfo authorizedUser = new UserInfo(false);
		authorizedUser.setId(Long.parseLong(USER_ID));

		Assertions.assertThrows(UnauthorizedException.class, ()-> {
			submissionManager.processUserCancelRequest(authorizedUser, subWithId.getId());
		});
	}

	@Test
	public void testProcessCancelRequestWithAuthorizedUserCancellable() {
		when(mockSubmissionDAO.get(eq(SUB_ID))).thenReturn(subWithId);
    	when(mockSubmissionStatusDAO.get(eq(SUB_ID))).thenReturn(subStatus);
		when(mockSubmissionDAO.getCreatedBy(subWithId.getId())).thenReturn(USER_ID);
		subStatus.setCanCancel(true);
		UserInfo authorizedUser = new UserInfo(false);
		authorizedUser.setId(Long.parseLong(USER_ID));

		submissionManager.processUserCancelRequest(authorizedUser, subWithId.getId());

		verify(mockSubmissionDAO).getCreatedBy(subWithId.getId());
		verify(mockSubmissionStatusDAO).get(subWithId.getId());
		subStatus.setCancelRequested(true);
		verify(mockSubmissionStatusDAO).update(Arrays.asList(subStatus));
		verify(mockSubmissionDAO).get(subWithId.getId());
		verify(mockEvaluationSubmissionsDAO).updateEtagForEvaluation(EVAL_ID_LONG, true, ChangeType.UPDATE);
	}

	// tests of limit and offset checks

	@Test
	public void testGetAllSubmissionsLimitLow() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissions(ownerInfo, null, SubmissionStatusEnum.OPEN, -1, 0);
		});
	}

	@Test
	public void testGetAllSubmissionsLimitHigh() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissions(ownerInfo, null, SubmissionStatusEnum.OPEN, 101, 0);
		});
	}

	@Test
	public void testGetAllSubmissionsOffsetNeg() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissions(ownerInfo, null, SubmissionStatusEnum.OPEN, 100, -1);
		});
	}

	@Test
	public void testGetAllSubmissionBundlesLimitLow() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionBundles(ownerInfo, null, SubmissionStatusEnum.OPEN, -1, 0);
		});
	}

	@Test
	public void testGetAllSubmissionBundlesLimitHigh() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionBundles(ownerInfo, null, SubmissionStatusEnum.OPEN, 101, 0);
		});
	}

	@Test
	public void testGetAllSubmissionBundlesOffsetNeg() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionBundles(ownerInfo, null, SubmissionStatusEnum.OPEN, 100, -1);
		});
	}

	@Test
	public void testGetAllSubmissionStatusesLimitLow() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionStatuses(ownerInfo, null, SubmissionStatusEnum.OPEN, -1, 0);
		});
	}

	@Test
	public void testGetAllSubmissionStatusesLimitHigh() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionStatuses(ownerInfo, null, SubmissionStatusEnum.OPEN, 101, 0);
		});
	}

	@Test
	public void testGetAllSubmissionStatusesOffsetNeg() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getAllSubmissionStatuses(ownerInfo, null, SubmissionStatusEnum.OPEN, 100, -1);
		});
	}

	@Test
	public void testgetMyOwnSubmissionsByEvaluationLimitLow() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionsByEvaluation(ownerInfo, null, -1, 0);
		});
	}

	@Test
	public void testgetMyOwnSubmissionsByEvaluationLimitHigh() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionsByEvaluation(ownerInfo, null, 101, 0);
		});
	}

	@Test
	public void testgetMyOwnSubmissionsByEvaluationOffsetNeg() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionsByEvaluation(ownerInfo, null, 100, -1);
		});
	}

	@Test
	public void testgetMyOwnSubmissionBundlesByEvaluationLimitLow() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionBundlesByEvaluation(ownerInfo, null, -1, 0);
		});
	}

	@Test
	public void testgetMyOwnSubmissionBundlesByEvaluationLimitHigh() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionBundlesByEvaluation(ownerInfo, null, 101, 0);
		});
	}

	@Test
	public void testgetMyOwnSubmissionBundlesByEvaluationOffsetNeg() {
		Assertions.assertThrows(IllegalArgumentException.class, ()-> {
			// Call under test
			submissionManager.getMyOwnSubmissionBundlesByEvaluation(ownerInfo, null, 100, -1);
		});
	}

	@Test
	public void testValidateContentWithEmptyAnnotationsV2() {

		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		subStatus.setAnnotations(annos.getFirst());

		// Call under test
		SubmissionManagerImpl.validateContent(subStatus, EVAL_ID);

		// Verifies that the submission annotations were not set
		assertNull(subStatus.getSubmissionAnnotations());

	}

	@Test
	public void testValidateContentWithoutAnnotationV1AndWithAnnotationsV2() {

		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		subStatus.setAnnotations(null);
		subStatus.setSubmissionAnnotations(annos.getSecond());

		// Call under test
		SubmissionManagerImpl.validateContent(subStatus, EVAL_ID);

		assertNull(subStatus.getAnnotations());
		assertEquals(annos.getSecond(), subStatus.getSubmissionAnnotations());

	}

	@Test
	public void testValidateContentWithAnnotationV1AndWithAnnotationsV2() {

		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		org.sagebionetworks.repo.model.annotation.v2.Annotations annotationsV2 = annos.getSecond();

		AnnotationsV2TestUtils.putAnnotations(annotationsV2, "additional", "addionalValue", AnnotationsValueType.STRING);

		subStatus.setAnnotations(annos.getFirst());
		subStatus.setSubmissionAnnotations(annotationsV2);

		// Call under test
		SubmissionManagerImpl.validateContent(subStatus, EVAL_ID);

		assertEquals(annos.getFirst(), subStatus.getAnnotations());
		assertEquals(annotationsV2, subStatus.getSubmissionAnnotations());

	}

	@Test
	public void testValidateContentWithMalformedAnnotationsV2() {

		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		org.sagebionetworks.repo.model.annotation.v2.Annotations annotationsV2 = annos.getSecond();

		AnnotationsV2TestUtils.putAnnotations(annotationsV2, "additional", "wrongValue", AnnotationsValueType.LONG);

		subStatus.setSubmissionAnnotations(annotationsV2);

		String errorMessage = assertThrows(IllegalArgumentException.class, ()->{
			// Call under test
			SubmissionManagerImpl.validateContent(subStatus, EVAL_ID);
		}).getMessage();

		assertEquals("Value associated with key=additional is not valid for type=LONG: wrongValue", errorMessage);

	}

	@Test
	public void testRemovePrivateAnnotationsFromBundle() {
		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		subStatus.setSubmissionAnnotations(annos.getSecond());

		submissionBundle.setSubmissionStatus(subStatus);

		// Call under test
		SubmissionManagerImpl.removePrivateAnnotations(submissionBundle);

		assertNotPrivate(subStatus.getAnnotations());
		assertNull(subStatus.getSubmissionAnnotations());
	}

	@Test
	public void testRemovePrivateAnnotationsFromStatus() {
		Pair<Annotations, org.sagebionetworks.repo.model.annotation.v2.Annotations> annos = createDummyAnnotations();

		subStatus.setSubmissionAnnotations(annos.getSecond());

		// Call under test
		SubmissionManagerImpl.removePrivateAnnotations(subStatus);

		assertNotPrivate(subStatus.getAnnotations());
		assertNull(subStatus.getSubmissionAnnotations());
	}

	private static void assertNotPrivate(Annotations annos) {
		assertNotPrivate(annos.getStringAnnos());
		assertNotPrivate(annos.getLongAnnos());
		assertNotPrivate(annos.getDoubleAnnos());
	}

	private static void assertNotPrivate(List<? extends AnnotationBase> annos) {
		annos.forEach( anno -> {
			assertFalse(anno.getIsPrivate());
		});
	}

	private static MessageToSend getSubmissionMessage(UserInfo user, String submissionId, ChangeType changeType) {
		return new MessageToSend()
				.withObjectType(ObjectType.SUBMISSION)
				.withChangeType(changeType)
				.withObjectId(submissionId)
				.withUserId(user.getId());
	}

}


text:
//////////////////////////////////////////////
/// 2DxFX - 2D SPRITE FX - by VETASOFT 2015 //
//////////////////////////////////////////////

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
[AddComponentMenu ("2DxFX/Standard/Fire")]
[System.Serializable]
public class _2dxFX_Fire : MonoBehaviour
{
	[HideInInspector] public Material ForceMaterial;
	[HideInInspector] public bool ActiveChange=true;
	private string shader = "2DxFX/Standard/Fire";
	[HideInInspector] [Range(0, 1)] public float _Alpha = 1f;

	[HideInInspector] public Texture2D __MainTex2;
	[HideInInspector] [Range(64, 256)] public float _Value1=64;
	[HideInInspector] [Range(0, 2)] public float _Value2=1;
	[HideInInspector] [Range(0, 1)] public float _Value3=1;
	[HideInInspector] public float _Value4;

	[HideInInspector] public bool _AutoScrollX;
	[HideInInspector] [Range(0, 10)]public float _AutoScrollSpeedX;
	[HideInInspector] public bool _AutoScrollY;
	[HideInInspector] [Range(0, 10)] public float _AutoScrollSpeedY;
	[HideInInspector]  private float _AutoScrollCountX;
	[HideInInspector]  private float _AutoScrollCountY;

	[HideInInspector] public int ShaderChange=0;
	Material tempMaterial;
	Material defaultMaterial;
	Image CanvasImage;

	void Awake()
	{
		if (this.gameObject.GetComponent<Image> () != null)
		{
			CanvasImage = this.gameObject.GetComponent<Image> ();
		}
	}
	void Start ()
	{
		__MainTex2 = Resources.Load ("_2dxFX_FireTXT") as Texture2D;
		ShaderChange = 0;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
					this.GetComponent<Renderer>().sharedMaterial.SetTexture ("_MainTex2", __MainTex2);
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
					CanvasImage.material.SetTexture ("_MainTex2", __MainTex2);
			}
	}

 	public void CallUpdate()
	{
		Update ();
	}

	void Update()
	{
		if (this.gameObject.GetComponent<Image> () != null)
		{
			if (CanvasImage==null) CanvasImage = this.gameObject.GetComponent<Image> ();
		}
		if ((ShaderChange == 0) && (ForceMaterial != null))
		{
			ShaderChange=1;
			if (tempMaterial!=null) DestroyImmediate(tempMaterial);
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = ForceMaterial;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = ForceMaterial;
			}
			ForceMaterial.hideFlags = HideFlags.None;
			ForceMaterial.shader=Shader.Find(shader);

		}
		if ((ForceMaterial == null) && (ShaderChange==1))
		{
			if (tempMaterial!=null) DestroyImmediate(tempMaterial);
			tempMaterial = new Material(Shader.Find(shader));
			tempMaterial.hideFlags = HideFlags.None;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = tempMaterial;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = tempMaterial;
			}
			ShaderChange=0;
		}

		#if UNITY_EDITOR
		string dfname = "";
		if(this.gameObject.GetComponent<SpriteRenderer>() != null) dfname=this.GetComponent<Renderer>().sharedMaterial.shader.name;
		if(this.gameObject.GetComponent<Image>() != null)
		{
			Image img = this.gameObject.GetComponent<Image>();
			if (img.material==null)	dfname="Sprites/Default";
		}
		if (dfname == "Sprites/Default")
		{
			ForceMaterial.shader=Shader.Find(shader);
			ForceMaterial.hideFlags = HideFlags.None;
			__MainTex2 = Resources.Load ("_2dxFX_FireTXT") as Texture2D;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = ForceMaterial;
				this.GetComponent<Renderer>().sharedMaterial.SetTexture ("_MainTex2", __MainTex2);
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
			Image img = this.gameObject.GetComponent<Image>();
				if (img.material==null)
				{
				CanvasImage.material = ForceMaterial;
				CanvasImage.material.SetTexture ("_MainTex2", __MainTex2);
				}
			}

		}
		#endif
		if (ActiveChange)
		{
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
			this.GetComponent<Renderer>().sharedMaterial.SetFloat("_Alpha", 1-_Alpha);
			this.GetComponent<Renderer>().sharedMaterial.SetFloat("_Value1",_Value1);
			this.GetComponent<Renderer>().sharedMaterial.SetFloat("_Value2",_Value2);
			this.GetComponent<Renderer>().sharedMaterial.SetFloat("_Value3",_Value3);
			this.GetComponent<Renderer>().sharedMaterial.SetFloat("_Value4",_Value4);
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material.SetFloat("_Alpha", 1-_Alpha);
				CanvasImage.material.SetFloat("_Value1",_Value1);
				CanvasImage.material.SetFloat("_Value2",_Value2);
				CanvasImage.material.SetFloat("_Value3",_Value3);
				CanvasImage.material.SetFloat("_Value4",_Value4);
			}

		}

	}

	void OnDestroy()
	{
	if (this.gameObject.GetComponent<Image> () != null)
		{
			if (CanvasImage==null) CanvasImage = this.gameObject.GetComponent<Image> ();
		}
		if ((Application.isPlaying == false) && (Application.isEditor == true)) {

			if (tempMaterial!=null) DestroyImmediate(tempMaterial);

			if (gameObject.activeSelf && defaultMaterial!=null) {
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = defaultMaterial;
				this.GetComponent<Renderer>().sharedMaterial.hideFlags = HideFlags.None;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = defaultMaterial;
				CanvasImage.material.hideFlags = HideFlags.None;
			}
		}
		}
	}
	void OnDisable()
	{
	if (this.gameObject.GetComponent<Image> () != null)
		{
			if (CanvasImage==null) CanvasImage = this.gameObject.GetComponent<Image> ();
		}
		if (gameObject.activeSelf && defaultMaterial!=null) {
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = defaultMaterial;
				this.GetComponent<Renderer>().sharedMaterial.hideFlags = HideFlags.None;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = defaultMaterial;
				CanvasImage.material.hideFlags = HideFlags.None;
			}
		}
	}

	void OnEnable()
	{
		if (this.gameObject.GetComponent<Image> () != null)
		{
			if (CanvasImage==null) CanvasImage = this.gameObject.GetComponent<Image> ();
		}

		if (defaultMaterial == null) {
			defaultMaterial = new Material(Shader.Find("Sprites/Default"));

		}
		if (ForceMaterial==null)
		{
			ActiveChange=true;
			tempMaterial = new Material(Shader.Find(shader));
			tempMaterial.hideFlags = HideFlags.None;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = tempMaterial;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = tempMaterial;
			}
			__MainTex2 = Resources.Load ("_2dxFX_FireTXT") as Texture2D;
		}
		else
		{
			ForceMaterial.shader=Shader.Find(shader);
			ForceMaterial.hideFlags = HideFlags.None;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
				this.GetComponent<Renderer>().sharedMaterial = ForceMaterial;
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
				CanvasImage.material = ForceMaterial;
			}
			__MainTex2 = Resources.Load ("_2dxFX_FireTXT") as Texture2D;
		}

		if (__MainTex2)
		{
			__MainTex2.wrapMode= TextureWrapMode.Repeat;
			if(this.gameObject.GetComponent<SpriteRenderer>() != null)
			{
					this.GetComponent<Renderer>().sharedMaterial.SetTexture ("_MainTex2", __MainTex2);
			}
			else if(this.gameObject.GetComponent<Image>() != null)
			{
					CanvasImage.material.SetTexture ("_MainTex2", __MainTex2);
			}

		}
	}

}

#if UNITY_EDITOR
[CustomEditor(typeof(_2dxFX_Fire)),CanEditMultipleObjects]
public class _2dxFX_Fire_Editor : Editor
{
	private SerializedObject m_object;

	public void OnEnable()
	{

		m_object = new SerializedObject(targets);
	}

	public override void OnInspectorGUI()
	{
		m_object.Update();
		DrawDefaultInspector();

		_2dxFX_Fire _2dxScript = (_2dxFX_Fire)target;

		Texture2D icon = Resources.Load ("2dxfxinspector-anim") as Texture2D;
		if (icon)
		{
			Rect r;
			float ih=icon.height;
			float iw=icon.width;
			float result=ih/iw;
			float w=Screen.width;
			result=result*w;
			r = GUILayoutUtility.GetRect(ih, result);
			EditorGUI.DrawTextureTransparent(r,icon);
		}

		EditorGUILayout.PropertyField(m_object.FindProperty("ForceMaterial"), new GUIContent("Shared Material", "Use a unique material, reduce drastically the use of draw call"));

		if (_2dxScript.ForceMaterial == null)
		{
			_2dxScript.ActiveChange = true;
		}
		else
		{
			if(GUILayout.Button("Remove Shared Material"))
			{
				_2dxScript.ForceMaterial= null;
				_2dxScript.ShaderChange = 1;
				_2dxScript.ActiveChange = true;
				_2dxScript.CallUpdate();
			}

			EditorGUILayout.PropertyField (m_object.FindProperty ("ActiveChange"), new GUIContent ("Change Material Property", "Change The Material Property"));
		}

		if (_2dxScript.ActiveChange)
		{

			EditorGUILayout.BeginVertical("Box");

			Texture2D icone = Resources.Load ("2dxfx-icon-time") as Texture2D;
			EditorGUILayout.PropertyField(m_object.FindProperty("_Value1"), new GUIContent("Fire Speed", icone, "Change the fire speed"));

			icone = Resources.Load ("2dxfx-icon-brightness") as Texture2D;
			EditorGUILayout.PropertyField(m_object.FindProperty("_Value2"), new GUIContent("Fire Intensity", icone, "Change the fire light intensity"));

			EditorGUILayout.BeginVertical("Box");

			icone = Resources.Load ("2dxfx-icon-fade") as Texture2D;
			EditorGUILayout.PropertyField(m_object.FindProperty("_Alpha"), new GUIContent("Fading", icone, "Fade from nothing to showing"));

			EditorGUILayout.EndVertical();
			EditorGUILayout.EndVertical();

		}

		m_object.ApplyModifiedProperties();

	}
}
#endif

redpajama_set_name: RedPajamaGithub


text:
package io.bootique.undertow.config;

import io.bootique.annotation.BQConfigProperty;

public class HttpListener {
    private Integer port;
    private String host;

    public HttpListener() {
        this.port = 8080;
    }

    public Integer getPort() {
        return port;
    }

    @BQConfigProperty
    public HttpListener setPort(Integer port) {
        this.port = port;
        return this;
    }

    public String getHost() {
        return host;
    }

    @BQConfigProperty
    public HttpListener setHost(String host) {
        this.host = host;
        return this;
    }
}


text:
//
//  main.cpp
//  socketexample
//
//  Created by Bob Polis on 16/09/14.
//  Revised by Jeroen de Haas on 22/11/2016
//  Copyright (c) 2014 Avans Hogeschool, 's-Hertogenbosch. All rights reserved.
//
#include <stdlib.h>
#include <crtdbg.h>
#include <thread>
#include <iostream>
#include <exception>
#include <memory>
#include <utility>
#include "GameLoopController.h"
#include "WaitController.h"
#include "GamelogicController.h"
#include "BuildingCardRepo.h"
#include "CharacterCardRepo.h"
#include "Socket.h"
#include "Sync_queue.h"
#include "ClientCommand.h"
#include "Player.h"
#include "ClientInfo.h"

using namespace std;

namespace machiavelli {
    const int tcp_port {1080};
}

static Sync_queue<ClientCommand> queue;
static shared_ptr<GameLoopController> gameLoopController = nullptr;

void consume_command() // runs in its own thread
{
    try {
        while (true) {
            ClientCommand command {queue.get()}; // will block here unless there are still command objects in the queue
            if (auto clientInfo = command.get_client_info().lock()) {
                auto &client = clientInfo->get_socket();
                auto &player = clientInfo->get_player();
                try {
					gameLoopController->ConsumeCommand(command);
                } catch (const exception& ex) {
                    cerr << "*** exception in consumer thread for player " << player.GetName() << ": " << ex.what() << '\n';
                    if (client.is_open()) {
                        client.write("Sorry, something went wrong during handling of your request.\r\n");
                    }
                } catch (...) {
                    cerr << "*** exception in consumer thread for player " << player.GetName() << '\n';
                    if (client.is_open()) {
                        client.write("Sorry, something went wrong during handling of your request.\r\n");
                    }
                }
            }
        }
    } catch (std::exception e) {
		cerr << e.what();
        cerr << "consume_command crashed\n";
		/* memory leaks even in the beginning of main.
		_CrtDumpMemoryLeaks();
		*/
		exit(0);
    }
}

std::shared_ptr<ClientInfo> init_client_session(Socket client) {
    client.write("Welcome to Server 1.0! To quit, type 'quit'.\r\n");
    client.write("What's your name?\r\n");
    string name {client.readline()};
	client.write("type 'Join' to try to enter the game.\r\n");
    return make_shared<ClientInfo>(move(client), Player { name });
}

void handle_client(Socket client) // this function runs in a separate thread
{
    try {
        auto client_info = init_client_session(move(client));
        auto &socket = client_info->get_socket();
        auto &player = client_info->get_player();
        while (true) { // game loop
            try {
                // read first line of request
                string cmd {socket.readline()};
                cerr << '[' << socket.get_dotted_ip() << " (" << socket.get_socket() << ") " << player.GetName() << "] " << cmd << "\r\n";

                if (cmd == "quit") {
                    socket.write("Bye!\r\n");
                    break; // out of game loop, will end this thread and close connection
                }

                ClientCommand command {cmd, client_info};
                queue.put(command);

            } catch (const exception& ex) {
                socket << "ERROR: " << ex.what() << "\r\n";
				break;
            } catch (...) {
                socket.write("ERROR: something went wrong during handling of your request. Sorry!\r\n");
				break;
            }
        }
        // close weg
    } catch (...) {
        cerr << "handle_client crashed\n";
    }
}

int main(int argc, const char * argv[])
{
	// to use current time in sec as seed to avoid bs.
	srand(time(0));

	// memory leaks even here?
	/*
	_CrtDumpMemoryLeaks();
	return 0;
	*/

	std::shared_ptr<BuildingCardRepo> bRepo = make_shared<BuildingCardRepo>("../Data/Bouwkaarten.csv");
	std::shared_ptr<CharacterCardRepo> cRepo = make_shared<CharacterCardRepo>("../Data/Karakterkaarten.csv");
	std::shared_ptr<WaitController> initialController = make_shared<WaitController>(WaitController());
	std::shared_ptr<GameLogicController> logicController = make_shared<GameLogicController>(GameLogicController(bRepo,cRepo));
	initialController->setActionController(logicController);
	gameLoopController = make_shared<GameLoopController>(GameLoopController(initialController));
	gameLoopController->Start();

    // start command consumer thread
    thread consumer {consume_command};

    // create a server socket
    ServerSocket server {machiavelli::tcp_port};

    while (true) {
        try {
            while (true) {
                // wait for connection from client; will create new socket
                cerr << "server listening" << '\n';
                Socket client {server.accept()};

                // communicate with client over new socket in separate thread
                thread handler {handle_client, move(client)};
                handler.detach(); // detaching is usually ugly, but in this case the right thing to do
            }
        } catch (const exception& ex) {
            cerr << ex.what() << ", resuming..." << '\n';
        } catch (...) {
            cerr << "problems, problems, but: keep calm and carry on!\n";
        }
    }
    consumer.join();
    return 0;
}


text:
package com.tmg.uifwk.orm.ui;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.tmg.uifwk.orm.BaseEntityAttributes;

/**
 * The persistent class for the DropDownElementItem database table.
 *
 */
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Entity
@Table(schema = "UI", name = "DropDownElementItem")
public class DropDownElementItem extends BaseEntityAttributes {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ItemID")
	private int itemID;

	@Column(name = "DisplayText")
	private String displayText;

	@Column(name = "IsActive")
	private boolean active;

	@Column(name = "Sequence")
	private int sequence;

	@Column(name = "Value")
	private String value;

	// bi-directional many-to-one association to DropDownUIElement
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "UIElementID")
	private DropDownUIElement dropDownUIElement;

	public DropDownElementItem() {
	}

	public int getItemID() {
		return this.itemID;
	}

	public void setItemID(int itemID) {
		this.itemID = itemID;
	}

	public String getDisplayText() {
		return this.displayText;
	}

	public void setDisplayText(String displayText) {
		this.displayText = displayText;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public int getSequence() {
		return this.sequence;
	}

	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	public String getValue() {
		return this.value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public DropDownUIElement getDropDownUIElement() {
		return dropDownUIElement;
	}

	public void setDropDownUIElement(DropDownUIElement dropDownUIElement) {
		this.dropDownUIElement = dropDownUIElement;
	}

}


text:
package io.subutai.core.environment.impl.workflow.creation.steps;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import io.subutai.common.environment.Topology;
import io.subutai.common.network.NetworkResourceImpl;
import io.subutai.common.network.UsedNetworkResources;
import io.subutai.common.peer.Peer;
import io.subutai.common.peer.PeerException;
import io.subutai.common.settings.Common;
import io.subutai.common.tracker.TrackerOperation;
import io.subutai.common.util.P2PUtil;
import io.subutai.common.util.PeerUtil;
import io.subutai.core.environment.api.exception.EnvironmentCreationException;
import io.subutai.core.environment.impl.entity.EnvironmentPeerImpl;
import io.subutai.core.environment.impl.entity.LocalEnvironment;
import io.subutai.core.identity.api.IdentityManager;
import io.subutai.core.peer.api.PeerManager;

/**
 * Network resources reservation step
 * TODO refactor - split into smaller methods
 */
public class ReservationStep
{
    private final Topology topology;
    private final LocalEnvironment environment;
    private final PeerManager peerManager;
    private final IdentityManager identityManager;
    private final TrackerOperation trackerOperation;
    protected PeerUtil<Object> peerUtil = new PeerUtil<>();

    public ReservationStep( final Topology topology, final LocalEnvironment environment, final PeerManager peerManager,
                            final IdentityManager identityManager, final TrackerOperation trackerOperation )
    {
        this.topology = topology;
        this.environment = environment;
        this.peerManager = peerManager;
        this.identityManager = identityManager;
        this.trackerOperation = trackerOperation;
    }

    public void execute() throws EnvironmentCreationException, PeerException
    {

        Set<Peer> peers = peerManager.resolve( topology.getAllPeers() );

        //obtain reserved net resources
        final Map<Peer, UsedNetworkResources> reservedNetResources = Maps.newConcurrentMap();

        for ( final Peer peer : peers )
        {
            peerUtil.addPeerTask( new PeerUtil.PeerTask<>( peer, new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    reservedNetResources.put( peer, peer.getUsedNetworkResources() );

                    return null;
                }
            } ) );
        }

        PeerUtil.PeerTaskResults<Object> netQueryResults = peerUtil.executeParallel();

        for ( PeerUtil.PeerTaskResult netQueryResult : netQueryResults.getResults() )
        {
            if ( netQueryResult.hasSucceeded() )
            {
                trackerOperation.addLog( String.format( "Obtained reserved network resources from peer %s",
                        netQueryResult.getPeer().getName() ) );
            }
            else
            {
                trackerOperation.addLog(
                        String.format( "Failed to obtain reserved network resources from peer %s. Reason: %s",
                                netQueryResult.getPeer().getName(), netQueryResult.getFailureReason() ) );
            }
        }

        if ( netQueryResults.hasFailures() )
        {
            throw new EnvironmentCreationException( "Failed to obtain reserved network resources from all peers" );
        }

        Set<String> allP2pSubnets = Sets.newHashSet();
        Set<String> allContainerSubnets = Sets.newHashSet();
        Set<Long> allVnis = Sets.newHashSet();

        for ( UsedNetworkResources netResources : reservedNetResources.values() )
        {
            allContainerSubnets.addAll( netResources.getContainerSubnets() );
            allP2pSubnets.addAll( netResources.getP2pSubnets() );
            allVnis.addAll( netResources.getVnis() );
        }

        //calculate free container subnet
        final String freeContainerSubnet = P2PUtil.generateContainerSubnet( allContainerSubnets );

        if ( freeContainerSubnet == null )
        {
            throw new EnvironmentCreationException( "Free container subnet not found" );
        }

        //calculate free p2p subnet
        final String freeP2pSubnet = P2PUtil.generateP2PSubnet( allP2pSubnets );

        if ( freeP2pSubnet == null )
        {
            throw new EnvironmentCreationException( "Free p2p subnet not found" );
        }

        //calculate free vni
        final Long freeVni = generateRandomVni( allVnis );

        if ( freeVni == -1 )
        {
            throw new EnvironmentCreationException( "Free VNI not found" );
        }

        //reserve network resources

        for ( final Peer peer : peers )
        {
            peerUtil.addPeerTask( new PeerUtil.PeerTask<>( peer, new Callable<Object>()
            {
                @Override
                public Integer call() throws Exception
                {
                    NetworkResourceImpl networkResource =
                            new NetworkResourceImpl( environment.getId(), freeVni, freeP2pSubnet, freeContainerSubnet,
                                    peerManager.getLocalPeer().getId(), identityManager.getActiveUser().getUserName(),
                                    identityManager.getActiveUser().getSecurityKeyId());

                    return peer.reserveNetworkResource( networkResource );
                }
            } ) );
        }

        PeerUtil.PeerTaskResults<Object> netReservationResults = peerUtil.executeParallel();

        for ( PeerUtil.PeerTaskResult<Object> netReservationResult : netReservationResults.getResults() )
        {
            if ( netReservationResult.hasSucceeded() )
            {
                trackerOperation.addLog( String.format( "Reserved network resources on peer %s",
                        netReservationResult.getPeer().getName() ) );

                environment.addEnvironmentPeer( new EnvironmentPeerImpl( netReservationResult.getPeer().getId(),
                        ( Integer ) netReservationResult.getResult() ) );
            }
            else
            {
                trackerOperation.addLog( String.format( "Failed to reserve network resources on peer %s. Reason: %s",
                        netReservationResult.getPeer().getName(), netReservationResult.getFailureReason() ) );
            }
        }

        if ( netReservationResults.hasFailures() )
        {
            throw new EnvironmentCreationException( "Failed to reserve network resources on all peers" );
        }

        //store network data in environment metadata
        environment.setVni( freeVni );
        environment.setP2PSubnet( freeP2pSubnet );
        environment.setSubnetCidr( String.format( "%s/24", freeContainerSubnet ) );
    }

    private long generateRandomVni( Set<Long> excludedVnis )
    {
        int maxIterations = 10000;
        int currentIteration = 0;
        long vni;

        do
        {
            vni = ( long ) ( Math.random() * ( Common.MAX_VNI_ID - Common.MIN_VNI_ID ) ) + Common.MIN_VNI_ID;
            currentIteration++;
        }
        while ( excludedVnis.contains( vni ) && currentIteration < maxIterations );

        if ( excludedVnis.contains( vni ) )
        {
            return -1;
        }

        return vni;
    }
}


text:
<?php

namespace Sygmaa\Grids\Fields;
use Illuminate\Http\Request;

/**
 * Class Date
 * @package Sygmaa\Grids\Fields
 */
class Date extends Field
{
    /**
     * @var
     */
    protected $formatting;

    public function __construct(Request $request, $name, $label, $formatting)
    {
        parent::__construct($request, $name, $label);
        $this->formatting = $formatting;
    }

    /**
     * @return mixed
     */
    public function getFormatting()
    {
        return $this->formatting;
    }

    /**
     * @param $data
     * @return mixed
     */
    function render($row)
    {
        $nameField = $this->getName();
        if(isset($row->$nameField)) {
            $datetime = new \DateTime($row->$nameField);
            return $datetime->format($this->formatting);
        }
        return "";
    }

    /**
     * @return $this
     */
    function renderFilter()
    {
        return view('grids::fields.date')
            ->with('request', $this->request)
            ->with('field', $this);
    }

    /**
     * @return mixed
     */
    function getFilters($model)
    {
        $start = $this->request->input($this->getName()."Start");
        $end   = $this->request->input($this->getName()."End");

        if($this->validateDate($start) && $this->validateDate($end)) {
            return $model
                ->where($this->getName(), '>=', new \DateTime($start))
                ->where($this->getName(), '<=', with(new \DateTime($end))->add(new \DateInterval('P1D')));
        }
        return $model;
    }

    /**
     * @param $date
     * @return bool
     */
    private function validateDate($date)
    {
        $d = \DateTime::createFromFormat('Y-m-d', $date);
        return $d && $d->format('Y-m-d') == $date;
    }
}


text:
{% extends "base.html" %}
{% block content %}
Thank you for your submission.
{% endblock %}


text:
/*** central column on page ***/
div#divContainer
{
text-align:center;
margin: 0 auto;
font-family: Calibri;
padding: 0.5em 0.5em 0.5em 0.5em;

/* rounded corners */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;

/* add gradient */
background-color: #BF8F30;
background: -webkit-gradient(linear, left top, left bottom, from(#BF8F30), to(#FFBF40));
background: -moz-linear-gradient(top, #BF8F30, #FFBF40);

/* add box shadows */
-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
}

input#searchBox,select { width:150px; height:25px;}
input#searchSubmit { width:70px; height:25px; }

h1 {color:#dadada; font-size:1.5em;}
h2 {color:#dadada; font-size:1.0em;}

/*** event table formatting ***/
table.eventTable {
width: 100%;
border-collapse:collapse;
text-align:left;
color: #606060;
}

/*** table's thead section, head row style ***/
table.eventTable thead tr td {
background-color: White;
vertical-align:middle;
padding: 0.6em;
font-size:0.8em;
}

/*** table's thead section, column header style ***/
table.eventTable thead tr th
{
padding: 0.5em;
/* add gradient */
background-color: #BF8F30;
background: -webkit-gradient(linear, left top, left bottom, from(#BF8F30), to(#FFBF40));
background: -moz-linear-gradient(top, #BF8F30, #FFBF40);
color: #A66F00;
}

/*** table's tbody section, odd rows style ***/
table.eventTable tbody tr:nth-child(odd) {
background-color: #fafafa;
}

/*** hover effect to table's tbody odd rows ***/
table.eventTable tbody tr:nth-child(odd):hover
{
cursor:pointer;
/* add gradient */
background-color: #BF8F30;
background: -webkit-gradient(linear, left top, left bottom, from(#BF8F30), to(#FFBF40));
background: -moz-linear-gradient(top, #BF8F30, #FFBF40);
color: #dadada;
}

/*** table's tbody section, even rows style ***/
table.eventTable tbody tr:nth-child(even) {
background-color: #efefef;
}

/*** hover effect to apply to table's tbody section, even rows ***/
table.eventTable tbody tr:nth-child(even):hover
{
cursor:pointer;
/* add gradient */
background-color: #BF8F30;
background: -webkit-gradient(linear, left top, left bottom, from(#BF8F30), to(#FFBF40));
background: -moz-linear-gradient(top, #BF8F30, #FFBF40);
color: #dadada;
}

/*** table's tbody section, last row style ***/
table.eventTable tbody tr:last-child {
border-bottom: solid 1px #404040;
}

/*** table's tbody section, separator row pseudo-class ***/
table.eventTable tbody tr.separator {
/* add gradient */
background-color: #BF8F30;
background: -webkit-gradient(linear, left top, left bottom, from(#BF8F30), to(#FFBF40));
background: -moz-linear-gradient(top, #BF8F30, #FFBF40);
color: #dadada;
}

/*** table's td element, all section ***/
table.eventTable td {
vertical-align:middle;
padding: 0.5em;
}

/*** table's tfoot section ***/
table.eventTable tfoot{
text-align:left;
color:#A66F00;
text-shadow: 0 1px 1px rgba(255,255,255,0.3);
}


text:
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var core_1 = require("@angular/core");
var platform_browser_1 = require("@angular/platform-browser");
var table_component_1 = require("./../items/table/table.component");
var DevicesModule = (function () {
    function DevicesModule() {
    }
    return DevicesModule;
}());
DevicesModule = __decorate([
    core_1.NgModule({
        imports: [platform_browser_1.BrowserModule, table_component_1.TableComponent],
        declarations: [table_component_1.TableComponent]
    })
], DevicesModule);
exports.DevicesModule = DevicesModule;
//# sourceMappingURL=devices.module.js.map


text:
package info.novatec.testit.webtester.conditions.pagefragments;

import static org.assertj.core.api.Assertions.assertThat;
import static utils.unit.MockFactory.fragment;

import org.junit.Test;

import info.novatec.testit.webtester.pagefragments.PageFragment;

public class InteractableTest {

    Interactable cut = new Interactable();

    @Test
    public void presentEnabledVisiblePageFragmentEvaluatesToTrue() {
        PageFragment fragment = fragment().present().enabled().visible().build();
        assertThat(cut.test(fragment)).isTrue();
    }

    @Test
    public void notPresentEnabledVisiblePageFragmentEvaluatesToFalse() {
        PageFragment fragment = fragment().notPresent().enabled().visible().build();
        assertThat(cut.test(fragment)).isFalse();
    }

    @Test
    public void presentDisabledVisiblePageFragmentEvaluatesToFalse() {
        PageFragment fragment = fragment().present().disabled().visible().build();
        assertThat(cut.test(fragment)).isFalse();
    }

    @Test
    public void presentEnabledInvisiblePageFragmentEvaluatesToFalse() {
        PageFragment fragment = fragment().present().enabled().invisible().build();
        assertThat(cut.test(fragment)).isFalse();
    }

    @Test
    public void toStringIsGeneratedCorrectly() {
        assertThat(cut).hasToString("interactable");
    }

}


text:
package TasksAndSyncs.typesystem;

/*Generated by MPS */

import jetbrains.mps.lang.typesystem.runtime.AbstractNonTypesystemRule_Runtime;
import jetbrains.mps.lang.typesystem.runtime.NonTypesystemRule_Runtime;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.typesystem.inference.TypeCheckingContext;
import jetbrains.mps.lang.typesystem.runtime.IsApplicableStatus;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.errors.messageTargets.MessageTarget;
import jetbrains.mps.errors.messageTargets.NodeMessageTarget;
import jetbrains.mps.errors.IErrorReporter;
import jetbrains.mps.smodel.SModelUtil_new;

public class checkNamedResourceRefForLValue_NonTypesystemRule extends AbstractNonTypesystemRule_Runtime implements NonTypesystemRule_Runtime {
  public checkNamedResourceRefForLValue_NonTypesystemRule() {
  }

  public void applyRule(final SNode syncResourceRef, final TypeCheckingContext typeCheckingContext, IsApplicableStatus status) {
    if (SNodeOperations.isInstanceOf(SNodeOperations.getParent(syncResourceRef), "com.mbeddr.core.pointers.structure.ReferenceExpr") || SNodeOperations.isInstanceOf(SNodeOperations.getParent(syncResourceRef), "com.mbeddr.core.expressions.structure.AssignmentExpr") && SLinkOperations.getTarget(SNodeOperations.cast(SNodeOperations.getParent(syncResourceRef), "com.mbeddr.core.expressions.structure.AssignmentExpr"), "left", true) == syncResourceRef) {
      {
        MessageTarget errorTarget = new NodeMessageTarget();
        IErrorReporter _reporter_2309309498 = typeCheckingContext.reportTypeError(syncResourceRef, "named resource may not be used as an lvalue", "r:daf934de-3466-4fa8-a227-270fedb7e2f2(TasksAndSyncs.typesystem)", "5721544581294116185", null, errorTarget);
      }
    }
  }

  public String getApplicableConceptFQName() {
    return "TasksAndSyncs.structure.SyncResourceRef";
  }

  public IsApplicableStatus isApplicableAndPattern(SNode argument) {
    {
      boolean b = SModelUtil_new.isAssignableConcept(argument.getConcept().getQualifiedName(), this.getApplicableConceptFQName());
      return new IsApplicableStatus(b, null);
    }
  }

  public boolean overrides() {
    return false;
  }
}