Mock some post database interactions.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / Mocks.java
index ff52581..3615828 100644 (file)
@@ -38,10 +38,19 @@ import java.util.Map;
 import java.util.Set;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.core.Options;
+import net.pterodactylus.sone.core.Options.DefaultOption;
+import net.pterodactylus.sone.core.Options.Option;
+import net.pterodactylus.sone.core.Options.OptionWatcher;
+import net.pterodactylus.sone.core.Preferences;
+import net.pterodactylus.sone.core.SoneInserter;
 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
 import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
+import net.pterodactylus.sone.utils.IntegerRangePredicate;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 
@@ -50,6 +59,7 @@ import freenet.support.api.HTTPRequest;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Predicates;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
@@ -68,6 +78,7 @@ public class Mocks {
 
        private final Multimap<Sone, Post> sonePosts = create();
        private final Map<String, Sone> sones = newHashMap();
+       private final Map<String, Post> posts = newHashMap();
        private final Multimap<Post, PostReply> postReplies = create();
        private final Multimap<String, Post> directedPosts = create();
        private final SetMultimap<Post, Sone> postLikingSones = HashMultimap.create();
@@ -92,12 +103,28 @@ public class Mocks {
                                return sones.values();
                        }
                });
+               when(core.getLocalSone(anyString())).then(new Answer<Optional<Sone>>() {
+                       @Override
+                       public Optional<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                               Sone localSone = sones.get(invocation.getArguments()[0]);
+                               return ((localSone == null) || (!localSone.isLocal())) ? Optional.<Sone>absent() : of(localSone);
+                       }
+               });
                when(core.getLocalSones()).then(new Answer<Collection<Sone>>() {
                        @Override
                        public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
                                return FluentIterable.from(sones.values()).filter(Sone.LOCAL_SONE_FILTER).toList();
                        }
                });
+               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(new PostCreated() {
+                       @Override
+                       public void postCreated(Post post) {
+                               posts.put(post.getId(), post);
+                               sonePosts.put(post.getSone(), post);
+                       }
+               }));
+               Options options = createOptions();
+               when(core.getPreferences()).thenReturn(new Preferences(options));
                when(database.getDirectedPosts(anyString())).then(new Answer<Collection<Post>>() {
                        @Override
                        public Collection<Post> answer(InvocationOnMock invocation) throws Throwable {
@@ -106,6 +133,22 @@ public class Mocks {
                });
        }
 
+       private Options createOptions() {
+               Options options = new Options();
+               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE)));
+               options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
+               options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
+               options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
+               options.addIntegerOption("PostCutOffLength", new DefaultOption<Integer>(200, Predicates.<Integer>or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
+               options.addBooleanOption("RequireFullAccess", new DefaultOption<Boolean>(false));
+               options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
+               options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
+               options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
+               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false));
+               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2));
+               return options;
+       }
+
        private static Core mockCore(Database database) {
                Core core = mock(Core.class);
                when(core.getDatabase()).thenReturn(database);
@@ -113,10 +156,15 @@ public class Mocks {
                return core;
        }
 
-       private static Database mockDatabase() {
+       private Database mockDatabase() {
                Database database = mock(Database.class);
                when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
-               when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
+               when(database.getPost(anyString())).then(new Answer<Optional<Post>>() {
+                       @Override
+                       public Optional<Post> answer(InvocationOnMock invocation) throws Throwable {
+                               return fromNullable(posts.get(invocation.getArguments()[0]));
+                       }
+               });
                when(database.getPostReply(anyString())).thenReturn(Optional.<PostReply>absent());
                return database;
        }
@@ -139,8 +187,10 @@ public class Mocks {
                return new PostReplyMocker(replyId, sone);
        }
 
-       public FreenetRequest mockRequest(String path) throws URISyntaxException {
-               HTTPRequest httpRequest = new HTTPRequestImpl(new URI(path), "GET");
+       public FreenetRequest mockRequest(String path) {
+               HTTPRequest httpRequest = mock(HTTPRequest.class);
+               when(httpRequest.getMethod()).thenReturn("GET");
+               when(httpRequest.getPath()).thenReturn(path);
                FreenetRequest request = mock(FreenetRequest.class);
                when(request.getHttpRequest()).thenReturn(httpRequest);
                return request;