Skip to content

Commit 3ee53a4

Browse files
committed
Integration test for Export
1 parent 20a6dbf commit 3ee53a4

File tree

14 files changed

+149
-58
lines changed

14 files changed

+149
-58
lines changed

src/main/java/org/hydev/mcpm/client/arguments/CommandsFactory.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,23 @@
22

33
import org.hydev.mcpm.client.DatabaseManager;
44
import org.hydev.mcpm.client.Downloader;
5-
import org.hydev.mcpm.client.arguments.parsers.CommandParser;
6-
import org.hydev.mcpm.client.arguments.parsers.ExportPluginsParser;
7-
import org.hydev.mcpm.client.arguments.parsers.InfoParser;
8-
import org.hydev.mcpm.client.arguments.parsers.InstallParser;
9-
import org.hydev.mcpm.client.arguments.parsers.ListParser;
10-
import org.hydev.mcpm.client.arguments.parsers.LoadParser;
11-
import org.hydev.mcpm.client.arguments.parsers.MirrorParser;
12-
import org.hydev.mcpm.client.arguments.parsers.PageParser;
13-
import org.hydev.mcpm.client.arguments.parsers.RefreshParser;
14-
import org.hydev.mcpm.client.arguments.parsers.ReloadParser;
15-
import org.hydev.mcpm.client.arguments.parsers.SearchParser;
16-
import org.hydev.mcpm.client.arguments.parsers.UninstallParser;
17-
import org.hydev.mcpm.client.arguments.parsers.UnloadParser;
18-
import org.hydev.mcpm.client.arguments.parsers.UpdateParser;
19-
import org.hydev.mcpm.client.commands.controllers.ExportPluginsController;
20-
import org.hydev.mcpm.client.commands.controllers.InfoController;
21-
import org.hydev.mcpm.client.commands.controllers.InstallController;
22-
import org.hydev.mcpm.client.commands.controllers.ListController;
23-
import org.hydev.mcpm.client.commands.controllers.LoadController;
24-
import org.hydev.mcpm.client.commands.controllers.MirrorController;
25-
import org.hydev.mcpm.client.commands.controllers.PageController;
26-
import org.hydev.mcpm.client.commands.controllers.RefreshController;
27-
import org.hydev.mcpm.client.commands.controllers.ReloadController;
28-
import org.hydev.mcpm.client.commands.controllers.SearchPackagesController;
29-
import org.hydev.mcpm.client.commands.controllers.UninstallController;
30-
import org.hydev.mcpm.client.commands.controllers.UnloadController;
31-
import org.hydev.mcpm.client.commands.controllers.UpdateController;
5+
import org.hydev.mcpm.client.arguments.parsers.*;
6+
import org.hydev.mcpm.client.commands.controllers.*;
327
import org.hydev.mcpm.client.display.presenters.LogExportPresenter;
33-
import org.hydev.mcpm.client.export.PasteBinStorage;
34-
import org.hydev.mcpm.client.updater.CheckForUpdatesInteractor;
35-
import org.hydev.mcpm.client.list.ListAllInteractor;
36-
import org.hydev.mcpm.client.local.LocalPluginTracker;
37-
import org.hydev.mcpm.client.matcher.MatchPluginsInteractor;
38-
import org.hydev.mcpm.client.export.ExportInteractor;
39-
import org.hydev.mcpm.client.local.LocalDatabaseFetcher;
408
import org.hydev.mcpm.client.display.progress.ProgressBarFetcherListener;
41-
import org.hydev.mcpm.client.local.MirrorSelector;
42-
import org.hydev.mcpm.client.search.SearchInteractor;
9+
import org.hydev.mcpm.client.export.ExportInteractor;
4310
import org.hydev.mcpm.client.injector.LocalJarFinder;
4411
import org.hydev.mcpm.client.injector.PluginLoader;
4512
import org.hydev.mcpm.client.installer.InstallInteractor;
4613
import org.hydev.mcpm.client.installer.SpigotPluginDownloader;
14+
import org.hydev.mcpm.client.list.ListAllInteractor;
15+
import org.hydev.mcpm.client.local.LocalDatabaseFetcher;
16+
import org.hydev.mcpm.client.local.LocalPluginTracker;
17+
import org.hydev.mcpm.client.local.MirrorSelector;
18+
import org.hydev.mcpm.client.matcher.MatchPluginsInteractor;
19+
import org.hydev.mcpm.client.search.SearchInteractor;
4720
import org.hydev.mcpm.client.uninstall.Uninstaller;
21+
import org.hydev.mcpm.client.updater.CheckForUpdatesInteractor;
4822
import org.hydev.mcpm.client.updater.UpdateInteractor;
4923

5024
import java.util.List;
@@ -84,11 +58,9 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
8458
var updateChecker = new CheckForUpdatesInteractor(matcher);
8559
var updater = new UpdateInteractor(updateChecker, installer, tracker);
8660

87-
var storage = new PasteBinStorage();
88-
8961
// Controllers
9062
var exportPluginsController = new ExportPluginsController(
91-
new ExportInteractor(tracker, storage), new LogExportPresenter());
63+
new ExportInteractor(tracker), new LogExportPresenter());
9264
var listController = new ListController(new ListAllInteractor(tracker));
9365
var searchController = new SearchPackagesController(searcher, pager);
9466
var mirrorController = new MirrorController(mirror);

src/main/java/org/hydev/mcpm/client/arguments/parsers/ExportPluginsParser.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.hydev.mcpm.client.commands.controllers.ExportPluginsController;
66
import org.hydev.mcpm.client.export.ExportPluginsInput;
77

8-
import java.io.OutputStream;
98
import java.util.function.Consumer;
109

1110
/**
@@ -35,15 +34,15 @@ public String description()
3534
@Override
3635
public void configure(Subparser parser)
3736
{
38-
parser.addArgument("outfile") // add optional output file
39-
.type(OutputStream.class).dest("outfile"); // of type OutputStream
40-
parser.addArgument("-c", "--cache")
41-
.type(boolean.class).dest("cache");
37+
parser.addArgument("type").nargs("?").setDefault("pastebin") // type of output
38+
.type(String.class).dest("type"); // of type OutputStream
39+
parser.addArgument("out").nargs("?")
40+
.type(String.class).dest("out");
4241
}
4342

4443
@Override
4544
public void run(Namespace details, Consumer<String> log)
4645
{
47-
controller.export(new ExportPluginsInput(details.get("cache")), log);
46+
controller.export(new ExportPluginsInput(details.get("type"), details.get("out")), log);
4847
}
4948
}

src/main/java/org/hydev/mcpm/client/commands/controllers/ExportPluginsController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.hydev.mcpm.client.commands.controllers;
22

3-
import org.hydev.mcpm.client.display.presenters.LogExportPresenter;
3+
import org.hydev.mcpm.client.commands.presenters.ExportPresenter;
44
import org.hydev.mcpm.client.export.ExportPluginsBoundary;
55
import org.hydev.mcpm.client.export.ExportPluginsInput;
66

@@ -14,7 +14,7 @@
1414
*/
1515
public record ExportPluginsController(
1616
ExportPluginsBoundary boundary,
17-
LogExportPresenter presenter
17+
ExportPresenter presenter
1818
) {
1919

2020
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hydev.mcpm.client.database.tracker;
2+
3+
import org.hydev.mcpm.client.local.LocalPluginTracker;
4+
import org.hydev.mcpm.client.models.PluginModel;
5+
import org.hydev.mcpm.client.models.PluginYml;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* A MockPluginTracker that only stores the yml.
12+
*/
13+
public class MockPluginTracker extends LocalPluginTracker {
14+
List<PluginYml> ymls;
15+
16+
/**
17+
* Creates a MockPluginTracker with the given plugins.
18+
*
19+
* @param plugins the given plugins
20+
*/
21+
public MockPluginTracker(List<PluginModel> plugins) {
22+
ymls = new ArrayList<>();
23+
for (var plugin : plugins) {
24+
ymls.add(plugin.getLatestPluginVersion().get().meta());
25+
}
26+
}
27+
28+
@Override
29+
public List<PluginYml> listInstalled() {
30+
return ymls;
31+
}
32+
}

src/main/java/org/hydev/mcpm/client/display/presenters/LogExportPresenter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ private String getColor(ExportPluginsResult exportPluginsResult) {
1919

2020
private String getMessage(ExportPluginsResult exportPluginsResult) {
2121
return switch ((exportPluginsResult.state())) {
22-
case SUCCESS -> exportPluginsResult.export();
23-
case FAILED_TO_FETCH_PLUGINS -> "Failed to fetch plugins";
22+
case SUCCESS -> "Exported to " + exportPluginsResult.export();
23+
case FAILED_TO_FETCH_PLUGINS -> "Export failed to fetch plugins";
2424
};
2525
}
2626

src/main/java/org/hydev/mcpm/client/export/ExportInteractor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import org.hydev.mcpm.client.database.tracker.PluginTracker;
5+
import org.hydev.mcpm.client.export.storage.StringStorage;
6+
import org.hydev.mcpm.client.export.storage.StringStorageFactory;
57

68

9+
import java.io.IOException;
10+
711
import static org.hydev.mcpm.Constants.JACKSON;
812

913
/**
1014
* An implementation of ExportPluginsBoundary that fetches from a database.
1115
*/
12-
public record ExportInteractor(PluginTracker tracker, StringStorage storage) implements ExportPluginsBoundary {
16+
public record ExportInteractor(PluginTracker tracker) implements ExportPluginsBoundary {
1317

1418
/**
1519
* Outputs the plugins on each line as its name and version separated by a space.
@@ -27,10 +31,14 @@ public ExportPluginsResult export(ExportPluginsInput input) {
2731

2832
try {
2933
var answer = JACKSON.writeValueAsString(models);
30-
return new ExportPluginsResult(ExportPluginsResult.State.SUCCESS, answer);
34+
StringStorage storage = StringStorageFactory.createStringStorage(input);
35+
var token = storage.store(answer);
36+
return new ExportPluginsResult(ExportPluginsResult.State.SUCCESS, token);
3137
} catch (JsonProcessingException e) {
3238
// Should never happen
3339
throw new RuntimeException(e);
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
3442
}
3543
}
3644
}

src/main/java/org/hydev/mcpm/client/export/ExportPluginsInput.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
/**
44
* Input for the ExportPluginsBoundary boundary.
55
*
6-
* @param pastebin Link to the pastebin to write to
6+
* @param type The type of export
7+
* @param out String describing what to write to
78
*/
89
public record ExportPluginsInput(
9-
String pastebin
10+
String type,
11+
String out
1012
) {
1113
}

src/main/java/org/hydev/mcpm/client/export/FixedFileStorage.java renamed to src/main/java/org/hydev/mcpm/client/export/storage/FixedFileStorage.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package org.hydev.mcpm.client.export;
1+
package org.hydev.mcpm.client.export.storage;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.nio.charset.StandardCharsets;
56
import java.nio.file.Files;
@@ -10,6 +11,11 @@
1011
*/
1112
public record FixedFileStorage(Path path) implements StringStorage
1213
{
14+
public FixedFileStorage(String path) {
15+
this(new File(path).toPath());
16+
}
17+
18+
1319
@Override
1420
public String store(String content) throws IOException
1521
{

src/main/java/org/hydev/mcpm/client/export/PasteBinStorage.java renamed to src/main/java/org/hydev/mcpm/client/export/storage/PasteBinStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package org.hydev.mcpm.client.export;
1+
package org.hydev.mcpm.client.export.storage;
22

33
import org.apache.hc.client5.http.fluent.Request;
44
import org.apache.hc.core5.http.ContentType;
55

66
import java.io.IOException;
77

88
/**
9-
* Store string content to a pastebin that supports raw text requests (e.g. https://github.com/w4/bin)
9+
* Store string content to a pastebin that supports raw text requests (e.g. <a href="https://github.com/w4/bin">w4bin</a>)
1010
*
1111
* @param host Pastebin host URL
1212
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.hydev.mcpm.client.export.storage;
2+
3+
import java.io.IOException;
4+
5+
public class StringLiteralStorage implements StringStorage {
6+
@Override
7+
public String store(String content) {
8+
return content;
9+
}
10+
11+
@Override
12+
public String load(String token) throws IOException {
13+
return token;
14+
}
15+
16+
@Override
17+
public String instruction() {
18+
return "Your content is saved in the memory. It will disappear";
19+
}
20+
}

0 commit comments

Comments
 (0)