diff --git a/.gradle/8.5/checksums/checksums.lock b/.gradle/8.5/checksums/checksums.lock index 6eaf5635..6e08c8dc 100644 Binary files a/.gradle/8.5/checksums/checksums.lock and b/.gradle/8.5/checksums/checksums.lock differ diff --git a/.gradle/8.5/checksums/md5-checksums.bin b/.gradle/8.5/checksums/md5-checksums.bin index c6db1e7d..4bee533e 100644 Binary files a/.gradle/8.5/checksums/md5-checksums.bin and b/.gradle/8.5/checksums/md5-checksums.bin differ diff --git a/.gradle/8.5/checksums/sha1-checksums.bin b/.gradle/8.5/checksums/sha1-checksums.bin index 42173c58..58c6af4a 100644 Binary files a/.gradle/8.5/checksums/sha1-checksums.bin and b/.gradle/8.5/checksums/sha1-checksums.bin differ diff --git a/.gradle/8.5/executionHistory/executionHistory.lock b/.gradle/8.5/executionHistory/executionHistory.lock index 0e02e946..c0e47d3f 100644 Binary files a/.gradle/8.5/executionHistory/executionHistory.lock and b/.gradle/8.5/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.5/fileHashes/fileHashes.lock b/.gradle/8.5/fileHashes/fileHashes.lock index b04e7be0..4dd1018a 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.lock and b/.gradle/8.5/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 4be9fb7c..6a9f825e 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.idea/modules/Flames.test.iml b/.idea/modules/Flames.test.iml index bc168bb7..f60a3c6f 100644 --- a/.idea/modules/Flames.test.iml +++ b/.idea/modules/Flames.test.iml @@ -1,8 +1,30 @@ - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 7910bb3a..dd044371 100644 --- a/build.gradle +++ b/build.gradle @@ -54,4 +54,6 @@ dependencies { implementation 'com.bugsnag:bugsnag:3.6.2' implementation 'org.yaml:snakeyaml:1.33' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' + testImplementation 'org.mockito:mockito-core:5.12.0' + testImplementation 'net.bytebuddy:byte-buddy-agent' } \ No newline at end of file diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index e0588c2c..de3394a4 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/com/severalcircles/flames/Flames.java b/src/main/java/com/severalcircles/flames/Flames.java index 6024fd5e..11ad6829 100644 --- a/src/main/java/com/severalcircles/flames/Flames.java +++ b/src/main/java/com/severalcircles/flames/Flames.java @@ -76,8 +76,10 @@ public static void main(String[] args) throws IOException { Locale.setDefault(Locale.ENGLISH); properties.load(is); version = properties.getProperty("version"); - try {new DataUpgradeUtil().upgradeData();} - catch (RuntimeException ignored) {} + if (args.length > 0 && args[0].equals("RunUpgrade")) { + new DataUpgradeUtil().upgradeData(); + System.exit(0); + } FlamesDataManager.prepare(); String logName = "Flames " + version + "@" + InetAddress.getLocalHost().getHostName() + " " + Instant.now().truncatedTo(ChronoUnit.SECONDS).toString().replace(":", " ").replace("T", " T") + ".log"; File logDir = new File(FlamesDataManager.FLAMES_DIRECTORY.getAbsolutePath() + "/logs"); diff --git a/src/main/java/com/severalcircles/flames/data/DataUpgradeUtil.java b/src/main/java/com/severalcircles/flames/data/DataUpgradeUtil.java index 5561fd3c..00e8836d 100644 --- a/src/main/java/com/severalcircles/flames/data/DataUpgradeUtil.java +++ b/src/main/java/com/severalcircles/flames/data/DataUpgradeUtil.java @@ -8,11 +8,9 @@ import com.severalcircles.flames.data.legacy.server.LegacyFlamesServer; import com.severalcircles.flames.data.legacy.user.LegacyFlamesUser; import com.severalcircles.flames.data.user.FlamesUser; -import jdk.jpackage.internal.Log; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.logging.Logger; @@ -23,9 +21,6 @@ public class DataUpgradeUtil { private final List flamesUsers = new LinkedList<>(); private final List flamesServers = new LinkedList<>(); public void upgradeData() { - if (new File("donotupgrade").exists()) { - throw new RuntimeException("Data has already been upgraded"); - } LegacyFlamesDataManager.prepare(); FlamesDataManager.prepare(); File userDirectory = LegacyFlamesDataManager.USER_DIRECTORY; @@ -62,11 +57,6 @@ public void upgradeData() { }); Logger.getGlobal().info("Saving servers"); flamesServers.forEach(FlamesDataManager::saveServer); - try { - new File("donotupgrade").createNewFile(); - } catch (IOException e) { - throw new RuntimeException(e); - } } public static void main(String[] args) { new DataUpgradeUtil().upgradeData(); diff --git a/src/main/java/com/severalcircles/flames/data/FlamesDataManager.java b/src/main/java/com/severalcircles/flames/data/FlamesDataManager.java index 9d76bbf4..906d84fc 100644 --- a/src/main/java/com/severalcircles/flames/data/FlamesDataManager.java +++ b/src/main/java/com/severalcircles/flames/data/FlamesDataManager.java @@ -80,11 +80,11 @@ public static FlamesUser getUser(String id, boolean skipConsent) throws ConsentE if (!userFile.exists()) { FlamesUser newUser = new FlamesUser(id); Files.write(userFile.toPath(), yaml.dump(newUser).getBytes()); - return newUser; + if (!skipConsent) throw new ConsentException(0); + else return newUser; } String contents = new String(Files.readAllBytes(userFile.toPath())); FlamesUser user = yaml.loadAs(contents, FlamesUser.class); - if (user.getId().equals("797283404654575657")) Logger.getGlobal().warning("An operation attempted to save user 797283404654575657"); if (user.getConsent() != 1 && !skipConsent) throw new ConsentException(user.getConsent()); else return user; } diff --git a/src/main/java/com/severalcircles/flames/data/FlamesDatatype.java b/src/main/java/com/severalcircles/flames/data/FlamesDatatype.java index a4804072..0e83d899 100644 --- a/src/main/java/com/severalcircles/flames/data/FlamesDatatype.java +++ b/src/main/java/com/severalcircles/flames/data/FlamesDatatype.java @@ -4,8 +4,10 @@ package com.severalcircles.flames.data; -import org.yaml.snakeyaml.Yaml; - +/** + * The FlamesDatatype class is an abstract class that represents a generic Flames data type. + * It provides a method for retrieving the ID of the data object. + */ public abstract class FlamesDatatype { public abstract String getID(); } diff --git a/src/main/java/com/severalcircles/flames/data/FlamesServer.java b/src/main/java/com/severalcircles/flames/data/FlamesServer.java index 41459cc7..629f16e6 100644 --- a/src/main/java/com/severalcircles/flames/data/FlamesServer.java +++ b/src/main/java/com/severalcircles/flames/data/FlamesServer.java @@ -10,69 +10,187 @@ import java.util.HashMap; import java.util.Map; +/** + * The FlamesServer class represents a Discord server in the Flames system. + * It extends the FlamesDatatype class. + */ public class FlamesServer extends FlamesDatatype { + /** + * The variable "id" is a string that represents the ID of a FlamesServer object. + * It is used to uniquely identify a server in the Flames system. + * + * This variable is declared in the FlamesServer class, which represents a Discord server in the Flames system. + * The FlamesServer class extends the FlamesDatatype class and defines various fields and methods related to a server. + * + * Below are some related methods in the FlamesServer class: + * + * - setId(String id): This method sets the ID of the server. + * + * - getId(): This method returns the ID of the server. + * + * - getHootenannyScores(): This method returns a map of hootenanny scores for the server. + * + * - setHootenannyScores(Map hootenannyScores): This method sets the map of hootenanny scores for the server. + * + * - todayIsHootenannyDay(): This method checks if today is a hootenanny day for the server. + * + * The "id" variable is also used in the LegacyFlamesServer class, which is a legacy implementation of the FlamesServer class. + * The LegacyFlamesServer class defines additional methods and fields related to a server. + * + * In this context, the "id" variable is used to store the ID of the server. + * + * Therefore, the "id" variable is a common identifier used in the Flames system to uniquely identify servers. + */ String id; + /** + * The score variable represents the score of a FlamesServer in the Flames system. + * It is an integer value. + */ int score; + /** + * The hootenannyDay variable represents the day of the month for the hootenanny event. + * It is an integer value. + */ int hootenannyDay; + /** + * Represents the hootenanny scores for a Flames server. + * + * The hootenanny scores are stored as a map, where the key is the user ID and the value is the score. + */ Map hootenannyScores; + /** + * The FlamesServer class represents a Discord server in the Flames system. + * It extends the FlamesDatatype class. + * + * @param legacyFlamesServer The legacy server object that is being used to initialize the FlamesServer object. + * It contains the score, id, and hootenannyDay of the server. + */ public FlamesServer(LegacyFlamesServer legacyFlamesServer) { this.id = legacyFlamesServer.getId(); this.score = legacyFlamesServer.getScore(); this.hootenannyDay = legacyFlamesServer.getHootenannyDay(); } + /** + * Sets the id of the FlamesServer. + * + * @param id the new id to set + */ public void setId(String id) { this.id = id; } + /** + * Retrieves the score of this FlamesServer. + * + * @return The score of this FlamesServer. + */ public int getScore() { return score; } + /** + * Sets the score for the FlamesServer. + * + * @param score the score to be set + */ public void setScore(int score) { this.score = score; } + /** + * Retrieves the hootenanny day for the FlamesServer. + * + * @return The hootenanny day for the FlamesServer. + */ public int getHootenannyDay() { return hootenannyDay; } + /** + * Sets the hootenanny day of the FlamesServer. + * + * @param hootenannyDay the hootenanny day to set + */ public void setHootenannyDay(int hootenannyDay) { this.hootenannyDay = hootenannyDay; } + /** + * Retrieves the ID of the FlamesServer object. + * + * @return The ID of the FlamesServer object as a String. + */ @Override public String getID() { return id; } + /** + * Constructs a new FlamesServer with the given parameters. + * + * @param id the ID of the server + * @param score the score of the server + * @param hootenannyDay the hootenanny day of the server + * @param hootenannyScores the hootenanny scores of the server + */ public FlamesServer(String id, int score, int hootenannyDay, Map hootenannyScores) { this.id = id; this.score = score; this.hootenannyDay = hootenannyDay; this.hootenannyScores = hootenannyScores; } + /** + * Creates a FlamesServer object with the given ID. + * + * @param id The ID of the server. + */ public FlamesServer(String id) { this.id = id; this.score = 0; this.hootenannyDay = 1; this.hootenannyScores = new HashMap<>(); } + /** + * The FlamesServer class represents a Discord server in the Flames system. + * It extends the FlamesDatatype class. + */ public FlamesServer() { this.id = null; } + /** + * Retrieves the ID of the FlamesServer. + * + * @return The ID of the FlamesServer. + */ public String getId() { return id; } + /** + * Retrieves the hootenanny scores for the Flames server. + * + * @return A map containing the hootenanny scores, where the key is the user ID + * and the value is the score. + */ public Map getHootenannyScores() { return hootenannyScores; } + /** + * Sets the hootenanny scores for the Flames server. + * + * @param hootenannyScores a Map containing the hootenanny scores of individual users + */ public void setHootenannyScores(Map hootenannyScores) { this.hootenannyScores = hootenannyScores; } + /** + * Checks if today is Hootenanny Day based on the hootenannyDay field of the FlamesServer class. + * + * @return true if today is Hootenanny Day, false otherwise + */ public boolean todayIsHootenannyDay() { return hootenannyDay == new java.util.Date().getDate(); } diff --git a/src/test/java/com/severalcircles/flames/data/FlamesDataManagerTest.java b/src/test/java/com/severalcircles/flames/data/FlamesDataManagerTest.java new file mode 100644 index 00000000..a7611cec --- /dev/null +++ b/src/test/java/com/severalcircles/flames/data/FlamesDataManagerTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2024 Several Circles. + */ + +package com.severalcircles.flames.data; + +import com.severalcircles.flames.data.user.FlamesUser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Random; +import java.util.logging.Logger; + +public class FlamesDataManagerTest { + + @Test + public void getUser_WhenUserDoesNotExists_ShouldCreateAndReturnNewUser() throws IOException, ConsentException { + FlamesDataManager.prepare(); + String id = new Random().nextInt(10000) + ""; + File expectedUserFile = new File(FlamesDataManager.USER_DIRECTORY.getAbsolutePath() + "/" + id + ".yml"); + + if (expectedUserFile.exists()) { + Assertions.fail("Test precondition failed. A user with the id already exists."); + } else { + FlamesUser user = FlamesDataManager.getUser(id, true); + Assertions.assertTrue(expectedUserFile.exists()); + Assertions.assertNotNull(user); + Assertions.assertEquals(id, user.getId()); + } + if (FlamesDataManager.FLAMES_DIRECTORY.delete()) Logger.getGlobal().info("Deleted Flames directory"); + + } + + @Test + public void getUser_WhenUserExists_ShouldReturnExistingUser() throws IOException, ConsentException { + FlamesDataManager.prepare(); + String id = "existingUserId"; + + FlamesUser expectedUser = createAndSaveUserWithId(id); + + FlamesUser actualUser = FlamesDataManager.getUser(id); + Assertions.assertEquals(expectedUser.getId(), actualUser.getId()); + if (FlamesDataManager.FLAMES_DIRECTORY.delete()) Logger.getGlobal().info("Deleted Flames directory"); + } + + private FlamesUser createAndSaveUserWithId(String id) { + FlamesDataManager.prepare(); + FlamesUser user = new FlamesUser(id); + user.setConsent(1); + try { + FlamesDataManager.saveUser(user); + } catch (IOException e) { + Assertions.fail("Error creating Flames User for test.", e); + } + if (FlamesDataManager.FLAMES_DIRECTORY.delete()) Logger.getGlobal().info("Deleted Flames directory"); + return user; + } +} \ No newline at end of file diff --git a/src/test/java/com/severalcircles/flames/data/FlamesServerTest.java b/src/test/java/com/severalcircles/flames/data/FlamesServerTest.java new file mode 100644 index 00000000..945928f7 --- /dev/null +++ b/src/test/java/com/severalcircles/flames/data/FlamesServerTest.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Several Circles. + */ + +package com.severalcircles.flames.data; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FlamesServerTest { + + /** + * This class tests the setId method of FlamesServer class. + * The setId method is used to set the value of "id" of a FlamesServer object. + */ + + @Test + public void testSetId() { + FlamesServer server = new FlamesServer(); + server.setId("12345"); + assertEquals("12345", server.getId()); + } +} \ No newline at end of file