package net.metadata.dataspace.atom.util;

import net.metadata.dataspace.app.Constants;
import org.apache.abdera.Abdera;
import org.apache.abdera.examples.xsltxpath.XPathExample;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.*;

import java.io.File;

/**
 * Author: alabri
 * Date: 08/11/2010
 * Time: 4:06:52 PM
 */
public class TestHelper {
    private static Abdera abdera = new Abdera();

    public static int login(HttpClient client, String username, String password) throws Exception {
        PostMethod postMethod = new PostMethod(Constants.URL_PREFIX + "login?username=" + username + "&password=" + password);
        return client.executeMethod(postMethod);
    }

    public static PostMethod postEntry(HttpClient client, String fileName, String pathForActivities) throws Exception {
        String fullURL = Constants.URL_PREFIX + pathForActivities;
        PostMethod post = new PostMethod(fullURL);
        String path = XPathExample.class.getResource(fileName).getFile();
        File inputFile = new File(path);
        RequestEntity entity = new FileRequestEntity(inputFile, Constants.ATOM_ENTRY_MIMETYPE);
        post.setRequestEntity(entity);
        client.executeMethod(post);
        return post;
    }

    public static PutMethod putEntry(HttpClient client, String fileName, String uri, String acceptHeader) throws Exception {
        PutMethod putMethod = new PutMethod(uri);
        putMethod.setRequestHeader("Accept", acceptHeader);
        String path = XPathExample.class.getResource(fileName).getFile();
        File inputFile = new File(path);
        RequestEntity entity = new FileRequestEntity(inputFile, Constants.ATOM_ENTRY_MIMETYPE);
        putMethod.setRequestEntity(entity);
        client.executeMethod(putMethod);
        return putMethod;
    }

    public static GetMethod getEntry(HttpClient client, String uri, String acceptHeader) throws Exception {
        GetMethod getMethod = new GetMethod(uri);
        getMethod.setRequestHeader("Accept", acceptHeader);
        client.executeMethod(getMethod);
        return getMethod;
    }

    public static DeleteMethod deleteEntry(HttpClient client, String uri) throws Exception {
        DeleteMethod deleteMethod = new DeleteMethod(uri);
        client.executeMethod(deleteMethod);
        return deleteMethod;
    }
}

@@ -290,16 +290,37 @@
 io.File;
+%0Aimport java.net.URL;
 %0A%0A/**%0A *
@@ -980,35 +980,36 @@
 L);%0A        
-String path
+URL resource
  = XPathExam
@@ -1035,32 +1035,71 @@
 ce(fileName)
-.getFile
+;%0A        String path = resource.toExternalForm
 ();%0A        
@@ -1573,27 +1573,28 @@
         
-String path
+URL resource
  = XPath
@@ -1632,16 +1632,55 @@
 ame)
-.getFile
+;%0A        String path = resource.toExternalForm
 ();%0A

fix the path to files