Use time text converter to display “last updated” text
[Sone.git] / src / test / java / net / pterodactylus / sone / web / WebPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static org.mockito.ArgumentMatchers.anyBoolean;
4 import static org.mockito.ArgumentMatchers.anyInt;
5 import static org.mockito.ArgumentMatchers.anyString;
6 import static org.mockito.ArgumentMatchers.eq;
7 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.when;
10
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import net.pterodactylus.sone.core.Core;
19 import net.pterodactylus.sone.core.Preferences;
20 import net.pterodactylus.sone.core.UpdateChecker;
21 import net.pterodactylus.sone.data.Post;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
24 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
25 import net.pterodactylus.sone.web.page.FreenetRequest;
26 import net.pterodactylus.util.notify.Notification;
27 import net.pterodactylus.util.template.Template;
28 import net.pterodactylus.util.template.TemplateContext;
29 import net.pterodactylus.util.web.Method;
30
31 import freenet.clients.http.ToadletContext;
32 import freenet.support.api.HTTPRequest;
33
34 import com.google.common.base.Optional;
35 import com.google.common.eventbus.EventBus;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.invocation.InvocationOnMock;
40 import org.mockito.stubbing.Answer;
41
42 /**
43  * Base class for web page tests.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public abstract class WebPageTest {
48
49         @Rule
50         public final ExpectedException expectedException = ExpectedException.none();
51
52         protected final Template template = new Template();
53         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
54         protected final EventBus eventBus = mock(EventBus.class);
55         protected final Core core = webInterface.getCore();
56
57         protected final Sone currentSone = mock(Sone.class);
58
59         protected final TemplateContext templateContext = new TemplateContext();
60         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
61         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
62         protected final ToadletContext toadletContext = mock(ToadletContext.class);
63
64         private final Set<OwnIdentity> ownIdentities = new HashSet<>();
65         private final List<Sone> localSones = new ArrayList<>();
66
67         @Before
68         public final void setupFreenetRequest() {
69                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
70                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
71                 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
72                         @Override
73                         public String answer(InvocationOnMock invocation) throws Throwable {
74                                 return "";
75                         }
76                 });
77         }
78
79         @Before
80         public final void setupCore() {
81                 UpdateChecker updateChecker = mock(UpdateChecker.class);
82                 when(core.getUpdateChecker()).thenReturn(updateChecker);
83                 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
84                 when(core.getLocalSone(anyString())).thenReturn(null);
85                 when(core.getLocalSones()).thenReturn(localSones);
86                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
87                 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
88         }
89
90         @Before
91         public final void setupIdentityManager() {
92                 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
93         }
94
95         @Before
96         public final void setupWebInterface() {
97                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
98                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
99                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
100         }
101
102         @Before
103         public void setupSone() {
104                 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
105         }
106
107         protected void unsetCurrentSone() {
108                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
109                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
110         }
111
112         protected void request(String uri, Method method) {
113                 try {
114                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
115                 } catch (URISyntaxException e) {
116                         throw new RuntimeException(e);
117                 }
118                 when(freenetRequest.getMethod()).thenReturn(method);
119         }
120
121         protected void addHttpRequestParameter(String name, final String value) {
122                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
123                         @Override
124                         public String answer(InvocationOnMock invocation) throws Throwable {
125                                 int maxLength = invocation.getArgument(1);
126                                 return value.substring(0, Math.min(maxLength, value.length()));
127                         }
128                 });
129         }
130
131         protected void addPost(String postId, Post post) {
132                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
133         }
134
135         protected void addSone(String soneId, Sone sone) {
136                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
137         }
138
139         protected void addLocalSone(String soneId, Sone sone) {
140                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
141                 localSones.add(sone);
142         }
143
144         protected void addOwnIdentity(OwnIdentity ownIdentity) {
145                 ownIdentities.add(ownIdentity);
146         }
147
148 }