2
* Copyright (c) 2009 Evenflow, Inc.
4
* Permission is hereby granted, free of charge, to any person
5
* obtaining a copy of this software and associated documentation
6
* files (the "Software"), to deal in the Software without
7
* restriction, including without limitation the rights to use,
8
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9
* copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following
13
* The above copyright notice and this permission notice shall be
14
* included in all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
* OTHER DEALINGS IN THE SOFTWARE.
26
package com.dropbox.client;
28
import org.json.simple.parser.*;
29
import org.json.simple.JSONValue;
30
import junit.framework.Test;
31
import junit.framework.TestCase;
32
import junit.framework.TestSuite;
33
import com.dropbox.client.DropboxClient;
34
import com.dropbox.client.Authenticator;
35
import java.io.IOException;
39
import java.util.HashMap;
40
import org.apache.http.StatusLine;
41
import org.apache.http.HttpResponse;
44
* Unit test for simple App.
46
public class DropboxClientTest extends TestCase
48
public static Map config = null;
49
public static Authenticator auth = null;
50
DropboxClient client = null;
54
config = Authenticator.loadConfig("config/testing.json");
55
auth = new Authenticator(config);
56
String url = auth.retrieveRequestToken(null);
57
System.out.println("Url is: " + url);
58
Util.authorizeForm(url, (String)config.get("testing_user"), (String)config.get("testing_password"));
59
auth.retrieveAccessToken("");
60
} catch (Exception e) {
62
assert false : "Total failure initializing the authenticator.";
68
public void setUp() throws Exception
70
assert auth != null : "Auth didn't get configured.";
71
this.client = new DropboxClient(this.config, DropboxClientTest.auth);
72
this.client.fileDelete("sandbox", "/tests", null);
76
* Create the test case
78
* @param testName name of the test case
80
public DropboxClientTest( String testName )
86
* @return the suite of tests being tested
88
public static Test suite()
90
return new TestSuite( DropboxClientTest.class );
93
public void test_urlencode() throws Exception {
94
String encoded = DropboxClient.urlencode(new Object[] {
97
assert encoded.equals("one=two&three=four") : "Encoded wrong: " + encoded;
100
public void test_request() throws Exception {
101
Object req = client.request("GET", "http", client.api_host, client.port, "/account/info", 0, null, client.auth);
102
assert req != null : "Should not get a null from request.";
106
public void test_buildFullURL() throws Exception
108
String url = client.buildFullURL("http", (String)config.get("server"), ((Long)config.get("port")).intValue(), "/0/account/info");
109
assert url.equals("http://" + (String)config.get("server") + "/0/account/info");
112
public void test_buildURL() throws Exception
114
String url = client.buildURL("/account/info", 0, new Object[] {});
115
assert url.equals("/0/account/info");
117
url = client.buildURL("/account/info", 0, null);
118
assert url.equals("/0/account/info");
120
url = client.buildURL("/account/info", 0, new Object[] {"one", "two"});
121
assert url.equals("/0/account/info?one=two");
124
public void assertValidResponse(Map resp, boolean json) {
125
assert resp != null : "Should always get a response.";
126
assert resp.get("ERROR") == null : "Should not get an error: " + resp.get("ERROR") + ":" + resp.get("BODY");
129
assert resp.get("RESULT") == null : "Should not get a raw result: " + resp.get("RESULT");
133
public void assertInvalidResponse(Map resp, int code)
136
assert resp.get("ERROR") != null : "SHOULD get an error:" + resp.get("BODY");
138
StatusLine status = (StatusLine)resp.get("ERROR");
139
assert status.getStatusCode() == code : "Should get a " + code + " got: " + status.getStatusCode();
142
public void test_accountInfo() throws Exception
144
Map resp = client.accountInfo(false, "");
145
assertValidResponse(resp, true);
147
resp = client.accountInfo(true, "testing");
148
assert resp.get("RESULT") != null;
151
public void test_fileCreateFolder() throws Exception
153
Map resp = client.fileCreateFolder("sandbox", "/tests", null);
154
assertValidResponse(resp, true);
156
resp = client.fileCreateFolder("sandbox", "/tests/tempthing", null);
157
assertValidResponse(resp, true);
159
resp = client.fileCreateFolder("dropbox", "/tests/noway", null);
160
assertInvalidResponse(resp, 401);
164
public void test_fileDelete() throws Exception
166
test_fileCreateFolder();
167
Map resp = client.fileDelete("sandbox", "/tests/tempthing", null);
168
assertValidResponse(resp, false);
170
resp = client.fileDelete("dropbox", "/tests/tempthing", null);
171
assertInvalidResponse(resp, 401);
175
public void test_fileCopy() throws Exception
177
test_fileCreateFolder();
178
Map resp = client.fileCopy("sandbox", "/tests/tempthing", "/tests/copiedthing", null);
179
assertValidResponse(resp, true);
181
resp = client.fileDelete("sandbox", "/tests/copiedthing", null);
182
assertValidResponse(resp, false);
185
public void test_fileMove() throws Exception
187
test_fileCreateFolder();
188
// BUG: These should pass but the current dropbox service doesn't return an error.
189
// Map resp = client.fileDelete("dropbox", "/toobigtofail.jpg", null);
190
// assertInvalidResponse(resp, 404);
192
// resp = client.fileCopy("sandbox", "/fail/toobigtoo.jpg", "/toobigtofail.jpg", null);
194
client.fileDelete("sandbox", "/tests/movedthing", null); // ignore the response
196
Map resp = client.fileMove("sandbox", "/tests/tempthing", "/tests/movedthing", null);
197
assertValidResponse(resp, true);
199
resp = client.fileMove("sandbox", "/tests/shouldfail", "/tests/didntfail", null);
200
assertInvalidResponse(resp, 404);
202
resp = client.fileDelete("sandbox", "/tests/movedthing", null);
203
assertValidResponse(resp, false);
206
public void test_links() throws Exception {
207
String url = client.links("sandbox", "/");
208
assert url.equals("http://" + (String)config.get("server") + "/0/links/sandbox/") : "Not equal: " + url;
211
public void test_metadata() throws Exception {
212
Map res = client.metadata("sandbox", "/", 100, null, false, false, null);
213
assertValidResponse(res, true);
216
public void test_getFile() throws Exception {
217
test_fileCreateFolder();
219
HttpResponse resp = client.getFile("sandbox", "/idont_exist.txt");
220
assert resp != null : "Should get a valid response.";
221
int status = resp.getStatusLine().getStatusCode();
222
assert status == 404 : "Should get a 404: " + status;
225
resp = client.getFile("sandbox", "/tests/DropboxClientTest.java");
226
assert resp != null : "Should get a valid response.";
227
status = resp.getStatusLine().getStatusCode();
228
assert status == 200 : "Should get valid status code:" + status;
229
resp.getEntity().consumeContent();
231
Map r = client.fileCopy("sandbox", "/tests/DropboxClientTest.java", "/tests/copied.java", null);
232
assertValidResponse(r, true);
234
r = client.fileMove("sandbox", "/tests/copied.java", "/tests/moved.java", null);
235
assertValidResponse(r, true);
237
r = client.fileDelete("sandbox", "/tests/DropboxClientTest.java", null);
238
assertValidResponse(r, false);
241
public void test_putFile() throws Exception {
242
test_fileCreateFolder();
243
File sample = new File("src/test/java/com/dropbox/client/DropboxClientTest.java");
244
HttpResponse resp = client.putFile("sandbox", "/tests/", sample);
245
assert resp != null : "Didn't get a valid response.";
246
int status = resp.getStatusLine().getStatusCode();
247
assert status == 200 : "Should get valid status code:" + status;