package org.sagebionetworks.repo.model.jdo;

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

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

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.UserGroupDAO;
import org.sagebionetworks.repo.model.UserGroup;
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 JDOUserGroupDAOImplTest {
	
	@Autowired
	private UserGroupDAO userGroupDAO;

	@Before
	public void setUp() throws Exception {
		cleanUpGroups();
	}

	@After
	public void tearDown() throws Exception {
		Collection<String> groupNames = new HashSet<String>();
		groupNames.add(GROUP_NAME);
		Map<String,UserGroup> map = userGroupDAO.getGroupsByNames(groupNames);
		UserGroup toDelete = map.get(GROUP_NAME);
		if (toDelete!=null) {
			userGroupDAO.delete(toDelete.getId());
		}
		cleanUpGroups();
	}
	
	public void cleanUpGroups() throws Exception {
		for (UserGroup g: userGroupDAO.getAll()) {
			if (g.getName().equals(AuthorizationConstants.ADMIN_GROUP_NAME)) {
				// leave it
			} else if (g.getName().equals(AuthorizationConstants.PUBLIC_GROUP_NAME)) {
				// leave it
			} else {
				userGroupDAO.delete(g.getId());
			}
		}
	}

	private static final String GROUP_NAME = "test-group";
	@Test
	public void testGetGroupsByNames() throws Exception {
		Collection<UserGroup> allGroups = null; 
		allGroups = userGroupDAO.getAll();
		assertEquals(allGroups.toString(), 2, allGroups.size()); // Public and Administrators
	
		Collection<String> groupNames = new HashSet<String>();
		groupNames.add(GROUP_NAME);
		Map<String,UserGroup> map = null;
		map = userGroupDAO.getGroupsByNames(groupNames);
		assertFalse(map.containsKey(GROUP_NAME));
		
		UserGroup group = new UserGroup();
		group.setName(GROUP_NAME);
		userGroupDAO.create(group);
		
		allGroups = userGroupDAO.getAll();
		assertEquals(allGroups.toString(), 3, allGroups.size()); // now the new group should be there
			
		groupNames.clear(); 	groupNames.add(GROUP_NAME);	
		map = userGroupDAO.getGroupsByNames(groupNames);
		assertTrue(groupNames.toString()+" -> "+map.toString(), map.containsKey(GROUP_NAME));
		
		
		groupNames.clear(); groupNames.add(AuthorizationConstants.ADMIN_GROUP_NAME);
		map = userGroupDAO.getGroupsByNames(groupNames);
		assertTrue(map.toString(), map.containsKey(AuthorizationConstants.ADMIN_GROUP_NAME));

	}

}

@@ -174,16 +174,63 @@
 rtTrue;%0D
+%0Aimport static org.junit.Assert.assertNotNull;%0D
 %0A%0D%0Aimpor
@@ -1733,57 +1733,311 @@
 lse 
-%7B%0D%0A%09%09%09%09userGroupDAO.delete(g.getId());%0D%0A%09%09%09%7D%0D%0A%09%09%7D
+if (g.getName().equals(AuthorizationConstants.ANONYMOUS_USER_ID)) %7B%0D%0A%09%09%09%09// leave it%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09userGroupDAO.delete(g.getId());%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void findAnonymousUser() throws Exception %7B%0D%0A%09%09assertNotNull(userGroupDAO.findGroup(AuthorizationConstants.ANONYMOUS_USER_ID, true));
 %0D%0A%09%7D
@@ -2284,9 +2284,9 @@
 (), 
-2
+3
 , al
@@ -2732,33 +2732,33 @@
 ups.toString(), 
-3
+4
 , allGroups.size

fix to broken test