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 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
|
* ClientViewHelper JUnit tests
|
29
|
* @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
|
final static String USR = "barteau";
|
37
|
final static String ORG = "NCEAS";
|
38
|
final static String PWD = "geekb8";
|
39
|
final static String DOWNLOAD_PACKAGE_DOCID = "barteau.179.1";
|
40
|
final static String DOWNLOAD_DATA_DOCID = "barteau.180.1";
|
41
|
final static String HOST = "localhost:8084";
|
42
|
final static String CONTEXT = "/sanparks";
|
43
|
|
44
|
/**
|
45
|
* Constructor
|
46
|
* @param testName String
|
47
|
*/
|
48
|
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
|
instance = new ClientViewHelper(HOST, CONTEXT, clientViewBean);
|
70
|
}
|
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
|
|
87
|
System.out.print("...setLoggedIn");
|
88
|
clientViewBean.setAction("Login");
|
89
|
|
90
|
result = instance.handleClientRequest(null);
|
91
|
returnResultTxt = (String) result.get("message");
|
92
|
assertTrue("Login: Failed to login", returnResultTxt.contains("Authentication successful for user:"));
|
93
|
System.out.println(" ...success!");
|
94
|
|
95
|
|
96
|
System.out.print("...download (data file)");
|
97
|
clientViewBean.setAction("Download");
|
98
|
clientViewBean.setDocId("DOWNLOAD_DATA_DOCID");
|
99
|
clientViewBean.setMetaFileDocId("DOWNLOAD_PACKAGE_DOCID");
|
100
|
|
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
|
|
111
|
System.out.print("...download (package zip file)");
|
112
|
clientViewBean.setDocId("DOWNLOAD_PACKAGE_DOCID");
|
113
|
clientViewBean.setMetaFileDocId("DOWNLOAD_PACKAGE_DOCID");
|
114
|
|
115
|
result = instance.handleClientRequest(null);
|
116
|
returnResultTxt = (String) result.get("contentType");
|
117
|
assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/zip");
|
118
|
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
|
|
125
|
System.out.print("...logout");
|
126
|
clientViewBean.setAction("Logout");
|
127
|
|
128
|
result = instance.handleClientRequest(null);
|
129
|
assertNull("Logout: Session ID still exists", clientViewBean.getSessionid());
|
130
|
System.out.println(" ...success!");
|
131
|
|
132
|
}
|
133
|
}
|