Skip to content

Commit 6a69200

Browse files
authored
modrinth: ensure requested loader for modpack is optional (#291)
1 parent b80e97e commit 6a69200

File tree

6 files changed

+82
-49
lines changed

6 files changed

+82
-49
lines changed

src/main/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private ModrinthPackFetcher buildModpackFetcher(
116116
} else {
117117
return new ModrinthApiPackFetcher(
118118
apiClient, projectRef, this.outputDirectory, this.gameVersion,
119-
this.defaultVersionType, this.loader.asLoader());
119+
defaultVersionType, loader != null ? loader.asLoader() : null);
120120
}
121121
}
122122
}

src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import me.itzg.helpers.modrinth.model.Version;
2626
import me.itzg.helpers.modrinth.model.VersionFile;
2727
import me.itzg.helpers.modrinth.model.VersionType;
28+
import org.jetbrains.annotations.Nullable;
2829
import reactor.core.publisher.Mono;
2930
import reactor.core.scheduler.Schedulers;
3031

@@ -114,7 +115,7 @@ public Mono<List<ResolvedProject>> bulkGetProjects(Stream<ProjectRef> projectRef
114115
* @param gameVersion can be null for any
115116
*/
116117
public Mono<Version> resolveProjectVersion(Project project, ProjectRef projectRef,
117-
Loader loader, String gameVersion,
118+
@Nullable Loader loader, String gameVersion,
118119
VersionType defaultVersionType) {
119120
if (projectRef.hasVersionName()) {
120121
return getVersionsForProject(project.getId(), loader, gameVersion)
@@ -161,7 +162,7 @@ public Mono<Path> downloadMrPack(VersionFile versionFile) {
161162
* @param gameVersion can be null for any
162163
*/
163164
public Mono<List<Version>> getVersionsForProject(String projectIdOrSlug,
164-
Loader loader, String gameVersion
165+
@Nullable Loader loader, String gameVersion
165166
) {
166167
return sharedFetch.fetch(
167168
uriBuilder.resolve("/v2/project/{id|slug}/version",

src/main/java/me/itzg/helpers/modrinth/ModrinthApiPackFetcher.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
import me.itzg.helpers.files.Manifests;
88
import me.itzg.helpers.http.FailedRequestException;
99
import me.itzg.helpers.modrinth.model.*;
10+
import org.jetbrains.annotations.Nullable;
1011
import reactor.core.publisher.Mono;
1112

1213
@Slf4j
1314
public class ModrinthApiPackFetcher implements ModrinthPackFetcher {
14-
private ModrinthApiClient apiClient;
15+
private final ModrinthApiClient apiClient;
1516

16-
private ProjectRef modpackProjectRef;
17+
private final ProjectRef modpackProjectRef;
1718

18-
private Loader modLoaderType;
19-
private String gameVersion;
20-
private VersionType defaultVersionType;
21-
private Path modpackOutputDirectory;
19+
private final Loader modLoaderType;
20+
private final String gameVersion;
21+
private final VersionType defaultVersionType;
22+
private final Path modpackOutputDirectory;
2223

2324
ModrinthApiPackFetcher(
2425
ModrinthApiClient apiClient, ProjectRef projectRef,
2526
Path outputDirectory, String gameVersion,
26-
VersionType defaultVersionType, Loader loader)
27+
VersionType defaultVersionType, @Nullable Loader loader)
2728
{
2829
this.apiClient = apiClient;
2930
this.modpackProjectRef = projectRef;
@@ -38,7 +39,7 @@ public Mono<Path> fetchModpack(ModrinthModpackManifest prevManifest) {
3839
.filter(version -> needsInstall(prevManifest, version))
3940
.flatMap(version ->
4041
Mono.just(ModrinthApiClient.pickVersionFile(version)))
41-
.flatMap(versionFile -> apiClient.downloadMrPack(versionFile));
42+
.flatMap(apiClient::downloadMrPack);
4243
}
4344

4445
private Mono<Version> resolveModpackVersion() {

src/main/java/me/itzg/helpers/modrinth/ResolvedProject.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@RequiredArgsConstructor
88
@Data
9-
class ResolvedProject {
9+
public class ResolvedProject {
1010
final private ProjectRef projectRef;
1111
final private Project project;
1212
}

src/test/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommandTest.java

+48-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package me.itzg.helpers.modrinth;
22

3-
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.io.TempDir;
5-
6-
import static org.assertj.core.api.Assertions.*;
7-
import static me.itzg.helpers.modrinth.ModrinthTestHelpers.*;
83
import static com.github.tomakehurst.wiremock.client.WireMock.*;
9-
10-
import java.io.IOException;
11-
import java.net.URISyntaxException;
12-
import java.nio.file.*;
13-
14-
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import static me.itzg.helpers.modrinth.ModrinthTestHelpers.*;
5+
import static org.assertj.core.api.Assertions.assertThat;
156

167
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
178
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
18-
9+
import java.io.IOException;
10+
import java.net.URISyntaxException;
11+
import java.nio.file.Path;
1912
import me.itzg.helpers.files.Manifests;
20-
import me.itzg.helpers.modrinth.model.*;
13+
import me.itzg.helpers.modrinth.model.ModpackIndex;
2114
import me.itzg.helpers.modrinth.model.ModpackIndex.ModpackFile;
15+
import me.itzg.helpers.modrinth.model.Version;
16+
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.io.TempDir;
2218

2319
@WireMockTest
2420
public class InstallModrinthModpackCommandTest {
25-
private String projectName = "test_project1";
26-
private String projectId = "efgh5678";
27-
private String projectVersionId = "abcd1234";
28-
private Version projectVersion =
21+
private final String projectName = "test_project1";
22+
private final String projectId = "efgh5678";
23+
private final String projectVersionId = "abcd1234";
24+
private final Version projectVersion =
2925
createModrinthProjectVersion(projectVersionId);
3026

3127
static InstallModrinthModpackCommand createInstallModrinthModpackCommand(
@@ -46,7 +42,7 @@ static InstallModrinthModpackCommand createInstallModrinthModpackCommand(
4642
@Test
4743
void downloadsAndInstallsModrinthModpack(
4844
WireMockRuntimeInfo wm, @TempDir Path tempDir
49-
) throws JsonProcessingException, IOException, URISyntaxException
45+
) throws IOException, URISyntaxException
5046
{
5147
String expectedFileData = "some test data";
5248
String relativeFilePath = "test_file";
@@ -71,11 +67,41 @@ void downloadsAndInstallsModrinthModpack(
7167
.isEqualTo(expectedFileData);
7268
}
7369

70+
@Test
71+
void downloadsAndInstallsModrinthModpack_versionNumberAndAnyLoader(
72+
WireMockRuntimeInfo wm, @TempDir Path tempDir
73+
) throws IOException, URISyntaxException
74+
{
75+
String expectedFileData = "some test data";
76+
String relativeFilePath = "test_file";
77+
ModpackFile testFile = createHostedModpackFile(
78+
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
79+
80+
ModpackIndex index = createBasicModpackIndex();
81+
index.getFiles().add(testFile);
82+
83+
String projectVersionNumber = "1.6.1";
84+
stubModrinthModpackApi(
85+
wm, projectName, this.projectId,
86+
createModrinthProjectVersion(projectVersionId).setVersionNumber(projectVersionNumber),
87+
createModrinthPack(index)
88+
);
89+
90+
InstallModrinthModpackCommand commandUT =
91+
createInstallModrinthModpackCommand(wm.getHttpBaseUrl(), tempDir,
92+
projectName, projectVersionNumber, null);
93+
94+
int commandStatus = commandUT.call();
95+
96+
assertThat(commandStatus).isEqualTo(0);
97+
assertThat(tempDir.resolve(relativeFilePath)).content()
98+
.isEqualTo(expectedFileData);
99+
}
100+
74101
@Test
75102
void createsModrinthModpackManifestForModpackInstallation(
76103
WireMockRuntimeInfo wm, @TempDir Path tempDir
77-
) throws JsonProcessingException, IOException, URISyntaxException
78-
{
104+
) throws IOException {
79105
ModpackIndex index = createBasicModpackIndex();
80106

81107
stubModrinthModpackApi(
@@ -107,7 +133,7 @@ void createsModrinthModpackManifestForModpackInstallation(
107133
@Test
108134
void removesFilesNoLongerNeedeByUpdatedModpack(
109135
WireMockRuntimeInfo wm, @TempDir Path tempDir
110-
) throws JsonProcessingException, IOException, URISyntaxException
136+
) throws IOException, URISyntaxException
111137
{
112138
String expectedFileData = "some test data";
113139
String relativeFilePath = "test_file";
@@ -146,7 +172,7 @@ void removesFilesNoLongerNeedeByUpdatedModpack(
146172
@Test
147173
void downloadsAndInstallsGenericModpacksOverHttp(
148174
WireMockRuntimeInfo wm, @TempDir Path tempDir
149-
) throws JsonProcessingException, IOException, URISyntaxException
175+
) throws IOException, URISyntaxException
150176
{
151177
String expectedFileData = "some test data";
152178
String relativeFilePath = "test_file";

src/test/java/me/itzg/helpers/modrinth/ModrinthTestHelpers.java

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package me.itzg.helpers.modrinth;
22

3+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
38
import java.io.ByteArrayOutputStream;
49
import java.io.IOException;
510
import java.net.URI;
@@ -9,29 +14,25 @@
914
import java.util.List;
1015
import java.util.zip.ZipEntry;
1116
import java.util.zip.ZipOutputStream;
12-
13-
import com.fasterxml.jackson.core.JsonProcessingException;
14-
import com.fasterxml.jackson.databind.JsonNode;
15-
import com.fasterxml.jackson.databind.ObjectMapper;
16-
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
17-
import static com.github.tomakehurst.wiremock.client.WireMock.*;
18-
19-
import me.itzg.helpers.modrinth.model.*;
17+
import me.itzg.helpers.modrinth.model.DependencyId;
18+
import me.itzg.helpers.modrinth.model.ModpackIndex;
19+
import me.itzg.helpers.modrinth.model.Project;
20+
import me.itzg.helpers.modrinth.model.Version;
21+
import me.itzg.helpers.modrinth.model.VersionFile;
2022

2123
class ModrinthTestHelpers {
2224
static final ObjectMapper mapper = new ObjectMapper();
2325

2426
static Version createModrinthProjectVersion(String versionId) {
2527
return new Version()
2628
.setId(versionId)
27-
.setFiles(new ArrayList<VersionFile>());
29+
.setFiles(new ArrayList<>());
2830
}
2931

3032
static void stubModrinthModpackApi(
3133
WireMockRuntimeInfo wm, String projectName, String projectId,
3234
Version projectVersion, byte[] expectedData
33-
) throws JsonProcessingException, IOException
34-
{
35+
) {
3536
String modpackDownloadPath = "/download/test_project1.mrpack";
3637

3738
JsonNode responseProject = mapper.valueToTree(
@@ -45,7 +46,7 @@ static void stubModrinthModpackApi(
4546
.setUrl(wm.getHttpBaseUrl() + modpackDownloadPath));
4647
JsonNode responseVersion = mapper.valueToTree(projectVersion);
4748

48-
List<Version> projectVersionList = new ArrayList<Version>();
49+
List<Version> projectVersionList = new ArrayList<>();
4950
projectVersionList.add(projectVersion);
5051
JsonNode responseVersionList = mapper.valueToTree(projectVersionList);
5152

@@ -58,6 +59,10 @@ static void stubModrinthModpackApi(
5859
.willReturn(ok()
5960
.withHeader("Content-Type", "application/json")
6061
.withJsonBody(responseVersionList)));
62+
stubFor(get(urlPathMatching("/v2/project/" + projectId + "/version"))
63+
.willReturn(ok()
64+
.withHeader("Content-Type", "application/json")
65+
.withJsonBody(responseVersionList)));
6166
stubFor(get("/v2/version/" + projectVersion.getId())
6267
.willReturn(ok()
6368
.withHeader("Content-Type", "application/json")
@@ -72,8 +77,8 @@ static ModpackIndex createBasicModpackIndex() {
7277
ModpackIndex index = new ModpackIndex()
7378
.setName(null)
7479
.setGame("minecraft")
75-
.setDependencies(new HashMap<DependencyId, String>())
76-
.setFiles(new ArrayList<ModpackIndex.ModpackFile>())
80+
.setDependencies(new HashMap<>())
81+
.setFiles(new ArrayList<>())
7782
.setVersionId(null);
7883
index.getDependencies().put(DependencyId.minecraft, "1.20.1");
7984

@@ -105,7 +110,7 @@ static ModpackIndex.ModpackFile createHostedModpackFile(
105110
.willReturn(ok().withBody(data)));
106111

107112
ModpackIndex.ModpackFile modpackFile = new ModpackIndex.ModpackFile()
108-
.setDownloads(new ArrayList<URI>())
113+
.setDownloads(new ArrayList<>())
109114
.setPath(relativeFileLocation);
110115
modpackFile.getDownloads().add(
111116
new URI(wmBaseUrl + "/files/" + relativeFileLocation));

0 commit comments

Comments
 (0)