From: David ‘Bombe’ Roden Date: Mon, 26 Feb 2018 06:00:04 +0000 (+0100) Subject: Use database instead of Core in Sone parser X-Git-Tag: v79^2~135 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2493e844ddeece59211c6a3c40e7ecc5d2cdb8b5;p=Sone.git Use database instead of Core in Sone parser --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index 9f0559c..f3d5068 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -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; diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index fdbd9ae..a9e2b3a 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -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); diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index f879cac..bac5f58 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -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()); } } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java index da5ca50..20edd2c 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -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() { + when(database.newSoneBuilder()).thenAnswer(new Answer() { @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() { + when(database.getAlbum(anyString())).thenAnswer(new Answer() { @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() { + when(database.getImage(anyString())).thenAnswer(new Answer() { @Override public Image answer(InvocationOnMock invocation) throws Throwable {