/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.servlet.facilities;

import java.net.MalformedURLException;
import java.net.URL;

import org.bodington.server.BuildingContext;
import org.bodington.server.BuildingServerException;
import org.bodington.server.BuildingServerTest;
import org.bodington.server.realm.PassPhrase;
import org.bodington.server.resources.BlogResource;

/**
 * @author buckett
 */
public class BlogFacilityTest extends BuildingServerTest
{
    BlogFacility blogFacility;
    BlogResource blogResource;
    
    protected void setUp() throws Exception 
    {
        super.setUp();
        blogFacility = new BlogFacility();
        blogResource = new BlogResource();
    }
    
    public void testTransformFeed() throws BuildingServerException {
        String output;
        BuildingContext.getContext().setUser(user);
        blogResource.setDisplayTitle(true);
        
        output = blogFacility.transformFeed(getClass().getResourceAsStream(
            "rss0-91.xml"), blogResource.loadTransformParams());
        assertTrue(output.indexOf("Channel Title") > 0);

        output = blogFacility.transformFeed(getClass().getResourceAsStream(
            "atom0-3.xml"), blogResource.loadTransformParams());
        assertTrue(output.indexOf("Feed Title") > 0);

    }
    
    
    
    public void testDenyAccess() throws BuildingServerException, MalformedURLException
    {
        // The user is called A
        PassPhrase passPhrase = new PassPhrase(user);
        passPhrase.setUserName("A");
        passPhrase.save();
        
        BuildingContext.getContext().setUser(user);
        assertFalse(blogFacility.denyAccess( new URL("http://othersite.com/index.rss")));
        assertFalse(blogFacility.denyAccess( new URL("http://hcdt1.oucs.ox.ac.uk/pebble/A/test.rss")));
        assertTrue(blogFacility.denyAccess( new URL("http://hcdt1.oucs.ox.ac.uk/pebble/B/test.rss")));
        assertTrue(blogFacility.denyAccess(new URL("http://hcdt1.oucs.ox.ac.uk/pebble/ABBA/test.rss")));
        assertTrue(blogFacility.denyAccess(new URL("http://hcdt1.oucs.ox.ac.uk/pebble/BBB/A.rss")));

        assertTrue(blogFacility.denyAccess(new URL("http://hcdt1.oucs.ox.ac.uk/ANOTHER/B.rss")));
    }

}
