package org.sagebionetworks.repo.model.dbo.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.repo.model.AuthorizationConstants;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.TrashEntity;
import org.sagebionetworks.repo.model.UserGroupDAO;
import org.sagebionetworks.repo.model.jdo.KeyFactory;
import org.sagebionetworks.repo.web.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:jdomodels-test-context.xml" })
public class DBOTrashCanDaoImplAutowiredTest {

	@Autowired
	private DBOTrashCanDao trashCanDao;

	@Autowired
	private UserGroupDAO userGroupDAO;

	private long userId;

	@Before
	public void before() throws Exception {

		userId = Long.parseLong(userGroupDAO.findGroup(
				AuthorizationConstants.BOOTSTRAP_USER_GROUP_NAME, false).getId());
		assertNotNull(userId);

		clear();

		List<TrashEntity> trashList = trashCanDao.getInRangeForUser(userId, 0L, Long.MAX_VALUE);
		assertTrue(trashList.size() == 0);
	}

	@After
	public void after() throws Exception {
		clear();
		List<TrashEntity> trashList = trashCanDao.getInRangeForUser(userId, 0L, Long.MAX_VALUE);
		assertTrue(trashList.size() == 0);
	}

	@Test
	public void testRoundTrip() throws DatastoreException, NotFoundException {

		long nodeId1 = 555L;
		long parentId1 = 5L;
		trashCanDao.create(userId, nodeId1, parentId1);
		List<TrashEntity> trashList = trashCanDao.getInRangeForUser(userId, 0L, 100L);
		assertNotNull(trashList);
		assertEquals(1, trashList.size());
		TrashEntity trash = trashList.get(0);
		assertEquals(KeyFactory.keyToString(nodeId1), trash.getEntityId());
		assertEquals(Long.toString(userId), trash.getDeletedByPrincipalId());
		assertEquals(KeyFactory.keyToString(parentId1), trash.getParentId());
		assertNotNull(trash.getDeletedOn());

		long nodeId2 = 666L;
		long parentId2 = 6L;
		trashCanDao.create(userId, nodeId2, parentId2);
		trashList = trashCanDao.getInRangeForUser(userId, 0L, 100L);
		assertNotNull(trashList);
		assertEquals(2, trashList.size());

		trashCanDao.delete(userId, nodeId1);
		trashList = trashCanDao.getInRangeForUser(userId, 0L, 100L);
		assertNotNull(trashList);
		assertEquals(1, trashList.size());
		trash = trashList.get(0);
		assertEquals(KeyFactory.keyToString(nodeId2), trash.getEntityId());
		assertEquals(Long.toString(userId), trash.getDeletedByPrincipalId());
		assertEquals(KeyFactory.keyToString(parentId2), trash.getParentId());
		assertNotNull(trash.getDeletedOn());

		trashCanDao.delete(userId, nodeId2);
		trashList = trashCanDao.getInRangeForUser(userId, 0L, 100L);
		assertNotNull(trashList);
		assertEquals(0, trashList.size());
	}

	private void clear() throws Exception {
		List<TrashEntity> trashList = trashCanDao.getInRangeForUser(userId, 0L, Long.MAX_VALUE);
		for (TrashEntity trash : trashList) {
			trashCanDao.delete(userId, KeyFactory.stringToKey(trash.getEntityId()));
		}
	}
}

@@ -473,16 +473,18 @@
 rash
+ed
 Entity;%0A
 impo
@@ -479,16 +479,16 @@
 Entity;%0A
-
 import o
@@ -1340,32 +1340,34 @@
 );%0A%0A%09%09List%3CTrash
+ed
 Entity%3E trashLis
@@ -1533,32 +1533,34 @@
 ();%0A%09%09List%3CTrash
+ed
 Entity%3E trashLis
@@ -1847,32 +1847,34 @@
 1);%0A%09%09List%3CTrash
+ed
 Entity%3E trashLis
@@ -1994,24 +1994,26 @@
 ());%0A%09%09Trash
+ed
 Entity trash
@@ -3181,24 +3181,26 @@
 %09%09List%3CTrash
+ed
 Entity%3E tras
@@ -3278,16 +3278,18 @@
 r (Trash
+ed
 Entity t

Rename TrashEntity to TrashedEntity