Skip to content

Commit

Permalink
Add data upgrade flag, tests, and Javadoc comments
Browse files Browse the repository at this point in the history
Refactored the data upgrade mechanism to run conditionally based on a command-line argument "RunUpgrade". Added unit tests for FlamesServer and FlamesDataManager. Enhanced existing classes with comprehensive Javadoc comments.
  • Loading branch information
SeveralCircles committed Aug 27, 2024
1 parent ff03ba0 commit 2be10d3
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 17 deletions.
Binary file modified .gradle/8.5/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/8.5/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/8.5/checksums/sha1-checksums.bin
Binary file not shown.
Binary file modified .gradle/8.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
24 changes: 23 additions & 1 deletion .idea/modules/Flames.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
6 changes: 4 additions & 2 deletions src/main/java/com/severalcircles/flames/Flames.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/severalcircles/flames/data/DataUpgradeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,9 +21,6 @@ public class DataUpgradeUtil {
private final List<FlamesUser> flamesUsers = new LinkedList<>();
private final List<FlamesServer> 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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
118 changes: 118 additions & 0 deletions src/main/java/com/severalcircles/flames/data/FlamesServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/severalcircles/flames/data/FlamesServerTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 2be10d3

Please sign in to comment.