View Javadoc
1   /*
2    * Copyright (C) 2003-2010 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  package org.exoplatform.social.service.rest;
18  
19  import org.exoplatform.services.rest.impl.ContainerResponse;
20  import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
21  import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
22  import org.exoplatform.social.service.test.AbstractResourceTest;
23  
24  import javax.ws.rs.core.MultivaluedMap;
25  
26  /**
27   * LinkShareRestServiceTest.java
28   *
29   * @author <a href="http://hoatle.net">hoatle</a>
30   * @since	 Mar 4, 2010
31   */
32  public class LinkShareRestServiceTest extends AbstractResourceTest {
33  
34    private final String RIGHT_LINK = "hTTp://google.com";
35    private final String WRONG_LINK = "http://google.com/ahgo/ahgoeh";
36    public void setUp() throws Exception {
37      super.setUp();
38  
39      addResource(LinkShareRestService.class, null);
40    }
41  
42    public void tearDown() throws Exception {
43      super.tearDown();
44  
45      removeResource(LinkShareRestService.class);
46    }
47  
48    public void testJsonRightLink() throws Exception {
49      byte[] data = ("{\"link\":\""+ RIGHT_LINK +"\", \"lang\": \"en\"}").getBytes("UTF-8");
50      MultivaluedMap<String, String> h = new MultivaluedMapImpl();
51      h.putSingle("content-type", "application/json");
52      h.putSingle("content-length", "" + data.length);
53      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
54      ContainerResponse response = service("POST", "/social/linkshare/show.json", "", h, data, writer);
55      assertEquals(200, response.getStatus());
56      assertEquals("application/json;charset=utf-8", response.getContentType().toString());
57      LinkShare linkShare = (LinkShare) response.getEntity();
58      assertEquals(RIGHT_LINK, linkShare.getLink());
59      assertNotNull(linkShare.getTitle());
60    }
61  
62    public void testXmlRightLink() throws Exception {
63      //TODO hoatle fix to work
64  /*    byte[] data =
65        ("<xml version=\"1.0\" encoding=\"UTF-8\"?>"
66          + "<link>" + RIGHT_LINK + "</link>"
67          + "<lang>en</lang>").getBytes("UTF-8");
68      MultivaluedMap<String, String> h = new MultivaluedMapImpl();
69      h.putSingle("content-type", "application/xml");
70      h.putSingle("content-length", "" + data.length);
71      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
72      ContainerResponse response = service("POST", "/social/linkshare/show.xml", "", h, data, writer);
73      assertEquals(200, response.getStatus());
74      assertEquals("application/xml", response.getContentType().toString());
75      LinkShare linkShare = (LinkShare) response.getEntity();
76      assertEquals(RIGHT_LINK, linkShare.getLink());
77      assertNotNull(linkShare.getTitle());*/
78    }
79  
80    public void testJsonWrongLink() throws Exception {
81      byte[] data = ("{\"link\":\""+ WRONG_LINK +"\", \"lang\": \"en\"}").getBytes("UTF-8");
82      MultivaluedMap<String, String> h = new MultivaluedMapImpl();
83      h.putSingle("content-type", "application/json");
84      h.putSingle("content-length", "" + data.length);
85      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
86      ContainerResponse response = service("POST", "/social/linkshare/show.json", "", h, data, writer);
87      assertEquals(200, response.getStatus());
88      //assertEquals("text/plain", response.getContentType().toString());
89    }
90  
91    public void testXmlWrongLink() throws Exception {
92      //TODO hoatle fix to work
93    }
94  
95    public void testBadRequest() throws Exception {
96      ContainerResponse response = service("GET", "/social/linkshare/show.json", "", null, null);
97      assertEquals(405, response.getStatus()); //Method Not Allowed
98      response = service("POST", "/social/linkshare/show.json", "", null, null);
99      assertEquals(400, response.getStatus()); //Bad Request
100   }
101 
102 }