-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract loading of overallContribution to a class
- Loading branch information
1 parent
9157a49
commit c2ce4f4
Showing
7 changed files
with
138 additions
and
31 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/java/org/araymond/joalcore/config/ConfigSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.araymond.joalcore.config; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public interface ConfigSupplier<T> extends Supplier<T> { | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/araymond/joalcore/core/sharing/application/OverallContributionsLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.araymond.joalcore.core.sharing.application; | ||
|
||
import org.araymond.joalcore.config.ConfigSupplier; | ||
import org.araymond.joalcore.core.metadata.domain.InfoHash; | ||
import org.araymond.joalcore.core.metadata.domain.TorrentMetadata; | ||
import org.araymond.joalcore.core.sharing.domain.Contribution; | ||
import org.araymond.joalcore.core.sharing.domain.DownloadAmount; | ||
import org.araymond.joalcore.core.sharing.domain.UploadAmount; | ||
|
||
public class OverallContributionsLoader { | ||
private final OverallContributionsRepository repo; | ||
private final ConfigSupplier<Boolean> skipDownload; | ||
|
||
public OverallContributionsLoader(OverallContributionsRepository overallContributions, ConfigSupplier<Boolean> skipDownload) { | ||
this.repo = overallContributions; | ||
this.skipDownload = skipDownload; | ||
} | ||
|
||
public Contribution load(TorrentMetadata metadata) { | ||
return this.repo.load(metadata.infoHash()) | ||
.orElseGet(() -> { | ||
Contribution overall = Contribution.ZERO; | ||
if (skipDownload.get()) { | ||
// return a fully Downloaded contribution when the torrent is not yet known and skip download is true | ||
overall = new Contribution(new DownloadAmount(metadata.size().bytes()), new UploadAmount(0)); | ||
} | ||
this.repo.save(metadata.infoHash(), overall); | ||
|
||
return overall; | ||
}); | ||
} | ||
|
||
public void save(InfoHash infoHash, Contribution contribution){ | ||
repo.save(infoHash, contribution); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...t/java/org/araymond/joalcore/core/sharing/application/OverallContributionsLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.araymond.joalcore.core.sharing.application; | ||
|
||
import org.araymond.joalcore.config.ConfigSupplier; | ||
import org.araymond.joalcore.core.fixtures.TestFixtures; | ||
import org.araymond.joalcore.core.metadata.domain.InfoHash; | ||
import org.araymond.joalcore.core.metadata.domain.TorrentMetadata; | ||
import org.araymond.joalcore.core.metadata.domain.TorrentSize; | ||
import org.araymond.joalcore.core.sharing.domain.Contribution; | ||
import org.araymond.joalcore.core.sharing.domain.DownloadAmount; | ||
import org.araymond.joalcore.core.sharing.domain.UploadAmount; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class OverallContributionsLoaderTest { | ||
private static final ConfigSupplier<Boolean> SKIP_DOWNLOAD = () -> true; | ||
private static final ConfigSupplier<Boolean> DO_NOT_SKIP_DOWNLOAD = () -> false; | ||
|
||
@Test | ||
public void shouldReturnContributionFullyDownloadedWhenTorrentIsNotKnownYetAndSkipDownloadIsTrue() { | ||
var loader = new OverallContributionsLoader(new DumbRepo(), SKIP_DOWNLOAD); | ||
var metadata = new TorrentMetadata(TestFixtures.randomInfoHash(), TorrentSize.ofBytes(7000)); | ||
var contrib = loader.load(metadata); | ||
|
||
assertThat(contrib).isEqualTo(new Contribution( | ||
new DownloadAmount(metadata.size().bytes()), | ||
new UploadAmount(0) | ||
)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnContributionZeroWhenTorrentIsNotKnownYetAndSkipDownloadIsFalse() { | ||
var loader = new OverallContributionsLoader(new DumbRepo(), DO_NOT_SKIP_DOWNLOAD); | ||
var metadata = new TorrentMetadata(TestFixtures.randomInfoHash(), TorrentSize.ofBytes(7000)); | ||
var contrib = loader.load(metadata); | ||
|
||
assertThat(contrib).isEqualTo(Contribution.ZERO); | ||
} | ||
|
||
@Test | ||
public void shouldReturnKnownContributionZeroWhenTorrentIsKnown() { | ||
var repo = new DumbRepo(); | ||
var loader = new OverallContributionsLoader(repo, DO_NOT_SKIP_DOWNLOAD); | ||
var metadata = new TorrentMetadata(TestFixtures.randomInfoHash(), TorrentSize.ofBytes(7000)); | ||
|
||
repo.contributions.put(metadata.infoHash(), new Contribution(new DownloadAmount(400), new UploadAmount(545))); | ||
|
||
var contrib = loader.load(metadata); | ||
|
||
assertThat(contrib).isEqualTo(new Contribution(new DownloadAmount(400), new UploadAmount(545))); | ||
} | ||
|
||
private static final class DumbRepo implements OverallContributionsRepository { | ||
private final Map<InfoHash, Contribution> contributions = new HashMap<>(); | ||
|
||
@Override | ||
public Optional<Contribution> load(InfoHash infoHash) { | ||
return Optional.ofNullable(contributions.get(infoHash)); | ||
} | ||
|
||
@Override | ||
public void save(InfoHash infoHash, Contribution contribution) { | ||
contributions.put(infoHash, contribution); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters