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.properties.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
|
PropertyService.getInstance();
|
66
|
|
67
|
clientViewBean = new ClientView();
|
68
|
clientViewBean.setUsername(USR);
|
69
|
clientViewBean.setOrganization(ORG);
|
70
|
clientViewBean.setPassword(PWD);
|
71
|
|
72
|
instance = new ClientViewHelper(HOST, CONTEXT, clientViewBean);
|
73
|
}
|
74
|
|
75
|
protected void tearDown() throws Exception {
|
76
|
clientViewBean = null;
|
77
|
instance = null;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Test of handleClientRequest method, of class edu.ucsb.nceas.metacat.clientview.ClientViewHelper.
|
82
|
*/
|
83
|
public void testHandleClientRequest() {
|
84
|
HashMap expResult, result;
|
85
|
String expResultTxt, returnResultTxt;
|
86
|
|
87
|
System.out.println("handleClientRequest");
|
88
|
|
89
|
|
90
|
System.out.print("...setLoggedIn");
|
91
|
clientViewBean.setAction("Login");
|
92
|
|
93
|
result = instance.handleClientRequest(null);
|
94
|
returnResultTxt = (String) result.get("message");
|
95
|
assertTrue("Login: Failed to login", (returnResultTxt.indexOf("Authentication successful for user:") > -1));
|
96
|
System.out.println(" ...success!");
|
97
|
|
98
|
|
99
|
System.out.print("...download (data file)");
|
100
|
clientViewBean.setAction("Download");
|
101
|
clientViewBean.setDocId(DOWNLOAD_DATA_DOCID);
|
102
|
clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
|
103
|
|
104
|
result = instance.handleClientRequest(null);
|
105
|
returnResultTxt = (String) result.get("contentType");
|
106
|
assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/octet-stream");
|
107
|
returnResultTxt = (String) result.get("Content-Disposition");
|
108
|
assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
|
109
|
assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
|
110
|
assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
|
111
|
System.out.println(" ...success!");
|
112
|
|
113
|
|
114
|
System.out.print("...download (package zip file)");
|
115
|
clientViewBean.setDocId(DOWNLOAD_PACKAGE_DOCID);
|
116
|
clientViewBean.setMetaFileDocId(DOWNLOAD_PACKAGE_DOCID);
|
117
|
|
118
|
result = instance.handleClientRequest(null);
|
119
|
returnResultTxt = (String) result.get("contentType");
|
120
|
assertEquals("Download: Content Type is incorrect for a package download", returnResultTxt, "application/zip");
|
121
|
returnResultTxt = (String) result.get("Content-Disposition");
|
122
|
assertNotNull("Download: Content-Disposition is not set", returnResultTxt);
|
123
|
assertNotNull("Download: OutputStream is not set", result.get("outputStream"));
|
124
|
assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0);
|
125
|
System.out.println(" ...success!");
|
126
|
|
127
|
|
128
|
System.out.print("...logout");
|
129
|
clientViewBean.setAction("Logout");
|
130
|
|
131
|
result = instance.handleClientRequest(null);
|
132
|
assertNull("Logout: Session ID still exists", clientViewBean.getSessionid());
|
133
|
System.out.println(" ...success!");
|
134
|
|
135
|
}
|
136
|
}
|