1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.util.Random;
24  import java.util.StringTokenizer;
25  
26  import junit.framework.TestCase;
27  
28  import org.dom4j.DocumentException;
29  import org.dom4j.DocumentHelper;
30  import org.junit.Ignore;
31  import org.springframework.extensions.webscripts.connector.RemoteClient;
32  import org.springframework.extensions.webscripts.connector.Response;
33  
34  /**
35   * JUnit test for Remote Store REST API.
36   * 
37   * Requires that an Alfresco repo is running on http://localhost:8080/alfresco
38   * and that the default remote store (an AVM store called 'sitestore') exists.
39   * Therefore this test is not auto-run during the build process.
40   * 
41   * It is suggested that the TestRemoteClient test is executed before this test - 
42   * as it requires the RemoteClient to perform the remote calls.
43   * 
44   * @author Kevin Roast
45   */
46  @Ignore public class TestRemoteStore extends TestCase
47  {
48     String TEST_CONTENT1 = "some test text";
49     String TEST_CONTENT2 = "some text text - updated";
50     
51     public void testRemoteStore()
52     {
53        RemoteClient remote = new RemoteClient("http://localhost:8080/alfresco/s");
54        
55        Response response = remote.call("/api/login?u=admin&pw=admin");
56        assertEquals(200, response.getStatus().getCode());
57        String ticket = null;
58        try
59        {
60            ticket = DocumentHelper.parseText(response.getResponse()).getRootElement().getTextTrim();
61        }
62        catch (DocumentException de)
63        {
64            fail("Failed to extract ticket from login API call.");
65        }
66        remote.setTicket(ticket);
67        
68        // POST with simple string body
69        String filename0 = Long.toString((new Random().nextLong())) + ".txt";
70        Response res = remote.call("/remotestore/create/" + filename0, TEST_CONTENT1);
71        assertEquals(200, res.getStatus().getCode());
72        
73        // POST to a random sub-dir path
74        String randdir = Long.toString((new Random().nextLong()));
75        res = remote.call("/remotestore/create/" + randdir + "/" + filename0, TEST_CONTENT1);
76        assertEquals(200, res.getStatus().getCode());
77        
78        // POST to a random sub-sub-dir path
79        String randdir2 = randdir + "/" + Long.toString((new Random().nextLong()));
80        res = remote.call("/remotestore/create/" + randdir2 + "/" + filename0, TEST_CONTENT1);
81        assertEquals(200, res.getStatus().getCode());
82        
83        // POST a new file from an inputstream
84        String filename = Long.toString((new Random().nextLong())) + ".txt";
85        res = remote.call("/remotestore/create/" + filename, new ByteArrayInputStream(TEST_CONTENT1.getBytes()));
86        assertEquals(200, res.getStatus().getCode());
87        
88        // get it back again into a response string
89        res = remote.call("/remotestore/get/" + filename);
90        assertEquals(200, res.getStatus().getCode());
91        assertEquals(TEST_CONTENT1, res.getResponse());
92        
93        // get it back again into an output stream
94        ByteArrayOutputStream out = new ByteArrayOutputStream();
95        res = remote.call("/remotestore/get/" + filename, out);
96        assertEquals(200, res.getStatus().getCode());
97        assertEquals(TEST_CONTENT1, out.toString());
98        
99        // test 'has' method - for true and false cases
100       res = remote.call("/remotestore/has/" + filename);
101       assertEquals(200, res.getStatus().getCode());
102       assertEquals(res.getResponse(), "true");
103       res = remote.call("/remotestore/has/_shouldnotexist.doc");
104       assertEquals(200, res.getStatus().getCode());
105       assertEquals(res.getResponse(), "false");
106       
107       // POST to update the file content
108       res = remote.call("/remotestore/update/" + filename, new ByteArrayInputStream(TEST_CONTENT2.getBytes()));
109       assertEquals(200, res.getStatus().getCode());
110       
111       // get it back again to confirm update
112       out = new ByteArrayOutputStream();
113       res = remote.call("/remotestore/get/" + filename, out);
114       assertEquals(200, res.getStatus().getCode());
115       assertEquals(TEST_CONTENT2, out.toString());
116       
117       // test 'get' fails with 404 when file does not exist
118       res = remote.call("/remotestore/get/_shouldnotexist.doc");
119       assertEquals(404, res.getStatus().getCode());
120       
121       // test listall on root of store
122       res = remote.call("/remotestore/listall");
123       assertEquals(200, res.getStatus().getCode());
124       StringTokenizer t = new StringTokenizer(res.getResponse(), "\n");
125       assertTrue(t.countTokens() != 0);
126       
127       // test list on the dir we made earlier
128       res = remote.call("/remotestore/list/" + randdir);
129       assertEquals(200, res.getStatus().getCode());
130       t = new StringTokenizer(res.getResponse(), "\n");
131       assertEquals(t.countTokens(), 1);
132       
133       // test listall on dir we made earlier
134       res = remote.call("/remotestore/listall/" + randdir);
135       assertEquals(200, res.getStatus().getCode());
136       t = new StringTokenizer(res.getResponse(), "\n");
137       assertEquals(t.countTokens(), 2);
138       
139       // test list fails with 404 when path does not exist
140       res = remote.call("/remotestore/list/shouldnotexist");
141       assertEquals(404, res.getStatus().getCode());
142       
143       // test listpattern on files we made earlier
144       res = remote.call("/remotestore/listpattern/" + randdir + "?m=*.nomatch");
145       assertEquals(200, res.getStatus().getCode());
146       t = new StringTokenizer(res.getResponse(), "\n");
147       assertTrue(t.countTokens() == 0);
148       
149       res = remote.call("/remotestore/listpattern/" + randdir + "?m=*.txt");
150       assertEquals(200, res.getStatus().getCode());
151       t = new StringTokenizer(res.getResponse(), "\n");
152       assertTrue(t.countTokens() == 2);
153    }
154 }