~nicoulaj/syncany/maven-local-repo

« back to all changes in this revision

Viewing changes to syncany/syncany-plugins/syncany-plugin-dropbox/dropbox-client-java/src/test/java/com/dropbox/client/DropboxClientTest.java

  • Committer: Philipp Heckel
  • Date: 2011-07-04 17:12:33 UTC
  • Revision ID: philipp.heckel@gmail.com-20110704171233-f2aos2lxb93kxqcd
Mavenized project as in lp:~julien-nicoulaud/syncany/maven

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2009 Evenflow, Inc.
 
3
 * 
 
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
 
11
 * conditions:
 
12
 * 
 
13
 * The above copyright notice and this permission notice shall be
 
14
 * included in all copies or substantial portions of the Software.
 
15
 * 
 
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.
 
24
*/
 
25
 
 
26
package com.dropbox.client;
 
27
 
 
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;
 
36
import java.io.File;
 
37
import java.net.*;
 
38
import java.util.Map;
 
39
import java.util.HashMap;
 
40
import org.apache.http.StatusLine;
 
41
import org.apache.http.HttpResponse;
 
42
 
 
43
/**
 
44
 * Unit test for simple App.
 
45
 */
 
46
public class DropboxClientTest extends TestCase
 
47
{
 
48
    public static Map config = null;
 
49
    public static Authenticator auth = null;
 
50
    DropboxClient client = null;
 
51
 
 
52
    static {
 
53
        try {
 
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) {
 
61
            e.printStackTrace();
 
62
            assert false : "Total failure initializing the authenticator.";
 
63
 
 
64
        }
 
65
    }
 
66
 
 
67
 
 
68
    public void setUp() throws Exception 
 
69
    {
 
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);
 
73
    }
 
74
 
 
75
    /**
 
76
     * Create the test case
 
77
     *
 
78
     * @param testName name of the test case
 
79
     */
 
80
    public DropboxClientTest( String testName )
 
81
    {
 
82
        super( testName );
 
83
    }
 
84
 
 
85
    /**
 
86
     * @return the suite of tests being tested
 
87
     */
 
88
    public static Test suite()
 
89
    {
 
90
        return new TestSuite( DropboxClientTest.class );
 
91
    }
 
92
 
 
93
    public void test_urlencode() throws Exception {
 
94
        String encoded = DropboxClient.urlencode(new Object[] {
 
95
                "one", "two",
 
96
                "three", "four"});
 
97
        assert encoded.equals("one=two&three=four") : "Encoded wrong: " + encoded;
 
98
    }
 
99
 
 
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.";
 
103
    }
 
104
 
 
105
 
 
106
    public void test_buildFullURL() throws Exception
 
107
    {
 
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");
 
110
    }
 
111
 
 
112
    public void test_buildURL() throws Exception
 
113
    {
 
114
        String url = client.buildURL("/account/info", 0, new Object[] {});
 
115
        assert url.equals("/0/account/info");
 
116
 
 
117
        url = client.buildURL("/account/info", 0, null);
 
118
        assert url.equals("/0/account/info");
 
119
 
 
120
        url = client.buildURL("/account/info", 0, new Object[] {"one", "two"});
 
121
        assert url.equals("/0/account/info?one=two");
 
122
    }
 
123
 
 
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");
 
127
 
 
128
        if(json) {
 
129
            assert resp.get("RESULT") == null : "Should not get a raw result: " + resp.get("RESULT");
 
130
        }
 
131
    }
 
132
 
 
133
    public void assertInvalidResponse(Map resp, int code)
 
134
    {
 
135
        assert resp != null;
 
136
        assert resp.get("ERROR") != null : "SHOULD get an error:" + resp.get("BODY");
 
137
 
 
138
        StatusLine status = (StatusLine)resp.get("ERROR");
 
139
        assert status.getStatusCode() == code : "Should get a " + code + " got: " + status.getStatusCode();
 
140
    }
 
141
 
 
142
    public void test_accountInfo() throws Exception 
 
143
    {
 
144
        Map resp = client.accountInfo(false, "");
 
145
        assertValidResponse(resp, true);
 
146
 
 
147
        resp = client.accountInfo(true, "testing");
 
148
        assert resp.get("RESULT") != null;
 
149
    }
 
150
 
 
151
    public void test_fileCreateFolder() throws Exception
 
152
    {
 
153
        Map resp = client.fileCreateFolder("sandbox", "/tests", null);
 
154
        assertValidResponse(resp, true);
 
155
 
 
156
        resp = client.fileCreateFolder("sandbox", "/tests/tempthing", null);
 
157
        assertValidResponse(resp, true);
 
158
 
 
159
        resp = client.fileCreateFolder("dropbox", "/tests/noway", null);
 
160
        assertInvalidResponse(resp, 401);
 
161
    }
 
162
 
 
163
 
 
164
    public void test_fileDelete() throws Exception
 
165
    {
 
166
        test_fileCreateFolder();
 
167
        Map resp = client.fileDelete("sandbox", "/tests/tempthing", null);
 
168
        assertValidResponse(resp, false);
 
169
 
 
170
        resp = client.fileDelete("dropbox", "/tests/tempthing", null);
 
171
        assertInvalidResponse(resp, 401);
 
172
    }
 
173
 
 
174
 
 
175
    public void test_fileCopy() throws Exception
 
176
    {
 
177
        test_fileCreateFolder();
 
178
        Map resp = client.fileCopy("sandbox", "/tests/tempthing", "/tests/copiedthing", null);
 
179
        assertValidResponse(resp, true);
 
180
 
 
181
        resp = client.fileDelete("sandbox", "/tests/copiedthing", null);
 
182
        assertValidResponse(resp, false);
 
183
    }
 
184
 
 
185
    public void test_fileMove() throws Exception
 
186
    {
 
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);
 
191
 
 
192
        // resp = client.fileCopy("sandbox", "/fail/toobigtoo.jpg", "/toobigtofail.jpg", null);
 
193
 
 
194
        client.fileDelete("sandbox", "/tests/movedthing", null);  // ignore the response
 
195
 
 
196
        Map resp = client.fileMove("sandbox", "/tests/tempthing", "/tests/movedthing", null);
 
197
        assertValidResponse(resp, true);
 
198
 
 
199
        resp = client.fileMove("sandbox", "/tests/shouldfail", "/tests/didntfail", null);
 
200
        assertInvalidResponse(resp, 404);
 
201
 
 
202
        resp = client.fileDelete("sandbox", "/tests/movedthing", null);
 
203
        assertValidResponse(resp, false);
 
204
    }
 
205
 
 
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;
 
209
    }
 
210
 
 
211
    public void test_metadata() throws Exception {
 
212
        Map res = client.metadata("sandbox", "/", 100, null, false, false, null);
 
213
        assertValidResponse(res, true);
 
214
    }
 
215
 
 
216
    public void test_getFile() throws Exception {
 
217
        test_fileCreateFolder();
 
218
 
 
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;
 
223
 
 
224
        test_putFile();
 
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();
 
230
 
 
231
        Map r = client.fileCopy("sandbox", "/tests/DropboxClientTest.java", "/tests/copied.java", null);
 
232
        assertValidResponse(r, true);
 
233
 
 
234
        r = client.fileMove("sandbox", "/tests/copied.java", "/tests/moved.java", null);
 
235
        assertValidResponse(r, true);
 
236
 
 
237
        r = client.fileDelete("sandbox", "/tests/DropboxClientTest.java", null);
 
238
        assertValidResponse(r, false);
 
239
    }
 
240
 
 
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;
 
248
    }
 
249
 
 
250
}
 
251
 
 
252