View Javadoc
1   /**
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   * 
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.webservice.cs.rest;
19  
20  import java.io.ByteArrayInputStream;
21  import java.net.URI;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.ws.rs.core.MultivaluedMap;
27  
28  import org.exoplatform.container.component.RequestLifeCycle;
29  import org.exoplatform.services.rest.ContainerResponseWriter;
30  import org.exoplatform.services.rest.impl.ContainerRequest;
31  import org.exoplatform.services.rest.impl.ContainerResponse;
32  import org.exoplatform.services.rest.impl.EnvironmentContext;
33  import org.exoplatform.services.rest.impl.InputHeadersMap;
34  import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
35  import org.exoplatform.services.rest.tools.DummyContainerResponseWriter;
36  import org.exoplatform.services.security.ConversationState;
37  import org.exoplatform.services.security.Identity;
38  import org.exoplatform.services.security.MembershipEntry;
39  
40  /**
41   * @author <a href="mailto:andrew00x@gmail.com">Andrey Parfonov</a>
42   * @version $Id: $
43   */
44  public abstract class AbstractResourceTest extends BaseTest {
45  
46    private Collection<MembershipEntry> membershipEntries = new ArrayList<MembershipEntry>();
47  
48    public ContainerResponse service(String method,
49                                     String requestURI,
50                                     String baseURI,
51                                     MultivaluedMap<String, String> headers,
52                                     byte[] data,
53                                     ContainerResponseWriter writer) throws Exception {
54    RequestLifeCycle.begin(container);
55      if (headers == null)
56        headers = new MultivaluedMapImpl();
57  
58      ByteArrayInputStream in = null;
59      if (data != null)
60        in = new ByteArrayInputStream(data);
61  
62      EnvironmentContext envctx = new EnvironmentContext();
63      HttpServletRequest httpRequest = new MockHttpServletRequest(in,
64                                                                  in != null ? in.available() : 0,
65                                                                  method,
66                                                                  new InputHeadersMap(headers));
67      envctx.put(HttpServletRequest.class, httpRequest);
68      EnvironmentContext.setCurrent(envctx);
69      ContainerRequest request = new ContainerRequest(method,
70                                                      new URI(requestURI),
71                                                      new URI(baseURI),
72                                                      in,
73                                                      new InputHeadersMap(headers));
74      ContainerResponse response = new ContainerResponse(writer);
75      requestHandler.handleRequest(request, response);
76      RequestLifeCycle.end();
77      return response;
78    }
79  
80    public ContainerResponse service(String method,
81                                     String requestURI,
82                                     String baseURI,
83                                     MultivaluedMap<String, String> headers,
84                                     byte[] data) throws Exception {
85      return service(method, requestURI, baseURI, headers, data, new DummyContainerResponseWriter());
86  
87    }
88  
89    protected void login(String username, String ...memberships) {
90      setMembershipEntry(memberships, true);
91      Identity identity = new Identity(username, membershipEntries);
92      ConversationState state = new ConversationState(identity);
93      ConversationState.setCurrent(state);
94    }
95  
96    private void setMembershipEntry(String[] memberships, boolean isNew) {
97      if (isNew) {
98        membershipEntries.clear();
99      }
100     for (String ms : memberships) {
101       String[] tmp = ms.split(":");
102       MembershipEntry membershipEntry = new MembershipEntry(tmp[0], tmp[1]);
103       membershipEntries.add(membershipEntry);      
104     }
105   }
106 }