Skip to content

Commit

Permalink
feat: Moved tests and made basic test for option ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Brancieq-Paul committed May 4, 2024
1 parent 5f48b1e commit 850ee5f
Show file tree
Hide file tree
Showing 41 changed files with 133 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.*;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.AnnotatedTypeAdapterFactory;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.OptionPathPart;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.SimpleOptionPathPart;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand All @@ -19,7 +20,11 @@ public OptionPath deserialize(JsonElement json, Type typeOfT, JsonDeserializatio
} else if (json.isJsonArray()) {
List<OptionPathPart> optionPathParts = new ArrayList<>();
for (JsonElement jsonElement : json.getAsJsonArray()) {
optionPathParts.add(gson.fromJson(jsonElement, OptionPathPart.class));
if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()) {
optionPathParts.addAll(SimpleOptionPathPart.fromMultiPartString(jsonElement.getAsString()));
} else {
optionPathParts.add(gson.fromJson(jsonElement, OptionPathPart.class));
}
}
return new OptionPath(optionPathParts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public OptionRef deserialize(JsonElement json, Type typeOfT, JsonDeserialization
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapterFactory(new AnnotatedTypeAdapterFactory()).create();
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
return new OptionRef(json.getAsString());
try {
return new OptionRef(json.getAsString());
} catch (IllegalArgumentException e) {
throw new JsonParseException(e.getMessage());
}
} else if (json.isJsonObject()) {
if (!json.getAsJsonObject().has("filepath") || !json.getAsJsonObject().get("filepath").isJsonPrimitive() ||
!json.getAsJsonObject().get("filepath").getAsJsonPrimitive().isString()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.JsonAdapter;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.JsonRequired;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.type.FilterValidatorType;
import org.jetbrains.annotations.Nullable;

@JsonAdapter(FilterOptionPathPartValidatorJsonDeserializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.AnnotatedTypeAdapterFactory;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.type.FilterValidatorType;

import java.lang.reflect.Type;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

public class FilterValidatorDeserializer implements JsonDeserializer<FilterValidatorType> {
@Override
public FilterValidatorType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
return FilterValidatorType.fromString(json.getAsString());
} catch (Exception e) {
throw new JsonParseException("Invalid FilterValidatorType: " + json.getAsString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators;
package fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.type;

import com.google.gson.annotations.JsonAdapter;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.FilterOptionPathPartValidator;
import fr.paulbrancieq.modpackconfigupdater.remake.path.pathpart.filters.validators.RangeFilterOptionPathPartValidator;

@JsonAdapter(FilterValidatorDeserializer.class)
public enum FilterValidatorType {
RANGE("range", RangeFilterOptionPathPartValidator.class); // TODO: Add more filter types
// Note for myself for when adding more filters: You have already done all that is needed to verify the filter fields.
Expand All @@ -20,4 +25,13 @@ public String getTypeString() {
public Class<? extends FilterOptionPathPartValidator> getFilterValidatorOptionPathPartClass() {
return filterValidatorOptionPathPartClass;
}

public static FilterValidatorType fromString(String type) {
for (FilterValidatorType filterValidatorType : FilterValidatorType.values()) {
if (filterValidatorType.getTypeString().equals(type)) {
return filterValidatorType;
}
}
throw new IllegalArgumentException("Unknown filter validator type: " + type);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest;

import com.google.gson.JsonParseException;

import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.basictestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.basictestfiles.McuFileData;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.basictestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.basictestfiles.McuFileData;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class BasicMcuJsonFileTest {
@Test
public void testFieldRetrieve() {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/basictestfiles/basic.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/basictestfiles/basic.json");
McuFileData mcuData = mcuFile.getData();
assertEquals("testId", mcuData.getId());
assertEquals("testName", mcuData.getName());
Expand All @@ -20,20 +20,20 @@ public void testFieldRetrieve() {
@Test
public void testMissingVersionField() {
assertThrows(JsonParseException.class, () -> {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/basictestfiles/missingVersionField.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/basictestfiles/missingVersionField.json");
});
}

@Test
public void testMissingRequiredField() {
assertThrows(JsonParseException.class, () -> {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/basictestfiles/missingRequiredField.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/basictestfiles/missingRequiredField.json");
});
}

@Test
public void testMissingOptionalField() {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/basictestfiles/missingOptionalField.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/basictestfiles/missingOptionalField.json");
McuFileData mcuData = mcuFile.getData();
assertEquals("test", mcuData.getId());
assertNull(mcuData.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest;

import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles.ComplexSubType;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles.McuFileData;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles.McuFileData;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles.ComplexSubType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ComplexSubTypeMcuJsonFileTest {
@Test
public void testFieldRetrieve() {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/complexsubtypetestfiles/basic.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/complexsubtypetestfiles/basic.json");
McuFileData mcuData = mcuFile.getData();
ComplexSubType object = mcuData.object;
assertEquals("test", object.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest;

import com.google.gson.JsonParseException;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles.McuFileData;
import fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles.SubType;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles.McuFileData;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles.McuFile;
import fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles.SubType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -12,7 +12,7 @@
public class SubTypeMcuJsonFileTest {
@Test
public void testFieldRetrieve() {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/subtypetestfiles/basic.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/subtypetestfiles/basic.json");
McuFileData mcuData = mcuFile.getData();
SubType object = mcuData.object;
assertEquals("test", object.id);
Expand All @@ -25,7 +25,7 @@ public void testFieldRetrieve() {
@Test
public void testMissingRequiredField() {
assertThrows(JsonParseException.class, () -> {
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/newtests/mcujsonfiletest/subtypetestfiles/missingRequiredField.json");
McuFile mcuFile = new McuFile("src/test/java/fr/paulbrancieq/modpackconfigupdater/mcujsonfiletest/subtypetestfiles/missingRequiredField.json");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.basictestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.basictestfiles;

import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.McuJsonFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.basictestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.basictestfiles;

import com.google.gson.annotations.Expose;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.JsonRequired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.JsonAdapter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles;

import com.google.gson.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles;

import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.McuJsonFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.complexsubtypetestfiles;

import com.google.gson.annotations.Expose;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.JsonRequired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.complexsubtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles;

import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.McuJsonFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles;

import com.google.gson.annotations.Expose;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.JsonRequired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.paulbrancieq.modpackconfigupdater.newtests.mcujsonfiletest.subtypetestfiles;
package fr.paulbrancieq.modpackconfigupdater.mcujsonfiletest.subtypetestfiles;

import com.google.gson.annotations.Expose;
import fr.paulbrancieq.modpackconfigupdater.remake.mcufile.JsonRequired;
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 850ee5f

Please sign in to comment.