Merge branch 'release-0.9.5'
[Sone.git] / src / test / java / net / pterodactylus / sone / web / ajax / BookmarkAjaxPageTest.java
1 /*
2  * © 2013 xplosion interactive
3  */
4
5 package net.pterodactylus.sone.web.ajax;
6
7 import static com.google.common.base.Optional.of;
8 import static org.hamcrest.CoreMatchers.is;
9 import static org.hamcrest.CoreMatchers.notNullValue;
10 import static org.junit.Assert.assertThat;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Matchers.argThat;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.net.URI;
21 import java.net.URISyntaxException;
22
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.web.WebInterface;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27
28 import freenet.clients.http.HTTPRequestImpl;
29 import freenet.support.api.HTTPRequest;
30 import org.junit.Test;
31
32 /**
33  * Tests for {@link BookmarkAjaxPage}.
34  *
35  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
36  */
37 public class BookmarkAjaxPageTest {
38
39         @Test
40         public void testBookmarkingExistingPost() throws URISyntaxException {
41                 /* create mocks. */
42                 Core core = mock(Core.class);
43                 Post post = mock(Post.class);
44                 when(core.getPost("abc")).thenReturn(of(post));
45                 WebInterface webInterface = mock(WebInterface.class);
46                 when(webInterface.getCore()).thenReturn(core);
47                 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET");
48                 FreenetRequest request = mock(FreenetRequest.class);
49                 when(request.getHttpRequest()).thenReturn(httpRequest);
50
51                 /* create JSON page. */
52                 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
53                 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
54
55                 /* verify response. */
56                 assertThat(jsonReturnObject, notNullValue());
57                 assertThat(jsonReturnObject.isSuccess(), is(true));
58
59                 /* verify behaviour. */
60                 verify(core).bookmarkPost(post);
61         }
62
63         @Test
64         public void testBookmarkingMissingPost() throws URISyntaxException {
65                 /* create mocks. */
66                 Core core = mock(Core.class);
67                 WebInterface webInterface = mock(WebInterface.class);
68                 when(webInterface.getCore()).thenReturn(core);
69                 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax"), "GET");
70                 FreenetRequest request = mock(FreenetRequest.class);
71                 when(request.getHttpRequest()).thenReturn(httpRequest);
72
73                 /* create JSON page. */
74                 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
75                 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
76
77                 /* verify response. */
78                 assertThat(jsonReturnObject, notNullValue());
79                 assertThat(jsonReturnObject.isSuccess(), is(false));
80                 assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
81
82                 /* verify behaviour. */
83                 verify(core, never()).bookmarkPost(any(Post.class));
84         }
85
86 }