/*
 * Created on 26-Jan-2005
 */
package org.bodington.server.resources;

import java.security.acl.NotOwnerException;

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import org.bodington.server.BuildingServerTest;
import org.bodington.server.realm.AclEntry;
import org.bodington.server.realm.Group;
import org.bodington.server.realm.Permission;

/**
 * @author buckett
 */
public class ResourceTest extends BuildingServerTest
{
    
    public void testCheckPermission() throws BuildingServerException, NotOwnerException
    {      
        Group group = new Group();
        group.setName("test");
        group.setResourceId(resource.getPrimaryKey());
        group.save();
        
        group.addMember(user);
        
        AclEntry aclEntry = new AclEntry();
        aclEntry.addPermission(Permission.VIEW);
        aclEntry.setAcl(acl);
        aclEntry.setGroup(group);
        aclEntry.save();
        
        acl.addEntry(user, aclEntry);
        
        assertTrue(resource.checkPermission(user, Permission.VIEW));
        assertFalse(resource.checkPermission(user, Permission.EDIT));
        
        assertTrue(resource.checkPermission(group, Permission.VIEW));
        assertFalse(resource.checkPermission(group, Permission.EDIT));
        
        assertFalse(resource.checkPermission(null, Permission.VIEW));
       
    }
    
    public void testRemove() throws BuildingServerException
    {
        PrimaryKey primaryKey = resource.getPrimaryKey();
        assertNotNull(Resource.findResource(primaryKey));
        resource.remove();
        assertNull(Resource.findResource(primaryKey));
    }


}
