Use database instead of Core in Sone parser
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 26 Feb 2018 06:00:04 +0000 (07:00 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 2 Mar 2018 20:59:53 +0000 (21:59 +0100)
src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java
src/test/java/net/pterodactylus/sone/core/SoneParserTest.java

index 9f0559c..f3d5068 100644 (file)
@@ -44,8 +44,6 @@ import freenet.keys.USK;
 import freenet.node.RequestStarter;
 import freenet.support.api.Bucket;
 
-import com.google.common.annotations.VisibleForTesting;
-
 /**
  * The Sone downloader is responsible for download Sones as they are updated.
  *
@@ -76,22 +74,9 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
         *              The core
         * @param freenetInterface
         *              The Freenet interface
-        */
-       @Inject
-       public SoneDownloaderImpl(Core core, FreenetInterface freenetInterface) {
-               this(core, freenetInterface, new SoneParser(core));
-       }
-
-       /**
-        * Creates a new Sone downloader.
-        *
-        * @param core
-        *              The core
-        * @param freenetInterface
-        *              The Freenet interface
         * @param soneParser
         */
-       @VisibleForTesting
+       @Inject
        SoneDownloaderImpl(Core core, FreenetInterface freenetInterface, SoneParser soneParser) {
                super("Sone Downloader", false);
                this.core = core;
index fdbd9ae..a9e2b3a 100644 (file)
@@ -14,6 +14,8 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.inject.Inject;
+
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
@@ -23,6 +25,7 @@ import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.DuplicateField;
 import net.pterodactylus.sone.data.Profile.EmptyFieldName;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
 import net.pterodactylus.sone.database.SoneBuilder;
@@ -40,10 +43,11 @@ public class SoneParser {
 
        private static final Logger logger = getLogger(SoneParser.class.getName());
        private static final int MAX_PROTOCOL_VERSION = 0;
-       private final Core core;
+       private final Database database;
 
-       public SoneParser(Core core) {
-               this.core = core;
+       @Inject
+       public SoneParser(Database database) {
+               this.database = database;
        }
 
        public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException {
@@ -60,7 +64,7 @@ public class SoneParser {
                        return null;
                }
 
-               SoneBuilder soneBuilder = core.soneBuilder().from(originalSone.getIdentity());
+               SoneBuilder soneBuilder = database.newSoneBuilder().from(originalSone.getIdentity());
                if (originalSone.isLocal()) {
                        soneBuilder = soneBuilder.local();
                }
@@ -180,7 +184,7 @@ public class SoneParser {
                                        return null;
                                }
                                try {
-                                       PostBuilder postBuilder = core.postBuilder();
+                                       PostBuilder postBuilder = database.newPostBuilder();
                                        /* TODO - parse time correctly. */
                                        postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
@@ -213,7 +217,7 @@ public class SoneParser {
                                        return null;
                                }
                                try {
-                                       PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
+                                       PostReplyBuilder postReplyBuilder = database.newPostReplyBuilder();
                                        /* TODO - parse time correctly. */
                                        postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
                                        replies.add(postReplyBuilder.build());
@@ -267,13 +271,13 @@ public class SoneParser {
                                }
                                Album parent = null;
                                if (parentId != null) {
-                                       parent = core.getAlbum(parentId);
+                                       parent = database.getAlbum(parentId);
                                        if (parent == null) {
                                                logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
                                                return null;
                                        }
                                }
-                               Album album = core.albumBuilder()
+                               Album album = database.newAlbumBuilder()
                                                .withId(id)
                                                .by(sone)
                                                .build()
@@ -307,7 +311,7 @@ public class SoneParser {
                                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
                                                        return null;
                                                }
-                                               Image image = core.imageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
+                                               Image image = database.newImageBuilder().withId(imageId).build().modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
                                                image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
                                                image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update();
                                                album.addImage(image);
index f879cac..bac5f58 100644 (file)
@@ -26,6 +26,7 @@ import net.pterodactylus.sone.core.FreenetInterface.Fetched;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.freenet.wot.Identity;
+import net.pterodactylus.sone.test.GuiceKt;
 
 import freenet.client.ClientMetadata;
 import freenet.client.FetchResult;
@@ -197,7 +198,9 @@ public class SoneDownloaderTest {
 
        @Test
        public void soneDownloaderCanBeCreatedByDependencyInjection() {
-           assertThat(getBaseInjector().getInstance(SoneDownloader.class), notNullValue());
+               assertThat(getBaseInjector().createChildInjector(
+                               GuiceKt.supply(SoneParser.class).byInstance(mock(SoneParser.class))
+               ).getInstance(SoneDownloader.class), notNullValue());
        }
 
 }
index da5ca50..20edd2c 100644 (file)
@@ -36,6 +36,7 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.ImageBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
@@ -64,8 +65,8 @@ import org.mockito.stubbing.Answer;
  */
 public class SoneParserTest {
 
-       private final Core core = mock(Core.class);
-       private final SoneParser soneParser = new SoneParser(core);
+       private final Database database = mock(Database.class);
+       private final SoneParser soneParser = new SoneParser(database);
        private final Sone sone = mock(Sone.class);
        private FreenetURI requestUri = mock(FreenetURI.class);
        private final PostBuilder postBuilder = mock(PostBuilder.class);
@@ -111,7 +112,7 @@ public class SoneParserTest {
 
        @Before
        public void setupSoneBuilder() {
-               when(core.soneBuilder()).thenAnswer(new Answer<SoneBuilder>() {
+               when(database.newSoneBuilder()).thenAnswer(new Answer<SoneBuilder>() {
                        @Override
                        public SoneBuilder answer(InvocationOnMock invocation) {
                                return new MemorySoneBuilder(null);
@@ -173,7 +174,7 @@ public class SoneParserTest {
                                return post;
                        }
                });
-               when(core.postBuilder()).thenReturn(postBuilder);
+               when(database.newPostBuilder()).thenReturn(postBuilder);
        }
 
        @Before
@@ -237,7 +238,7 @@ public class SoneParserTest {
                                return postReply;
                        }
                });
-               when(core.postReplyBuilder()).thenReturn(postReplyBuilder);
+               when(database.newPostReplyBuilder()).thenReturn(postReplyBuilder);
        }
 
        @Before
@@ -328,12 +329,12 @@ public class SoneParserTest {
                                return album;
                        }
                });
-               when(core.albumBuilder()).thenReturn(albumBuilder);
+               when(database.newAlbumBuilder()).thenReturn(albumBuilder);
        }
 
        @Before
        public void setupAlbums() {
-               when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
+               when(database.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
                        @Override
                        public Album answer(InvocationOnMock invocation)
                        throws Throwable {
@@ -439,12 +440,12 @@ public class SoneParserTest {
                                return image;
                        }
                });
-               when(core.imageBuilder()).thenReturn(imageBuilder);
+               when(database.newImageBuilder()).thenReturn(imageBuilder);
        }
 
        @Before
        public void setupImages() {
-               when(core.getImage(anyString())).thenAnswer(new Answer<Image>() {
+               when(database.getImage(anyString())).thenAnswer(new Answer<Image>() {
                        @Override
                        public Image answer(InvocationOnMock invocation)
                        throws Throwable {