Project

General

Profile

1
/*
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 java.io.ByteArrayOutputStream;
11
import java.io.File;
12
import junit.framework.*;
13
import com.oreilly.servlet.multipart.MultipartParser;
14
import java.io.InputStream;
15
import java.util.HashMap;
16
import java.util.Stack;
17
import java.util.TreeMap;
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
import javax.servlet.http.HttpSession;
21
import javax.xml.xpath.XPath;
22
import org.w3c.dom.Document;
23
import org.w3c.dom.Node;
24

    
25
import edu.ucsb.nceas.metacat.client.MetacatClient;
26
import edu.ucsb.nceas.metacat.service.PropertyService;
27

    
28
/**
29
 * ClientViewHelper JUnit tests
30
 * @author barteau
31
 */
32
public class ClientViewHelperTest extends TestCase {
33
    ClientViewHelper            instance;
34
    ClientView                  clientViewBean;
35
    
36
//    final static String         PATH_TO_PROPERTIES = "projects/metacat/build/war/WEB-INF/metacat.properties";
37
    final static String         PATH_TO_PROPERTIES = "projects/metacat/build/war/WEB-INF";
38
    final static String         USR = "";
39
    final static String         ORG = "NCEAS";
40
    final static String         PWD = "";
41
    final static String         DOWNLOAD_PACKAGE_DOCID = "";
42
    final static String         DOWNLOAD_DATA_DOCID = "";
43
    final static String         HOST = "localhost:8084";
44
    final static String         CONTEXT = "/sanparks";
45
    
46
    /**
47
     * Constructor
48
     * @param testName String
49
     */
50
    public ClientViewHelperTest(String testName) {
51
        super(testName);
52
    }
53
    
54
    protected void setUp() throws Exception {
55
        if (PWD.equals("")) 
56
            fail("Please set the class property PWD before running this test.");
57
        if (USR.equals("")) 
58
            fail("Please set the class property USR before running this test.");
59
        if (DOWNLOAD_PACKAGE_DOCID.equals("")) 
60
            fail("Please set the class property DOWNLOAD_PACKAGE_DOCID before running this test.");
61
        if (DOWNLOAD_DATA_DOCID.equals("")) 
62
            fail("Please set the class property DOWNLOAD_DATA_DOCID before running this test.");
63
        
64
        PropertyService.getInstance(PATH_TO_PROPERTIES);
65
        
66
        clientViewBean = new ClientView();
67
        clientViewBean.setUsername(USR);
68
        clientViewBean.setOrganization(ORG);
69
        clientViewBean.setPassword(PWD);
70
        
71
        instance = new ClientViewHelper(HOST, CONTEXT, clientViewBean);
72
    }
73
    
74
    protected void tearDown() throws Exception {
75
        clientViewBean = null;
76
        instance = null;
77
    }
78
    
79
    /**
80
     * Test of handleClientRequest method, of class edu.ucsb.nceas.metacat.clientview.ClientViewHelper.
81
     */
82
    public void testHandleClientRequest() {
83
        HashMap             expResult, result;
84
        String              expResultTxt, returnResultTxt;
85
        
86
        System.out.println("handleClientRequest");
87
        
88
        
89
        System.out.print("...setLoggedIn");
90
        clientViewBean.setAction("Login");
91
        
92
        result = instance.handleClientRequest(null);
93
        returnResultTxt = (String) result.get("message");
94
        assertTrue("Login: Failed to login", (returnResultTxt.indexOf("Authentication successful for user:") > -1));
95
        System.out.println(" ...success!");
96
        
97
        
98
        System.out.print("...download (data file)");
99
        clientViewBean.setAction("Download");
100
        clientViewBean.setDocId(DOWNLOAD_DATA_DOCID);
101
        clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
102
        
103
        result = instance.handleClientRequest(null);
104
        returnResultTxt = (String) result.get("contentType");
105
        assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/octet-stream");
106
        returnResultTxt = (String) result.get("Content-Disposition");
107
        assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
108
        assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
109
        assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
110
        System.out.println(" ...success!");
111
        
112
        
113
        System.out.print("...download (package zip file)");
114
        clientViewBean.setDocId(DOWNLOAD_PACKAGE_DOCID);
115
        clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
116
        
117
        result = instance.handleClientRequest(null);
118
        returnResultTxt = (String) result.get("contentType");
119
        assertEquals("Download: Content Type is incorrect for a package download", returnResultTxt, "application/zip");
120
        returnResultTxt = (String) result.get("Content-Disposition");
121
        assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
122
        assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
123
        assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
124
        System.out.println(" ...success!");
125

    
126
        
127
        System.out.print("...logout");
128
        clientViewBean.setAction("Logout");
129
        
130
        result = instance.handleClientRequest(null);
131
        assertNull("Logout: Session ID still exists", clientViewBean.getSessionid());
132
        System.out.println(" ...success!");
133
        
134
    }
135
}
    (1-1/1)