/*
 * Created on Nov 24, 2004
 * Copyright (C) Andrea Schweer, 2004
 *
 * This file is part of the Greenstone Alerting Service.
 * Refer to the COPYING file in the base directory of this package
 * for licensing information.
 */
package org.greenstone.gsdlas;

import java.io.StringWriter;
import java.util.Map;
import java.util.TreeMap;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.*;

import junit.framework.TestCase;

/**
 * @author schweer
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AlertingServiceTest extends TestCase {

    private AlertingService as;
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(AlertingServiceTest.class);
    }

    public final void testSubscribe() {
        try {
            Velocity.init("/research/schweer/gsdl3/extensions/gsdl-as/build/velocity.properties");
            VelocityContext context = new VelocityContext();
            
            Map arguments = new TreeMap();
            arguments.put(Constants.TYPE_FIELD, "new_document");
            arguments.put(Constants.COLLECTION_ID_FIELD, "testing");
            arguments.put(Constants.DOCUMENT_CONTENT_FIELD, "albatros");
            
            String template = as.createSubscription(arguments, context);
            
            StringWriter w = new StringWriter();
            boolean b = Velocity.mergeTemplate(template, 
                    org.apache.velocity.runtime.RuntimeSingleton.getString(Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 
                    context, 
                    w);
            assertTrue(b);
            System.out.println(w);
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
            fail("no template");
        } catch (ParseErrorException e) {
            e.printStackTrace();
            fail();
        } catch (MethodInvocationException e) {
            e.printStackTrace();
            fail();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    public final void testListSubscriptions() {
        try {
            Velocity.init("/research/schweer/gsdl3/extensions/gsdl-as/build/velocity.properties");
            VelocityContext context = new VelocityContext();
            
            Map arguments = new TreeMap();
            
            String template = as.listSubscriptions(arguments, context);
            
            StringWriter w = new StringWriter();
            boolean b = Velocity.mergeTemplate(template, 
                    org.apache.velocity.runtime.RuntimeSingleton.getString(Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 
                    context, 
                    w);
            assertTrue(b);
            System.out.println(w);
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
            fail("no template");
        } catch (ParseErrorException e) {
            e.printStackTrace();
            fail();
        } catch (MethodInvocationException e) {
            e.printStackTrace();
            fail();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    public final void testReceiveEvent() {
        try {
            Velocity.init("/research/schweer/gsdl3/extensions/gsdl-as/build/velocity.properties");
            VelocityContext context = new VelocityContext();
            
            Map arguments = new TreeMap();
            arguments.put(Constants.COLLECTION_ID_FIELD, "testing");
            arguments.put(Constants.TYPE_FIELD, "new_document");
            arguments.put(Constants.DOCUMENT_ID_FIELD, "20041124:1");
            arguments.put(Constants.DOCUMENT_TITLE_FIELD, "keine albatrosse in sicht");
            arguments.put(Constants.HOST_ID_FIELD, "http://localhost:8080/soap/servlet/rpcrouter");
            
            as.receiveEvent(arguments);
            
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
            fail("no template");
        } catch (ParseErrorException e) {
            e.printStackTrace();
            fail();
        } catch (MethodInvocationException e) {
            e.printStackTrace();
            fail();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    public final void testShowFeed() {
        try {
            Velocity.init("/research/schweer/gsdl3/extensions/gsdl-as/build/velocity.properties");
            VelocityContext context = new VelocityContext();
            
            Map arguments = new TreeMap();
            arguments.put("subscriptionID", "0");
            
            String template = as.showFeed(arguments, context);
            
            StringWriter w = new StringWriter();
            boolean b = Velocity.mergeTemplate(template, 
                    org.apache.velocity.runtime.RuntimeSingleton.getString(Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 
                    context, 
                    w);
            assertTrue(b);
            System.out.println(w);
        } catch (ResourceNotFoundException e) {
            e.printStackTrace();
            fail("no template");
        } catch (ParseErrorException e) {
            e.printStackTrace();
            fail();
        } catch (MethodInvocationException e) {
            e.printStackTrace();
            fail();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
    protected void setUp() throws Exception {
        as = new AlertingService();
    }
}
