Project

General

Profile

1 3535 barteau
/*
2
 * ClientViewHelperTest.java
3
 * JUnit based test
4
 *
5
 * Created on October 25, 2007, 5:11 PM
6
 */
7
8
package edu.ucsb.nceas.metacat.clientview;
9
10
import edu.ucsb.nceas.utilities.Options;
11
import java.io.ByteArrayOutputStream;
12
import java.io.File;
13
import junit.framework.*;
14
import com.oreilly.servlet.multipart.MultipartParser;
15
import edu.ucsb.nceas.metacat.client.MetacatClient;
16
import java.io.InputStream;
17
import java.util.HashMap;
18
import java.util.Stack;
19
import java.util.TreeMap;
20
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
22
import javax.servlet.http.HttpSession;
23
import javax.xml.xpath.XPath;
24
import org.w3c.dom.Document;
25
import org.w3c.dom.Node;
26
27
/**
28 3536 barteau
 * ClientViewHelper JUnit tests
29 3535 barteau
 * @author barteau
30
 */
31
public class ClientViewHelperTest extends TestCase {
32
    ClientViewHelper            instance;
33
    ClientView                  clientViewBean;
34
35
    final static String         PATH_TO_PROPERTIES = "projects/metacat/build/war/WEB-INF/metacat.properties";
36 3537 barteau
    final static String         USR = "";
37 3535 barteau
    final static String         ORG = "NCEAS";
38 3537 barteau
    final static String         PWD = "";
39
    final static String         DOWNLOAD_PACKAGE_DOCID = "";
40
    final static String         DOWNLOAD_DATA_DOCID = "";
41 3536 barteau
    final static String         HOST = "localhost:8084";
42
    final static String         CONTEXT = "/sanparks";
43 3535 barteau
44 3536 barteau
    /**
45
     * Constructor
46
     * @param testName String
47
     */
48 3535 barteau
    public ClientViewHelperTest(String testName) {
49
        super(testName);
50
    }
51
52
    protected void setUp() throws Exception {
53
        if (PWD.equals(""))
54
            fail("Please set the class property PWD before running this test.");
55
        if (USR.equals(""))
56
            fail("Please set the class property USR before running this test.");
57
        if (DOWNLOAD_PACKAGE_DOCID.equals(""))
58
            fail("Please set the class property DOWNLOAD_PACKAGE_DOCID before running this test.");
59
        if (DOWNLOAD_DATA_DOCID.equals(""))
60
            fail("Please set the class property DOWNLOAD_DATA_DOCID before running this test.");
61
62
        Options.initialize(new File(PATH_TO_PROPERTIES));
63
64
        clientViewBean = new ClientView();
65
        clientViewBean.setUsername(USR);
66
        clientViewBean.setOrganization(ORG);
67
        clientViewBean.setPassword(PWD);
68
69 3536 barteau
        instance = new ClientViewHelper(HOST, CONTEXT, clientViewBean);
70 3535 barteau
    }
71
72
    protected void tearDown() throws Exception {
73
        clientViewBean = null;
74
        instance = null;
75
    }
76
77
    /**
78
     * Test of handleClientRequest method, of class edu.ucsb.nceas.metacat.clientview.ClientViewHelper.
79
     */
80
    public void testHandleClientRequest() {
81
        HashMap             expResult, result;
82
        String              expResultTxt, returnResultTxt;
83
84
        System.out.println("handleClientRequest");
85
86 3536 barteau
87 3535 barteau
        System.out.print("...setLoggedIn");
88
        clientViewBean.setAction("Login");
89 3536 barteau
90 3535 barteau
        result = instance.handleClientRequest(null);
91
        returnResultTxt = (String) result.get("message");
92 3677 leinfelder
        assertTrue("Login: Failed to login", (returnResultTxt.indexOf("Authentication successful for user:") > -1));
93 3535 barteau
        System.out.println(" ...success!");
94
95 3536 barteau
96 3535 barteau
        System.out.print("...download (data file)");
97
        clientViewBean.setAction("Download");
98 3538 barteau
        clientViewBean.setDocId(DOWNLOAD_DATA_DOCID);
99
        clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
100 3535 barteau
101
        result = instance.handleClientRequest(null);
102
        returnResultTxt = (String) result.get("contentType");
103
        assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/octet-stream");
104
        returnResultTxt = (String) result.get("Content-Disposition");
105
        assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
106
        assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
107
        assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
108
        System.out.println(" ...success!");
109
110 3536 barteau
111 3535 barteau
        System.out.print("...download (package zip file)");
112 3538 barteau
        clientViewBean.setDocId(DOWNLOAD_PACKAGE_DOCID);
113
        clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
114 3535 barteau
115
        result = instance.handleClientRequest(null);
116
        returnResultTxt = (String) result.get("contentType");
117 3538 barteau
        assertEquals("Download: Content Type is incorrect for a package download", returnResultTxt, "application/zip");
118 3535 barteau
        returnResultTxt = (String) result.get("Content-Disposition");
119
        assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
120
        assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
121
        assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
122
        System.out.println(" ...success!");
123
124 3536 barteau
125 3535 barteau
        System.out.print("...logout");
126
        clientViewBean.setAction("Logout");
127 3536 barteau
128 3535 barteau
        result = instance.handleClientRequest(null);
129
        assertNull("Logout: Session ID still exists", clientViewBean.getSessionid());
130
        System.out.println(" ...success!");
131
132
    }
133
}