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